Librosa Split .wav file into 15s intervals

Question:

I’m new to working with audio files. I have several 60 second long files that I want to split into 15 second files (or any length). I’m able to split files into 1 second long files (so 60 files) but can’t seem to get 15 second intervals to work. How can I create the intervals I’m looking for?

import os
import numpy as np
import librosa
import librosa.display

audio_dir = r'dataacousticsrecordings'

out_dir = r'dataacousticssplits'
os.makedirs(out_dir, exist_ok=True)

audio_file = os.path.join(audio_dir, 'rec_20220729T160547Z.wav')

wave, sr = librosa.load(audio_file, sr=None)  

num_sections = int(np.ceil(len(wave) / sr)

split = []

for i in range(num_sections):
    t = wave[i * sr : i * sr + sr]
    split.append(t)

for i in range(num_sections):
    recording_name = os.path.basename(audio_file[:-4])
    out_file = f"{recording_name}_{str(i)}.wav"
    sf.write(os.path.join(out_dir, out_file), split[i], sr)
Asked By: andrewr

||

Answers:

What you have done is mostly correct. It just need minor changes.
First is getting the data which you have done correctly.

import os
import numpy as np
import librosa
import librosa.display
import soundfile as sf  # Missing import

audio_dir = r'dataacousticsrecordings'

out_dir = r'dataacousticssplits'
os.makedirs(out_dir, exist_ok=True)

audio_file = os.path.join(audio_dir, 'rec_20220729T160547Z.wav')

wave, sr = librosa.load(audio_file, sr=None)  

Calculate the length of segment:

segment_dur_secs = 15  
segment_length = sr * segment_dur_secs

Breaking up the data and saving to file:

num_sections = int(np.ceil(len(wave) / segment_length))

split = []

for i in range(num_sections):
    t = wave[i * segment_length: (i + 1) * segment_length]
    split.append(t)

for i in range(num_sections):
    recording_name = os.path.basename(audio_file[:-4])
    out_file = f"{recording_name}_{str(i)}.wav"
    sf.write(os.path.join(out_dir, out_file), split[i], sr)

Alternatively:

split = []    
for s in range(0, len(wave), segment_length):
    t = wave[s: s + segment_length]
    split.append(t)


recording_name = os.path.basename(audio_file[:-4])
for i, segment in enumerate(split):
    out_file = f"{recording_name}_{i}.wav"
    sf.write(os.path.join(out_dir, out_file), segment, sr)

Edit: There is an issue with the code here because sf is not defined. (Fixed the import)

Answered By: MYousefi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.