Skip to content

Commit

Permalink
Add example for converting to float32 numpy array (jiaaro#470)
Browse files Browse the repository at this point in the history
* Add example for converting to float32 numpy array

This question comes up frequently enough that I thought it would
be useful to have an "official" example people can copy.

* Support for stereo and conversion back to segment.

* fix typo, reuse wav_io
  • Loading branch information
cghawthorne committed Aug 25, 2020
1 parent 74bfa9a commit a5166ec
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions API.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,33 @@ shifted_samples_array = array.array(sound.array_type, shifted_samples)
new_sound = sound._spawn(shifted_samples_array)
```

Here's how to convert to a numpy float32 array:

```python
import numpy as np
from pydub import AudioSegment

sound = AudioSegment.from_file("sound1.wav")
sound = sound.set_frame_rate(16000)
channel_sounds = seg.split_to_mono()
samples = [s.get_array_of_samples() for s in channel_sounds]

fp_arr = np.array(samples).T.astype(np.float32)
fp_arr /= np.iinfo(samples[0].typecode).max
```

And how to convert it back to an AudioSegment:

```python
import io
import scipy.io.wavfile

wav_io = io.BytesIO()
scipy.io.wavfile.write(wav_io, 16000, fp_arr)
wav_io.seek(0)
sound = pydub.AudioSegment.from_wav(wav_io)
```

### AudioSegment(…).get_dc_offset()

Returns a value between -1.0 and 1.0 representing the DC offset of a channel. This is calculated using `audioop.avg()` and normalizing the result by samples max value.
Expand Down

0 comments on commit a5166ec

Please sign in to comment.