2017-01-03 18:27:35 +00:00
|
|
|
// g++ -Wall `sdl-config --cflags --libs` testsdl.cpp -o testsdl
|
|
|
|
// emcc -s USE_SDL=2 -Wall testsdl.cpp -o testsdl.html
|
2017-01-03 18:08:59 +00:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cmath>
|
|
|
|
#include <ctime>
|
2017-01-03 18:27:35 +00:00
|
|
|
#include <cstdlib>
|
2017-01-03 18:08:59 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
|
2017-01-03 18:27:35 +00:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
#include <emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2017-01-03 18:08:59 +00:00
|
|
|
#define BUF_SIZE 4096
|
|
|
|
|
2017-01-05 07:19:27 +00:00
|
|
|
unsigned long long audio_pos = 0;
|
|
|
|
unsigned short buf[2048];
|
2017-01-03 18:08:59 +00:00
|
|
|
|
2017-01-05 07:19:27 +00:00
|
|
|
float freq1 = 440;
|
|
|
|
float freq2 = 410;
|
2017-01-03 18:08:59 +00:00
|
|
|
const int rate = 44100;
|
|
|
|
|
|
|
|
/* The audio function callback takes the following parameters:
|
|
|
|
stream: A pointer to the audio buffer to be filled
|
|
|
|
len: The length (in bytes) of the audio buffer
|
|
|
|
*/
|
2017-01-05 07:19:27 +00:00
|
|
|
void fill_audio(void *userdata, Uint8 *stream, int len)
|
2017-01-03 18:08:59 +00:00
|
|
|
{
|
2017-01-05 07:19:27 +00:00
|
|
|
printf("fill audio %d\n", len);
|
|
|
|
for (int i = 0; i < len / 4; i++) {
|
|
|
|
buf[i * 2 + 0] = (int)(0.75 * 32768.0 *
|
|
|
|
sin(2 * M_PI * freq1 * ((float) (audio_pos + i)/rate)));
|
|
|
|
buf[i * 2 + 1] = (int)(0.75 * 32768.0 *
|
|
|
|
sin(2 * M_PI * freq2 * ((float) (audio_pos + i)/rate)));
|
2017-01-03 18:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mix as much data as possible */
|
2017-01-05 07:19:27 +00:00
|
|
|
SDL_MixAudio(stream, (unsigned char *)buf, len, SDL_MIX_MAXVOLUME);
|
|
|
|
audio_pos += len / 4;
|
2017-01-03 18:08:59 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 18:27:35 +00:00
|
|
|
time_t now;
|
|
|
|
bool done = false;
|
|
|
|
|
|
|
|
void mainloop(void)
|
|
|
|
{
|
|
|
|
if ( time(0) - now >= 10 )
|
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
#else
|
|
|
|
done = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-01-03 18:08:59 +00:00
|
|
|
extern "C" int main(int argc, char **argv)
|
|
|
|
{
|
2017-01-03 18:27:35 +00:00
|
|
|
printf("main()\n");
|
2017-01-03 18:08:59 +00:00
|
|
|
SDL_AudioSpec wanted;
|
|
|
|
|
|
|
|
/* Set the audio format */
|
|
|
|
wanted.freq = rate;
|
2017-01-05 07:19:27 +00:00
|
|
|
wanted.format = AUDIO_S16;
|
|
|
|
wanted.channels = 2; /* 1 = mono, 2 = stereo */
|
2017-01-03 18:08:59 +00:00
|
|
|
wanted.samples = 1024; /* Good low-latency value for callback */
|
|
|
|
wanted.callback = fill_audio;
|
|
|
|
wanted.userdata = NULL;
|
|
|
|
|
|
|
|
/* Open the audio device, forcing the desired format */
|
|
|
|
if ( SDL_OpenAudio(&wanted, NULL) < 0 ) {
|
|
|
|
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-01-03 18:27:35 +00:00
|
|
|
printf("opened audio\n");
|
2017-01-03 18:08:59 +00:00
|
|
|
|
|
|
|
/* Let the callback function play the audio chunk */
|
|
|
|
SDL_PauseAudio(0);
|
2017-01-03 18:27:35 +00:00
|
|
|
printf("unpaused audio, playing...\n");
|
2017-01-03 18:08:59 +00:00
|
|
|
|
2017-01-03 18:27:35 +00:00
|
|
|
now = time(0);
|
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
emscripten_set_main_loop(mainloop, 10, true);
|
|
|
|
#else
|
|
|
|
while(!done) {
|
|
|
|
mainloop();
|
|
|
|
SDL_Delay(100);
|
2017-01-03 18:08:59 +00:00
|
|
|
}
|
2017-01-03 18:27:35 +00:00
|
|
|
#endif
|
|
|
|
|
2017-01-03 18:08:59 +00:00
|
|
|
SDL_CloseAudio();
|
2017-01-03 18:27:35 +00:00
|
|
|
printf("closing...\n");
|
2017-01-03 18:08:59 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|