def augment(afile, jfile, deformer, outpath): jam = muda.load_jam_audio(jfile, afile, strict=False) base = root(afile) outfile = os.path.join(outpath, base) for i, jam_out in enumerate(deformer.transform(jam)): muda.save('{}.{}.flac'.format(outfile, i), '{}.{}.jams'.format(outfile, i), jam_out, strict=False)
def run(): #Captura os nomes das pastas names_folders = glob.glob(path_database + '*') for names_ in names_folders: #Captura o caminho completo do audio path_audios = glob.glob(names_ + '/*.' + audio_format) for path_ in path_audios: #Captura o nome do audio sem extensao audio_name = re.sub(names_, "", path_) audio_name = re.sub(r'/', "", audio_name) audio_name_wo_ext = re.sub(r'\.wav', "", audio_name) #Captura o caminho completo no arquivo de anotacao jam_path = re.sub(r'\.wav', ".jams", path_) #Captura o label da classe label = audio_name.split('-')[1] # A variacao de semitons e maior para as classes 1 e 6 if label == '1' or label == '6': semitones = [1, 1.5, 2, 2.5, 3, 3.5] else: semitones = [1.5, 2, 2.5, 3] jam_orig = muda.load_jam_audio(jam_path, path_) #Gerando variacoes no tom ps = muda.deformers.PitchShift(n_semitones=semitones) output_name_ps_pattern = names_ + '/' + audio_name_wo_ext + '-ps-' #O audio modificado e salvo em um novo .wav e as deformacoes aplicadas sao salvas no seu respectivo .jams for i, jam_out in enumerate(ps.transform(jam_orig)): muda.save(output_name_ps_pattern + str(semitones[i]) + '.wav', output_name_ps_pattern + str(semitones[i]) + '.jams', jam_out) #Gerando deformacoes de ruido de fundo bg = muda.deformers.BackgroundNoise(n_samples=1, files=files, weight_min=0.1, weight_max=0.5) output_name_bg_pattern = names_ + '/' + audio_name_wo_ext + '-bg-' #O audio modificado e salvo em um novo .wav e as deformacoes aplicadas sao salvas no seu respectivo .jams for i, jam_out in enumerate(bg.transform(jam_orig)): bg_noise_name = files[i].split('/')[5] bg_noise_name_w_extension = re.sub(r'\.wav', "", bg_noise_name) muda.save( output_name_bg_pattern + bg_noise_name_w_extension + '.wav', output_name_bg_pattern + bg_noise_name_w_extension + '.jams', jam_out)
def deform_audio(aud, ann, deformer, aud_out, ann_out, out_dict): build_dirs(aud_out) build_dirs(ann_out) orig_audio = muda.load_jam_audio(ann, aud) filename = base(aud) n_dict = len(out_dict) for i, jam_out in enumerate(deformer.transform(orig_audio)): out_aud = os.path.join(aud_out,filename+'_'+out_dict[i%n_dict]+'.wav') out_ann = os.path.join(ann_out,filename+'_'+out_dict[i%n_dict]+'.jams') muda.save(out_aud, out_ann, jam_out)
def augment(afile, jfile, deformer, outpath, audio_ext, jams_ext, sr=44100): '''Run the data through muda''' jam = muda.load_jam_audio(jfile, afile, sr=sr) base = milsed.utils.base(afile) outfile = os.path.join(outpath, base) for i, jam_out in enumerate(deformer.transform(jam)): muda.save('{}.{}.{}'.format(outfile, i, audio_ext), '{}.{}.{}'.format(outfile, i, jams_ext), jam_out, strict=False)
def augment(afile, jfile, deformer, outpath, audio_ext, jams_ext): '''Run the data through muda''' jam = muda.load_jam_audio(jfile, afile) base = crema.utils.base(afile) outfile = os.path.join(outpath, base) for i, jam_out in enumerate(deformer.transform(jam)): muda.save('{}.{}.{}'.format(outfile, i, audio_ext), '{}.{}.{}'.format(outfile, i, jams_ext), jam_out, strict=False)
def test_save(): jam = muda.load_jam_audio('data/fixture.jams', 'data/fixture.wav') _, jamfile = tempfile.mkstemp(suffix='.jams') _, audfile = tempfile.mkstemp(suffix='.wav') muda.save(audfile, jamfile, jam) jam2 = muda.load_jam_audio(jamfile, audfile) jam2_raw = jams.load(jamfile) os.unlink(audfile) os.unlink(jamfile) assert hasattr(jam2.sandbox, 'muda') assert '_audio' in jam2.sandbox.muda assert '_audio' not in jam2_raw.sandbox.muda eq_(jam2.file_metadata.duration, librosa.get_duration(**jam2.sandbox.muda['_audio']))
def test_save(jam_in, audio_file, strict, fmt): jam = muda.load_jam_audio(jam_in, audio_file) _, jamfile = tempfile.mkstemp(suffix='.jams') _, audfile = tempfile.mkstemp(suffix='.wav') muda.save(audfile, jamfile, jam, strict=strict, fmt=fmt) jam2 = muda.load_jam_audio(jamfile, audfile, fmt=fmt) jam2_raw = jams.load(jamfile, fmt=fmt) os.unlink(audfile) os.unlink(jamfile) assert hasattr(jam2.sandbox, 'muda') assert '_audio' in jam2.sandbox.muda assert '_audio' not in jam2_raw.sandbox.muda duration = librosa.get_duration(**jam2.sandbox.muda['_audio']) assert jam2.file_metadata.duration == duration
def test_save(): jam = muda.load_jam_audio('data/fixture.jams', 'data/fixture.wav') _, jamfile = tempfile.mkstemp(suffix='.jams') _, audfile = tempfile.mkstemp(suffix='.wav') muda.save(audfile, jamfile, jam) jam2 = muda.load_jam_audio(jamfile, audfile) jam2_raw = jams.load(jamfile) os.unlink(audfile) os.unlink(jamfile) assert hasattr(jam2.sandbox, 'muda') assert '_audio' in jam2.sandbox.muda assert '_audio' not in jam2_raw.sandbox.muda eq_(jam2.file_metadata.duration, librosa.get_duration(**jam2.sandbox.muda['_audio']))
def test_save(jam_in, audio_file, strict, fmt): jam = muda.load_jam_audio(jam_in, audio_file) _, jamfile = tempfile.mkstemp(suffix='.jams') _, audfile = tempfile.mkstemp(suffix='.wav') muda.save(audfile, jamfile, jam, strict=strict, fmt=fmt) jam2 = muda.load_jam_audio(jamfile, audfile, fmt=fmt) jam2_raw = jams.load(jamfile, fmt=fmt) os.unlink(audfile) os.unlink(jamfile) assert hasattr(jam2.sandbox, 'muda') assert '_audio' in jam2.sandbox.muda assert '_audio' not in jam2_raw.sandbox.muda duration = librosa.get_duration(**jam2.sandbox.muda['_audio']) assert jam2.file_metadata.duration == duration
def pitch_shifting(audio_fname, jams_fname, audio_folder, jams_folder, n_samples=5, l=-2, u=2): print(audio_folder, audio_fname) orig = muda.load_jam_audio(os.path.join(jams_folder, jams_fname), os.path.join(audio_folder, audio_fname)) pitchshift = muda.deformers.LinearPitchShift(n_samples=n_samples, lower=l, upper=u) for i, jam_out in enumerate(pitchshift.transform(orig)): muda.save(os.path.join(audio_folder, '{}_{}'.format(i, audio_fname)), os.path.join(jams_folder, '{}_{}'.format(i, jams_fname)), jam_out)
giantsteps_key = mirdata.initialize('giantsteps_key', data_home='/scratch/qx244/data/gskey') gs_data = giantsteps_key.load_tracks() with open('/scratch/qx244/data/gskey/good_files.json', 'r') as fp: good_files = json.load(fp) pitch_shifter = muda.deformers.LinearPitchShift(n_samples=12, lower=-5, upper=6) for idx in tqdm(good_files.keys()): track = gs_data[idx] track_jams_path = os.path.join('/scratch/qx244/data/gskey/jams/', track.title + '.jams') #check if already augmented: if os.path.isfile( '/scratch/qx244/data/gskey/augmentation/{}.11.jams'.format( track.title)): continue j_orig = muda.load_jam_audio(track_jams_path, track.audio_path) for i, jam_out in enumerate(pitch_shifter.transform(j_orig)): muda.save( '/scratch/qx244/data/gskey/augmentation/{}.{:02d}.ogg'.format( track.title, i), '/scratch/qx244/data/gskey/augmentation/{}.{:02d}.jams'.format( track.title, i), jam_out)
def deform_and_save(j_orig, deform, path): if hasattr(j_orig.sandbox.muda, '_audio'): jams_tmp = list(deform.transform(j_orig))[0] muda.save(path+'.wav', path+'.jams', jams_tmp)
original_wav_name = os.path.split(wav_path)[-1] original_wav_split = original_wav_name.split("_") suffix = "_".join([aug_str, instance_str]) # Generate path of augmented WAV file. wav_suffix = suffix + ".wav" augmented_wav_split = original_wav_split[:-1] + [wav_suffix] augmented_wav_name = "_".join(augmented_wav_split) augmented_wav_path = os.path.join(out_unit_dir, augmented_wav_name) # Generate path of augmented JAMS file. jam_suffix = suffix + ".jams" augmented_jam_split = original_wav_split[:-1] + [jam_suffix] augmented_jam_name = "_".join(augmented_jam_split) augmented_jam_path = os.path.join(out_unit_dir, augmented_jam_name) # Export augmented audio and metadata. muda.save(augmented_wav_path, augmented_jam_path, jam) # Print elapsed time. print(str(datetime.datetime.now()) + " Finish.") elapsed_time = time.time() - int(start_time) elapsed_hours = int(elapsed_time / (60 * 60)) elapsed_minutes = int((elapsed_time % (60 * 60)) / 60) elapsed_seconds = elapsed_time % 60. elapsed_str = "{:>02}:{:>02}:{:>05.2f}".format(elapsed_hours, elapsed_minutes, elapsed_seconds) print("Total elapsed time: " + elapsed_str + ".")
os.makedirs(category_path) for file_name in files: file_path = os.path.join(category_path, file_name.rsplit('.', 1)[0]) j_orig = muda.load_jam_audio(TEMPLATE_FILE_NAME, os.path.join(SRC_PATH, class_id, file_name)) # for pitch_shift in PITCH_SHIFT_LIST: pitch = muda.deformers.PitchShift(n_semitones=pitch_shift) jam_out = pitch.transform(j_orig).next() wav_filename = file_path + '_pitch_{0:+}.wav'.format(pitch_shift) jams_filename = wav_filename.replace('.wav', '.jams') muda.save(wav_filename, jams_filename, jam_out) stretch = muda.deformers.LogspaceTimeStretch( n_samples=TIME_STRETCH['samples'], lower=TIME_STRETCH['lower_bound'], upper=TIME_STRETCH['upper_bound']) for i, jam_out in enumerate(stretch.transform(j_orig)): wav_filename = file_path + '_stretch_' \ + str(TIME_STRETCH['lower_bound']) \ + '_' + str(TIME_STRETCH['upper_bound'])\ + '_{:02d}.wav'.format(i) jams_filename = wav_filename.replace('.wav', '.jams') muda.save(wav_filename, jams_filename, jam_out)