Factored seq.c out of synth.c, rewrote both.
[cantaveria.git] / audio.c
blobbfec06a154829bfe3f24fb40da94bf01283858b3
1 /*
2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 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++;
48 void audio_init(){
49 SDL_AudioSpec want;
50 SDL_AudioSpec got;
53 want.freq = SAMPLE_RATE;
54 want.format = AUDIO_S16;
55 want.channels = 2;
56 want.samples = BUFFER_SIZE;
57 want.callback = audio_callback;
60 if(SDL_OpenAudio(&want, &got)<0){
61 report_error("sdl: cannot open audio (%s)\n", SDL_GetError());
62 exit(-1);
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);