Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
Created April 17, 2025 17:05
Show Gist options
  • Save julianrubisch/2495a6d7e84065a775dbc04449f206ae to your computer and use it in GitHub Desktop.
Save julianrubisch/2495a6d7e84065a775dbc04449f206ae to your computer and use it in GitHub Desktop.
DragonRuby Libpd
static void audio_callback(void *userdata, Uint8 *stream, int len) {
float *inbuf = (float *)stream; // Point inbuf to the stream
int num_samples = len / sizeof(float); // Total number of float samples
int ticks = num_samples / 64 * 2; // Each tick is 64 samples per channel (128 samples total)
float outbuf[num_samples]; // Allocate outbuf on the stack with the same size as inbuf
// Process the samples
libpd_process_float(ticks, inbuf, outbuf);
// Copy output samples back to the stream
memcpy(stream, outbuf, len);
}
static void start_processing()
{
drb_api->drb_log_write("Game", 2, "* INFO - Starting processing");
SDL_AudioSpec want, have;
drb_api->SDL_memset(&want, 0, sizeof(want));
// TODO populate from ivars
want.freq = 48000; // Sample rate
want.format = AUDIO_F32SYS; // 32-bit floating-point audio
want.channels = 2; // Stereo output
want.samples = 256; // Buffer size (64 samples per channel, 2 channels)
want.callback = audio_callback;
want.userdata = NULL;
if (drb_api->SDL_OpenAudio(&want, &have) < 0) {
printf("Could not open audio: %s\n", "");
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment