python raw data_Python open raw audio data file

本文讨论了如何使用Python打开和播放.raw格式的原始音频数据文件。提到了使用file()函数打开文件,通过将秒数转换为字节来定位文件中的特定位置。还推荐了在Linux上播放音频的pyAO库,以及使用PySoundFile和sounddevice库来读取和播放文件中的特定部分。
摘要由CSDN通过智能技术生成

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I have these files with the extension ".adc". They are simply raw data files. I can open them with Audacity using File->Import->Raw data with encoding "Signed 16 bit" and sample rate "16000 Khz".

I would like to do the same with python. I think that audioop module is what I need, but I can't seem to find examples on how to use it for something that simple.

The main goal is to open the file and play a certain location in the file, for example from the second 10 to the second 20. Is there something out there for my task ?

Thanx in advance.

回答1:

For opening the file, you just need file(). For finding a location, you don't need audioop: you just need to convert seconds to bytes and get the required bytes of the file. For instance, if your file is 16 kHz 16bit mono, each second is 32,000 bytes of data. So the 10th second is 320kB into the file. Just seek to the appropriate place in the file and then read the appropriate number of bytes.

And audioop can't help you with the hardest part: namely, playing the audio. The correct way to do this very much depends on your OS.

EDIT: Sorry, I just noticed your username is "thelinuxer". Consider pyAO for playing audio from Python on Linux. You will probably need to change the sample format to play the audio---audioop will help you with this (see ratecv, tomono/tostereo, lin2lin, and bias)

回答2:

Thanx a lot I was able to do the following: def play_data(filename, first_sec, second_sec): import ao from ao import AudioDevice dev = AudioDevice(2, bits=16, rate=16000,channels=1) f = open(filename, 'r') data_len = (second_sec-first_sec)*32000 f.seek(32000*first_sec) data = f.read(data_len) dev.play(data) f.close() play_data('AR001_3.adc', 2.5, 5)

回答3:

You can use PySoundFile to open the file as a NumPy array and play it with python-sounddevice. import soundfile as sf import sounddevice as sd sig, fs = sf.read('myfile.adc', channels=2, samplerate=16000, format='RAW', subtype='PCM_16') sd.play(sig, fs)

You can use indexing on the NumPy array to select a certain part of the audio data.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值