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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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import json
import pretty_midi
import pypianoroll
import hdf5_getters
from tqdm import tqdm
import os
import concurrent.futures
import collections
import utils
from glob import glob
import pandas as pd
import csv
from copy import deepcopy

"""
Written by Serkan Sulun

Creates labels for Lakh MIDI (or pianoroll) dataset.
Labels include low-level MIDI features such as tempo, note density and number of MIDI files.
They also include high-level features obtained from Spotify Developer API, such as valence, energy, etc.

See utils.py and fill in the variables client_id and client_secret.

When the user quota is exceeded, Spotify blocks access and the script gets stuck.
In that case, you may need to re-run the script some time later,
or use a different account with different client ID and secret.
"""

def run_parallel(func, my_iter):
    # Parallel processing visualized with tqdm
    with concurrent.futures.ProcessPoolExecutor() as executor:
        results = list(tqdm(executor.map(func, my_iter), total=len(my_iter)))
    return results

write = False
redo = True

main_output_dir = "../../data_files/features"
os.makedirs(main_output_dir, exist_ok=True)

match_scores_path = "../../data_files/match_scores.json"
msd_summary_path = "../../data_files/msd_summary_file.h5"
echonest_folder_path = "../../data_files/millionsongdataset_echonest"

use_pianoroll_dataset = True
if use_pianoroll_dataset:
    midi_dataset_path = "../../data_files/lpd_full/lpd/lpd_full"
    extension = ".npz"
    output_dir = os.path.join(main_output_dir, "pianoroll")
else:
    midi_dataset_path = "lmd_full"
    extension = ".mid"
    output_dir = os.path.join(main_output_dir, "midi")
os.makedirs(output_dir, exist_ok=True)

### PART I: Map track_ids (in midi dataset) to Spotify features

### 1- Create mappings track_id (in midi dataset) -> metadata (in Echonest)

output_path = os.path.join(output_dir, "trackid_to_songid.json")

with open(match_scores_path, "r") as f:
    match_scores = json.load(f)

track_ids = sorted(list(match_scores.keys()))

if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        trackid_to_songid = json.load(f)
else:
    h5_msd = hdf5_getters.open_h5_file_read(msd_summary_path)
    n_msd = hdf5_getters.get_num_songs(h5_msd)

    trackid_to_songid = {}
    print("Adding metadata to each track in Lakh dataset")

    for i in tqdm(range(n_msd)):
        track_id = hdf5_getters.get_track_id(h5_msd, i).decode("utf-8")
        if track_id in track_ids:
            # get data from MSD
            song_id = hdf5_getters.get_song_id(h5_msd, i).decode("utf-8")
            artist = hdf5_getters.get_artist_name(h5_msd, i).decode("utf-8")
            title = hdf5_getters.get_title(h5_msd, i).decode("utf-8")
            release = hdf5_getters.get_release(h5_msd, i).decode("utf-8")
            trackid_to_songid[track_id] = {"song_id": song_id,"title": title, 
                            "artist": artist, "release": release}

    # sort
    trackid_to_songid = collections.OrderedDict(sorted(trackid_to_songid.items()))
    if write:
        with open(output_path, "w") as f:
            json.dump(trackid_to_songid, f, indent=4)
            print(f"Output saved to {output_path}")

### 2- Create mappings metadata (in Echonest) -> Spotify IDs
output_path = os.path.join(output_dir, "songid_to_spotify.json")
if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        songid_to_spotify = json.load(f)
else:
    song_ids = sorted([val["song_id"] for val in trackid_to_songid.values()])
    songid_to_spotify = {}
    print("Mapping Echonest song IDs to Spotify song IDs")
    for song_id in tqdm(song_ids):
        file_path = os.path.join(echonest_folder_path, song_id[2:4], song_id + ".json")
        spotify_ids = utils.get_spotify_ids(file_path)
        songid_to_spotify[song_id] = spotify_ids
    if write:
        with open(output_path, "w") as f:
            json.dump(songid_to_spotify, f, indent=4)
            print(f"Output saved to {output_path}")


### 3- Merge and add Spotify features
output_path = os.path.join(output_dir, "trackid_to_spotify_features.json")
# When user quota is exceeded, Spotify blocks access and the script gets stuck.
# In that case, you may need to re-run the script some time later,
# or use a different account with different client ID and secret.
# So we keep an incomplete csv file, so that we can continue later from where we left.
output_path_incomplete = os.path.join(output_dir, "incomplete_trackid_to_spotify_features.csv")

