如何使用numpy数组创建pydub AudioSegment?

时间:2016-03-01 23:13:01

标签: python numpy audio scipy pydub

我在python中有以下代码

from scipy.io.wavfile import read
rate, signal = read('./data/input.wav')
# get only one channel
signal = signal[:,0] 
# do a bunch of processing here

现在我想使用'signal'和'rate'

创建一个pydub段
audio_segment = pydub.AudioSegment()

那么如何创建这个音频片段呢?之后, 我怎么能把我的信号作为一个numpy阵列取回来?

1 个答案:

答案 0 :(得分:2)

我能够在我的机器上运行此代码:

from scipy.io.wavfile import read
from pydub import AudioSegment

rate, signal = read("./test/data/test1.wav")
channel1 = signal[:,0]

audio_segment = pydub.AudioSegment(
    channel1.tobytes(), 
    frame_rate=rate,
    sample_width=channel1.dtype.itemsize, 
    channels=1
)

# test that it sounds right (requires ffplay, or pyaudio):
from pydub.playback import play
play(audio_segment)