Skip to content

Commit c442de2

Browse files
committed
CoreMIDIDevice::PlayerLoop(): use std::chrono where applicable
1 parent 3181bd9 commit c442de2

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

‎source/mididevices/music_coremidi_mididevice.mm‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,8 @@
574574
const std::chrono::nanoseconds buffer_step{40000000};
575575

576576
Tempo = InitialTempo;
577-
// Initialize midi clock with current host time
578-
MIDITimeStamp buffer_timestamp = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime());
577+
// Initialize midi clock with current host time, CoreAudio and CoreMidi work in nano seconds.
578+
std::chrono::nanoseconds buffer_timestamp{AudioConvertHostTimeToNanos(AudioGetCurrentHostTime())};
579579

580580
// Process all available events and schedule them with CoreMIDI
581581
while (!Exit.load(std::memory_order_relaxed))
@@ -586,10 +586,10 @@
586586
continue;
587587
}
588588

589-
// CoreAudio and CoreMidi work in nano seconds so multiply by 1000.
590-
auto pulled_ev_time_delta = 1000 * uint64_t{PulledEvent.tick_delta} * Tempo / Division;
591-
MIDITimeStamp pulled_ev_timestamp = buffer_timestamp + pulled_ev_time_delta;
592-
std::chrono::nanoseconds time_until_pulled_ev{pulled_ev_timestamp - AudioConvertHostTimeToNanos(AudioGetCurrentHostTime())};
589+
std::chrono::microseconds pulled_ev_time_delta{uint64_t{PulledEvent.tick_delta} * Tempo / Division};
590+
auto pulled_ev_timestamp = buffer_timestamp + pulled_ev_time_delta;
591+
std::chrono::nanoseconds current_timestamp{AudioConvertHostTimeToNanos(AudioGetCurrentHostTime())};
592+
auto time_until_pulled_ev = pulled_ev_timestamp - current_timestamp;
593593
auto schedule_time = time_until_pulled_ev - buffer_step;
594594
if (schedule_time >= buffer_step)
595595
{ // Try to keep buffered events under 2x buffer_step
@@ -611,7 +611,7 @@
611611
Tempo = PulledEvent.data.tempo;
612612
break;
613613
case EVENT_MESSAGE:
614-
HandleEvent(PulledEvent.data.msg_buffer, PulledEvent.word_count, AudioConvertNanosToHostTime(pulled_ev_timestamp));
614+
HandleEvent(PulledEvent.data.msg_buffer, PulledEvent.word_count, AudioConvertNanosToHostTime(pulled_ev_timestamp.count()));
615615
break;
616616
case EVENT_NOP:
617617
default:

0 commit comments

Comments
 (0)