Dayvid Oliveira
Dayvid Oliveira

Reputation: 1217

How to create a pydub AudioSegment using an numpy array?

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?

Upvotes: 23

Views: 18062

Answers (2)

maniac
maniac

Reputation: 1192

If you prefer to have a function:

from pydub import AudioSegment

def split_music(
        audio_file: AudioSegment | str,
        from_second: int = 0,
        to_second: int = None,
        save_file_path: str = None,
        save_file_format: str = "wav") -> AudioSegment:
    """
    This code splits an audio file based on the provided seconds
    :param audio_file: either a string to load from file or already loaded file as AudioSegment
    :param from_second: the second when the split starts
    :param to_second: the second when the split ends
    :param save_file_path: if provided audio snippet will be saved at location
    :param save_file_format: in which format to save the file
    :return: the audio snippet as AudioSegment
    """
    
    
    t1 = from_second * 1000  # Works in milliseconds
    t2 = to_second * 1000

    # load audio from file
    if type(audio_file) == str:
        AudioSegment.from_file(audio_file)

    # split audio
    audio_file = audio_file[t1:t2]

    if save_file_path is not None and type(save_file_path) == str:
        audio_file.export(save_file_path, format=save_file_format)  # Exports to a wav file in the curren

    return audio_file

Upvotes: 0

Jiaaro
Jiaaro

Reputation: 76918

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)

Upvotes: 20

Related Questions