| import numpy as np |
| import soundfile as sf |
| import csv |
| from pathlib import Path |
| import librosa |
| import hashlib |
| import argparse |
| import warnings |
| |
| warnings.filterwarnings("ignore", category=Warning) |
|
|
| INPUT_FOLDER = "Aebersold" |
| OUTPUT_FOLDER = "Backing" |
| SCRIPTS_FOLDER = "Scripts" |
| CSV_FILENAME = "Edit_Data.csv" |
| CSV_CHECKSUM = "799806dbc9756b3baf0c7df8027dea95" |
| CROSS_FADE_SAMPLES = 4410 |
| SR = 44100 |
| NORMALISE_TARGET = 0.95 |
| VERSION_OPTIONS = ['full', 'lite'] |
| INPUT_OPTIONS = ['-version', '-overwrite'] |
|
|
| def md5(fname): |
| hash_md5 = hashlib.md5() |
| with open(fname, "rb") as f: |
| for chunk in iter(lambda: f.read(4096), b""): |
| hash_md5.update(chunk) |
| return hash_md5.hexdigest() |
|
|
| def check_files(): |
| |
| backing_path = Path.cwd() / OUTPUT_FOLDER |
| if not backing_path.exists(): |
| raise OSError("Error: folder structure incomplete. Run this script inside the Filosax folder, which should contain a 'Backing' folder.") |
|
|
| |
| aebersold_path = Path.cwd() / INPUT_FOLDER |
| if not aebersold_path.exists(): |
| raise OSError("Error: folder structure incomplete. Run this script inside the Filosax folder, which should contain an 'Aebersold' folder.") |
|
|
| |
| csv_path = Path.cwd() / SCRIPTS_FOLDER / CSV_FILENAME |
| if not csv_path.exists(): |
| raise OSError("Error: %s file missing." % CSV_FILENAME) |
| if md5(csv_path) != CSV_CHECKSUM: |
| raise OSError("Error: %s wrong file version." % CSV_FILENAME) |
| |
| def load_data(): |
| |
| csv_path = Path.cwd() / SCRIPTS_FOLDER / CSV_FILENAME |
| csv_data = [] |
| with open(csv_path) as csv_file: |
| csv_reader = csv.reader(csv_file, delimiter=',') |
| line_count = 0 |
| for row in csv_reader: |
| if line_count == 0: |
| line_count += 1 |
| else: |
| csv_data.append(row) |
| return csv_data |
|
|
| def crossfade_ramps(sample_count, fade_type='equal_power'): |
| if fade_type == 'equal_power': |
| in_ramp = np.zeros(sample_count) |
| for i in np.arange(sample_count): |
| in_ramp[i] = np.sqrt(i/sample_count) |
| out_ramp = np.flip(in_ramp) |
| else: |
| |
| in_ramp = np.linspace(0, 1, sample_count) |
| out_ramp = np.flip(in_ramp) |
| return in_ramp, out_ramp |
|
|
| def perform_edits(filosax_version='full', overwrite_files=False): |
|
|
| num_files = 0 |
| if filosax_version == 'full': |
| num_files = 48 |
| if filosax_version == 'lite': |
| num_files = 5 |
|
|
| csv_data = load_data() |
| in_ramp, out_ramp = crossfade_ramps(CROSS_FADE_SAMPLES) |
| num_files_processed = 0 |
|
|
| |
| for track_num in np.arange(0, num_files): |
| track_data = csv_data[track_num] |
| track_id = track_data[0] |
| track_name = track_data[1] |
| print("Processing Track", track_id, ": '%s'... " % track_name, end = '') |
| |
| |
| this_folder = Path.cwd() / OUTPUT_FOLDER / track_id |
| if not this_folder.exists(): |
| print("Backing folder does not exist.") |
| continue |
| |
| |
| file_name = track_data[3] |
| file_ext = track_data[4] |
| aebersold_path = Path.cwd() / INPUT_FOLDER / (file_name + file_ext) |
| if not aebersold_path.exists(): |
| print("Aebersold file does not exist.") |
| continue |
| if (md5(aebersold_path) != track_data[5]): |
| print("Aebersold file wrong version.") |
| continue |
|
|
| |
| if not(overwrite_files): |
| bass_drums_file = this_folder / "Bass_Drums.wav" |
| piano_drums_file = this_folder / "Piano_Drums.wav" |
| if bass_drums_file.exists() or piano_drums_file.exists(): |
| print("One or both backing tracks already exists.") |
| continue |
| |
| |
| track_length = int(track_data[7]) |
| bass_drums = np.zeros(track_length) |
| piano_drums = np.zeros(track_length) |
| file_adjust = int(track_data[6]) |
| num_edits = int(track_data[9]) |
| fade_dur = int(track_data[8]) |
| |
| audio_data, sr = librosa.load(aebersold_path, sr=SR, mono=False) |
| audio_data_l = audio_data[0] |
| audio_data_r = audio_data[1] |
| |
| |
| audio_data_l = librosa.util.normalize(audio_data_l) * NORMALISE_TARGET |
| audio_data_r = librosa.util.normalize(audio_data_r) * NORMALISE_TARGET |
| |
| |
| for n in np.arange(num_edits): |
| file_ref = int(track_data[10+(n*3)]) |
| edit_start = int(track_data[11+(n*3)]) |
| edit_dur = int(track_data[12+(n*3)]) |
| if (n > 0): |
| edit_start -= CROSS_FADE_SAMPLES |
| edit_dur += CROSS_FADE_SAMPLES |
| |
| |
| |
| start_sample = edit_start - file_ref - file_adjust |
| end_sample = start_sample + edit_dur |
| edit_end = edit_start + edit_dur |
| if edit_end > track_length: |
| edit_end = track_length |
| end_sample = start_sample + (edit_end - edit_start) |
| |
| |
| |
| l_splice = audio_data_l[start_sample:end_sample].copy() |
| r_splice = audio_data_r[start_sample:end_sample].copy() |
| |
| |
| |
| l_splice[:CROSS_FADE_SAMPLES] = l_splice[:CROSS_FADE_SAMPLES] * in_ramp |
| r_splice[:CROSS_FADE_SAMPLES] = r_splice[:CROSS_FADE_SAMPLES] * in_ramp |
| |
| if (n == num_edits-1) and (fade_dur > 0) and (fade_dur <= len(l_splice)): |
| |
| fade_ramp = np.flip(np.linspace(0, 1, fade_dur+1, endpoint=False))[:-1] |
| l_splice[-fade_dur:] = l_splice[-fade_dur:] * fade_ramp |
| r_splice[-fade_dur:] = r_splice[-fade_dur:] * fade_ramp |
| else: |
| |
| l_splice[-CROSS_FADE_SAMPLES:] = l_splice[-CROSS_FADE_SAMPLES:] * out_ramp |
| r_splice[-CROSS_FADE_SAMPLES:] = r_splice[-CROSS_FADE_SAMPLES:] * out_ramp |
| |
| bass_drums[edit_start:edit_end] += l_splice |
| piano_drums[edit_start:edit_end] += r_splice |
| |
| |
| bass_drums_file = this_folder / "Bass_Drums.wav" |
| piano_drums_file = this_folder / "Piano_Drums.wav" |
| sf.write(bass_drums_file, bass_drums, sr, subtype="PCM_24") |
| sf.write(piano_drums_file, piano_drums, sr, subtype="PCM_24") |
| print("Edited backing tracks created.") |
| num_files_processed += 1 |
|
|
| print("***") |
| print("%d of %d files processed." % (num_files_processed, num_files)) |
| print("***") |
| |
| def main(args): |
| check_files() |
| perform_edits(args.version, args.overwrite) |
| |
| if __name__ == "__main__": |
| PARSER = argparse.ArgumentParser(description="Make Filosax backing edits") |
| PARSER.add_argument("-version", choices=VERSION_OPTIONS, default='full', help="which Filosax version?") |
| PARSER.add_argument("-overwrite", action="store_true", help="overwrite existing?") |
| main(PARSER.parse_args()) |
|
|