Skip to content

Instantly share code, notes, and snippets.

@SiLiKhon
Created May 4, 2020 20:19
Show Gist options
  • Save SiLiKhon/bc374a58a43a4e1db9abb16aeda50236 to your computer and use it in GitHub Desktop.
Save SiLiKhon/bc374a58a43a4e1db9abb16aeda50236 to your computer and use it in GitHub Desktop.
import numpy as np
import pydub
def audiosegment2array(segment):
return np.array(segment.get_array_of_samples()).reshape(-1, segment.channels)
def array2audiosegment(array, rate=44100):
assert array.ndim == 2
num_channels = array.shape[1]
array = bytes(array.astype(np.int16).reshape(-1).data)
return pydub.AudioSegment(
array,
sample_width=2,
frame_rate=rate,
channels=num_channels
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment