简体   繁体   中英

Updating/appending to a .wav file in Python

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()

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM