使用 Python、Numpy 数组和 WAVE 模块编写 WAV 文件

Writing WAV file using Python, Numpy array and WAVE module

我正在尝试实现 Karplus-Strong 算法。

当我播放(通过 Jupyter Notebook 使用 Audio(y, rate=Fs))收集的 numpy 数组(代表吉他和弦)时,一切看起来都很好。

不幸的是,使用 WAVE 模块将 numpy 数组:y 写入 wav 文件是不正确的(使用下一个 python 代码):

noise_output = wave.open('k-s.wav', 'w')
noise_output.setparams((1, 4, Fs, 0, 'NONE', 'not compressed'))

for i in range(0, len(y)):
     value = y[i]
     packed_value = struct.pack('f', value)
     noise_output.writeframes(packed_value)

noise_output.close()

y的每个元素是

<type 'numpy.float64'>

如何修改写入循环才能正确写入WAV文件?

有关此问题的更多信息。在写入 WAV 之前,y 数组的第一个元素是:

 [ 0.33659756  0.33659756 -0.43915295 -0.87036152  1.40708988  0.32123558
-0.6889402   1.9739982  -1.29587159 -0.12299964  2.18381762  0.82228042
 0.24593503 -1.28067426 -0.67568838 -0.01843234 -1.830472    1.2729578
-0.56575346  0.55410736]

将元素写入WAV文件后,关闭WAV文件并再次读取,我得到了收集数组的前20个元素:

[ 1051481732  1051481732 -1092560728 -1084305405  1068768133  1050966269
 -1087349149  1073523705 -1079648481 -1107564740  1074512811  1062371576
  1048303204 -1079775966 -1087571478 -1130954901 -1075163928  1067642952
 -1089415880  1057872379]
import scipy.io.wavfile
scipy.io.wavfile.write("karplus.wav", Fs, y)

多田! AFAIK 适用于 float64 和 float32,可能还有其他。对于立体声,形状必须是 (nb_samples, 2)。参见 scipy.io.wavfile.write

以下是使用 wave 标准库编写(立体声)波形文件的代码示例。 我包括了两个示例:一个使用 numpy,另一个不需要任何依赖项。

使用 numpy 数组

请注意,如果您的数据位于 numpy 数组中,则不需要 struct 库。

import wave
import numpy as np

samplerate = 44100

# A note on the left channel for 1 second.
t = np.linspace(0, 1, samplerate)
left_channel = 0.5 * np.sin(2 * np.pi * 440.0 * t)

# Noise on the right channel.
right_channel = np.random.random(size=samplerate)

# Put the channels together with shape (2, 44100).
audio = np.array([left_channel, right_channel]).T

# Convert to (little-endian) 16 bit integers.
audio = (audio * (2 ** 15 - 1)).astype("<h")

with wave.open("sound1.wav", "w") as f:
    # 2 Channels.
    f.setnchannels(2)
    # 2 bytes per sample.
    f.setsampwidth(2)
    f.setframerate(samplerate)
    f.writeframes(audio.tobytes())

使用列表

这是(几乎)相同的代码,但没有使用 numpy。不需要外部依赖。

import math
import random
import struct
import wave

samplerate = 44100

left_channel = [
    0.5 * math.sin(2 * math.pi * 440.0 * i / samplerate) for i in range(samplerate)
]
right_channel = [random.random() for _ in range(samplerate)]

with wave.open("sound2.wav", "w") as f:
    f.setnchannels(2)
    f.setsampwidth(2)
    f.setframerate(samplerate)
    for samples in zip(left_channel, right_channel):
        for sample in samples:
            sample = int(sample * (2 ** 15 - 1))
            f.writeframes(struct.pack("<h", sample))

从文件读取和写入 wav 文件:

from  scipy.io import wavfile 

sampling_rate, data = wavfile.read(wpath)
wavfile.write('abc1.wav', sampling_rate, data)