commits
tags
// Runtime check 5: audio latency. Every second a click and a white flash
// start in the same frame; A clicks and flashes at once. Up/down choose how
// many 5.33 ms frames are kept queued ahead (2, 4, 8, 15): if the click
// trails the flash by the same amount at 2 as at 15, the delay is past the
// title (the kernel's driver or the output). Logs to game:/audio-log.txt.
// Back+Start or the Guide's Xbox Home leaves.
#include <cstdio>
#include <cstdarg>
#include <cstring>
#include <cmath>
#include "xex_runtime.h"
#include "xex_video.h"
#include "xex_input.h"
#include "xex_audio.h"
static void logLine(const char *fmt, ...) {
char line[256];
va_list ap;
va_start(ap, fmt);
vsnprintf(line, sizeof line, fmt, ap);
va_end(ap);
printf("audio: %s\n", line);
if (FILE *f = fopen("game:/audio-log.txt", "a")) {
fprintf(f, "%s\n", line);
fclose(f);
}
}
static const unsigned AheadChoices[4] = {2, 4, 8, 15};
int main(int, char **) {
remove("game:/audio-log.txt");
xex_video_trace = logLine;
if (xex_video_init() != 0) {
logLine("video init failed: %s", xex_video_error());
return 1;
}
xex_video_trace = nullptr;
if (xex_audio_open() != 0) {
logLine("audio open failed");
xex_notify("audio test: XAudio registration failed");
xex_sleep_ms(3000);
return 1;
}
logLine("audio open");
const uint64_t freq = xex_timebase_freq();
uint64_t start = xex_timebase(), lastLog = start;
unsigned lastCalls = xex_audio_callbacks();
int aheadChoice = 1;
uint16_t lastButtons = 0;
int clickSamplesLeft = 0; // samples of the current click still to queue
double phase = 0;
int flashFrames = 0;
uint64_t nextBeat = start + freq;
for (;;) {
xex_pad pad;
xex_input_get(0, &pad);
uint16_t pressed = pad.buttons & ~lastButtons;
lastButtons = pad.buttons;
if ((pad.buttons & XEX_PAD_BACK) && (pad.buttons & XEX_PAD_START)) break;
if (pressed & XEX_PAD_UP && aheadChoice < 3) aheadChoice++;
if (pressed & XEX_PAD_DOWN && aheadChoice > 0) aheadChoice--;
if (pressed & (XEX_PAD_UP | XEX_PAD_DOWN)) logLine("frames ahead: %u", AheadChoices[aheadChoice]);
uint64_t now = xex_timebase();
bool click = (pressed & XEX_PAD_A) != 0;
if (now >= nextBeat) { click = true; nextBeat += freq; }
if (click) { clickSamplesLeft = 2400; flashFrames = 3; } // 50 ms tone
// keep the queue at the chosen depth; the click starts in the next
// frame queued, the same video frame as the flash
unsigned ahead = AheadChoices[aheadChoice];
while (xex_audio_queued() < ahead) {
float *frame = xex_audio_next();
if (!frame) break;
for (int i = 0; i < XEX_AUDIO_FRAME_SAMPLES; i++) {
float v = 0;
if (clickSamplesLeft > 0) {
v = 0.5f * (float)sin(phase);
phase += 2 * M_PI * 1000.0 / XEX_AUDIO_RATE;
clickSamplesLeft--;
}
frame[i] = v;
frame[XEX_AUDIO_FRAME_SAMPLES + i] = v;
}
xex_audio_queue();
}
xex_frame f = xex_video_begin();
uint32_t bg = flashFrames > 0 ? xex_video_rgb(255, 255, 255) : xex_video_rgb(20, 20, 40);
if (flashFrames > 0) flashFrames--;
for (int y = 0; y < f.window_height; y++)
for (int x = 0; x < f.window_width; x++) {
uint32_t c = bg;
// bars: frames ahead chosen (yellow), queued now (green)
if (y >= 40 && y < 80 && x >= 40 && x < 40 + (int)ahead * 40 && (x - 40) % 40 < 34)
c = xex_video_rgb(255, 236, 39);
if (y >= 100 && y < 140 && x >= 40 && x < 40 + (int)xex_audio_queued() * 40 && (x - 40) % 40 < 34)
c = xex_video_rgb(0, 228, 54);
f.pixels[xex_video_tiled_index(f.window_x + x, f.window_y + y, f.pitch)] = c;
}
xex_video_present();
if (now - lastLog >= 2 * freq) {
unsigned calls = xex_audio_callbacks();
logLine("%.1f s: callbacks %u/s, queued %u (ahead %u), underruns %u",
(double)(now - start) / freq, (unsigned)((calls - lastCalls) * freq / (now - lastLog)),
xex_audio_queued(), ahead, xex_audio_underruns());
lastCalls = calls;
lastLog = now;
}
}
logLine("exit");
return 0;
}