Skip to content

Commit 1a9cc63

Browse files
committed
working on vibrato pitch wheel oscillation, and improving scheduler
1 parent 921b51a commit 1a9cc63

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

textbeat/player.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def __init__(self):
8585
self.version = '0'
8686
self.auto = False
8787
self.embedded_files = {}
88+
self.vibrato_tracks = set() # tracks that currently have vibrato going
8889

8990
# require enable at top of file
9091
self.devices = ['midi']

textbeat/schedule.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def logic(self, t):
3737
# self.dontsleep = (clock - self.last_clock)
3838
# self.last_clock = clock
3939

40-
# clock = time.clock()
4140
# if self.started:
4241
# tdelta = (clock - self.passed)
4342
# self.passed += tdelta
@@ -48,6 +47,9 @@ def logic(self, t):
4847
# self.passed = 0.0
4948
# log(self.clock)
5049

50+
# pending_events_count = sum(1 for e in self.events if e.t > 0.0 and e.t < 2.0)
51+
# print(pending_events_count)
52+
5153
try:
5254
self.events = sorted(self.events, key=lambda e: e.t)
5355
for ev in self.events:

textbeat/track.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,34 @@ def reset(self):
9696
self.lanes = []
9797
self.ccs = {}
9898
self.dev = 0
99+
100+
# pitch wheel oscillation
101+
self.vibrato_enabled = False # don't set this directly, call vibrato()
102+
self.vibrato_amp = 0.0
103+
self.vibrato_freq = 0.0
104+
# self.vibrato_offset = 0.0 # value that gets added to pitch
105+
106+
def vibrato(self, b, amp=0.0, freq=0.0):
107+
self.vibrato_amp = amp
108+
self.vibrato_freq = freq
109+
self.vibrato_t = 0.0
110+
if b == self.vibrato_enabled:
111+
return
112+
if b:
113+
try:
114+
self.ctx.vibrato_tracks.remove(self)
115+
except KeyError:
116+
pass
117+
else:
118+
self.ctx.vibrato_tracks.add(self)
119+
self.pitch(self.pitchval)
120+
self.vibrato_enabled = b
121+
122+
def vibrato_logic(self, t):
123+
# TODO: test this
124+
self.vibrato_t += t
125+
v = math.sin(self.vibrato_t * self.vibrato_freq * math.tau) * self.vibrato_amp
126+
self.pitch(self.pitchval + v, False) # don't save new pitchval on call
99127

100128
# def _lazychannelfunc(self):
101129
# # get active channel numbers
@@ -263,27 +291,27 @@ def midi_channel(self, midich, stackidx=-1):
263291
self.channels = [(0,midich)]
264292
elif midich not in self.channels:
265293
self.channels.append(midich)
266-
def pitch(self, val): # [-1.0,1.0]
294+
def write_short(self, ch, status, val, val2):
295+
if self.ctx.midifile:
296+
self.midifile_write(ch,mido.UnknownMetaMessage(status,data=[val,val2], time=self.us()))
297+
else:
298+
self.midi[ch].write_short(status,val,val2)
299+
def pitch(self, val, save=True): # [-1.0,1.0]
300+
if save:
301+
self.pitchval = val
267302
val = min(max(0,int((1.0 + val)*0x2000)),16384)
268-
self.pitchval = val
269303
val2 = (val>>0x7f)
270304
val = val&0x7f
271305
for ch in self.channels:
272306
status = (MIDI_PITCH<<4) + ch[1]
273307
if self.ctx.showmidi: log(FG.YELLOW + 'MIDI: PITCH (%s, %s)' % (val,val2))
274-
if self.ctx.midifile:
275-
self.midifile_write(ch[0],mido.UnknownMetaMessage(status,data=[val1,val2], time=self.us()))
276-
else:
277-
self.midi[ch[0]].write_short(status,val,val2)
308+
self.write_short(ch[0], status, val, val2)
278309
def cc(self, cc, val): # control change
279310
if type(val) ==type(bool): val = 127 if val else 0 # allow cc bool switches
280311
for ch in self.channels:
281312
status = (MIDI_CC<<4) + ch[1]
282313
if self.ctx.showmidi: log(FG.YELLOW + 'MIDI: CC (%s, %s, %s)' % (status, cc,val))
283-
if self.ctx.midifile:
284-
self.midifile_write(ch[0], mido.UnknownMetaMessage(status,data=[cc,val],time=self.us()))
285-
else:
286-
self.midi[ch[0]].write_short(status,cc,val)
314+
self.write_short(ch[0], status, cc, val)
287315
self.ccs[cc] = v
288316
if cc==1:
289317
self.modval = val

0 commit comments

Comments
 (0)