Better error handling in zip.c
[cantaveria.git] / audio.c
blobc5b4df3eb7688b82a9f08b2757c272efb2022890
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 report_error("sdl: cannot open audio (%s)\n", SDL_GetError());
63 exit(-1);
66 printf("audio:\n");
67 printf(" sample rate = %d\n", got.freq);
68 printf(" channels = %d\n", got.channels);
69 printf(" samples = %d\n", got.samples);
70 printf(" format = %d\n", got.format);
72 if(got.format != AUDIO_S16){
73 printf(" WARNING: audio format not AUDIO_S16 :(\n");
74 return;
77 lout = xmalloc(got.samples*sizeof(float));
78 rout = xmalloc(got.samples*sizeof(float));
80 printf(" sound on\n");
81 SDL_PauseAudio(0);
85 void audio_quit(){
86 SDL_CloseAudio();
87 free(lout);
88 free(rout);