Skip to content

Instantly share code, notes, and snippets.

@bwhitman
Created October 30, 2013 01:33
Show Gist options
  • Save bwhitman/7225846 to your computer and use it in GitHub Desktop.
Save bwhitman/7225846 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
"""
salami.py
"""
import sys
import re
import os
# Basedir you put the chord files in
_base = "/Users/bwhitman/sounds/billboard/"
def parse_salami_file(number):
"""
Parse a salami_chords.txt file and return a dict with all the stuff innit
"""
fn = _base + number + "/salami_chords.txt"
s = open(fn).read().split('\n')
o = {}
o["id"] = number
for x in s:
if x.startswith("#"):
if x[2:].startswith("title:"):
o["title"] = x[9:]
if x[2:].startswith("artist:"):
o["artist"] = x[10:]
if x[2:].startswith("metre:"):
o["meter"] = o.get("meter",[]) + [x[9:]]
if x[2:].startswith("tonic:"):
o["tonic"] = o.get("tonic",[]) + [x[9:]]
elif len(x) > 1:
spot = x.find('\t')
if spot>0:
time = float(x[0:spot])
event = {}
event["time"] = time
rest = x[spot+1:]
items = rest.split(', ')
for i in items:
chords = re.findall(r"(?=\| (.*?) \|)", i)
if len(chords):
event["chords"] = chords
else:
event["notes"] = event.get("notes", []) + [i]
o["events"] = o.get("events", []) + [event]
return o
def main():
# Test
for id_string in open(_base+'all_ids').read().split('\n')[:-1]:
print id_string
o = parse_salami_file(id_string)
print str(o)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment