File size: 7,544 Bytes
4048d4f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import numpy as np
import soundfile as sf
import csv
from pathlib import Path
import librosa
import hashlib
import argparse
import warnings
# Silence librosa MP3 warning
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():
# Check if backing folder exists
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.")
# Check if Aebersold folder exists
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.")
# Check if CSV file exists and matches checksum
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():
# Load data from CSV file
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:
# Linear ramps
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
# Iterate over tracks
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 = '')
# Does folder exist?
this_folder = Path.cwd() / OUTPUT_FOLDER / track_id
if not this_folder.exists():
print("Backing folder does not exist.")
continue
# Does Aebersold track exist? Does it match the checksum?
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
# Do the files already exist?
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
# If all this works, proceed
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]
# Normalise audio data
audio_data_l = librosa.util.normalize(audio_data_l) * NORMALISE_TARGET
audio_data_r = librosa.util.normalize(audio_data_r) * NORMALISE_TARGET
# Iterate over edits
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
#print(file_adjust, file_ref, edit_start, edit_dur)
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)
#print(start_sample, end_sample, edit_start, edit_end)
l_splice = audio_data_l[start_sample:end_sample].copy()
r_splice = audio_data_r[start_sample:end_sample].copy()
# Cross fade in
#if (n > 0):
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)):
# Longer fade out for final edit if required
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:
# Cross fade out
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
# Export audio files
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())
|