Skip to content

Instantly share code, notes, and snippets.

@fredrb
Created August 9, 2023 07:04
Show Gist options
  • Save fredrb/9eec7f6249ee5a5333deff32eb6ce19c to your computer and use it in GitHub Desktop.
Save fredrb/9eec7f6249ee5a5333deff32eb6ce19c to your computer and use it in GitHub Desktop.
const int SAMPLE_RATE = 44100;
const int BUFFER_SIZE = 4096;
void callback(void *userdata, Uint8 *stream, int len) {
for (int i = 0; i < len; i++) {
stream[i] = /* ADD SAMPLE HERE */ ;
}
}
int main() {
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_EVENTS) < 0) {
printf("Failed to initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_AudioSpec spec = {
.format = AUDIO_F32,
.channels = 1,
.freq = SAMPLE_RATE,
.samples = BUFFER_SIZE,
.callback = callback,
};
if (SDL_OpenAudio(&spec, NULL) < 0) {
printf("Failed to open Audio Device: %s\n", SDL_GetError());
return 1;
}
SDL_PauseAudio(0);
while (true) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
return 0;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment