import itertools import os from pathlib import Path import datasets from typing import Optional from tqdm import tqdm from pathlib import Path import re datasets.logging.set_verbosity_info() _DESCRIPTION = """\ Dataset of music tablature, in alphaTex (https://alphatab.net/docs/alphatex) format, converted from Guitar Pro files (gp3, gp4, gp5, which are downloaded from https://rutracker.org/forum/viewtopic.php?t=2888130 """ INSTRUMENT_MAP = { 0: "Acoustic Grand Piano", 1: "Bright Grand Piano", 2: "Electric Grand Piano", 3: "Honky tonk Piano", 4: "Electric Piano 1", 5: "Electric Piano 2", 6: "Harpsichord", 7: "Clavinet", 8: "Celesta", 9: "Glockenspiel", 10: "Musicbox", 11: "Vibraphone", 12: "Marimba", 13: "Xylophone", 14: "Tubularbells", 15: "Dulcimer", 16: "Drawbar Organ", 17: "Percussive Organ", 18: "Rock Organ", 19: "Church Organ", 20: "Reed Organ", 21: "Accordion", 22: "Harmonica", 23: "Tango Accordion", 24: "Acoustic Guitar Nylon", 25: "Acoustic Guitar Steel", 26: "Electric Guitar Jazz", 27: "Electric Guitar Clean", 28: "Electric Guitar Muted", 29: "Overdriven Guitar", 30: "Distortion Guitar", 31: "Guitar Harmonics", 32: "Acoustic Bass", 33: "Electric Bass Finger", 34: "Electric Bass Pick", 35: "Fretless Bass", 36: "Slap Bass 1", 37: "Slap Bass 2", 38: "Synth Bass 1", 39: "Synth Bass 2", 40: "Violin", 41: "Viola", 42: "Cello", 43: "Contrabass", 44: "Tremolo Strings", 45: "Pizzicato Strings", 46: "Orchestral Harp", 47: "Timpani", 48: "String Ensemble 1", 49: "String Ensemble 2", 50: "Synth Strings 1", 51: "Synth Strings 2", 52: "Choir Aahs", 53: "Voice Oohs", 54: "Synth Voice", 55: "Orchestra Hit", 56: "Trumpet", 57: "Trombone", 58: "Tuba", 59: "Muted Trumpet", 60: "French Horn", 61: "Brass Section", 62: "Synth Brass 1", 63: "Synth Brass 2", 64: "Soprano Sax", 65: "Alto Sax", 66: "Tenor Sax", 67: "Baritone Sax", 68: "Oboe", 69: "English Horn", 70: "Bassoon", 71: "Clarinet", 72: "Piccolo", 73: "Flute", 74: "Recorder", 75: "Pan Flute", 76: "Blown bottle", 77: "Shakuhachi", 78: "Whistle", 79: "Ocarina", 80: "Lead 1 Square", 81: "Lead 2 Sawtooth", 82: "Lead 3 Calliope", 83: "Lead 4 Chiff", 84: "Lead 5 Charang", 85: "Lead 6 Voice", 86: "Lead 7 Fifths", 87: "Lead 8 Bass and Lead", 88: "Pad 1 newage", 89: "Pad 2 warm", 90: "Pad 3 polysynth", 91: "Pad 4 choir", 92: "Pad 5 bowed", 93: "Pad 6 metallic", 94: "Pad 7 halo", 95: "Pad 8 sweep", 96: "Fx 1 rain", 97: "Fx 2 soundtrack", 98: "Fx 3 crystal", 99: "Fx 4 atmosphere", 100: "Fx 5 brightness", 101: "Fx 6 goblins", 102: "Fx 7 echoes", 103: "Fx 8 scifi", 104: "Sitar", 105: "Banjo", 106: "Shamisen", 107: "Koto", 108: "Kalimba", 109: "Bag pipe", 110: "Fiddle", 111: "Shanai", 112: "Tinkle Bell", 113: "Agogo", 114: "Steel Drums", 115: "Woodblock", 116: "Taiko Drum", 117: "Melodic Tom", 118: "Synth Drum", 119: "Reverse Cymbal", 120: "Guitar Fret Noise", 121: "Breath Noise", 122: "Seashore", 123: "Bird Tweet", 124: "Telephone Ring", 125: "Helicopter", 126: "Applause", 127: "Gunshot", } class Builder(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.2.1") def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "file": datasets.Value("string"), "tex": datasets.Value("string"), "title": datasets.Value("string"), "track_name": datasets.Value("string"), "track_number": datasets.Value("int32"), "instrument": datasets.Value("string"), "tempo": datasets.Value("int32"), "tuning": datasets.Value("string"), "frets": datasets.Value("int32"), # "subtitle": datasets.Value("string"), # "artist": datasets.Value("string"), # "album": datasets.Value("string"), # "words": datasets.Value("string"), # "music": datasets.Value("string"), # "copyright": datasets.Value("string"), # "tab": datasets.Value("string"), # "instructions": datasets.Value("string"), } ), ) def _split_generators(self, dl_manager: datasets.DownloadManager): data_dir = dl_manager.download_and_extract("data.zip") if paths := list(Path(data_dir).glob("**/*.tex")): print(f"Found {len(paths)} AlphaTex files") else: raise ValueError(f"No GP files found in {data_dir}") return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "filepaths": [p for p in paths], }, ), ] def _generate_examples(self, filepaths): for idx, path in enumerate(filepaths): with open(path) as f: examples = _parse_examples(f.read()) for example in examples: example["file"] = path.name yield f"{idx}-{example['track_number']}", example def _parse_examples(tex): """ Returns a dictionary for each track """ tmpl = { "file": None, "tex": None, "title": None, "track_name": None, "track_number": None, "instrument": None, "tempo": None, "tuning": None, "frets": None, } tracks = [tmpl.copy()] def _parse_track_header(k, v): if k == "instrument": tracks[-1][k] = INSTRUMENT_MAP.get(int(v), "unknown") elif k == "tuning": tracks[-1][k] = v.split(" ") elif k == "frets": tracks[-1][k] = int(v) lines = (l.strip() for l in tex.splitlines() if l.strip()) for line in itertools.takewhile(lambda l: l != ".", lines): assert line.startswith("\\") line = line.lstrip("\\") kv = line.split(" ", 1) if len(kv) == 1: continue k, v = kv if k in [ "title", # "subtitle", # "artist", # "album", # "words", # "music", # "copyright", # "tab", # "instructions", ]: tracks[-1][k] = v.strip('"').strip("'") elif k == "tempo": tracks[-1][k] = int(v) else: _parse_track_header(k, v) track_lines = [] for line in lines: if line.startswith("\\"): line = line.lstrip("\\") kv = line.split(" ", 1) if len(kv) == 2: k = kv[0] v = kv[1].strip('"').strip("'") else: k = kv[0] v = None if k == "track": if track_lines: tracks[-1]["tex"] = " ".join(track_lines) track_lines = [] tracks.append(tmpl.copy()) track_number = len(tracks) + 1 tracks[-1]["track_number"] = track_number tracks[-1]["track_name"] = v if v else f"Track {track_number}" else: _parse_track_header(k, v) else: track_lines.append(line) if track_lines: tracks[-1]["tex"] = " ".join(track_lines) return tracks