Skip to content

Commit 2ef5556

Browse files
committed
more wip on midi support, trivial fixes, comments
1 parent c9d8f3d commit 2ef5556

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

textbeat/__main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
--flats prefer flats in output (default)
4444
--sharps prefer sharps in output
4545
--lint (STUB) analyze file
46-
--follow (old) print newlines every line, no output
46+
--follow tracks file output for editors by printing newlines every line
4747
--quiet no output
4848
-a --analyze (STUB) midi input chord analyzer
4949
"""
@@ -55,7 +55,8 @@
5555
def main():
5656
# if __name__!='__main__':
5757
# sys.exit(0)
58-
ARGS = docopt(__doc__.replace('textbeat',os.path.basename(sys.argv[0]).lower()))
58+
# ARGS = docopt(__doc__.replace('textbeat',os.path.basename(sys.argv[0]).lower()))
59+
ARGS = docopt(__doc__)
5960
set_args(ARGS)
6061

6162
from . import support

textbeat/player.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ def has_flags(self, f):
201201
return
202202
return self.flags & f
203203

204+
# for editor integration: "follows" the output of the file by printing
205+
# newlines to stdout for every parsed line
204206
def follow(self):
205207
if self.startrow==-1 and self.canfollow:
206208
cursor = self.row + 1
@@ -226,6 +228,13 @@ def run(self):
226228
self.header = True
227229
embedded_file = False
228230

231+
# set initial midifile tempo
232+
if not self.midifile.tracks:
233+
self.midifile.tracks.append(mido.MidiTrack())
234+
self.midifile.tracks[0].append(mido.MetaMessage(
235+
'set_tempo', tempo=mido.bpm2tempo(self.tempo)
236+
))
237+
229238
while not self.quitflag:
230239
self.follow()
231240

@@ -695,15 +704,15 @@ def run(self):
695704

696705
cell = cell.strip()
697706
if cell:
698-
self.header = False
707+
self.header = False # contents here, no longer in file header
699708

700709
if cell.count('\"') == 1: # " is recall, but multiple " means lyrics/speak?
701710
cell = cell.replace("\"", self.track_history[cell_idx])
702711
else:
703712
self.track_history[cell_idx] = cell
704713

705714
fullcell_sub = cell[:]
706-
715+
707716
# empty
708717
# if not cell:
709718
# cell_idx += 1
@@ -821,7 +830,9 @@ def run(self):
821830
# try to get roman numberal or number
822831
c,ct = peel_roman_s(tok)
823832
ambiguous = 0
824-
for amb in ('ion','dor','dim','dom','alt','dou','egy','aeo','dia','gui','bas','aug'): # TODO: make these auto
833+
# Help parser ambiguities (TODO: make these automatic)
834+
# (These are names that begin with letters or roman numerals)
835+
for amb in ('ion','dor','dim','dom','alt','dou','egy','aeo','dia','gui','bas','aug'):
825836
ambiguous += tok.lower().startswith(amb)
826837
if ct and not ambiguous:
827838
lower = (c.lower()==c)

textbeat/schedule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def logic(self, t):
6161

6262
slp = t*(1.0-self.passed) # remaining time
6363
if slp > 0.0:
64+
self.ctx.t += self.ctx.speed*slp
6465
if self.ctx.cansleep and self.ctx.startrow == -1:
65-
self.ctx.t += self.ctx.speed*slp
6666
time.sleep(self.ctx.speed*slp)
6767
self.passed = 0.0
6868

textbeat/track.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .defs import *
2+
import math
23

34
class Recording(object):
45
def __init__(self, name, slot):
@@ -42,7 +43,8 @@ def __init__(self, ctx, idx, midich):
4243
self.reset()
4344
def us(self):
4445
# microseconds
45-
return int(self.ctx.t)*1000000
46+
# return int(self.ctx.t)*1000000
47+
return math.floor(mido.second2tick(self.ctx.t, self.ctx.grid, self.ctx.tempo))
4648
def reset(self):
4749
Lane.reset(self)
4850
self.mode = 0 # 0 is NONE which inherits global mode
@@ -144,6 +146,7 @@ def midifile_write(self, ch, msg):
144146
# ch: midi channel index, not midi channel # (index 0 of self.channels tuple item)
145147
while ch >= len(self.ctx.midifile.tracks):
146148
self.ctx.midifile.tracks.append(mido.MidiTrack())
149+
# print(msg)
147150
self.ctx.midifile.tracks[ch].append(msg)
148151
def enable(self, v=True):
149152
was = v

0 commit comments

Comments
 (0)