SyncTERM 1.9: report audio playback position -- APC A;Pos The same patch as the master series' 0002, applied on top of R0006. One APC verb, so a board can ask a channel how much sound it has actually PLAYED and show the video frame that belongs with it, instead of estimating the position and drifting: board -> terminal APC "SyncTERM:A;Pos;C=" ST terminal -> board CSI = 7 ; ; 9 ; n is the channel's output frame count divided by the sample rate, read under the stream mutex. Marker 9 keeps it distinct from the existing state answer (busy 1 / idle 0), and it is numeric, so a board that already strips "CSI = [0-9;]* n" from application input removes this one too. xp_audio_frames_played() is the xpdev half: the mixer already knows how many frames it pulled for a stream on each cycle, this keeps the running total and exports it. REQUIRES R0006 -- without the subsystem there is nothing to extend. --- a/src/xpdev/xpbeep.h +++ b/src/xpdev/xpbeep.h @@ -180,6 +180,7 @@ * Intended for non-blocking channel-state polling (e.g., APC audio * idle-transition notifications). */ DLLEXPORT bool xp_audio_is_idle(xp_audio_handle_t h); +DLLEXPORT uint64_t xp_audio_frames_played(xp_audio_handle_t h); /* Signal that no more data will be appended. The stream will drain naturally. */ DLLEXPORT void xp_audio_finish(xp_audio_handle_t h); --- a/src/xpdev/xpbeep.c +++ b/src/xpdev/xpbeep.c @@ -2308,6 +2308,10 @@ * malloc or a drainer on queue * empty can re-check. */ pthread_mutex_t mutex; + /* Total output frames this stream has been mixed for since it was + * opened -- the playback position the board reads to know exactly + * where the ear is, so it can show the matching video frame. */ + uint64_t frames_played; }; /* Waiters use this timeout as a safety net so they always re-check the @@ -2705,6 +2709,10 @@ } if (dropped_any || s->head == NULL) SetEvent(s->progress); + /* Advance this stream's playback position by what it output + * this pull (off), so xp_audio_frames_played reports where the + * ear actually is. */ + s->frames_played += (uint64_t)off; assert_pthread_mutex_unlock(&s->mutex); } @@ -2990,6 +2998,25 @@ return idle; } +uint64_t +xp_audio_frames_played(xp_audio_handle_t h) +{ + /* Total output frames this stream has been mixed for since it was + * opened. Divided by the sample rate this is the playback position + * in seconds -- where the ear actually is. The board anchors this + * once against the source second of the audio it started sending, + * then knows the ear's source second exactly, with no estimate. */ + struct xp_audio_stream *s = stream_from_handle(h); + uint64_t n; + + if (!s) + return 0; + assert_pthread_mutex_lock(&s->mutex); + n = s->frames_played; + assert_pthread_mutex_unlock(&s->mutex); + return n; +} + void xp_audio_finish(xp_audio_handle_t h) { --- a/src/syncterm/audio_apc.c +++ b/src/syncterm/audio_apc.c @@ -590,6 +590,44 @@ apc_notify[ch] = true; } +static void +do_pos(struct cterminal *cterm, char *str) +{ + /* Report the channel's playback POSITION -- how much sound it has + * actually played, in milliseconds -- so the board knows where the + * ear is exactly and shows the matching video frame, instead of + * estimating. Answer: CSI = 7 ; ; 9 ; n + * + * The board strips CSI = ... n reports from app input, and the + * "9" marker distinguishes this from the busy/idle answer so an + * older board that never asks is unaffected. */ + char *p; + char *end; + unsigned long ch = AUDIO_APC_CHANNELS; + xp_audio_handle_t h; + uint64_t frames; + uint64_t ms; + char tmp[64]; + int n; + + init_state_once(); + for (p = str; p && *p == ';'; p = strchr(p + 1, ';')) { + if (p[1] == 'C' && p[2] == '=') + ch = strtoul(p + 3, &end, 10); + } + if (ch >= AUDIO_APC_CHANNELS) + return; + h = channel_handle(cterm, (int)ch); + frames = (h >= 0) ? xp_audio_frames_played(h) : 0; + ms = (frames * 1000ULL) / (uint64_t)XPBEEP_SAMPLE_RATE; + /* Marker 9 = "position, in ms, follows". Numeric so the board's + * input filter (which strips CSI = [0-9;]* n) removes it for apps too; + * busy is ;1n and idle is ;0n, so 9 never collides. */ + n = snprintf(tmp, sizeof(tmp), "\x1b[=7;%lu;9;%lun", + (unsigned long)ch, (unsigned long)ms); + cterm_respond(cterm, tmp, (size_t)n); +} + /* ----- top-level dispatchers ----- */ void @@ -620,6 +658,8 @@ do_volume(cterm, strbuf + 6); else if (strncmp(strbuf, "Wait", 4) == 0 && (strbuf[4] == ';' || strbuf[4] == '\0')) do_wait(cterm, strbuf + 4); + else if (strncmp(strbuf, "Pos", 3) == 0 && (strbuf[3] == ';' || strbuf[3] == '\0')) + do_pos(cterm, strbuf + 3); else if (strncmp(strbuf, "Update", 6) == 0 && (strbuf[6] == ';' || strbuf[6] == '\0')) do_update(strbuf + 6); }