2020-06-17 06:35:34 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
# include standard modules
|
|
|
|
|
import argparse
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import re
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
mydir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
|
2020-06-18 10:06:03 +02:00
|
|
|
# Lookup value is value of note for ( string * 1000 ) + value of fret
|
|
|
|
|
lookuptable = {
|
|
|
|
|
57: ["Cr", "x"],
|
|
|
|
|
49: ["Cr", "x"],
|
|
|
|
|
"XX": ["Hh", "x"],
|
|
|
|
|
51: ["Ri", "x"],
|
|
|
|
|
3048: ["HT", "o"],
|
|
|
|
|
3047: ["MT", "o"],
|
|
|
|
|
4038: ["Sn", "o"],
|
|
|
|
|
3043: ["FT", "o"],
|
|
|
|
|
5035: ["Bd", "o"],
|
|
|
|
|
44: ["Hf", "x"]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
### DrumBurp notation Wikipedia notation Wikipedia notation
|
|
|
|
|
### Cr - Crash Drums Cymbals
|
|
|
|
|
### Hh - HiHat |-o-| Strike |-x-| Strike cymbal or hi-hat
|
|
|
|
|
### Ri - Ride |-O-| Accent |-X-| Strike loose hi-hat, or hit crash hard
|
|
|
|
|
### HT - High Tom |-g-| Ghost note |-o-| Open hi-hat
|
|
|
|
|
### MT - Mid Tom |-f-| Flam |-#-| Choke cymbal (grab cymbal with hand after striking it)
|
|
|
|
|
### Sn - Snare |-d-| Drag |-s-| Splash cymbal
|
|
|
|
|
### FT - Floor Tom |-b-| Soft one-handed roll |-c-| China cymbal
|
|
|
|
|
### Bd - Kick |-B-| Accented one-handed roll |-b-| Bell of ride
|
|
|
|
|
### Hf - Foot pedal |-@-| Snare rim |-x-| Click hi-hat with foot
|
|
|
|
|
### DrumBurp: https://whatang.org/
|
|
|
|
|
### Wikipedia: https://en.wikipedia.org/wiki/Drum_tablature
|
|
|
|
|
|
2020-06-17 06:35:34 +02:00
|
|
|
def commandline():
|
|
|
|
|
# initiate the parser
|
2020-06-18 10:06:03 +02:00
|
|
|
parser = argparse.ArgumentParser(description="Make ASCII tabs from songsterr tabs.")
|
2020-06-17 06:35:34 +02:00
|
|
|
# option without argument via 'store_true'
|
|
|
|
|
parser.add_argument("-V", "--version", help="show program version", action="store_true")
|
|
|
|
|
# option with argument
|
2020-06-18 10:06:03 +02:00
|
|
|
parser.add_argument("--width", "-w", help="set output width in measures")
|
2020-06-17 06:35:34 +02:00
|
|
|
parser.add_argument("--input", "-i", help="set input file")
|
2020-06-18 10:06:03 +02:00
|
|
|
parser.add_argument("--url", "-u", help="set input url")
|
2020-06-17 06:35:34 +02:00
|
|
|
|
|
|
|
|
# read arguments from the command line
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
# handle arguments
|
|
|
|
|
# check for --version or -V
|
|
|
|
|
if args.version:
|
2020-06-18 10:06:03 +02:00
|
|
|
print("Version 0.1")
|
2020-06-17 06:35:34 +02:00
|
|
|
# check for --width
|
|
|
|
|
if args.width:
|
|
|
|
|
print("set output width to %s" % args.width)
|
2020-06-18 10:06:03 +02:00
|
|
|
if args.url:
|
|
|
|
|
print("url not handled yet %s" % args.width)
|
|
|
|
|
if args.input and args.url:
|
|
|
|
|
print("Give either a url, or an input file, not both.")
|
|
|
|
|
exit()
|
|
|
|
|
if not args.input and not args.url:
|
|
|
|
|
print("Give either a url, or an input file.")
|
|
|
|
|
exit()
|
2020-06-17 06:35:34 +02:00
|
|
|
|
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_json(html):
|
|
|
|
|
result = re.search(r'<script id="state" type="application/json">(?P<json>.*?)</script', html, re.MULTILINE)
|
|
|
|
|
if result:
|
|
|
|
|
data = json.loads(result.group("json"))
|
|
|
|
|
else:
|
|
|
|
|
print("Can't find the magic")
|
|
|
|
|
return data
|
|
|
|
|
|
2020-06-18 10:06:03 +02:00
|
|
|
def print_meta(jsondata):
|
|
|
|
|
print( "Artist: " + jsondata["meta"]["artist"] )
|
|
|
|
|
print( "Title: " + jsondata["meta"]["title"] )
|
|
|
|
|
print( "Instrument: " + jsondata["data"]["part"]["instrument"] )
|
|
|
|
|
print( "BPM: " + str( jsondata["data"]["part"]["measures"][0]["voices"][0]["beats"][0]["tempo"]["bpm"] ) )
|
|
|
|
|
print("")
|
2020-06-17 06:35:34 +02:00
|
|
|
|
2020-06-18 10:06:03 +02:00
|
|
|
return
|
2020-06-17 06:35:34 +02:00
|
|
|
|
|
|
|
|
|
2020-06-18 10:06:03 +02:00
|
|
|
def get_skip(tp, typelength):
|
2020-06-17 21:13:18 +02:00
|
|
|
if tp < typelength:
|
2020-06-18 10:06:03 +02:00
|
|
|
skip = int( ( typelength / tp ) )
|
2020-06-17 21:13:18 +02:00
|
|
|
else:
|
2020-06-18 10:06:03 +02:00
|
|
|
skip = 1
|
2020-06-17 21:13:18 +02:00
|
|
|
|
2020-06-18 10:06:03 +02:00
|
|
|
return skip
|
2020-06-17 21:13:18 +02:00
|
|
|
|
|
|
|
|
def get_typelength(jsondata):
|
|
|
|
|
typelength = 0
|
|
|
|
|
for i in range(len(jsondata["data"]["part"]["measures"])):
|
|
|
|
|
for j in range(len(jsondata["data"]["part"]["measures"][i]["voices"][0]["beats"])):
|
|
|
|
|
beat = jsondata["data"]["part"]["measures"][i]["voices"][0]["beats"][j]
|
|
|
|
|
if typelength < beat["type"]:
|
|
|
|
|
typelength = beat["type"]
|
|
|
|
|
return typelength
|
|
|
|
|
|
2020-06-18 10:06:03 +02:00
|
|
|
def parse_instruments(jsondata):
|
|
|
|
|
# In the end I want an array of measures for each instrument
|
|
|
|
|
# That'll make for easy printing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
instruments = {
|
|
|
|
|
"Cr": [],
|
|
|
|
|
"Hh": [],
|
|
|
|
|
"Ri": [],
|
|
|
|
|
"HT": [],
|
|
|
|
|
"MT": [],
|
|
|
|
|
"Sn": [],
|
|
|
|
|
"FT": [],
|
|
|
|
|
"Bd": [],
|
|
|
|
|
"Hf": []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typelength = get_typelength(jsondata)
|
|
|
|
|
#print("Typelength: " + str(typelength))
|
|
|
|
|
#print("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for measurecnt in range(len(jsondata["data"]["part"]["measures"])):
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
|
|
# add one measure to each instrument
|
|
|
|
|
for instr in ["Cr", "Hh", "Ri", "HT", "MT", "Sn", "FT", "Bd", "Hf"]:
|
|
|
|
|
instruments[instr].append([])
|
|
|
|
|
for i in range(typelength):
|
|
|
|
|
instruments[instr][-1].append("-")
|
|
|
|
|
|
|
|
|
|
for beatcnt in range(len(jsondata["data"]["part"]["measures"][measurecnt]["voices"][0]["beats"])):
|
|
|
|
|
beat = jsondata["data"]["part"]["measures"][measurecnt]["voices"][0]["beats"][beatcnt]
|
|
|
|
|
skip = get_skip(beat["type"], typelength)
|
|
|
|
|
|
|
|
|
|
for note in beat["notes"]:
|
|
|
|
|
if note != {'rest': True}:
|
|
|
|
|
lookupval = ( note["string"] * 1000 ) + note["fret"]
|
|
|
|
|
inst, marker = lookuptable[lookupval]
|
|
|
|
|
#print(measurecnt, inst, count)
|
|
|
|
|
if inst != None:
|
|
|
|
|
instruments[inst][-1][count] = marker
|
|
|
|
|
else:
|
|
|
|
|
print("Unhandled: " + note + " in measure " + str(measurecnt+1))
|
|
|
|
|
print("Add: " + str(( note["string"] * 1000 ) + note["fret"]) + " to lookuptable")
|
|
|
|
|
count += skip
|
|
|
|
|
|
|
|
|
|
return instruments
|
|
|
|
|
|
|
|
|
|
def print_instruments(instruments):
|
|
|
|
|
for instr in ["Cr", "Hh", "Ri", "HT", "MT", "Sn", "FT", "Bd", "Hf"]:
|
|
|
|
|
for i in range(len(instruments["Cr"])):
|
|
|
|
|
print(instr + " " + "".join(instruments[instr][i]))
|
|
|
|
|
|
|
|
|
|
args = commandline()
|
|
|
|
|
|
|
|
|
|
with open(args.input, 'r') as content_file:
|
|
|
|
|
content = content_file.read()
|
|
|
|
|
|
|
|
|
|
jsondata = get_json(content)
|
|
|
|
|
print_meta(jsondata)
|
|
|
|
|
|
|
|
|
|
instr = parse_instruments(jsondata)
|
|
|
|
|
print_instruments(instr)
|
2020-06-17 06:35:34 +02:00
|
|
|
|