File size: 3,183 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
132
133
134
135
136
137
138
139
140
141
142
143
144
"""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 pqdm.processes import pqdm

import librosa


INPUT_DIR = "mp3s"
OUTPUT_DIR = "audio_features"
OUT_JSON = "info.json"
N_JOBS = 12

# Features params
SR = 24000
N_FFT = 2048
HOP_LENGTH = 1024
WINDOW = "hann"
CENTER = True
PAD_MODE = "constant"
POWER = 2.0
N_MELS = 256
MEL_FMIN = 30
MEL_FMAX = 12000


def compute_melspecs(audio):
    """Computes a mel-spectrogram from the given audio data."""
    return librosa.feature.melspectrogram(
        y=audio,
        sr=SR,
        n_fft=N_FFT,
        hop_length=HOP_LENGTH,
        window=WINDOW,
        center=CENTER,
        pad_mode=PAD_MODE,
        power=POWER,
        n_mels=N_MELS,
        fmin=MEL_FMIN,
        fmax=MEL_FMAX,
    )


def compute_all_features(mp3_file, output_dir):
    """Computes all the audio features."""
    # Decode and read mp3
    audio, _ = librosa.load(mp3_file, sr=SR)

    # Compute mels
    mel = compute_melspecs(audio)

    # Save
    out_file = os.path.join(
        output_dir, os.path.basename(mp3_file).replace(".mp3", "-mel.npy")
    )
    np.save(out_file, mel)


def save_params(output_dir):
    """Saves the parameters to a JSON file."""
    out_json = os.path.join(output_dir, OUT_JSON)
    out_dict = {
        "librosa_version": librosa.__version__,
        "numpy_version": np.__version__,
        "SR": SR,
        "N_FFT": N_FFT,
        "HOP_LENGTH": HOP_LENGTH,
        "WINDOW": WINDOW,
        "CENTER": CENTER,
        "PAD_MODE": PAD_MODE,
        "POWER": POWER,
        "N_MELS": N_MELS,
        "MEL_FMIN": MEL_FMIN,
        "MEL_FMAX": MEL_FMAX,
    }
    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
    pqdm_args = [[mp3_file, args.output_dir] for mp3_file in mp3s]

    pqdm(
        pqdm_args,
        compute_all_features,
        n_jobs=args.n_jobs,
        argument_type="args",
    )

    # Save parameters
    save_params(args.output_dir)

    # Done!
    print("Done! Took %.2f seconds." % (time.time() - start_time))