diff --git a/platform/bt.c b/platform/bt.c index 2cbb993..142ad4b 100644 --- a/platform/bt.c +++ b/platform/bt.c @@ -110,10 +110,15 @@ static void bt_poll(void) { /* TIMER5, beside usb_poll */ was_live = live; } -static void bt_midi_out(uint32_t pkt) { /* midi_send, interrupts off */ +static void bt_midi_out(uint32_t pkt, uint32_t ms) { /* midi_send, interrupts off; ms: when it falls (midi_ms) */ + static uint16_t last; + if ((int16_t)((uint16_t)ms - last) < 0) { /* never before the last one: a packet's time stamps only go forward */ + ms = last; + } + last = (uint16_t)ms; if (bt_live && bo_w - bo_r < BQ) { bt_out[bo_w % BQ].pkt = pkt; - bt_out[bo_w % BQ].ms = (uint16_t)fm1_ms; + bt_out[bo_w % BQ].ms = (uint16_t)ms; RING_PUBLISH(); bo_w++; } diff --git a/platform/clock.c b/platform/clock.c index 6cd7499..3e9f51c 100644 --- a/platform/clock.c +++ b/platform/clock.c @@ -45,6 +45,7 @@ static void ck_add(uint32_t at, int what) { ck_n++; } if (ck_out) { + midi_frame(at); midi_send(ST[what], 0, 0); } } diff --git a/platform/fm1.c b/platform/fm1.c index be8cd56..301230b 100644 --- a/platform/fm1.c +++ b/platform/fm1.c @@ -34,7 +34,9 @@ #endif bootguard_t bootguard __attribute__((section(".noinit"))); -static volatile uint32_t fm1_ms; /* milliseconds since power-on (TIMER5) */ +static volatile uint32_t fm1_ms; /* milliseconds since power-on (TIMER5) */ +static volatile uint32_t fm1_ms_t4; /* TIMER4's count when fm1_ms last stepped */ +static volatile uint8_t in_audio; /* the audio interrupt is running */ // clang-format off #define RING_PUBLISH() __asm__ volatile("" ::: "memory") @@ -212,12 +214,21 @@ int midi_read(uint8_t *status, uint8_t *d1, uint8_t *d2) { return pk & 0x20u ? MIDI_BLE : pk & 0x10u ? MIDI_TRS : MIDI_USB; /* the cable: 0 USB, 1 TRS, 2 Bluetooth */ } +/* the time of a send, in ms: in audio(), the frame midi_frame named (the block's start + its offset); else now */ +static uint32_t au_ms, au_t4, au_at; /* the block audio() renders: fm1_ms, TIMER4 ticks past it, the frame */ +void midi_frame(uint32_t at) { au_at = at; } +static uint32_t midi_ms(void) { + if (!in_audio) { + return fm1_ms; + } + return au_ms + (au_t4 + au_at * (24000000u / 4410u) / 10u) / (1000u * FM1_TICKS_PER_US); /* 44.1 kHz frames */ +} void midi_send(uint8_t status, uint8_t d1, uint8_t d2) { uint32_t st = status, f = irq_save(); /* (loop() and audio() may both send) */ uint32_t pk = (st >> 4) | st << 8 | (uint32_t)(d1 & 0x7Fu) << 16 | (uint32_t)(d2 & 0x7Fu) << 24; midi_out_event(pk); #if FOMA_BLUETOOTH - bt_midi_out(pk); + bt_midi_out(pk, midi_ms()); #endif irq_restore(f); } @@ -285,7 +296,6 @@ void print(const char *s) { (void)s; } /* ---- the timer: 10 kHz key scan, milliseconds, USB at 2 kHz. It outranks the audio ISR; nested in a * render it only scans, counts and feeds the USB audio stream */ -static volatile uint8_t in_audio; void fm1_timer5_irq(void) { static uint32_t sub, owed, last, acc; uint32_t t0 = fm1_ticks(), usb_due = sub % 5u == 0u; @@ -297,6 +307,7 @@ void fm1_timer5_irq(void) { acc -= 1000u * FM1_TICKS_PER_US; fm1_ms++; } + fm1_ms_t4 = t0 - acc; if (usb_due) { owed = 1; } @@ -343,6 +354,13 @@ void fm1_alnk0_irq(void) { /* via isr_alnk0 (hal/fm1_isr.S) */ fm1_audio_ack_aux(p); if (p & FM1_AUDIO_HALF) { int32_t *o = &abuf[fm1_audio_free_half() * HALF_WORDS]; + { + uint32_t m, t; + do { + m = fm1_ms, t = fm1_ms_t4; + } while (m != fm1_ms); /* (TIMER5 may step it meanwhile) */ + au_ms = m, au_t4 = fm1_ticks() - t, au_at = 0; + } uint32_t i; for (i = 0; i < HALF_WORDS; i++) { o[i] = 0; @@ -353,6 +371,7 @@ void fm1_alnk0_irq(void) { /* via isr_alnk0 (hal/fm1_isr.S) */ #if FOMA_MIDI_CLOCK clock_block(HALF_FRAMES, fm1_ticks()); #endif + au_at = 0; audio(o, HALF_FRAMES); for (i = 0; i < HALF_WORDS; i++) { int32_t v = (o[i] * master_q12) >> 12; diff --git a/platform/fm1.h b/platform/fm1.h index 7a9dfc2..8d735fb 100644 --- a/platform/fm1.h +++ b/platform/fm1.h @@ -117,6 +117,8 @@ enum { MIDI_USB = 1, MIDI_ANY = 7 }; int midi_read(uint8_t *status, uint8_t *d1, uint8_t *d2); /* 0: none; else where it came from: MIDI_USB, MIDI_TRS, MIDI_BLE */ void midi_send(uint8_t status, uint8_t d1, uint8_t d2); +void midi_frame(uint32_t at); /* audio() only: the frame of this block the next midi_send calls fall on (Bluetooth + * MIDI stamps them with that time; USB has no time stamps). 0 when audio() starts */ #if FOMA_MIDI_CLOCK /* ---- MIDI clock (midi_clock): one clock for the app, 24 ticks a beat. It runs at clock_tempo(), or follows the diff --git a/platform/mvave/port.c b/platform/mvave/port.c index 2731f3b..7bdd4d3 100644 --- a/platform/mvave/port.c +++ b/platform/mvave/port.c @@ -127,6 +127,7 @@ int midi_read(uint8_t *status, uint8_t *d1, uint8_t *d2) { /* ---- MIDI out: midi_send (loop() or audio()) queues, the control task sends (the SDK's rule) */ static uint32_t mo_q[MQ]; static volatile uint32_t mo_w, mo_r; +void midi_frame(uint32_t at) { (void)at; } /* (the SDK sends no time stamps) */ void midi_send(uint8_t status, uint8_t d1, uint8_t d2) { uint32_t s = mv_irq_save(); if (mo_w - mo_r < MQ) { diff --git a/platform/web.c b/platform/web.c index dc40a63..37c4a0b 100644 --- a/platform/web.c +++ b/platform/web.c @@ -158,6 +158,7 @@ int midi_read(uint8_t *status, uint8_t *d1, uint8_t *d2) { *d2 = (uint8_t)(m >> 16); return MIDI_USB; /* (the page's MIDI in stands for USB) */ } +void midi_frame(uint32_t at) { (void)at; } void midi_send(uint8_t status, uint8_t d1, uint8_t d2) { if (mo_w - mo_r < MQ) { mq_out[mo_w++ % MQ] = status | (uint32_t)(d1 & 0x7Fu) << 8 | (uint32_t)(d2 & 0x7Fu) << 16;