scipy.io.wavfile.
write#
- scipy.io.wavfile.write(filename, rate, data)[source]#
將 NumPy 陣列寫入為 WAV 檔。
- 參數:
- filename字串或開啟的檔案控制代碼
輸出 wav 檔。
- rateint
取樣率 (samples/sec)。
- datandarray
1-D 或 2-D NumPy 陣列,資料型別為整數或浮點數。
說明
寫入簡單的未壓縮 WAV 檔。
若要寫入多聲道,請使用形狀為 (Nsamples, Nchannels) 的 2-D 陣列。
位元深度和 PCM/float 將由資料型別決定。
常見資料型別: [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 Corporation and Microsoft Corporation, “Multimedia Programming Interface and Data Specifications 1.0”, section “Data Format of the Samples”, August 1991 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))