用Python读取将要附加数据的wav文件

时间:2018-09-04 05:48:36

标签: python wav

我需要转录正在写入wav文件的语音。我已经实现了以下迭代器,以尝试从文件中逐步读取音频:

import wave
def read_audio(path, chunk_size=1024):
    wave_file = wave.open(open(path, 'rb'))
    while True:
        data = wave_file.readframes(chunk_size)
        if data != "":
            yield data

为了测试生成器,我实现了一个功能,该功能会将计算机麦克风捕获的音频不断写入wav文件中:

import pyaudio
def record_to_file(out_path):
    fmt = pyaudio.paInt16
    channels = 1
    rate = 16000
    chunk = 1024
    audio = pyaudio.PyAudio()
    stream = audio.open(format=fmt, channels=channels,
                        rate=rate, input=True,
                        frames_per_buffer=chunk)
    wave_file = wave.open(out_path, 'wb')
    wave_file.setnchannels(channels)
    wave_file.setsampwidth(audio.get_sample_size(fmt))
    wave_file.setframerate(rate)
    while True:
        data = stream.read(chunk)
        waveFile.writeframes(data)

下面是测试脚本:

import threading
import time

WAV_PATH='out.wav'

def record_worker():
    record_to_file(WAV_PATH)

if __name__=='__main__':
    t = threading.Thread(target=record_worker)
    t.setDaemon(True)
    t.start()
    time.sleep(5)
    reader = read_audio(WAV_PATH)
    for chunk in reader:
        print(len(chunk))

它没有按我预期的那样工作-一段时间后读者停止屈服。由于如果我使record_file事先将wav文件的nframes设置为一个很大的数字并用writeframesraw进行书写,则测试成功了,所以我猜是{{ 1}}急切地读取wave.open,而不是在读取了该数量的帧后尝试读取任何内容。

是否有可能在Python 2.7中获得增量读取而无需诉诸nframes hack?值得注意的是,与测试脚本相反,在我计划利用该功能的情况下,我无法控制setnframes文件的生成。编写工作是通过名为wavhttp://www.pjsip.org/python/pjsua.htm)的SWIG自适应C库完成的,因此我不希望为此进行任何修改。

0 个答案:

没有答案
相关问题