【问题标题】:How to create a pydub AudioSegment using an numpy array?如何使用 numpy 数组创建 pydub AudioSegment?
【发布时间】:2016-03-01 23:13:00
【问题描述】:

我在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 数组取回?

【问题讨论】:

    标签: python numpy audio scipy pydub


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 当我尝试这个时,我得到 IndexError: too many indices on line "channel1 = signal[:,0]"
    • 但是呃..这是因为我的文件只有一个频道。用简单的 singal.shape 打印语句检查 另外,在 numpy 版本 1.9 之前,我不得不使用 tostring 而不是 tobytes
    猜你喜欢
    • 2016-10-27
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2020-07-06
    • 2020-02-18
    • 1970-01-01
    相关资源
    最近更新 更多