PCM格式音频数据的读取

这篇博客介绍了如何使用Python读取PCM格式的音频文件,通过Audacity软件设置编码和参数,然后利用numpy从文件中加载数据。代码示例展示了如何处理音频数据,包括从文件中读取16位整数数据,分离左右声道,并提供了调整采样率的选项。
摘要由CSDN通过智能技术生成

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值