File size: 3,997 Bytes
4de2f2a |
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 |
"""Script to compute audio features from the
original Harmonix audio files.
Created by Oriol Nieto.
"""
import argparse
import glob
import json
import os
import time
import numpy as np
from joblib import Parallel, delayed
import madmom
from madmom.processors import ParallelProcessor, SequentialProcessor
from madmom.audio.signal import SignalProcessor, FramedSignalProcessor
from madmom.audio.stft import ShortTimeFourierTransformProcessor
from madmom.audio.spectrogram import (
FilteredSpectrogramProcessor, LogarithmicSpectrogramProcessor,
SpectrogramDifferenceProcessor)
INPUT_DIR = "mp3s"
OUTPUT_DIR = "madmom_features"
OUT_JSON = "info.json"
N_JOBS = 12
# Features params
SR = 44100
FRAME_SIZES = [1024, 2048, 4096]
NUM_BANDS = [3, 6, 12]
FPS = 100
FMIN = 30
FMAX = 17000
DIFF_RATIO = 0.5
def compute_all_features(mp3_file, output_dir):
"""Computes all the audio features."""
sig = SignalProcessor(num_channels=1, sample_rate=SR)
# process the multi-resolution spec & diff in parallel
multi = ParallelProcessor([])
for frame_size, num_band in zip(FRAME_SIZES, NUM_BANDS):
frames = FramedSignalProcessor(frame_size=frame_size, fps=FPS)
stft = ShortTimeFourierTransformProcessor() # caching FFT window
filt = FilteredSpectrogramProcessor(
num_bands=num_band, fmin=FMIN, fmax=FMAX, norm_filters=True)
spec = LogarithmicSpectrogramProcessor(mul=1, add=1)
diff = SpectrogramDifferenceProcessor(
diff_ratio=DIFF_RATIO, positive_diffs=True, stack_diffs=np.hstack)
# process each frame size with spec and diff sequentially
multi.append(SequentialProcessor((frames, stft, filt, spec, diff)))
# stack the features and processes everything sequentially
pre_processor = SequentialProcessor((sig, multi, np.hstack))
# Compute mels
feat = pre_processor(mp3_file)
# Save
out_file = os.path.join(
output_dir, os.path.basename(mp3_file).replace(".mp3", "-seq.npy"))
np.save(out_file, feat)
def save_params(output_dir):
"""Saves the parameters to a JSON file."""
out_json = os.path.join(output_dir, OUT_JSON)
out_dict = {
"madmom_version": madmom.__version__,
"numpy_version": np.__version__,
"SR": SR,
"FRAME_SIZES": FRAME_SIZES,
"NUM_BANDS": NUM_BANDS,
"FPS": FPS,
"FMIN": FMIN,
"FMAX": FMAX,
"DIFF_RATIO": DIFF_RATIO
}
with open(out_json, 'w') as fp:
json.dump(out_dict, fp, indent=4)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Computes audio features for the Harmonix set.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-i",
"--input_dir",
default=INPUT_DIR,
action="store",
help="Path to the Harmonix set audio.")
parser.add_argument("-o",
"--output_dir",
default=OUTPUT_DIR,
action="store",
help="Output directory.")
parser.add_argument("-j",
"--n_jobs",
default=N_JOBS,
action="store",
type=int,
help="Number of jobs to run in parallel.")
args = parser.parse_args()
start_time = time.time()
# Create output dir if doesn't exist
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
# Read mp3s
mp3s = glob.glob(os.path.join(args.input_dir, "*.mp3"))
# Compute features for each mp3 in parallel
Parallel(n_jobs=args.n_jobs)(
delayed(compute_all_features)(mp3_file, args.output_dir)
for mp3_file in mp3s)
# Save parameters
save_params(args.output_dir)
# Done!
print("Done! Took %.2f seconds." % (time.time() - start_time))
|