Created
May 4, 2020 20:19
-
-
Save SiLiKhon/bc374a58a43a4e1db9abb16aeda50236 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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