Less bad ORG_DEFAULT loudness adjustment.
[cantaveria.git] / audio.c
blobd95c7c9c9674ce9f1c6209fd4974325c07f5d406
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 <org.h>
28 #include <seq.h>
29 #include <synth.h>
30 #include <util.h>
31 #include <audio.h>
33 float* lout;
34 float* rout;
36 void audio_callback(void *userdata, Uint8 *stream, int bytes){
37 int i, j;
38 Sint16* out = (Sint16*)stream;
39 int buflen = bytes / 2; /* Sint16 = 2 bytes */
40 int samples = buflen / 2; /* 2 channels */
42 synth_generate(lout, rout, samples);
44 for(i=0, j=0; i<samples; i++){
45 out[j] = (Sint16)(lout[i]*32767); j++;
46 out[j] = (Sint16)(rout[i]*32767); j++;
52 char* sample_format_str(int format){
53 switch(format){
54 case AUDIO_S16: return "signed 16-bit LE";
55 case AUDIO_U16: return "unsigned 16-bit LE";
56 case AUDIO_S16MSB: return "signed 16-bit BE";
57 case AUDIO_U16MSB: return "unsigned 16-bit BE";
58 case AUDIO_S8: return "signed 8-bit";
59 case AUDIO_U8: return "unsigned 8-bit";
60 default: return "unknown";
64 void audio_init(){
65 SDL_AudioSpec want;
66 SDL_AudioSpec got;
69 want.freq = SAMPLE_RATE;
70 want.format = AUDIO_S16;
71 want.channels = 2;
72 want.samples = BUFFER_SIZE;
73 want.callback = audio_callback;
76 if(SDL_OpenAudio(&want, &got)<0){
77 report_error("sdl: cannot open audio (%s)\n", SDL_GetError());
78 exit(-1);
81 printf("audio:\n");
82 printf(" spec:\n");
83 printf(" sample rate = %d\n", got.freq);
84 printf(" channels = %d\n", got.channels);
85 printf(" samples = %d\n", got.samples);
86 printf(" format = %s\n", sample_format_str(got.format));
88 if(got.format != AUDIO_S16){
89 printf(" WARNING: audio format not AUDIO_S16 :(\n");
90 SDL_CloseAudio();
91 printf(" *no sound*\n");
92 return;
94 lout = xmalloc(got.samples*sizeof(float));
95 rout = xmalloc(got.samples*sizeof(float));
96 memset(lout, 0, got.samples*sizeof(float));
97 memset(rout, 0, got.samples*sizeof(float));
99 org_init();
100 synth_init();
101 seq_init();
103 printf(" sound on\n");
104 SDL_PauseAudio(0);
109 void audio_quit(){
110 SDL_CloseAudio();
111 free(lout);
112 free(rout);
117 void audio_lock(){
118 SDL_LockAudio();
121 void audio_unlock(){
122 SDL_UnlockAudio();