if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        trackid_to_spotify_features = json.load(f)
else:
    fieldnames = ["track_id", "song_id", "title", "artist", "release",
        "spotify_id", "spotify_title", "spotify_artist", "spotify_album", "spotify_audio_features"]

    data_to_process = deepcopy(trackid_to_songid)
    write_header = True

    if os.path.exists(output_path_incomplete):
        # Continue from where we've left
        data_already_processed = utils.read_csv(output_path_incomplete)
        track_ids_already_processed = [entry["track_id"] for entry in data_already_processed]
        data_to_process = {key: value for key, value in data_to_process.items() if key not in track_ids_already_processed}
        write_header = False

    with open(output_path_incomplete, "a") as f_out:
        csv_writer = csv.DictWriter(f_out, fieldnames=fieldnames)
        if write_header:
            csv_writer.writeheader()

        print("Adding Spotify features")
        for track_id, data in tqdm(data_to_process.items()):
            data["track_id"] = track_id
            album = data["release"]
            spotify_ids = songid_to_spotify[data["song_id"]]
            if spotify_ids == []:
                # use metadata to search spotify
                best_spotify_track = utils.search_spotify_flexible(data["title"], data["artist"], data["release"])
            else:
                spotify_tracks = utils.get_spotify_tracks(spotify_ids)
                if spotify_tracks == None:
                    for key in ["id", "title", "artist", "album", "audio_features"]:
                        data["spotify_" + key] = None
                elif len(spotify_tracks) > 1:
                    # find best spotify id by comparing album names
                    best_match_score = 0
                    best_match_ind = 0
                    for i, track in enumerate(spotify_tracks):
                        if track is not None:
                            spotify_album = track["album"]["name"] if track is not None else ""
                            match_score = utils.matching_strings_flexible(album, spotify_album)
                            
                            if match_score > best_match_score:
                                best_match_score = match_score
                                best_match_ind = i

                    best_spotify_track = spotify_tracks[best_match_ind]
                else:
                    best_spotify_track = spotify_tracks[0]
            
            if best_spotify_track is not None:
                spotify_id = best_spotify_track["uri"].split(":")[-1]
                spotify_audio_features = utils.get_spotify_features(spotify_id)[0]

                # if spotify_audio_features["valence"] == 0.0:
                #     # A large portion of files have 0.0 valence, although they are NaNs
                #     spotify_audio_features["valence"] = float("nan")
                spotify_artists = ", ".join([artist["name"] for artist in best_spotify_track["artists"]])

                data["spotify_id"] = spotify_id
                data["spotify_title"] = best_spotify_track['name']
                data["spotify_artist"] = spotify_artists
                data["spotify_album"] = best_spotify_track["album"]["name"]
                data["spotify_audio_features"] = spotify_audio_features
            else:
                for key in ["id", "title", "artist", "album", "audio_features"]:
                    data["spotify_" + key] = None
            
            csv_writer.writerow(data)

    # Now write final data to json
    trackid_to_spotify_features_list = utils.read_csv(output_path_incomplete)
    trackid_to_spotify_features = {}
    # unlike json, csv doesnt support dict within dict, so convert it to dict manually
    for item in trackid_to_spotify_features_list:
        spotify_audio_features = item["spotify_audio_features"]
        if spotify_audio_features != "":
            spotify_audio_features = eval(spotify_audio_features)
        item["spotify_audio_features"] = spotify_audio_features
        track_id = deepcopy(item["track_id"])
        del item["track_id"]
        trackid_to_spotify_features[track_id] = item

    if write:   
        with open(output_path, "w") as f:
            json.dump(trackid_to_spotify_features, f, indent=4)
            print(f"Output saved to {output_path}")


### PART II: Dealing with symbolic music data
### 4- Revert matching scores
""" Matched data has the format: track_ID -> midi_file 
where multiple tracks could be mapped to a single midi file.
We want to revert this mapping and then keep unique midi files
Revert match scores file to have mapping midi_file -> track_ID
"""

output_path = os.path.join(output_dir, "match_scores_reverse.json")
if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        match_scores_reversed = json.load(f)
else:
    with open(match_scores_path, "r") as f:
        in_data = json.load(f)
    match_scores_reversed = {}
    print("Reversing match scores.")
    for track_id, matching in tqdm(in_data.items()):
        for file_, score in matching.items():
            if file_ not in match_scores_reversed.keys():
                match_scores_reversed[file_] = {track_id: score}
            else:
                match_scores_reversed[file_][track_id] = score

    # order match scores
    for k in match_scores_reversed.keys():
        match_scores_reversed[k] = collections.OrderedDict(sorted(match_scores_reversed[k].items(), reverse=True, key=lambda x: x[-1]))

    # order filenames
    match_scores_reversed = collections.OrderedDict(sorted(match_scores_reversed.items(), key=lambda x: x[0]))
    if write:
        with open(output_path, "w") as f:
            json.dump(match_scores_reversed, f, indent=4)
            print(f"Output saved to {output_path}")

