23

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?

2 Answers 2

20

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)
Sign up to request clarification or add additional context in comments.

3 Comments

When I try this I get IndexError: too many indices on line "channel1 = signal[:,0]"
But er.. .this is cause my file only had one channel. Checked with simple singal.shape print statement Also, prior to numpy version 1.9, I had to use tostring instead of tobytes
This produces a garbled audio for me. I have only managed to convert from numpy array to AudioSegment by first encoding the array as a wav file then calling AudioSegment(wav_bytes)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.