triple7
triple7

Reputation: 574

Python-pydub: bad sample width error when exporting AudioSegment created from numpy array

I've got a numpy array of length 109890 floating point numbers

Then create an AudioSegment instance with:

segment = AudioSegment(data=sinewave.tobytes(), sample_width=sinewave.dtype.itemsize, frame_rate=frameRate, channels=1)

#where frameRate is 44100

segment.export('redgiant.mp3', format='mp3')

But I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/osiris/anaconda3/lib/python3.6/site-packages/pydub/audio_segment.py", line 787, in export
    wave_data.setsampwidth(self.sample_width)
  File "/Users/osiris/anaconda3/lib/python3.6/wave.py", line 343, in setsampwidth
    raise Error('bad sample width')
wave.Error: bad sample width

is this because I have an arbitrary length for my initial sine wave which doesn't fit as a multiple of sampleWidth*frameRate?

Upvotes: 2

Views: 4855

Answers (1)

I had similar problem

segment = AudioSegment(data=sinewave.astype("float32").tobytes(), sample_width=4, frame_rate=frameRate, channels=1)

solved it. It seems to be related to issue: https://github.com/jiaaro/pydub/issues/293.

Upvotes: 2

Related Questions