Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pydub Slice Audio Segment By Sample

Suppose I have two audio segments of the same sample rate that I have imported from .wav files in Pydub, and assume I know which is shorter. Now suppose I want to split the longer audio file into two segments so that the first segment is the exact same length (down to the exact same number of samples!) as the shorter audio file and assign each of these two segments to new audio segments. How can I do this in Pydub? If I cannot get this level of precision using Pydub, can you give me an alternative that will work just as well?

Basically I just want to split the longer audio segment into two at the sample-perfect level. Here's an illustration of what I want: enter image description here

like image 420
jippyjoe4 Avatar asked Jul 18 '18 18:07

jippyjoe4


People also ask

What is audio segment in PyDub?

AudioSegment (pydubseg, name) Bases: object. This class is a wrapper for a pydub. AudioSegment that provides additional methods. auditory_scene_analysis (debug=False, debugplot=False)

How do I read an audio file in Python?

open() This function opens a file to read/write audio data. The function needs two parameters - first the file name and second the mode. The mode can be 'wb' for writing audio data or 'rb' for reading.


1 Answers

You can use AudioSegment().get_array_of_samples() and then construct new segments with the precise number of samples you want:

samples = sound.get_array_of_samples()  
shorter = sound._spawn(samples[:20000])

Note: this code is not tested - I wrote it on my iPhone :)

like image 111
Jiaaro Avatar answered Nov 03 '22 05:11

Jiaaro