Skip to content

Commit 44b97f2

Browse files
committed
MIDI - optimise midi_clock_beat
Now no longer requires 24 messages to be sent per beat from Spider to Tau. Instead, just one message is sent with the number of ms for a beat. Tau then does the scheduling for 24 MIDI clock tick messages over the beat duration to be sent to sp_midi.
1 parent b2e2dba commit 44b97f2

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

app/server/erlang/tau/src/tau_server/tau_server_midi.erl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ midi_send(_Time, <<Data/binary>>) ->
148148
case tau_server_midi_out:encode_midi_from_osc(Data) of
149149
{ok, multi_chan, _, PortName, MIDIBinaries} ->
150150
[sp_midi:midi_send(PortName, MB) || MB <- MIDIBinaries];
151+
{ok, clock_beat, PortName, BeatDurationMs, MIDIBinary} ->
152+
%% Send a full MIDI beat's worth of clock tick messages.
153+
%% This means 24 message each separated by TickIntervalMs.
154+
155+
TickIntervalMs = BeatDurationMs / 24.0,
156+
lists:foreach(
157+
fun(I) ->
158+
spawn(fun () ->
159+
timer:sleep(round(I * TickIntervalMs)),
160+
sp_midi:midi_send(PortName, MIDIBinary)
161+
end)
162+
end, lists:seq(0, 23));
151163
{ok, _, PortName, MIDIBinary} ->
152164
sp_midi:midi_send(PortName, MIDIBinary);
153165
{error, ErrStr} ->

app/server/erlang/tau/src/tau_server/tau_server_midi_out.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ encode_midi_from_osc(<<Bin/binary>>) ->
166166
Data = encode(start),
167167
{ok, start, PortName, Data};
168168

169+
{cmd, ["/clock_beat", PortName, TickIntervalMs]} ->
170+
Data = encode(clock),
171+
{ok, clock_beat, PortName, TickIntervalMs, Data};
172+
169173
{cmd, ["/stop", PortName]} ->
170174
Data = encode(stop),
171175
{ok, stop, PortName, Data};

app/server/ruby/lib/sonicpi/lang/midi.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,13 +1391,11 @@ def midi_clock_beat(*args)
13911391
end
13921392

13931393
ports.each do |p|
1394-
time_warp times do |i, el|
1395-
__midi_send_timed("/clock", p)
1396-
end
1394+
__midi_send_timed_param_2("/clock_beat", p, __get_beat_dur_in_ms)
1395+
__midi_message "midi_clock_beat port: #{port}"
13971396
end
1398-
1399-
__midi_message "midi_clock_beat port: #{port}"
14001397
else
1398+
14011399
__midi_rest_message "midi_clock_beat port: #{port}"
14021400
end
14031401
end

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ def __change_spider_beat_and_time_by_beat_delta!(beat_delta)
165165
end
166166
end
167167

168+
def __get_beat_dur_in_ms
169+
__get_spider_sleep_mul * 1000.0
170+
end
171+
168172
def __get_spider_time
169173
if __in_link_bpm_mode
170174
@tau_api.link_get_clock_time_at_beat(__get_spider_beat)

0 commit comments

Comments
 (0)