PCM格式音频数据的读取
PCM格式音频文件比方方式采用Audacity软件播放。具体实现过程:打开软件–>导入–>原始数据,修改编码:Signed 16bit PCM;字节序:小尾端,声道:单声道和双声道(立体声);采样率根据实际填写,快鱼的拾音器的采样率为16000Hz
python实现PCM格式音频文件的加载
PCM里仅保留了数据,没有采样率值和通道值。采样率是16bit、32bit float等参数采样。
import array
import os
from matplotlib import pyplot
fileName = ‘e:/music/qianqian.pcm’ # 2 channel, 16 bit per sample
file = open(fileName, ‘rb’)
base = 1 / (1<<15)
shortArray = array.array(‘h’) # int16
size = int(os.path.getsize(fileName) / shortArray.itemsize)
count = int(size / 2)
shortArray.fromfile(file, size) # faster than struct.unpack
file.close()
leftChannel = shortArray[::2]
rightChannel = shortArray[1::2]
上述代码参考
**
我的代码如下:
**
def read_audio(path, target_fs=None, data_type=np.int16):
#(audio, fs) = soundfile.read(path)
f = open(path, “rb”)
#f.seek(0)
#f.read(44)
audio = np.fromfile(f, dtype = data_type)
‘’‘if audio.ndim > 1:
audio = np.mean(audio, axis=1)
if target_fs is not None and fs != target_fs:
audio = librosa.resample(audio, orig_sr=fs, target_sr=target_fs)
fs = target_fs’’’
return audio