foxygit / fake-08-xbox360 Log in
main xbox360 commits tags

/platform/xex/runtime/audio.c · 2.99 KB

raw
/*
 * Sound through the kernel's audio render driver; see xex_audio.h.
 * Facts from xsdk's hardware runs (docs/HARDWARE.md, "Runtime, milestone
 * 2" and "Audio's feeding, milestone 58"): the callback runs on a kernel
 * thread every 5.33 ms and may submit the frame itself; frames are in
 * cached physical memory; the callback uses integers only.
 */
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <xbox360/xboxkrnl.h>

#include "xex_audio.h"

#define FRAME_FLOATS (XEX_AUDIO_FRAME_SAMPLES * XEX_AUDIO_CHANNELS)
#define SILENCE_SLOT XEX_AUDIO_RING_FRAMES

static float *frames;              /* ring + one silent frame */
static PVOID volatile driver;      /* 0 while closed */
static volatile uint32_t queued;   /* written by the title */
static volatile uint32_t taken;    /* written by the callback */
static volatile uint32_t silent;
static volatile uint32_t calls;     /* callbacks so far */
static XAUDIORENDERDRVCLIENT client;

/* Kernel thread, once per frame. Called through xex_audio_callback_entry
 * (kernel_entries.S), which keeps r14-r31 whole for the kernel. */
void xex_audio_callback(PXAUDIORENDERDRVCALLBACKDATA data)
{
    (void)data;
    calls = calls + 1;
    PVOID d = driver;
    if (!d) return;
    uint32_t t = taken;
    const float *frame;
    if (queued != t) {
        frame = frames + (t % XEX_AUDIO_RING_FRAMES) * FRAME_FLOATS;
        __asm__ volatile("lwsync" ::: "memory");
        taken = t + 1;
    } else {
        frame = frames + SILENCE_SLOT * FRAME_FLOATS;
        if (queued) silent = silent + 1;
    }
    XAudioSubmitRenderDriverFrame(d, frame);
}

extern void xex_audio_callback_entry(PXAUDIORENDERDRVCALLBACKDATA data);

int xex_audio_open(void)
{
    if (driver) return 0;
    if (!frames) {
        uint32_t bytes = (XEX_AUDIO_RING_FRAMES + 1) * FRAME_FLOATS * sizeof(float);
        frames = (float *)MmAllocatePhysicalMemoryEx(0, bytes, PAGE_READWRITE, 0, 0xffffffffu, 0);
        if (!frames) return -1;
        memset(frames, 0, bytes);   /* every channel silent */
    }
    queued = 0;
    taken = 0;
    silent = 0;
    client.Callback = xex_audio_callback_entry;
    client.Context = NULL;
    PVOID d = NULL;
    HRESULT hr = XAudioRegisterRenderDriverClient(&client, &d);
    if (hr < 0 || !d) return -1;
    __asm__ volatile("lwsync" ::: "memory");
    driver = d;
    atexit(xex_audio_close);
    return 0;
}

unsigned xex_audio_queued(void)
{
    return queued - taken;
}

float *xex_audio_next(void)
{
    if (!driver || queued - taken >= XEX_AUDIO_RING_FRAMES - 1) return NULL;
    return frames + (queued % XEX_AUDIO_RING_FRAMES) * FRAME_FLOATS;
}

void xex_audio_queue(void)
{
    __asm__ volatile("lwsync" ::: "memory");
    queued = queued + 1;
}

unsigned xex_audio_callbacks(void)
{
    return calls;
}

unsigned xex_audio_underruns(void)
{
    return silent;
}

void xex_audio_close(void)
{
    PVOID d = driver;
    if (!d) return;
    driver = NULL;
    /* the frames stay allocated: a callback can still run after this */
    XAudioUnregisterRenderDriverClient(d);
}