scipy.io.wavfile.
写入#
- scipy.io.wavfile.write(filename, rate, data)[source]#
- 将 NumPy 数组写入 WAV 文件。 - 参数:
- filename字符串或已打开的文件句柄
- 输出 WAV 文件。 
- rateint
- 采样率(单位:样本/秒)。 
- datandarray
- 一个一维或二维的 NumPy 数组,其数据类型为整数或浮点数。 
 
 - 注意 - 写入一个简单的未压缩 WAV 文件。 
- 要写入多声道,请使用形状为 (Nsamples, Nchannels) 的二维数组。 
- 每样本位数和 PCM/浮点格式将由数据类型决定。 
 - 常见数据类型: [1] - WAV 格式 - 最小值 - 最大值 - NumPy 数据类型 - 32 位浮点 - -1.0 - +1.0 - float32 - 32 位 PCM - -2147483648 - +2147483647 - int32 - 16 位 PCM - -32768 - +32767 - int16 - 8 位 PCM - 0 - 255 - uint8 - 请注意,8 位 PCM 是无符号的。 - 参考 [1]- IBM 公司和微软公司,“Multimedia Programming Interface and Data Specifications 1.0”,“Data Format of the Samples”章节,1991 年 8 月 http://www.tactilemedia.com/info/MCI_Control_Info.html - 示例 - 创建一个 100Hz 的正弦波,采样率为 44100Hz。写入 16 位 PCM,单声道。 - >>> from scipy.io.wavfile import write >>> import numpy as np >>> samplerate = 44100; fs = 100 >>> t = np.linspace(0., 1., samplerate) >>> amplitude = np.iinfo(np.int16).max >>> data = amplitude * np.sin(2. * np.pi * fs * t) >>> write("example.wav", samplerate, data.astype(np.int16))