Skip to content

Instantly share code, notes, and snippets.

@grohith327
Created October 30, 2019 03:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save grohith327/2f9121157039840a77b5d527e2313f06 to your computer and use it in GitHub Desktop.
Save grohith327/2f9121157039840a77b5d527e2313f06 to your computer and use it in GitHub Desktop.
import os
import librosa
import random
for files in os.listdir('./Dataset'):
count = 21
for comm in os.listdir('./Dataset/'+str(files)):
sound = AudioSegment.from_file('./Dataset/'+str(files)+'/'+str(comm))
halfway_point = len(sound) // 2
for _ in range(10):
val = random.randint(1, halfway_point)
crop_audio = sound[val:val+halfway_point]
crop_audio.export('test.wav',format='wav')
data, sf = librosa.load('test.wav')
new_crop = np.pad(data,(0,22026),'constant') # Pad data so that the duration matches (2 Sec)
librosa.output.write_wav('./Dataset/'+str(files)+'/'+str(count)+'.wav',new_crop,sf)
count += 1
@thanduong
Copy link

Hey 👋

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment