xi2

a plain text language that compiles to MIDI
git clone https://git.ce9e.org/xi2.git

commit
365fdeedb85413c1eb0d28a0a20160dd3feb55d1
parent
7ff3ea8c0f2c3046dde2d0fc35c101f2af5bcebf
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-09-19 08:43
misc refactoring

Diffstat

M midi.py 8 ++++----
M xi2.py 17 ++++++-----------

2 files changed, 10 insertions, 15 deletions


diff --git a/midi.py b/midi.py

@@ -57,7 +57,7 @@ class Midi:
   57    57 
   58    58     def set_tempo(self, bpm):
   59    59         # midi uses microsec/quarter note
   60    -1         msqn = 60000000 // bpm
   -1    60         msqn = 60_000_000 // bpm
   61    61         self.meta_event(0, 0x51, 3, msqn)
   62    62 
   63    63     def note_on(self, dt, ch, key, vol=1):
@@ -84,12 +84,12 @@ def write_file(fh, tracks):
   84    84     f.write_fixed(0x4D546864, 4)  # chunk ID "MThd"
   85    85     f.write_fixed(6, 4)  # chunk size
   86    86     f.write_fixed(1, 2)  # format type
   87    -1     f.write_fixed(len(tracks), 2)  # numer of tracks
   88    -1     f.write_fixed(TIME_DEVISION, 2)  # time devision
   -1    87     f.write_fixed(len(tracks), 2)
   -1    88     f.write_fixed(TIME_DEVISION, 2)
   89    89     for track in tracks:
   90    90         f.write_fixed(0x4D54726B, 4)  # chunk ID "MTtr"
   91    91         buf = track.fh.getvalue()
   92    -1         f.write_fixed(len(buf) + 4, 4)  # chunk size
   -1    92         f.write_fixed(len(buf) + 4, 4)
   93    93         f.fh.write(buf)
   94    94         f.meta_event(0, 0x2f, 0, b'')  # end_track event
   95    95 

diff --git a/xi2.py b/xi2.py

@@ -49,7 +49,7 @@ if __name__ == '__main__':
   49    49     ll = re.sub('[ \t]', '', ll)
   50    50     # remove c++ style comments
   51    51     # we have to escape linebreaks
   52    -1     ll = re.sub('\n', '\\\\n', ll)
   -1    52     ll = re.sub('\n', r'\\n', ll)
   53    53     ll = re.sub('/\*[^(\*)]*\*/', '', ll)
   54    54     ll = re.sub('\\\\n', '\n', ll)
   55    55     # remove c style comments
@@ -70,14 +70,9 @@ if __name__ == '__main__':
   70    70         ll = re.sub(re.escape(key), val, ll)
   71    71     ll = re.sub('\\\\n', '\n', ll)
   72    72 
   73    -1     # remove trailing newlines
   74    -1     ll = re.sub('^\n*', '', ll)
   75    -1     # remove \n\n+
   76    -1     ll = re.sub('\n', '\\\\n', ll)
   77    -1     ll = re.sub('\\\\n(\\\\n)+', '\\\\n\\\\n', ll)
   78    -1     ll = re.sub('\\\\n', '\n', ll)
   79    -1     # remove newlines at the end
   80    -1     ll = re.sub('\n*$', '', ll)
   -1    73     # trim newlines
   -1    74     ll = ll.strip('\n')
   -1    75     ll = re.sub('\n\n+', '\n\n', ll)
   81    76 
   82    77     # join track parts from different sets
   83    78     tracks = dict()
@@ -89,9 +84,9 @@ if __name__ == '__main__':
   89    84         for track in s.split('\n'):
   90    85             try:
   91    86                 (name, data) = track.split(':', 1)
   92    -1             except Exception as e:
   -1    87             except Exception:
   93    88                 print(track)
   94    -1                 raise e
   -1    89                 raise
   95    90             data = parse(data)
   96    91             if name not in tracks:
   97    92                 tracks[name] = [''] * l