Skip to content

Commit 7b27188

Browse files
committed
-h fix, small changes for midifile support (not yet done)
1 parent 07f3119 commit 7b27188

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

textbeat/__main__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
textbeat song.txbt play song
1010
1111
Usage:
12-
textbeat [--dev=<device>] [--midi=<fn>] [--ring] [--follow] [--stdin] [-adeftnpsrxhvL] [INPUT]
13-
textbeat [+RANGE] [--dev=<device> | --midi=<fn> | --ring | --follow | --stdin] [-adeftnpsrxhvL] [INPUT]
14-
textbeat [-rT]
12+
textbeat [--dev=<device>] [--midi=<fn>] [--ring] [--follow] [--stdin] [-adeftnpsrxvL] [INPUT]
13+
textbeat [+RANGE] [--dev=<device>] [--midi=<fn>] [--ring] [--follow] [--stdin] [-adeftnpsrxL] [INPUT]
14+
textbeat [-rhT]
1515
textbeat -c [COMMANDS ...]
1616
textbeat -l [LINE_CONTENT ...]
1717
@@ -36,7 +36,7 @@
3636
+<range> play from line or maker, for range use start:end
3737
-e --edit (STUB) open file in editor
3838
--vi (STUB) shell vi mode
39-
-h --transpose transpose (in half steps)
39+
-H --transpose transpose (in half steps)
4040
--sustain start with sustain enabled
4141
--numbers use note numbers in output
4242
--notenames use note names in output
@@ -55,7 +55,7 @@
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()))
5959
set_args(ARGS)
6060

6161
from . import support
@@ -86,6 +86,7 @@ def main():
8686
elif arg == '--midi':
8787
midifn = val
8888
player.midifile = mido.MidiFile()
89+
player.cansleep = False
8990
elif arg == '--grid': player.grid = float(val)
9091
elif arg == '--note': player.grid = float(val)/4.0
9192
elif arg == '--speed': player.speed = float(val)

textbeat/player.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,15 @@ def run(self):
511511
else: assert False # no such var
512512
else: assert False # no such op
513513

514+
if var=='T':
515+
if self.midifile:
516+
if not self.midifile.tracks:
517+
self.midifile.tracks.append(mido.MidiTrack())
518+
# self.midifile.tracks[0].append(mido.MetaMessage(
519+
# 'set_tempo', tempo=mido.bpm2tempo(int(
520+
# val.split('x')[0]
521+
# ))
522+
# ))
514523
self.row += 1
515524
continue
516525

textbeat/track.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def __init__(self, ctx, idx, midich):
4040
self.initial_channel = midich
4141
self.non_drum_channel = midich
4242
self.reset()
43+
def us(self):
44+
# microseconds
45+
return int(self.ctx.t)*1000000
4346
def reset(self):
4447
Lane.reset(self)
4548
self.mode = 0 # 0 is NONE which inherits global mode
@@ -179,13 +182,14 @@ def note_on(self, n, v=-1, sustain=False):
179182
if self.ctx.showmidi: log(FG.YELLOW + 'MIDI: NOTE ON (%s, %s, %s)' % (n,v,ch))
180183
if (not self.ctx.muted or (self.ctx.muted and self.soloed))\
181184
and self.enabled and self.ctx.startrow==-1:
182-
self.midi[ch[0]].note_on(n,v,ch[1])
183185
if self.ctx.midifile:
184186
while ch[0] >= len(self.ctx.midifile.tracks):
185187
self.ctx.midifile.tracks.append(mido.MidiTrack())
186188
self.ctx.midifile.tracks[ch[0]].append(mido.Message(
187-
'note_on',velocity=v,time=int(self.ctx.t),channel=ch[1]
189+
'note_on',note=n,velocity=v,time=self.us(),channel=ch[1]
188190
))
191+
else:
192+
self.midi[ch[0]].note_on(n,v,ch[1])
189193
def note_off(self, n, v=-1):
190194
if v == -1:
191195
v = self.vel
@@ -195,10 +199,21 @@ def note_off(self, n, v=-1):
195199
# log("off " + str(n))
196200
for ch in self.channels:
197201
if self.ctx.showmidi: log(FG.YELLOW + 'MIDI: NOTE OFF (%s, %s, %s)' % (n,v,ch))
198-
self.midi[ch[0]].note_on(self.notes[n],0,ch[1])
199-
self.midi[ch[0]].note_off(self.notes[n],v,ch[1])
202+
if not self.ctx.midifile:
203+
self.midi[ch[0]].note_on(self.notes[n],0,ch[1])
204+
self.midi[ch[0]].note_off(self.notes[n],v,ch[1])
200205
self.notes[n] = 0
201206
self.sustain_notes[n] = 0
207+
if self.ctx.midifile:
208+
while ch[0] >= len(self.ctx.midifile.tracks):
209+
self.ctx.midifile.tracks.append(mido.MidiTrack())
210+
self.ctx.midifile.tracks[ch[0]].append(mido.Message(
211+
'note_on',note=n,velocity=0,time=self.us(),channel=ch[1]
212+
))
213+
self.ctx.midifile.tracks[ch[0]].append(mido.Message(
214+
'note_off',note=n,velocity=v,time=self.us(),channel=ch[1]
215+
))
216+
202217
self.cc(MIDI_SUSTAIN_PEDAL, True)
203218
def release_all(self, mute_sus=False, v=-1):
204219
if v == -1:

0 commit comments

Comments
 (0)