Skip to content

Commit 272a931

Browse files
committed
Link - wait for next BPM change after calling set
This gives chance for link to register the new bpm and then report it back so it can be used in the next instruction. If no new BPM is received after 100ms, execution continues anyway.
1 parent 00f8a6c commit 272a931

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

app/server/ruby/lib/sonicpi/tau_api.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class TauAPI
2121
attr_reader :booted
2222

2323
def initialize(ports, handlers)
24+
@incoming_tempo_change_cv = ConditionVariable.new
25+
@incoming_tempo_change_mut = Mutex.new
2426
@tau_api_events = IncomingEvents.new
2527
@client_id = @tau_api_events.gensym("RubyTauAPI")
2628
@osc_cues_port = ports[:osc_cues_port]
@@ -40,6 +42,7 @@ def initialize(ports, handlers)
4042
@link_time_micros = nil
4143
@local_time_micros = nil
4244
@link_time_delta_micros_prom = Promise.new
45+
4346
Thread.new do
4447
initialize_link_info!
4548
end
@@ -125,7 +128,13 @@ def link_get_beat_at_clock_time(clock_time, quantum = 4)
125128
end
126129

127130
def link_set_bpm_at_clock_time!(bpm, clock_time)
128-
@tau_comms.send_ts(clock_time, "/link-set-tempo", bpm.to_f)
131+
res = @tau_comms.send_ts(clock_time, "/link-set-tempo", bpm.to_f)
132+
133+
# Wait for a max of 100ms for the next tempo change to come in...
134+
@incoming_tempo_change_mut.synchronize do
135+
@incoming_tempo_change_cv.wait(@incoming_tempo_change_mut, 0.1)
136+
end
137+
res
129138
end
130139

131140
def link_disable
@@ -190,6 +199,7 @@ def initialize_link_info!
190199

191200
def add_incoming_api_handlers!
192201
@tau_comms.add_method("/link-tempo-change") do |args|
202+
@incoming_tempo_change_cv.broadcast
193203
gui_id = args[0]
194204
tempo = args[1].to_f
195205
@tempo = tempo

0 commit comments

Comments
 (0)