Skip to content

Instantly share code, notes, and snippets.

@andirkh
Created February 8, 2022 08:10
Show Gist options
  • Save andirkh/74f4db84fdce5f356e2595862e749694 to your computer and use it in GitHub Desktop.
Save andirkh/74f4db84fdce5f356e2595862e749694 to your computer and use it in GitHub Desktop.
Cut gamelan sound by their onsets
from pydub import AudioSegment
import uuid
import librosa
import numpy as np
import os
BASE_FOLDER = './dir/'
FILE = 'Saron_mono_1.wav'
file_path = os.path.join(BASE_FOLDER, FILE)
sound = AudioSegment.from_wav(file_path)
signal, sr = librosa.load(file_path)
ONSET = librosa.onset.onset_detect(y=signal, sr=sr, units='time')
ONSET_LIST = ONSET.tolist()
for i in range(len(ONSET_LIST)):
start = 0
if i > 0:
start = int(ONSET_LIST[i-1] * 1000) - 20
end = int(ONSET_LIST[i] * 1000) - 30
sound_cut = sound[start:end]
new_file_name = FILE.split('_')[0] + '_' + FILE.split('_')[2]
sound_cut.export(new_file_name + '_' + str(i) + '_' + uuid.uuid4().hex + '.wav', format='wav')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment