more codes, add instrument index, compress empty intro

This commit is contained in:
Ward Wouts 2020-06-18 13:52:32 +02:00
parent 756cf79423
commit e8d1977f05

104
stt.py
View file

@ -6,6 +6,7 @@ import os
import sys import sys
import re import re
import json import json
import urllib.request
mydir = os.path.dirname(os.path.realpath(__file__)) mydir = os.path.dirname(os.path.realpath(__file__))
@ -13,14 +14,39 @@ mydir = os.path.dirname(os.path.realpath(__file__))
lookuptable = { lookuptable = {
57: ["Cr", "x"], 57: ["Cr", "x"],
49: ["Cr", "x"], 49: ["Cr", "x"],
"XX": ["Hh", "x"], 1042: ["Hh", "x"],
46: ["Hh", "o"],
51: ["Ri", "x"], 51: ["Ri", "x"],
1051: ["Ri", "x"],
3048: ["HT", "o"], 3048: ["HT", "o"],
1048: ["MT", "o"],
3047: ["MT", "o"], 3047: ["MT", "o"],
3045: ["MT", "o"], # LT
31: ["St", "o"], # Sticks
2038: ["Sn", "o"],
4038: ["Sn", "o"], 4038: ["Sn", "o"],
4040: ["Sn", "o"],
3041: ["FT", "o"], # LFT
4041: ["FT", "o"], # LFT
2043: ["FT", "o"],
3043: ["FT", "o"], 3043: ["FT", "o"],
4043: ["FT", "o"],
5035: ["Bd", "o"], 5035: ["Bd", "o"],
44: ["Hf", "x"] 5036: ["Bd", "o"],
44: ["Hf", "x"],
5044: ["Hf", "x"]
}
instr_index = {
"Cr": "Cr - Crash ",
"Hh": "Hh - HiHat ",
"Ri": "Ri - Ride ",
"HT": "HT - High Tom ",
"MT": "MT - Mid Tom ",
"Sn": "Sn - Snare ",
"FT": "FT - Floor Tom ",
"Bd": "Bd - Kick ",
"Hf": "Hf - Foot pedal "
} }
### DrumBurp notation Wikipedia notation Wikipedia notation ### DrumBurp notation Wikipedia notation Wikipedia notation
@ -41,11 +67,13 @@ def commandline():
parser = argparse.ArgumentParser(description="Make ASCII tabs from songsterr tabs.") parser = argparse.ArgumentParser(description="Make ASCII tabs from songsterr tabs.")
# option without argument via 'store_true' # option without argument via 'store_true'
parser.add_argument("-V", "--version", help="show program version", action="store_true") parser.add_argument("-V", "--version", help="show program version", action="store_true")
# option with argument parser.add_argument("--dump", "-d", help="dump json and exit", action="store_true")
parser.add_argument("--width", "-w", help="set output width in measures (default: 2)") parser.add_argument("--width", "-w", help="set output width in measures (default: 2)")
parser.add_argument("--input", "-i", help="set input file") parser.add_argument("--input", "-i", help="set input file")
parser.add_argument("--url", "-u", help="set input url") parser.add_argument("--url", "-u", help="set input url")
parser.add_argument("--exclude", "-x", help="exclude unused instruments", action="store_true") parser.add_argument("--exclude", "-x", help="exclude unused instruments", action="store_true")
parser.add_argument("--compress", "-c", help="compress empty intro", action="store_true")
# read arguments from the command line # read arguments from the command line
args = parser.parse_args() args = parser.parse_args()
@ -55,12 +83,8 @@ def commandline():
if args.version: if args.version:
print("Version 0.1") print("Version 0.1")
# check for --width # check for --width
if args.width: if not args.width:
print("set output width to %s" % args.width)
else:
args.width = 2 args.width = 2
if args.url:
print("url not handled yet %s" % args.width)
if args.input and args.url: if args.input and args.url:
print("Give either a url, or an input file, not both.") print("Give either a url, or an input file, not both.")
exit() exit()
@ -70,6 +94,19 @@ def commandline():
return args return args
def read_content():
content = ""
if args.input:
with open(args.input, 'r') as content_file:
content = content_file.read()
elif args.url:
fp = urllib.request.urlopen(args.url)
mybytes = fp.read()
content = mybytes.decode("utf8")
fp.close()
return content
def get_json(html): def get_json(html):
result = re.search(r'<script id="state" type="application/json">(?P<json>.*?)</script', html, re.MULTILINE) result = re.search(r'<script id="state" type="application/json">(?P<json>.*?)</script', html, re.MULTILINE)
@ -79,6 +116,12 @@ def get_json(html):
print("Can't find the magic") print("Can't find the magic")
return data return data
def dump_json(html):
result = re.search(r'<script id="state" type="application/json">(?P<json>.*?)</script', html, re.MULTILINE)
if result:
print(result.group("json"))
exit()
def print_meta(jsondata): def print_meta(jsondata):
print( "Artist: " + jsondata["meta"]["artist"] ) print( "Artist: " + jsondata["meta"]["artist"] )
print( "Title: " + jsondata["meta"]["title"] ) print( "Title: " + jsondata["meta"]["title"] )
@ -139,7 +182,7 @@ def parse_instruments(jsondata):
#print("Typelength: " + str(typelength)) #print("Typelength: " + str(typelength))
#print("") #print("")
first_instrument = -1
for measurecnt in range(len(jsondata["data"]["part"]["measures"])): for measurecnt in range(len(jsondata["data"]["part"]["measures"])):
count = 0 count = 0
@ -156,24 +199,44 @@ def parse_instruments(jsondata):
for note in beat["notes"]: for note in beat["notes"]:
if note != {'rest': True}: if note != {'rest': True}:
lookupval = ( note["string"] * 1000 ) + note["fret"] lookupval = ( note["string"] * 1000 ) + note["fret"]
if lookupval in lookuptable:
if first_instrument < 0:
first_instrument = measurecnt
inst, marker = lookuptable[lookupval] inst, marker = lookuptable[lookupval]
#print(measurecnt, inst, count) if inst != None and inst in instruments:
if inst != None:
instruments[inst][-1][count] = marker instruments[inst][-1][count] = marker
used_instr[inst] = True used_instr[inst] = True
else: else:
print("Unhandled: " + note + " in measure " + str(measurecnt+1)) print("Unhandled: " + str(note) + " in measure " + str(measurecnt+1))
print("Add: " + str(( note["string"] * 1000 ) + note["fret"]) + " to lookuptable") print("Add: " + str(( note["string"] * 1000 ) + note["fret"]) + " to lookuptable")
count += skip count += skip
return instruments, used_instr return instruments, used_instr, first_instrument
def print_instruments(instruments, used_instr): def print_instr_index(used_instr):
for instr in ["Cr", "Hh", "Ri", "HT", "MT", "Sn", "FT", "Bd", "Hf"]:
if not args.exclude or used_instr[instr]:
print(instr_index[instr])
print("")
return
def print_instruments(instruments, used_instr, first_instrument):
measuresnr = len(instruments["Cr"]) measuresnr = len(instruments["Cr"])
width = int(args.width) width = int(args.width)
rows = ( measuresnr / width ) rows = ( measuresnr / width )
count = 0 count = 0
if args.compress and first_instrument > 0:
for instr in ["Cr", "Hh", "Ri", "HT", "MT", "Sn", "FT", "Bd", "Hf"]:
if not args.exclude or used_instr[instr]:
sys.stdout.write(instr + " |")
sys.stdout.write("".join(instruments[instr][0]) + "|")
print("")
print("Repeat " + str(first_instrument) + " times")
print("")
count = first_instrument
while count < measuresnr: while count < measuresnr:
for instr in ["Cr", "Hh", "Ri", "HT", "MT", "Sn", "FT", "Bd", "Hf"]: for instr in ["Cr", "Hh", "Ri", "HT", "MT", "Sn", "FT", "Bd", "Hf"]:
if not args.exclude or used_instr[instr]: if not args.exclude or used_instr[instr]:
@ -187,11 +250,16 @@ def print_instruments(instruments, used_instr):
args = commandline() args = commandline()
with open(args.input, 'r') as content_file: content = read_content()
content = content_file.read()
if args.dump:
dump_json(content)
jsondata = get_json(content) jsondata = get_json(content)
print_meta(jsondata) print_meta(jsondata)
instr, used_instr = parse_instruments(jsondata) instr, used_instr, first_instrument = parse_instruments(jsondata)
print_instruments(instr, used_instr)
print_instr_index(used_instr)
print_instruments(instr, used_instr, first_instrument)