-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautomatic_bass_line_transcription.py
77 lines (55 loc) · 2.85 KB
/
automatic_bass_line_transcription.py
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
import os
import argparse
import numpy as np
from ablt.utilities import read_track_dicts
from ablt.bass_line_extractor import extract_single_bass_line
from ablt.bass_line_transcriber import transcribe_single_bass_line
from ablt.directories import OUTPUT_DIR, TRACK_DICTS_PATH, AUDIO_DIR
from ablt.constants import HOP_RATIO, M
# TODO: integrate parallel processing
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Bassline Transcription Parameters.')
parser.add_argument('-a', '--audio-dir', type=str, help="Directory containing all the audio files.", default=AUDIO_DIR)
parser.add_argument('-n', '--n-bars', type=int, help="Number of chorus bars to extract.", default=4)
parser.add_argument('-f', '--hop-ratio', type=int, help="Number of F0 samples that makes up a beat.", default=HOP_RATIO)
parser.add_argument('-t', '--track-dicts', action="store_true", help="Use a track_dicts.json file.")
args = parser.parse_args()
audio_dir = args.audio_dir
N_bars = args.n_bars
hop_ratio = args.hop_ratio
# Load BPM values if provided
if args.track_dicts:
track_dicts = read_track_dicts(TRACK_DICTS_PATH)
else:
track_dicts = None
if os.path.isfile(audio_dir): # if a single file is specified
title = os.path.splitext(os.path.basename(audio_dir))[0]
if track_dicts is None:
BPM = 0
else:
BPM = track_dicts[title]['BPM']
extract_single_bass_line(audio_dir, N_bars=N_bars, separator=None, BPM=BPM)
# Update with the estimated BPM
if track_dicts is None:
BPM_path = os.path.join(OUTPUT_DIR, title, 'beat_grid', 'BPM.npy')
BPM = np.load(BPM_path)
bassline_path = os.path.join(OUTPUT_DIR, title, 'bass_line', title+'.npy')
transcribe_single_bass_line(bassline_path, BPM=BPM, M=M,
N_bars=N_bars, hop_ratio=hop_ratio)
else: # if a folder of audio files is specified
audio_names = os.listdir(audio_dir)
for title_ext in audio_names:
title = os.path.splitext(title_ext)[0]
if track_dicts is None:
BPM = 0
else:
BPM = track_dicts[title]['BPM']
audio_path = os.path.join(audio_dir, title_ext)
extract_single_bass_line(audio_path, N_bars=N_bars, separator=None, BPM=BPM)
# Update with the estimated BPM
if track_dicts is None:
BPM_path = os.path.join(OUTPUT_DIR, title, 'beat_grid', 'BPM.npy')
BPM = np.load(BPM_path)
bassline_path = os.path.join(OUTPUT_DIR, title, 'bass_line', title+'.npy')
transcribe_single_bass_line(bassline_path, BPM=BPM, M=M,
N_bars=N_bars, hop_ratio=hop_ratio)