如何用Python编写立体声wav文件?

2024-05-19 14:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码将频率为400Hz的简单正弦写入单声道WAV文件。如何更改此代码以生成立体声WAV文件。第二个通道的频率应该不同。

import math
import wave
import struct

freq = 440.0
data_size = 40000
fname = "WaveTest.wav"
frate = 11025.0  # framerate as a float
amp = 64000.0     # multiplier for amplitude

sine_list_x = []
for x in range(data_size):
    sine_list_x.append(math.sin(2*math.pi*freq*(x/frate)))

wav_file = wave.open(fname, "w")

nchannels = 1
sampwidth = 2
framerate = int(frate)
nframes = data_size
comptype = "NONE"
compname = "not compressed"

wav_file.setparams((nchannels, sampwidth, framerate, nframes,
    comptype, compname))

for s in sine_list_x:
    # write the audio frames to file
    wav_file.writeframes(struct.pack('h', int(s*amp/2)))

wav_file.close()

Tags: 文件代码importfordatasizemathlist
3条回答

有关生成立体声.wav文件的示例,请参见^{} module。 测试生成一个全零文件。 可以通过插入交替采样值进行修改。

nchannels = 2
sampwidth = 2
framerate = 8000
nframes = 100

# ...

    def test_it(self):
        self.f = wave.open(TESTFN, 'wb')
        self.f.setnchannels(nchannels)
        self.f.setsampwidth(sampwidth)
        self.f.setframerate(framerate)
        self.f.setnframes(nframes)
        output = '\0' * nframes * nchannels * sampwidth
        self.f.writeframes(output)
        self.f.close()

用另一个频率/通道构建一个并行的sine_list_y列表,设置nchannels=2,并在输出循环中使用for s, t in zip(sine_list_x, sine_list_y):作为头子句,一个具有两个writeframes调用的主体——一个用于s,一个用于t。瞧,文件中两个通道“交替”对应的帧。

有关所有可能的WAV文件格式的详细说明,请参见this页,我引用:

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

下表清楚地显示了频道的左,右,左,右。。。

用另一个频率/通道构建一个并行的sine_list_y列表,设置nchannels=2,在输出循环中使用for s, t in zip(sine_list_x, sine_list_y):作为头子句,并使用一个带有两个writeframes调用的主体——一个用于s,一个用于t。瞧,文件中两个通道“交替”对应的帧。

有关所有可能的WAV文件格式的详细说明,请参见this页,我引用:

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

下表清楚地显示了频道的左,右,左,右。。。

相关问题 更多 >