在Python中更新/附加到.wav文件 [英] Updating/appending to a .wav file in Python

查看:76
本文介绍了在Python中更新/附加到.wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python脚本中加入了PCM音频帧流,并且能够将这些帧的块保存为.wav文件,如下所示:

I have a stream of PCM audio frames coming into my Python script, and I am able to save blocks of these frames as .wav files as such:

def update_wav():
    filename = "test.wav"
    wav_file = wave.open(filename, "wb")
    n_frames = len(audio)

    wav_file.setparams((n_channels, sample_width, sample_rate, n_frames, comptype, compname))
    for sample in audio:
        wav_file.writeframes(struct.pack('h', int(sample * 32767.0)))
    wav_file.close()

但是,我希望随着新框架的加入而不断更新.是否可以通过附加到现有.wav文件的方式来写入框架?现在,我只能完成覆盖.

However, I'd like this to continually update as new frames come in. Is there way to writeframe in a way that appends to an existing .wav file? Right now I am only able to accomplish an overwrite.

推荐答案

我找到了一种使用

I found a way of doing this with SciPy, it actually seems to be the default functionality for their writing method.

import scipy.io.wavfile

def update_wav():
    numpy_data = numpy.array(audio, dtype=float)
    scipy.io.wavfile.write("test.wav", 8000, numpy_data)

这篇关于在Python中更新/附加到.wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