os.chdir('..') if os.path.exists(os.path.join(os.getcwd(), folder_name)): shutil.rmtree(os.path.join(os.getcwd(), folder_name)) os.makedirs(os.path.join(os.getcwd(), folder_name)) os.chdir(os.path.join(os.getcwd(), folder_name)) return os.getcwd() filepath = '/Users/harrison/Desktop/Thesis_Test/Kesha - Cmon copy/Stems' csv_path = create_folder_above(filepath) for stem in os.listdir(filepath): file_name, file_extension = os.path.splitext(stem) if file_extension == '.wav': # Get discretized audio to run through features print "Importing %s for analysis" % file_name data, fs, t = feature_extraction.import_audio(os.path.join(filepath, stem)) # Calculate Dynamics Features print "Calculating dynamics features for %s" % file_name loudness_momentary, t_ = feature_extraction.calc_loudness(data, measurement = 'momentary') print "done!" loudness_short, t_ = feature_extraction.calc_loudness(data, measurement = 'short') print "done!" loudness_integrated = feature_extraction.calc_loudness(data, measurement = 'integrated') print "done!" lra = feature_extraction.calc_loudness(data, measurement = 'lra') print "done!" crest_factor_1s, t_ = feature_extraction.calc_crest_factor(data, win_size=1000, fs=44100) print "done!" crest_factor_100ms, t_ = feature_extraction.calc_crest_factor(data, win_size=100, fs=44100) print "done!"
def normalize_multitracks(filepath): """ filepath: string pointing to audio file on disk This function takes a directory of .wav multi-track files and normalizes them such that the track with the highest peak-level is normalized as normal, and then all other tracks are raised the same amount of dB as the track with the highest peak-level. returns: A new directory of normalized data """ peak_amplitudes = [] tracks = os.listdir(filepath) norm_path = filepath + "/normalized_tracks" # Create the normalized data folder to house all our normalized data print("Creating folder to store normalized data") create_folder(norm_path) # Scan tracks, and get the peak amplitudes of each track for track in tracks: file_name, file_extension = os.path.splitext(track) if file_extension == '.wav': data, fs, t = feature_extraction.import_audio(filepath + "/" + track) peak_amplitude = calc_peak(data) peak_amplitudes.append(peak_amplitude) # Find the highest amplitude track by index idxs_norm = np.argwhere(peak_amplitudes == np.amax(peak_amplitudes)) idxs_norm = idxs_norm.flatten().tolist() # Normalize the highest amplitude tracks and write them out track_num = -1 for track in tracks: file_name, file_extension = os.path.splitext(track) if file_extension == '.wav': track_num += 1 if track_num in idxs_norm: data, fs, t = feature_extraction.import_audio(filepath + "/" + track) write_path = (norm_path + "/" + file_name + "_normalized" + file_extension) print("Normalizing %s") % track librosa.output.write_wav(write_path, data, sr=44100, norm=True) # Find the indexes of all the other tracks that need to be raised relative to those normalized to 0 idxs_relative = np.argwhere(peak_amplitudes != np.amax(peak_amplitudes)) idxs_relative = idxs_relative.flatten().tolist() # This value will be used to scale all other tracks to those normalized scalar = np.amax(peak_amplitudes) # Raise the amplitude of all tracks the same amount that those normalized to 0 were raised track_num = -1 for track in tracks: file_name, file_extension = os.path.splitext(track) if file_extension == '.wav': track_num += 1 if track_num in idxs_relative: data, fs, t = feature_extraction.import_audio(filepath + "/" + track) # Perform the Relative Normalization data = data/scalar write_path = (norm_path + "/" + file_name + "_normalized" + file_extension) print("Normalizing %s") % track librosa.output.write_wav(write_path, data, sr=44100, norm=False)