# 5- Filter match scores to only keep best match
output_path = os.path.join(output_dir, "best_match_scores.json")
if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        best_match_scores_reversed = json.load(f)
else:
    best_match_scores_reversed = {}
    print("Selecting best matching tracks.")
    for midi_file, match in tqdm(match_scores_reversed.items()):
        best_match_scores_reversed[midi_file] = list(match.items())[0]
    if write:
        with open(output_path, "w") as f:
            json.dump(best_match_scores_reversed, f, indent=4)
            print(f"Output saved to {output_path}")

### 6- Filter unique midis
"""LMD was created by creating hashes for the entire files
and then keeping files with unique hashes.
However, some files' musical content are the same, and only their metadata are different.
So we hash the content (pianoroll array), and further filter out the unique ones."""
# Create hashes for midis

output_path = os.path.join(output_dir, "hashes.json")

if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        midi_file_to_hash = json.load(f)
else:
    def get_hash_and_file(path):
        hash_ = utils.get_hash(path)
        file_ = os.path.basename(path)
        file_ = file_[:-4]
        return [file_, hash_]

    file_paths = sorted(glob(midi_dataset_path + "/**/*" + extension, recursive=True))
    assert len(file_paths) > 0, f"No MIDI files found at {midi_dataset_path}"
    print("Getting hashes for MIDIs.")
    midi_file_to_hash = run_parallel(get_hash_and_file, file_paths)
    midi_file_to_hash = sorted(midi_file_to_hash, key=lambda x:x[0])
    midi_file_to_hash = dict(midi_file_to_hash)
    if write:
        with open(output_path, "w") as f:
            json.dump(midi_file_to_hash, f, indent=4)
            print(f"Output saved to {output_path}")

# also do the reverse hash -> midi
output_path = os.path.join(output_dir, "unique_files.json")
if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        midi_files_unique = json.load(f)
else:
    hash_to_midi_file = {}
    for midi_file, hash in midi_file_to_hash.items():
        try:
            best_match_score = best_match_scores_reversed[midi_file][1]
        except:
            best_match_score = 0
        if hash in hash_to_midi_file.keys():
            hash_to_midi_file[hash].append((midi_file, best_match_score))
        else:
            hash_to_midi_file[hash] = [(midi_file, best_match_score)]

    midi_files_unique = []
    # Get unique midis (with highest match score)
    print("Getting unique MIDIs.")
    for hash, midi_files_and_match_scores in hash_to_midi_file.items():
        if hash != "empty_pianoroll":
            midi_files_and_match_scores = sorted(midi_files_and_match_scores, key=lambda x: x[1], reverse=True)
            midi_files_unique.append(midi_files_and_match_scores[0][0])
    if write:
        with open(output_path, "w") as f:
            json.dump(midi_files_unique, f, indent=4)
            print(f"Output saved to {output_path}")

# create unique matched midis list
midi_files_matched = list(match_scores_reversed.keys())

output_path = os.path.join(output_dir, "midis_matched_unique.json")
if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        midi_files_matched_unique = json.load(f)
else:
    print("Getting unique matched MIDIs.")
    midi_files_matched_unique = sorted(list(set(midi_files_matched).intersection(midi_files_unique)))
    if write:
        with open(output_path, "w") as f:
            json.dump(midi_files_matched_unique, f, indent=4)
            print(f"Output saved to {output_path}")

# create unique unmatched midis list
output_path = os.path.join(output_dir, "midis_unmatched_unique.json")
if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        midi_files_unmatched_unique = json.load(f)
else:
    print("Getting unique unmatched MIDIs.")
    midi_files_unmatched_unique = sorted(list(set(midi_files_unique) - set(midi_files_matched_unique)))
    if write:
        with open(output_path, "w") as f:
            json.dump(midi_files_unmatched_unique, f, indent=4)
            print(f"Output saved to {output_path}")

### 6- Create mappings: midi -> best matching track ID, spotify features
output_path = os.path.join(output_dir, "spotify_features.json")
if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        midi_file_to_spotify_features = json.load(f)
else:
    midi_file_to_spotify_features = {}
    print("Adding Spotify for matched unique MIDIs.")
    for pr in tqdm(midi_files_matched_unique):
        sample_data = {}
        sample_data["track_id"], sample_data["match_score"] = best_match_scores_reversed[pr]
        metadata_and_spotify = trackid_to_spotify_features[sample_data["track_id"]]
        sample_data.update(metadata_and_spotify)
        midi_file_to_spotify_features[pr] = sample_data
    if write:
        with open(output_path, "w") as f:
            json.dump(midi_file_to_spotify_features, f, indent=4)
            print(f"Output saved to {output_path}")

