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)