20

I have the following code in 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

Now I want to create an pydub segment using 'signal' and 'rate'

audio_segment = pydub.AudioSegment()

So how can I create this audio segment, and after that, how can I get back my signal as an numpy array?

Jiaaro
  • 74,485
  • 42
  • 169
  • 190
Dayvid Oliveira
  • 1,157
  • 2
  • 14
  • 34

1 Answers1

18

I was able to run this code on my machine:

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)
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
  • 1
    When I try this I get IndexError: too many indices on line "channel1 = signal[:,0]" – hubatish May 29 '16 at 22:22
  • 1
    But er.. .this is cause my file only had one channel. Checked with simple singal.shape print statement Also, prior to numpy version 1.9, I had to use tostring instead of tobytes – hubatish May 29 '16 at 22:51
  • 1
    This produces a garbled audio for me. I have only managed to convert from numpy array to AudioSegment by first encoding the array as a wav file then calling `AudioSegment(wav_bytes)` – Le Frite Jan 24 '23 at 14:16