Created
February 8, 2022 08:10
-
-
Save andirkh/74f4db84fdce5f356e2595862e749694 to your computer and use it in GitHub Desktop.
Cut gamelan sound by their onsets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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