def segment_spec_custom(key, df, DT_ID, save=False, plot=False): # load wav rate, data = load_wav(df.data["wav_loc"]) # filter data data = butter_bandpass_filter(data, butter_min, butter_max, rate) # segment # results = dynamic_threshold_segmentation( # data, # rate, # n_fft=n_fft, # hop_length_ms=hop_length_ms, # win_length_ms=win_length_ms, # min_level_db_floor=min_level_db_floor, # db_delta=db_delta, # ref_level_db=ref_level_db, # pre=pre, # min_silence_for_spec=min_silence_for_spec, # max_vocal_for_spec=max_vocal_for_spec, # min_level_db=min_level_db, # silence_threshold=silence_threshold, # verbose=True, # min_syllable_length_s=min_syllable_length_s, # spectral_range=spectral_range, # ) results = dynamic_threshold_segmentation(data, hparams, verbose=True, min_syllable_length_s=min_syllable_length_s, spectral_range=spectral_range) if results is None: return if plot: plot_segmentations( results["spec"], results["vocal_envelope"], results["onsets"], results["offsets"], hop_length_ms, rate, figsize=(15, 3) ) plt.show() # save the results json_out = DATA_DIR / "processed" / (DATASET_ID + "_segmented") / DT_ID / "JSON" / ( key + ".JSON" ) json_dict = df.data.copy() json_dict["indvs"][list(df.data["indvs"].keys())[0]]["syllables"] = { "start_times": list(results["onsets"]), "end_times": list(results["offsets"]), } json_txt = json.dumps(json_dict, cls=NoIndentEncoder, indent=2) # save json if save: ensure_dir(json_out.as_posix()) with open(json_out.as_posix(), "w") as json_file: json.dump(json_dict, json_file, cls=NoIndentEncoder, indent=2) json_file.close() # print(json_txt, file=open(json_out.as_posix(), "w")) #print(json_txt) return results
show_cbar=True, figsize=(20, 6), ) # segment results = dynamic_threshold_segmentation(data, hparams, verbose=True, min_syllable_length_s=min_syllable_length_s, spectral_range=spectral_range) plot_segmentations( results["spec"], results["vocal_envelope"], results["onsets"], results["offsets"], int(hparams.hop_length_ms), int(hparams.sample_rate), figsize=(15,5) ) plt.show() # Function for batch processing all segments import joblib import json from avgn.utils.json import NoIndent, NoIndentEncoder def segment_spec_custom(key, df, DT_ID, save=False, plot=False): # load wav rate, data = load_wav(df.data["wav_loc"]) # filter data