|
574 | 574 | const std::chrono::nanoseconds buffer_step{40000000}; |
575 | 575 |
|
576 | 576 | 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())}; |
579 | 579 |
|
580 | 580 | // Process all available events and schedule them with CoreMIDI |
581 | 581 | while (!Exit.load(std::memory_order_relaxed)) |
|
586 | 586 | continue; |
587 | 587 | } |
588 | 588 |
|
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; |
593 | 593 | auto schedule_time = time_until_pulled_ev - buffer_step; |
594 | 594 | if (schedule_time >= buffer_step) |
595 | 595 | { // Try to keep buffered events under 2x buffer_step |
|
611 | 611 | Tempo = PulledEvent.data.tempo; |
612 | 612 | break; |
613 | 613 | 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())); |
615 | 615 | break; |
616 | 616 | case EVENT_NOP: |
617 | 617 | default: |
|
0 commit comments