Skip to content

Instantly share code, notes, and snippets.

@AZiCoDec
Created December 18, 2010 22:05
Show Gist options
  • Save AZiCoDec/746919 to your computer and use it in GitHub Desktop.
Save AZiCoDec/746919 to your computer and use it in GitHub Desktop.
Minimal code for callback function on SDL audio
#include <SDL/SDL.h>
#include <stdio.h>
using namespace std;
void func_callback(void *unused, Uint8 *stream, int len) {
for (int i=0;i<len;i++) {
stream[i] = i;
}
}
int main(int argc, char *argv[])
{
if( SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO ) <0 ) {
SDL_WM_SetCaption( "Something busy..", NULL );
return 1;
}
SDL_AudioSpec as;
as.freq = 22000;
as.format = AUDIO_U8;
as.samples = 1024;
as.callback=func_callback;
as.userdata=NULL;
as.channels = 1;
if ( SDL_OpenAudio(&as, NULL) < 0 ) {
SDL_WM_SetCaption( "Unable to open audio..", NULL );
return 1;
}
SDL_Surface *screen = SDL_SetVideoMode(200,200, 16, SDL_SWSURFACE);
SDL_PauseAudio(0);
SDL_Delay(3000);
fclose(file);
SDL_Quit();
return 0;
}
@alexpanter
Copy link

If anyone is maintaining this page, perhaps it should be updated to use SDL2 ? also, several of the functions called seems to be outdated.. or am I missing something? :) Cheers

@fgheng
Copy link

fgheng commented Nov 23, 2020

hello, do you know how to change the parameters of the callback function?
I want to change the parameter "len" smaller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment