How to write stereo wav files in Python?

后端 未结 3 821
孤独总比滥情好
孤独总比滥情好 2020-12-09 18:20

The following code writes a simple sine at frequency 400Hz to a mono WAV file. How should this code be changed in order to produce a stereo WAV file. The se

3条回答
  •  有刺的猬
    2020-12-09 18:32

    Build a parallel sine_list_y list with the other frequency / channel, set nchannels=2, and in the output loop use for s, t in zip(sine_list_x, sine_list_y): as the header clause, and a body with two writeframes calls -- one for s, one for t. IOW, corresponding frames for the two channels "alternate" in the file.

    See e.g. this page for a thorough description of all possible WAV file formats, and I quote:

    Multi-channel digital audio samples are stored as interlaced wave data which simply means that the audio samples of a multi-channel (such as stereo and surround) wave file are stored by cycling through the audio samples for each channel before advancing to the next sample time. This is done so that the audio files can be played or streamed before the entire file can be read. This is handy when playing a large file from disk (that may not completely fit into memory) or streaming a file over the Internet. The values in the diagram below would be stored in a Wave file in the order they are listed in the Value column (top to bottom).

    and the following table clearly shows the channels' samples going left, right, left, right, ...

提交回复
热议问题