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

标签 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

现在我想使用“信号”和“速率”创建一个 pydub 段

audio_segment = pydub.AudioSegment()

那么我怎样才能创建这个音频片段,然后, 我怎样才能以 numpy 数组的形式取回我的信号?

最佳答案

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

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)

关于python - 如何使用 numpy 数组创建 pydub AudioSegment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35735497/

相关文章:

python - Python中的惰性求值

.net - IronPython 无法运行导入 numpy 的脚本

python - 将 3D numpy 转换为数组再转换为 4D 数组,无需更改

android - Android:在不同的 Activity 中播放很多短音轨

Python 不释放 Linux 中的内存

python - Flask - 无法访问根地址的静态文件

Python - For循环和If语句计算抛硬币的概率

xcode - 在 Mac OS X Lion 10.7 上安装 Numpy

audio - AVAssetWriter实时处理文件中的音频和AVCaptureSession中的音频

python - 结构错误 : unpack requires a string argument of length 4 - audio file