python 生成24bit音频数据实例解析

一 概念

24 bit 是指音频文件的 采样深度 (bit depth)。 它代表了每个采样点的数据精度,也就是音频每个样本所使用的比特数。 24 bit 的采样深度相较于 16 bit 提供了更高的动态范围和更精确的音频信息表示。 动态范围:24 bit 的动态范围大约为 144 dB,而 16 bit 的动态范围约为 96 dB。 目前,24bit音频数据成为了很多硬件的标配。

二 源码

直接上源码:

复制代码
import wave, struct, math, random
import serial
import numpy as np
import struct
import operator
import binascii

sampleRate = 96000.0 # hertz
obj = wave.open('sound_96k_b.wav','w')
obj.setnchannels(1) # mono
obj.setsampwidth(3)
obj.setframerate(sampleRate)

fp = open("test.bin",'rb')
def bin_file_process(fp):
    cnt=0
    while True:
            data=fp.read(1)
            if data == b'':
                break

            if data == b'\xff':
                    data_next=fp.read(1)
                    #print("hello world")
                    if data_next == b'\xff':
                        cnt += 1
                        data_lens=fp.read(2)
                        print("read data_cnt is:",cnt)
                        data_audio=fp.read(1920)
                        count=len(data_audio)
                        for idx in range(0,len(data_audio),3):
                            raw_data = data_audio[idx:idx+3]
                            obj.writeframes(struct.pack('B',raw_data[0]))
                            obj.writeframes(struct.pack('B',raw_data[1]))
                            obj.writeframes(struct.pack('B',raw_data[2]))



if __name__ == '__main__':
    bin_file_process(fp)
相关推荐
Want5951 分钟前
C/C++大雪纷飞①
c语言·开发语言·c++
Blossom.11837 分钟前
把AI“刻”进玻璃:基于飞秒激光量子缺陷的随机数生成器与边缘安全实战
人工智能·python·单片机·深度学习·神经网络·安全·机器学习
Kratzdisteln1 小时前
【Python OOP Diary 1.1】题目二:简单计算器,改错与优化
python·面向对象编程
小白银子1 小时前
零基础从头教学Linux(Day 53)
linux·运维·python
有时间要学习1 小时前
Qt——窗口
开发语言·qt
消失的旧时光-19432 小时前
@JvmStatic 的作用
java·开发语言·kotlin
EasyGBS2 小时前
EasyGBS如何通过流媒体技术提升安防监控效率?
网络·音视频
skywalk81632 小时前
基于频域的数字盲水印blind-watermark
linux·开发语言·python
applepie_max2 小时前
GraphRAG本地部署 v2.7.0
python·rag·graphrag
Tiger_shl2 小时前
三大并发集合ConcurrentDictionary、ConcurrentBag、ConcurrentQueue
开发语言·c#