### 7- For all midis, get low level features 
# (tempo, note density, number of instruments)

output_path = os.path.join(output_dir, "midi_features.json")
if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        midi_file_to_midi_features = json.load(f)
else:
    def get_midi_features(midi_file):
        midi_path = os.path.join(midi_dataset_path, midi_file[0], midi_file + extension)
        if use_pianoroll_dataset:
            mid = pypianoroll.load(midi_path).to_pretty_midi()
        else:
            mid = pretty_midi.PrettyMIDI(midi_path)
        note_density = utils.get_note_density(mid)
        tempo = utils.get_tempo(mid)
        n_instruments = utils.get_n_instruments(mid)
        duration = mid.get_end_time()
        midi_features = {
            "note_density": note_density,
            "tempo": tempo,
            "n_instruments": n_instruments,
            "duration": duration,
        }
        return [midi_file, midi_features]
    print("Getting low-level MIDI features")
    midi_file_to_midi_features = run_parallel(get_midi_features, midi_files_unique)
    midi_file_to_midi_features = dict(midi_file_to_midi_features)
    if write:
        with open(output_path, "w") as f:
            json.dump(midi_file_to_midi_features, f, indent=4)
            print(f"Output saved to {output_path}")

### 8- Merge MIDI features and matched (Spotify) features
output_path = os.path.join(output_dir, "full_dataset_features.json")
if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        midi_file_to_merged_features = json.load(f)
else:
    midi_file_to_merged_features = {}
    print("Merging MIDI features and Spotify features for full dataset.")
    for midi_file in tqdm(midi_file_to_midi_features.keys()):
        midi_file_to_merged_features[midi_file] = {}
        midi_file_to_merged_features[midi_file]["midi_features"] = midi_file_to_midi_features[midi_file]
        if midi_file in midi_file_to_spotify_features.keys():
            matched_features = midi_file_to_spotify_features[midi_file]
        else:
            matched_features = {}
        midi_file_to_merged_features[midi_file]["matched_features"] = matched_features
    if write:
        with open(output_path, "w") as f:
            json.dump(midi_file_to_merged_features, f, indent=4)
            print(f"Output saved to {output_path}")

### Do the same for matched dataset
output_path = os.path.join(output_dir, "matched_dataset_features.json")
if os.path.exists(output_path) and not redo:
    with open(output_path, "r") as f:
        matched_midi_file_to_merged_features = json.load(f)
else:
    print("Merging MIDI features and Spotify features for the matched dataset.")
    matched_midi_file_to_merged_features = \
        {file_: midi_file_to_merged_features[file_] for file_ in tqdm(midi_files_matched_unique)}
    if write:
        with open(output_path, "w") as f:
            json.dump(matched_midi_file_to_merged_features, f, indent=4)
            print(f"Output saved to {output_path}")

### PART III: Constructing training dataset
### 9- Summarize matched dataset features by only taking valence and note densities per instrument,
# number of instruments, durations, is_matched

output_path = os.path.join(output_dir, "full_dataset_features_summarized.csv")
if not os.path.exists(output_path) or redo:
    print("Constructing training dataset (final file)")
    dataset_summarized = []
    for midi_file, features in tqdm(midi_file_to_merged_features.items()):
        midi_features = features["midi_features"]
        n_instruments = midi_features["n_instruments"]
        note_density_per_instrument = midi_features["note_density"] / n_instruments
        matched_features = features["matched_features"]
        if matched_features == {}:
            is_matched = False
            valence = float("nan")
        else:
            is_matched = True
            spotify_audio_features = matched_features["spotify_audio_features"]
            if spotify_audio_features is None or spotify_audio_features == "":
                valence = float("nan")
            else:
                if spotify_audio_features["valence"] == 0.0:
                    # An unusual number of samples have a valence of 0.0
                    # which is possibly due to an error. Feel free to comment out.
                    valence = float("nan")
                else:
                    valence = spotify_audio_features["valence"]
        
        dataset_summarized.append({
            "file": midi_file,
            "is_matched": is_matched,
            "n_instruments": n_instruments,
            "note_density_per_instrument": note_density_per_instrument,
            "valence": valence
        })
    dataset_summarized = pd.DataFrame(dataset_summarized)
    if write:
        dataset_summarized.to_csv(output_path, index=False)
        print(f"Output saved to {output_path}")