Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

break up a .wav file by timestamp

I am new to the audio processing scene. I have a set of timestamps generated by a speech parsing program. What I want to do now is to break up the full wav file into segments specified by the list of timestamps. Can someone recommend a python library I can use for this job?

like image 859
bhomass Avatar asked Jan 27 '23 16:01

bhomass


2 Answers

One (of the many) solutions would be to use SciPy:

from scipy.io import wavfile

# the timestamp to split at (in seconds)
split_at_timestamp = 42

# read the file and get the sample rate and data
rate, data = wavfile.read('foo.wav') 

# get the frame to split at
split_at_frame = rate * split_at_timestamp

# split
left_data, right_data = data[:split_at_frame-1], data[split_at_frame:]  # split

# save the result
wavfile.write('foo_left.wav', rate, left_data)
wavfile.write('foo_right.wav', rate, right_data)
like image 84
Joren Avatar answered Jan 31 '23 21:01

Joren


pydub has easier methods to split audio files of different formats (wav, mp3 etc) between two intervals.

Here's sample code

from pydub import AudioSegment

audio_file= "your_wav_file.wav"
audio = AudioSegment.from_wav(audio_file)
list_of_timestamps = [ 10, 20, 30, 40, 50 ,60, 70, 80, 90 ] #and so on in *seconds*

start = ""
for  idx,t in enumerate(list_of_timestamps):
    #break loop if at last element of list
    if idx == len(list_of_timestamps):
        break

    end = t * 1000 #pydub works in millisec
    print "split at [ {}:{}] ms".format(start, end)
    audio_chunk=audio[start:end]
    audio_chunk.export( "audio_chunk_{}.wav".format(end), format="wav")

    start = end * 1000 #pydub works in millisec

Result:

split at [ :10000] ms
split at [ 10000000:20000] ms
split at [ 20000000:30000] ms
split at [ 30000000:40000] ms
split at [ 40000000:50000] ms
split at [ 50000000:60000] ms
split at [ 60000000:70000] ms
split at [ 70000000:80000] ms
split at [ 80000000:90000] ms
like image 34
Anil_M Avatar answered Jan 31 '23 23:01

Anil_M