Sure, here is a simple test program that does what you ask. I think we
all have to write something like this to get started with sound in SDL.
Bob PendletonOn Sun, 2003-11-09 at 14:13, William Clayton wrote:
Hello,
I’m trying to create a simple SDL-based program that will play a basic
sinusoisal signal that is generated in the code as a launching point
for my software project. Could somebody provide an example of a
callback function and how it is utilized for me to look at? I’m not
really sure what should go in it and how it should be called…
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include “SDL.h”
void initOrQuit(Uint32 flags)
{
if (-1 == SDL_Init(flags))
{
printf(“Failed to initialize SDL error=%s\n”, SDL_GetError());
exit(1);
}
else
{
printf(“Initialized SDL\n”);
}
}
typedef struct
{
Uint32 value;
char *name;
} valueName;
valueName audioStatus[] =
{
{SDL_AUDIO_STOPPED, “SDL_AUDIO_STOPPED”},
{SDL_AUDIO_PLAYING, “SDL_AUDIO_PLAYING”},
{SDL_AUDIO_PAUSED, “SDL_AUDIO_PAUSED”},
};
const int numAudioStatus = sizeof(audioStatus) / sizeof(valueName);
valueName audioFormats[] =
{
{AUDIO_U8, " AUDIO_U8"},
{AUDIO_S8, " AUDIO_S8"},
{AUDIO_U16LSB, " AUDIO_U16LSB"},
{AUDIO_S16LSB, " AUDIO_S16LSB"},
{AUDIO_U16MSB, " AUDIO_U16MSB"},
{AUDIO_S16MSB, " AUDIO_S16MSB"},
{AUDIO_U16, " AUDIO_U16"},
{AUDIO_S16, " AUDIO_S16"},
{AUDIO_U16SYS, " AUDIO_U16SYS"},
{AUDIO_S16SYS, " AUDIO_S16SYS"},
};
const int numAudioFormats = sizeof(audioFormats) / sizeof(valueName );
void printSDLAudioSpec(SDL_AudioSpec *a)
{
int i;
printf(“frequency =%d\n”, (Sint32)a->freq);
printf(“format =%04x – “, (Uint32)a->format);
for (i = 0; i < numAudioFormats; i++)
{
if (a->format == audioFormats[i].value)
{
printf(”%s”, audioFormats[i].name);
}
}
printf("\n");
printf(“channels =%d\n”, (Uint32)a->channels);
printf(“silence =%d\n”, (Uint32)a->silence);
printf(“samples =%d\n”, (Uint32)a->samples);
printf(“padding =%d\n”, (Uint32)a->padding);
printf(“size =%d\n”, (Uint32)a->size);
printf(“callback =%08x\n”, (Uint32)a->callback);
printf(“userdata =%08x\n”, (Uint32)a->userdata);
}
typedef struct
{
SDL_AudioSpec *as;
} soundUserData;
void soundCallBack(void *userdata, Uint8 *stream, int len)
{
int i = 0;
Sint16 *p = (Sint16 *)stream;
soundUserData *s = (soundUserData *)userdata;
int samples = s->as->samples;
int freq = s->as->freq;
static int wave = 1;
static int count = -1;
static int dir = 1;
for (i = 0; i < samples; i++)
{
//printf("%4d %5d %5d\n", i, count, wave);
if (count > wave)
{
wave += dir;
if (wave >= (freq / 300))
{
dir = -1;
}
if (wave <= 1)
{
dir = 1;
}
count = -wave;
}
if (0 > count)
{
*p++ = 32767;
*p++ = 32767;
}
else
{
*p++ = -32767;
*p++ = -32767;
}
count += 2;
}
}
void testSound()
{
SDL_AudioSpec desired;
SDL_AudioSpec obtained;
soundUserData info;
initOrQuit(SDL_INIT_AUDIO);
atexit(SDL_Quit);
desired.freq = 44100;
desired.format = AUDIO_S16SYS;
desired.channels = 2;
desired.silence = 0;
desired.samples = 1024;
desired.padding = 0;
desired.size = 0;
desired.callback = &soundCallBack;
desired.userdata = &info;
printSDLAudioSpec(&desired);
if (SDL_OpenAudio(&desired, &obtained) < 0)
{
fprintf(stderr, “Couldn’t open audio: %s\n”, SDL_GetError());
exit(-1);
}
info.as = &obtained;
printSDLAudioSpec(&desired);
printSDLAudioSpec(&obtained);
SDL_PauseAudio(0);
SDL_Delay(2 * 1000);
SDL_PauseAudio(1);
}
int main(int argc, char **argv)
{
testSound();
}
Thanks,
Will
–
±--------------------------------------+