Merge branch 'io'
[cantaveria.git] / audio.c
blob5cfb9ec9b3ec963c0c88f93dbb87d57c85c573dd
1 /*
2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 2010 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
23 #include <stdlib.h>
25 #include <SDL/SDL.h>
27 #include <synth.h>
28 #include <audio.h>
29 #include <util.h>
31 float* lout;
32 float* rout;
34 void audio_callback(void *userdata, Uint8 *stream, int bytes){
35 int i, j;
36 Sint16* out = (Sint16*)stream;
37 int buflen = bytes / 2; /* Sint16 = 2 bytes */
38 int samples = buflen / 2; /* 2 channels */
40 synth_generate(lout, rout, samples);
42 for(i=0, j=0; i<samples; i++){
43 out[j] = (Sint16)(lout[i]*32767); j++;
44 out[j] = (Sint16)(rout[i]*32767); j++;
49 void audio_init(){
50 SDL_AudioSpec want;
51 SDL_AudioSpec got;
54 want.freq = SAMPLE_RATE;
55 want.format = AUDIO_S16;
56 want.channels = 2;
57 want.samples = BUFFER_SIZE;
58 want.callback = audio_callback;
61 if(SDL_OpenAudio(&want, &got)<0){
62 fatal_error("sdl: cannot open audio (%s)\n", SDL_GetError());
65 printf("audio:\n");
66 printf(" sample rate = %d\n", got.freq);
67 printf(" channels = %d\n", got.channels);
68 printf(" samples = %d\n", got.samples);
69 printf(" format = %d\n", got.format);
71 if(got.format != AUDIO_S16){
72 printf(" WARNING: audio format not AUDIO_S16 :(\n");
73 return;
76 lout = xmalloc(got.samples*sizeof(float));
77 rout = xmalloc(got.samples*sizeof(float));
79 printf(" sound on\n");
80 SDL_PauseAudio(0);
84 void audio_quit(){
85 SDL_CloseAudio();
86 free(lout);
87 free(rout);