mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-26 20:40:28 +00:00
Remove callback from cosmoaudio API
Using callbacks is still problematic with cosmo_dlopen() due to the need to restore the TLS register. So using callbacks is even more strict than using signal handlers. We are better off introducing a cosmoaudio_poll() function. It makes the API more UNIX-like. How bad could the latency be?
This commit is contained in:
parent
d99f066114
commit
d50d954a3c
17 changed files with 433 additions and 158 deletions
|
@ -58,7 +58,9 @@ static struct {
|
|||
typeof(cosmoaudio_open) *open;
|
||||
typeof(cosmoaudio_close) *close;
|
||||
typeof(cosmoaudio_write) *write;
|
||||
typeof(cosmoaudio_flush) *flush;
|
||||
typeof(cosmoaudio_read) *read;
|
||||
typeof(cosmoaudio_poll) *poll;
|
||||
} g_audio;
|
||||
|
||||
static const char *cosmoaudio_tmp_dir(void) {
|
||||
|
@ -232,7 +234,9 @@ WeAreGood:
|
|||
g_audio.open = cosmo_dlsym(handle, "cosmoaudio_open");
|
||||
g_audio.close = cosmo_dlsym(handle, "cosmoaudio_close");
|
||||
g_audio.write = cosmo_dlsym(handle, "cosmoaudio_write");
|
||||
g_audio.flush = cosmo_dlsym(handle, "cosmoaudio_flush");
|
||||
g_audio.read = cosmo_dlsym(handle, "cosmoaudio_read");
|
||||
g_audio.poll = cosmo_dlsym(handle, "cosmoaudio_poll");
|
||||
}
|
||||
|
||||
static void cosmoaudio_init(void) {
|
||||
|
@ -295,3 +299,34 @@ COSMOAUDIO_ABI int cosmoaudio_read(struct CosmoAudio *ca, float *data,
|
|||
cosmoaudio_describe_status(sbuf, sizeof(sbuf), status));
|
||||
return status;
|
||||
}
|
||||
|
||||
COSMOAUDIO_ABI int cosmoaudio_flush(struct CosmoAudio *ca) {
|
||||
int status;
|
||||
char sbuf[32];
|
||||
if (g_audio.flush)
|
||||
status = g_audio.flush(ca);
|
||||
else
|
||||
status = COSMOAUDIO_ELINK;
|
||||
DATATRACE("cosmoaudio_flush(%p) → %s", ca,
|
||||
cosmoaudio_describe_status(sbuf, sizeof(sbuf), status));
|
||||
return status;
|
||||
}
|
||||
|
||||
COSMOAUDIO_ABI int cosmoaudio_poll(struct CosmoAudio *ca,
|
||||
int *in_out_readFrames,
|
||||
int *in_out_writeFrames) {
|
||||
int status;
|
||||
char sbuf[32];
|
||||
char fbuf[2][20];
|
||||
if (g_audio.poll)
|
||||
status = g_audio.poll(ca, in_out_readFrames, in_out_writeFrames);
|
||||
else
|
||||
status = COSMOAUDIO_ELINK;
|
||||
DATATRACE("cosmoaudio_poll(%p, %s, %s) → %s", ca,
|
||||
cosmoaudio_describe_poll_frames(fbuf[0], sizeof(fbuf[0]),
|
||||
in_out_readFrames),
|
||||
cosmoaudio_describe_poll_frames(fbuf[1], sizeof(fbuf[1]),
|
||||
in_out_writeFrames),
|
||||
cosmoaudio_describe_status(sbuf, sizeof(sbuf), status));
|
||||
return status;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
# nmake /f Makefile.msvc check
|
||||
# nmake /f Makefile.msvc MODE=debug check
|
||||
#
|
||||
# Note: MSVC 2019 makes the DLL 64kb smaller than MSVC 2022.
|
||||
|
||||
# Compiler and linker
|
||||
CC=cl
|
||||
|
|
|
@ -39,15 +39,16 @@
|
|||
#endif
|
||||
|
||||
struct CosmoAudio {
|
||||
ma_device device;
|
||||
ma_pcm_rb input;
|
||||
ma_pcm_rb output;
|
||||
ma_uint32 sampleRate;
|
||||
ma_uint32 channels;
|
||||
ma_uint32 periods;
|
||||
enum CosmoAudioDeviceType deviceType;
|
||||
cosmoaudio_data_callback_f* dataCallback;
|
||||
void* argument;
|
||||
ma_uint32 outputBufferFrames;
|
||||
ma_uint32 inputBufferFrames;
|
||||
int sampleRate;
|
||||
int channels;
|
||||
int isLeft;
|
||||
ma_device device;
|
||||
ma_pcm_rb output;
|
||||
ma_pcm_rb input;
|
||||
ma_event event;
|
||||
};
|
||||
|
||||
static int read_ring_buffer(ma_pcm_rb* rb, float* pOutput, ma_uint32 frameCount,
|
||||
|
@ -69,8 +70,10 @@ static int read_ring_buffer(ma_pcm_rb* rb, float* pOutput, ma_uint32 frameCount,
|
|||
framesToRead * channels * sizeof(float));
|
||||
result = ma_pcm_rb_commit_read(rb, framesToRead);
|
||||
if (result != MA_SUCCESS) {
|
||||
if (result == MA_AT_END)
|
||||
if (result == MA_AT_END) {
|
||||
framesRead += framesToRead;
|
||||
break;
|
||||
}
|
||||
LOG("ma_pcm_rb_commit_read failed: %s\n", ma_result_description(result));
|
||||
return COSMOAUDIO_ERROR;
|
||||
}
|
||||
|
@ -99,8 +102,10 @@ static int write_ring_buffer(ma_pcm_rb* rb, const float* pInput,
|
|||
framesToWrite * channels * sizeof(float));
|
||||
result = ma_pcm_rb_commit_write(rb, framesToWrite);
|
||||
if (result != MA_SUCCESS) {
|
||||
if (result == MA_AT_END)
|
||||
if (result == MA_AT_END) {
|
||||
framesWritten += framesToWrite;
|
||||
break;
|
||||
}
|
||||
LOG("ma_pcm_rb_commit_write failed: %s\n", ma_result_description(result));
|
||||
return COSMOAUDIO_ERROR;
|
||||
}
|
||||
|
@ -111,15 +116,38 @@ static int write_ring_buffer(ma_pcm_rb* rb, const float* pInput,
|
|||
static void data_callback_f32(ma_device* pDevice, float* pOutput,
|
||||
const float* pInput, ma_uint32 frameCount) {
|
||||
struct CosmoAudio* ca = (struct CosmoAudio*)pDevice->pUserData;
|
||||
if (ca->dataCallback) {
|
||||
ca->dataCallback(ca, pOutput, pInput, frameCount, ca->channels,
|
||||
ca->argument);
|
||||
} else {
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypePlayback)
|
||||
read_ring_buffer(&ca->output, pOutput, frameCount, ca->channels);
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypeCapture)
|
||||
write_ring_buffer(&ca->input, pInput, frameCount, ca->channels);
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypePlayback) {
|
||||
//
|
||||
// "By default, miniaudio will pre-silence the data callback's
|
||||
// output buffer. If you know that you will always write valid data
|
||||
// to the output buffer you can disable pre-silencing by setting
|
||||
// the noPreSilence config option in the device config to true."
|
||||
//
|
||||
// —Quoth miniaudio documentation § 16.1. Low Level API
|
||||
//
|
||||
if (ca->isLeft) {
|
||||
int framesCopied =
|
||||
read_ring_buffer(&ca->output, pOutput, frameCount, ca->channels);
|
||||
if (framesCopied < (int)frameCount)
|
||||
ca->isLeft = 0;
|
||||
} else {
|
||||
// TODO(jart): Maybe we should stretch the audio too short?
|
||||
int frameOffset;
|
||||
int availableFrames = ma_pcm_rb_available_read(&ca->output);
|
||||
if (availableFrames >= (int)frameCount) {
|
||||
frameOffset = 0;
|
||||
} else {
|
||||
frameOffset = frameCount - availableFrames;
|
||||
frameCount = availableFrames;
|
||||
}
|
||||
read_ring_buffer(&ca->output, pOutput + frameOffset * ca->channels,
|
||||
frameCount, ca->channels);
|
||||
ca->isLeft = 1;
|
||||
}
|
||||
}
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypeCapture)
|
||||
write_ring_buffer(&ca->input, pInput, frameCount, ca->channels);
|
||||
ma_event_signal(&ca->event);
|
||||
}
|
||||
|
||||
static void data_callback(ma_device* pDevice, void* pOutput, const void* pInput,
|
||||
|
@ -156,7 +184,7 @@ COSMOAUDIO_ABI int cosmoaudio_open( //
|
|||
return COSMOAUDIO_EINVAL;
|
||||
if (options->sizeofThis < (int)sizeof(struct CosmoAudioOpenOptions))
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (options->periods < 0)
|
||||
if (options->bufferFrames < 0)
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (options->sampleRate < 8000)
|
||||
return COSMOAUDIO_EINVAL;
|
||||
|
@ -170,14 +198,18 @@ COSMOAUDIO_ABI int cosmoaudio_open( //
|
|||
|
||||
// Allocate cosmo audio object.
|
||||
struct CosmoAudio* ca;
|
||||
if (!(ca = (struct CosmoAudio*)calloc(1, sizeof(struct CosmoAudio))))
|
||||
ca = (struct CosmoAudio*)calloc(1, sizeof(struct CosmoAudio));
|
||||
if (!ca)
|
||||
return COSMOAUDIO_ERROR;
|
||||
ca->channels = options->channels;
|
||||
ca->sampleRate = options->sampleRate;
|
||||
ca->deviceType = options->deviceType;
|
||||
ca->periods = options->periods ? options->periods : 10;
|
||||
ca->dataCallback = options->dataCallback;
|
||||
ca->argument = options->argument;
|
||||
|
||||
// Create win32-style condition variable.
|
||||
if (ma_event_init(&ca->event) != MA_SUCCESS) {
|
||||
free(ca);
|
||||
return COSMOAUDIO_ERROR;
|
||||
}
|
||||
|
||||
// Initialize device.
|
||||
ma_result result;
|
||||
|
@ -197,18 +229,26 @@ COSMOAUDIO_ABI int cosmoaudio_open( //
|
|||
deviceConfig.pUserData = ca;
|
||||
result = ma_device_init(NULL, &deviceConfig, &ca->device);
|
||||
if (result != MA_SUCCESS) {
|
||||
ma_event_uninit(&ca->event);
|
||||
free(ca);
|
||||
return COSMOAUDIO_ERROR;
|
||||
}
|
||||
|
||||
// Initialize the speaker ring buffer.
|
||||
if (!ca->dataCallback && (ca->deviceType & kCosmoAudioDeviceTypePlayback)) {
|
||||
result = ma_pcm_rb_init(
|
||||
ma_format_f32, ca->channels,
|
||||
ca->device.playback.internalPeriodSizeInFrames * ca->periods, NULL,
|
||||
NULL, &ca->output);
|
||||
int period = ca->device.playback.internalPeriodSizeInFrames;
|
||||
if (!options->bufferFrames) {
|
||||
ca->outputBufferFrames = period * 10;
|
||||
} else if (options->bufferFrames < period * 2) {
|
||||
ca->outputBufferFrames = period * 2;
|
||||
} else {
|
||||
ca->outputBufferFrames = options->bufferFrames;
|
||||
}
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypePlayback) {
|
||||
result = ma_pcm_rb_init(ma_format_f32, ca->channels, ca->outputBufferFrames,
|
||||
NULL, NULL, &ca->output);
|
||||
if (result != MA_SUCCESS) {
|
||||
ma_device_uninit(&ca->device);
|
||||
ma_event_uninit(&ca->event);
|
||||
free(ca);
|
||||
return COSMOAUDIO_ERROR;
|
||||
}
|
||||
|
@ -216,15 +256,22 @@ COSMOAUDIO_ABI int cosmoaudio_open( //
|
|||
}
|
||||
|
||||
// Initialize the microphone ring buffer.
|
||||
if (!ca->dataCallback && (ca->deviceType & kCosmoAudioDeviceTypeCapture)) {
|
||||
result = ma_pcm_rb_init(
|
||||
ma_format_f32, ca->channels,
|
||||
ca->device.capture.internalPeriodSizeInFrames * ca->periods, NULL, NULL,
|
||||
&ca->input);
|
||||
period = ca->device.capture.internalPeriodSizeInFrames;
|
||||
if (!options->bufferFrames) {
|
||||
ca->inputBufferFrames = period * 10;
|
||||
} else if (options->bufferFrames < period * 2) {
|
||||
ca->inputBufferFrames = period * 2;
|
||||
} else {
|
||||
ca->inputBufferFrames = options->bufferFrames;
|
||||
}
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypeCapture) {
|
||||
result = ma_pcm_rb_init(ma_format_f32, ca->channels, ca->inputBufferFrames,
|
||||
NULL, NULL, &ca->input);
|
||||
if (result != MA_SUCCESS) {
|
||||
if (!ca->dataCallback && (ca->deviceType & kCosmoAudioDeviceTypePlayback))
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypePlayback)
|
||||
ma_pcm_rb_uninit(&ca->output);
|
||||
ma_device_uninit(&ca->device);
|
||||
ma_event_uninit(&ca->event);
|
||||
free(ca);
|
||||
return COSMOAUDIO_ERROR;
|
||||
}
|
||||
|
@ -233,11 +280,12 @@ COSMOAUDIO_ABI int cosmoaudio_open( //
|
|||
|
||||
// Start audio playback.
|
||||
if (ma_device_start(&ca->device) != MA_SUCCESS) {
|
||||
if (!ca->dataCallback && (ca->deviceType & kCosmoAudioDeviceTypeCapture))
|
||||
ma_pcm_rb_uninit(&ca->input);
|
||||
if (!ca->dataCallback && (ca->deviceType & kCosmoAudioDeviceTypePlayback))
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypePlayback)
|
||||
ma_pcm_rb_uninit(&ca->output);
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypeCapture)
|
||||
ma_pcm_rb_uninit(&ca->input);
|
||||
ma_device_uninit(&ca->device);
|
||||
ma_event_uninit(&ca->event);
|
||||
free(ca);
|
||||
return COSMOAUDIO_ERROR;
|
||||
}
|
||||
|
@ -249,8 +297,13 @@ COSMOAUDIO_ABI int cosmoaudio_open( //
|
|||
/**
|
||||
* Closes audio device and frees all associated resources.
|
||||
*
|
||||
* This function is non-blocking and will drop buffered audio. In
|
||||
* playback mode, you need to call cosmoaudio_flush() to ensure data
|
||||
* supplied by cosmoaudio_write() gets played on your speaker.
|
||||
*
|
||||
* Calling this function twice on the same object will result in
|
||||
* undefined behavior.
|
||||
* undefined behavior. Even if this function fails, the `ca` will be
|
||||
* freed to the greatest extent possible.
|
||||
*
|
||||
* @param ca is CosmoAudio object returned earlier by cosmoaudio_open()
|
||||
* @return 0 on success, or negative error code on failure
|
||||
|
@ -258,11 +311,12 @@ COSMOAUDIO_ABI int cosmoaudio_open( //
|
|||
COSMOAUDIO_ABI int cosmoaudio_close(struct CosmoAudio* ca) {
|
||||
if (!ca)
|
||||
return COSMOAUDIO_EINVAL;
|
||||
ma_device_uninit(&ca->device);
|
||||
if (!ca->dataCallback && (ca->deviceType & kCosmoAudioDeviceTypePlayback))
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypePlayback)
|
||||
ma_pcm_rb_uninit(&ca->output);
|
||||
if (!ca->dataCallback && (ca->deviceType & kCosmoAudioDeviceTypeCapture))
|
||||
if (ca->deviceType & kCosmoAudioDeviceTypeCapture)
|
||||
ma_pcm_rb_uninit(&ca->input);
|
||||
ma_device_uninit(&ca->device);
|
||||
ma_event_uninit(&ca->event);
|
||||
free(ca);
|
||||
return COSMOAUDIO_SUCCESS;
|
||||
}
|
||||
|
@ -289,10 +343,10 @@ COSMOAUDIO_ABI int cosmoaudio_write(struct CosmoAudio* ca, const float* data,
|
|||
return COSMOAUDIO_EINVAL;
|
||||
if (frames < 0)
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (ca->dataCallback)
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (!(ca->deviceType & kCosmoAudioDeviceTypePlayback))
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (1u + frames > ca->outputBufferFrames)
|
||||
return COSMOAUDIO_ENOBUF;
|
||||
if (!frames)
|
||||
return 0;
|
||||
if (!data)
|
||||
|
@ -322,8 +376,6 @@ COSMOAUDIO_ABI int cosmoaudio_read(struct CosmoAudio* ca, float* data,
|
|||
return COSMOAUDIO_EINVAL;
|
||||
if (frames < 0)
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (ca->dataCallback)
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (!(ca->deviceType & kCosmoAudioDeviceTypeCapture))
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (!frames)
|
||||
|
@ -333,20 +385,80 @@ COSMOAUDIO_ABI int cosmoaudio_read(struct CosmoAudio* ca, float* data,
|
|||
return read_ring_buffer(&ca->input, data, frames, ca->channels);
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <Windows.h>
|
||||
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
|
||||
LPVOID lpReserved) {
|
||||
switch (ul_reason_for_call) {
|
||||
case DLL_PROCESS_ATTACH:
|
||||
case DLL_THREAD_ATTACH:
|
||||
case DLL_THREAD_DETACH:
|
||||
case DLL_PROCESS_DETACH:
|
||||
break;
|
||||
/**
|
||||
* Waits for read and/or write to become possible.
|
||||
*
|
||||
* @param ca is CosmoAudio object returned earlier by cosmoaudio_open()
|
||||
* @param in_out_readFrames if non-NULL specifies how many frames of
|
||||
* capture data be immediately readable by cosmoaudio_read() before
|
||||
* this can return; it must not exceed the buffer size; on return
|
||||
* this will be set to the actual number of frames in the buffer;
|
||||
* if the caller supplies a zero then this call is a non-blocking
|
||||
* way to query buffer sizes
|
||||
* @param in_out_writeFrames if non-NULL specifies how many frames of
|
||||
* capture data be immediately writable by cosmoaudio_write() before
|
||||
* this can return; it must not exceed the buffer size; on return
|
||||
* this will be set to the actual number of frames in the buffer;
|
||||
* if the caller supplies a zero then this call is a non-blocking
|
||||
* way to query buffer sizes
|
||||
* @return 0 on success, or negative error code on error
|
||||
*/
|
||||
COSMOAUDIO_ABI int cosmoaudio_poll(struct CosmoAudio* ca,
|
||||
int* in_out_readFrames,
|
||||
int* in_out_writeFrames) {
|
||||
if (!ca)
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (!in_out_readFrames && !in_out_writeFrames)
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (in_out_readFrames && !(ca->deviceType & kCosmoAudioDeviceTypeCapture))
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (in_out_writeFrames && !(ca->deviceType & kCosmoAudioDeviceTypePlayback))
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (in_out_readFrames && 1u + *in_out_readFrames > ca->inputBufferFrames)
|
||||
return COSMOAUDIO_ENOBUF;
|
||||
if (in_out_writeFrames && 1u + *in_out_writeFrames > ca->outputBufferFrames)
|
||||
return COSMOAUDIO_ENOBUF;
|
||||
for (;;) {
|
||||
int done = 0;
|
||||
ma_uint32 readable = 0;
|
||||
ma_uint32 writable = 0;
|
||||
if (in_out_readFrames) {
|
||||
readable = ma_pcm_rb_available_read(&ca->input);
|
||||
done |= readable >= (ma_uint32)*in_out_readFrames;
|
||||
}
|
||||
if (in_out_writeFrames) {
|
||||
writable = ma_pcm_rb_available_write(&ca->output);
|
||||
done |= writable >= (ma_uint32)*in_out_writeFrames;
|
||||
}
|
||||
if (done) {
|
||||
if (in_out_readFrames)
|
||||
*in_out_readFrames = readable;
|
||||
if (in_out_writeFrames)
|
||||
*in_out_writeFrames = writable;
|
||||
return COSMOAUDIO_SUCCESS;
|
||||
}
|
||||
if (ma_event_wait(&ca->event) != MA_SUCCESS)
|
||||
return COSMOAUDIO_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for written samples to be sent to device.
|
||||
*
|
||||
* This function is only valid to call in playback or duplex mode.
|
||||
*
|
||||
* @param ca is CosmoAudio object returned earlier by cosmoaudio_open()
|
||||
* @return 0 on success, or negative error code on failure
|
||||
*/
|
||||
COSMOAUDIO_ABI int cosmoaudio_flush(struct CosmoAudio* ca) {
|
||||
if (!ca)
|
||||
return COSMOAUDIO_EINVAL;
|
||||
if (!(ca->deviceType & kCosmoAudioDeviceTypePlayback))
|
||||
return COSMOAUDIO_EINVAL;
|
||||
for (;;) {
|
||||
if (!ma_pcm_rb_available_read(&ca->output))
|
||||
return COSMOAUDIO_SUCCESS;
|
||||
if (ma_event_wait(&ca->event) != MA_SUCCESS)
|
||||
return COSMOAUDIO_ERROR;
|
||||
}
|
||||
return TRUE;
|
||||
(void)hModule;
|
||||
(void)lpReserved;
|
||||
(void)ul_reason_for_call;
|
||||
}
|
||||
#endif
|
||||
|
|
Binary file not shown.
|
@ -21,6 +21,7 @@
|
|||
#define COSMOAUDIO_ERROR -1 // unspecified error
|
||||
#define COSMOAUDIO_EINVAL -2 // invalid parameters passed to api
|
||||
#define COSMOAUDIO_ELINK -3 // loading cosmoaudio dso failed
|
||||
#define COSMOAUDIO_ENOBUF -4 // invalid buffering parameters
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -28,14 +29,6 @@ extern "C" {
|
|||
|
||||
struct CosmoAudio;
|
||||
|
||||
typedef void cosmoaudio_data_callback_f( //
|
||||
struct CosmoAudio *ca, //
|
||||
float *outputSamples, //
|
||||
const float *inputSamples, //
|
||||
int frameCount, //
|
||||
int channels, //
|
||||
void *argument);
|
||||
|
||||
enum CosmoAudioDeviceType {
|
||||
kCosmoAudioDeviceTypePlayback = 1,
|
||||
kCosmoAudioDeviceTypeCapture = 2,
|
||||
|
@ -62,20 +55,10 @@ struct CosmoAudioOpenOptions {
|
|||
// for mono or 2 for stereo.
|
||||
int channels;
|
||||
|
||||
// Number of periods in ring buffer. Set to 0 for default. Higher
|
||||
// numbers (e.g. 20) means more buffering. Lower numbers (e.g. 2)
|
||||
// means less buffering. This is ignored if callback is specified.
|
||||
int periods;
|
||||
|
||||
// If callback is NULL, then cosmoaudio_write() and cosmoaudio_read()
|
||||
// should be used, which ring buffer audio to the default internal
|
||||
// routine. Setting this callback to non-NULL puts CosmoAudio in
|
||||
// manual mode, where the callback is responsible for copying PCM
|
||||
// samples each time the device calls this.
|
||||
cosmoaudio_data_callback_f *dataCallback;
|
||||
|
||||
// This is an arbitrary value passed to the callback.
|
||||
void *argument;
|
||||
// Number of frames in each ring buffer. A frame consists of a PCM
|
||||
// sample for each channel. Set to 0 for default. If this is less than
|
||||
// the device period size times two, it'll be increased to that value.
|
||||
int bufferFrames;
|
||||
};
|
||||
|
||||
COSMOAUDIO_API int cosmoaudio_version(void) COSMOAUDIO_ABI;
|
||||
|
@ -95,12 +78,22 @@ COSMOAUDIO_API int cosmoaudio_write( //
|
|||
int frameCount //
|
||||
) COSMOAUDIO_ABI;
|
||||
|
||||
COSMOAUDIO_API int cosmoaudio_flush( //
|
||||
struct CosmoAudio *ca //
|
||||
) COSMOAUDIO_ABI;
|
||||
|
||||
COSMOAUDIO_API int cosmoaudio_read( //
|
||||
struct CosmoAudio *ca, //
|
||||
float *out_samples, //
|
||||
int frameCount //
|
||||
) COSMOAUDIO_ABI;
|
||||
|
||||
COSMOAUDIO_API int cosmoaudio_poll( //
|
||||
struct CosmoAudio *ca, //
|
||||
int *in_out_readFrames, //
|
||||
int *in_out_writeFrames //
|
||||
) COSMOAUDIO_ABI;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -9,50 +9,68 @@
|
|||
#endif
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "cosmoaudio.h"
|
||||
|
||||
#define SAMPLING_RATE 44100
|
||||
#define WAVE_INTERVAL 440
|
||||
#define CHANNELS 2
|
||||
|
||||
#ifndef M_PIf
|
||||
#define M_PIf 3.14159265358979323846f
|
||||
#endif
|
||||
|
||||
int g_hz = 44100;
|
||||
int g_channels = 2;
|
||||
int g_generation = 0;
|
||||
int g_freq = 440;
|
||||
|
||||
void data_callback(struct CosmoAudio *ca, float *outputSamples,
|
||||
const float *inputSamples, int frameCount, int channels,
|
||||
void *argument) {
|
||||
for (int i = 0; i < frameCount; i++) {
|
||||
float t = (float)g_generation++ / g_hz;
|
||||
if (g_generation == g_hz)
|
||||
g_generation = 0;
|
||||
float s = sinf(2 * M_PIf * g_freq * t);
|
||||
for (int j = 0; j < channels; j++)
|
||||
outputSamples[i * channels + j] = s;
|
||||
}
|
||||
(void)inputSamples;
|
||||
(void)argument;
|
||||
(void)ca;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
struct CosmoAudioOpenOptions cao = {};
|
||||
struct CosmoAudioOpenOptions cao = {0};
|
||||
cao.sizeofThis = sizeof(struct CosmoAudioOpenOptions);
|
||||
cao.deviceType = kCosmoAudioDeviceTypePlayback;
|
||||
cao.sampleRate = g_hz;
|
||||
cao.channels = g_channels;
|
||||
cao.dataCallback = data_callback;
|
||||
cao.sampleRate = SAMPLING_RATE;
|
||||
cao.channels = CHANNELS;
|
||||
|
||||
int status;
|
||||
struct CosmoAudio *ca;
|
||||
if (cosmoaudio_open(&ca, &cao) != COSMOAUDIO_SUCCESS) {
|
||||
fprintf(stderr, "failed to open audio\n");
|
||||
status = cosmoaudio_open(&ca, &cao);
|
||||
if (status != COSMOAUDIO_SUCCESS) {
|
||||
fprintf(stderr, "failed to open audio: %d\n", status);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fgetc(stdin);
|
||||
float buf[256 * CHANNELS];
|
||||
for (int g = 0; g < SAMPLING_RATE;) {
|
||||
int frames = 1;
|
||||
status = cosmoaudio_poll(ca, NULL, &frames);
|
||||
if (status != COSMOAUDIO_SUCCESS) {
|
||||
fprintf(stderr, "failed to poll output: %d\n", status);
|
||||
return 2;
|
||||
}
|
||||
if (frames > 256)
|
||||
frames = 256;
|
||||
if (frames > SAMPLING_RATE - g)
|
||||
frames = SAMPLING_RATE - g;
|
||||
for (int f = 0; f < frames; ++f) {
|
||||
float t = (float)g++ / SAMPLING_RATE;
|
||||
float s = sinf(2 * M_PIf * WAVE_INTERVAL * t);
|
||||
for (int c = 0; c < CHANNELS; c++)
|
||||
buf[f * CHANNELS + c] = s;
|
||||
}
|
||||
status = cosmoaudio_write(ca, buf, frames);
|
||||
if (status != frames) {
|
||||
fprintf(stderr, "failed to write output: %d\n", status);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
cosmoaudio_close(ca);
|
||||
status = cosmoaudio_flush(ca);
|
||||
if (status != COSMOAUDIO_SUCCESS) {
|
||||
fprintf(stderr, "failed to flush output: %d\n", status);
|
||||
return 4;
|
||||
}
|
||||
|
||||
status = cosmoaudio_close(ca);
|
||||
if (status != COSMOAUDIO_SUCCESS) {
|
||||
fprintf(stderr, "failed to close audio: %d\n", status);
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,6 +34,8 @@ const char *cosmoaudio_describe_status(char *buf, int n, int status) {
|
|||
return "COSMOAUDIO_EINVAL";
|
||||
case COSMOAUDIO_ELINK:
|
||||
return "COSMOAUDIO_ELINK";
|
||||
case COSMOAUDIO_ENOBUF:
|
||||
return "COSMOAUDIO_ENOBUF";
|
||||
default:
|
||||
ksnprintf(buf, n, "%d", status);
|
||||
return buf;
|
||||
|
@ -81,24 +83,11 @@ const char *cosmoaudio_describe_open_options(
|
|||
gotsome = true;
|
||||
}
|
||||
|
||||
if (options->dataCallback) {
|
||||
if (options->bufferFrames) {
|
||||
if (gotsome)
|
||||
append(", ");
|
||||
append(".dataCallback=%t", options->dataCallback);
|
||||
append(".bufferFrames=%d", options->bufferFrames);
|
||||
gotsome = true;
|
||||
if (options->argument) {
|
||||
if (gotsome)
|
||||
append(", ");
|
||||
append(".argument=%p", options->argument);
|
||||
gotsome = true;
|
||||
}
|
||||
} else {
|
||||
if (options->periods) {
|
||||
if (gotsome)
|
||||
append(", ");
|
||||
append(".periods=%d", options->periods);
|
||||
gotsome = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (options->sizeofThis) {
|
||||
|
@ -111,3 +100,15 @@ const char *cosmoaudio_describe_open_options(
|
|||
append("}");
|
||||
return buf;
|
||||
}
|
||||
|
||||
const char *cosmoaudio_describe_poll_frames(char *buf, int n,
|
||||
int *in_out_frames) {
|
||||
if (!in_out_frames)
|
||||
return "NULL";
|
||||
if (kisdangerous(in_out_frames)) {
|
||||
ksnprintf(buf, n, "%p", in_out_frames);
|
||||
return buf;
|
||||
}
|
||||
ksnprintf(buf, n, "[%d]", *in_out_frames);
|
||||
return buf;
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ COSMOPOLITAN_C_START_
|
|||
const char *cosmoaudio_describe_status(char *, int, int);
|
||||
const char *cosmoaudio_describe_open_options(
|
||||
char *, int, const struct CosmoAudioOpenOptions *);
|
||||
const char *cosmoaudio_describe_poll_frames(char *, int, int *);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* COSMOPOLITAN_DSP_AUDIO_DESCRIBE_H_ */
|
||||
|
|
|
@ -53,11 +53,11 @@ struct SamplingSolution {
|
|||
static double ComputeWeight(double x) {
|
||||
if (-1.5 < x && x < 1.5) {
|
||||
if (-.5 < x && x < .5) {
|
||||
return.75 - SQR(x);
|
||||
return .75 - SQR(x);
|
||||
} else if (x < 0) {
|
||||
return.5 * SQR(x + 1.5);
|
||||
return .5 * SQR(x + 1.5);
|
||||
} else {
|
||||
return.5 * SQR(x - 1.5);
|
||||
return .5 * SQR(x - 1.5);
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
|
@ -164,12 +164,19 @@ static void GyaradosImpl(long dyw, long dxw, int dst[dyw][dxw], long syw,
|
|||
tmp0[dy][sx] = QRS(M, eax);
|
||||
}
|
||||
}
|
||||
for (dy = 0; dy < dyn; ++dy) {
|
||||
for (sx = 0; sx < sxn; ++sx) {
|
||||
tmp1[dy][sx] = sharpen ? Sharpen(tmp0[MIN(dyn - 1, MAX(0, dy - 1))][sx],
|
||||
tmp0[dy][sx],
|
||||
tmp0[MIN(dyn - 1, MAX(0, dy + 1))][sx])
|
||||
: tmp0[dy][sx];
|
||||
if (sharpen) {
|
||||
for (dy = 0; dy < dyn; ++dy) {
|
||||
for (sx = 0; sx < sxn; ++sx) {
|
||||
tmp1[dy][sx] =
|
||||
Sharpen(tmp0[MIN(dyn - 1, MAX(0, dy - 1))][sx], tmp0[dy][sx],
|
||||
tmp0[MIN(dyn - 1, MAX(0, dy + 1))][sx]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (dy = 0; dy < dyn; ++dy) {
|
||||
for (sx = 0; sx < sxn; ++sx) {
|
||||
tmp1[dy][sx] = tmp0[dy][sx];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (dx = 0; dx < dxn; ++dx) {
|
||||
|
@ -180,12 +187,19 @@ static void GyaradosImpl(long dyw, long dxw, int dst[dyw][dxw], long syw,
|
|||
tmp2[dy][dx] = QRS(M, eax);
|
||||
}
|
||||
}
|
||||
for (dx = 0; dx < dxn; ++dx) {
|
||||
for (dy = 0; dy < dyn; ++dy) {
|
||||
dst[dy][dx] = sharpen ? Sharpen(tmp2[dy][MIN(dxn - 1, MAX(0, dx - 1))],
|
||||
tmp2[dy][dx],
|
||||
tmp2[dy][MIN(dxn - 1, MAX(0, dx + 1))])
|
||||
: tmp2[dy][dx];
|
||||
if (sharpen) {
|
||||
for (dx = 0; dx < dxn; ++dx) {
|
||||
for (dy = 0; dy < dyn; ++dy) {
|
||||
dst[dy][dx] =
|
||||
Sharpen(tmp2[dy][MIN(dxn - 1, MAX(0, dx - 1))], tmp2[dy][dx],
|
||||
tmp2[dy][MIN(dxn - 1, MAX(0, dx + 1))]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (dx = 0; dx < dxn; ++dx) {
|
||||
for (dy = 0; dy < dyn; ++dy) {
|
||||
dst[dy][dx] = tmp2[dy][dx];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue