Tennix 0.6.0 and documentation updates
[tennix.git] / sound.c
blobe550384a80f28dba2f260b5dbb353d21511d5da3
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008 Thomas Perl <thp@perli.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 **/
24 #include "tennix.h"
25 #include "sound.h"
27 static Sound* sounds;
29 #include "data/sounds_data.c"
30 static const ResourceData samples[] = {
31 RESOURCE(racket1),
32 RESOURCE(racket2),
33 RESOURCE(ground1),
34 RESOURCE(ground2),
35 RESOURCE(audience),
36 RESOURCE(applause),
37 RESOURCE(out),
38 RESOURCE(background)
41 void init_sound() {
42 int i;
43 Mix_Chunk* data;
45 if( Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 1024) < 0) {
46 fprintf( stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
49 sounds = (Sound*)calloc(SOUND_MAX, sizeof(Sound));
51 for( i=0; i<SOUND_MAX; i++) {
52 data = Mix_LoadWAV_RW(SDL_RWFromConstMem(samples[i].data, samples[i].size), 1);
53 if( !data) {
54 fprintf( stderr, "Error: %s\n", SDL_GetError());
55 continue;
58 sounds[i].data = data;
63 void play_sample_n(sound_id id, int n)
65 if (id >= SOUND_MAX) {
66 fprintf(stderr, "Cannot play sound #%d.\n", id);
67 return;
70 if (n == -2) {
71 Mix_FadeInChannel(id, sounds[id].data, -1, FADE_IN_MS);
72 } else {
73 Mix_PlayChannel(id, sounds[id].data, n);
76 if (id == SOUND_RACKET) {
77 stop_sample(SOUND_APPLAUSE);
81 void stop_sample(sound_id id)
83 Mix_FadeOutChannel(id, FADE_OUT_MS);
86 void pan_sample(sound_id id, float position)
88 if (position == 0.5) {
89 Mix_SetPanning(id, 255, 255);
91 else {
92 Mix_SetPanning(id, 255*(1.0-position), 255*(position));