Voice support, experimental wind, extended referee
[tennix.git] / sound.c
blobf93bf7c6f9c0ce9a1dfd3a38d1c3d07db89c48c2
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 #include <stdarg.h>
29 static Sound* sounds;
31 #include "data/sounds_data.c"
32 static const ResourceData samples[] = {
33 RESOURCE(racket1),
34 RESOURCE(racket2),
35 RESOURCE(rain),
36 RESOURCE(ground1),
37 RESOURCE(ground2),
38 RESOURCE(audience),
39 RESOURCE(applause),
40 RESOURCE(out),
41 RESOURCE(background),
42 RESOURCE(mouseover),
43 RESOURCE(mouseclick),
45 #ifdef HAVE_VOICE_FILES
46 RESOURCE(to),
47 RESOURCE(all),
48 RESOURCE(love_in),
49 RESOURCE(love_out),
50 RESOURCE(fifteen_in),
51 RESOURCE(fifteen_out),
52 RESOURCE(thirty_in),
53 RESOURCE(thirty_out),
54 RESOURCE(forty_in),
55 RESOURCE(forty_out),
56 RESOURCE(deuce),
57 RESOURCE(advantage_player_one),
58 RESOURCE(advantage_player_two),
60 RESOURCE(zero_in),
61 RESOURCE(zero_out),
62 RESOURCE(one_in),
63 RESOURCE(one_out),
64 RESOURCE(two_in),
65 RESOURCE(two_out),
66 RESOURCE(three_in),
67 RESOURCE(three_out),
68 RESOURCE(four_in),
69 RESOURCE(four_out),
70 RESOURCE(five_in),
71 RESOURCE(five_out),
72 RESOURCE(six_in),
73 RESOURCE(six_out),
74 RESOURCE(seven_in),
75 RESOURCE(seven_out),
77 RESOURCE(in_the_first_set),
78 RESOURCE(in_the_second_set),
79 RESOURCE(in_the_third_set),
80 RESOURCE(in_the_fourth_set),
81 RESOURCE(in_the_fifth_set),
83 RESOURCE(quit_it1),
84 RESOURCE(quit_it2),
85 RESOURCE(quit_it3),
86 RESOURCE(quit_it4),
88 RESOURCE(new_game1),
89 RESOURCE(new_game2),
90 RESOURCE(new_game3),
91 RESOURCE(new_game4),
92 RESOURCE(new_game5),
93 RESOURCE(new_game6),
95 RESOURCE(lets_go1),
96 RESOURCE(lets_go2),
97 RESOURCE(lets_go3),
98 RESOURCE(lets_go4),
99 #endif
102 static sound_id voice_queue[VOICE_QUEUE_MAX];
103 static int voice_queue_size = 0;
104 static int voice_queue_position = 0;
106 int voice_finished_flag = 1;
108 void init_sound() {
109 int i;
110 Mix_Chunk* data;
112 if( Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 1024) < 0) {
113 fprintf( stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
116 sounds = (Sound*)calloc(SOUND_MAX, sizeof(Sound));
118 for( i=0; i<SOUND_MAX; i++) {
119 data = Mix_LoadWAV_RW(SDL_RWFromConstMem(samples[i].data, samples[i].size), 1);
120 if( !data) {
121 fprintf( stderr, "Error: %s\n", SDL_GetError());
122 continue;
125 sounds[i].data = data;
128 /* for Voice Queue processing */
129 Mix_ChannelFinished(voice_channel_finished);
132 void play_sample_n(sound_id id, int n)
134 if (id >= SOUND_MAX) {
135 fprintf(stderr, "Cannot play sound #%d.\n", id);
136 return;
139 if (n == -2) {
140 Mix_FadeInChannel(CHANNEL_BY_ID(id), sounds[id].data, -1, FADE_IN_MS);
141 } else {
142 Mix_PlayChannel(CHANNEL_BY_ID(id), sounds[id].data, n);
145 /* Audience stops clapping when ball is served */
146 if (id >= SOUND_RACKET_FIRST && id <= SOUND_RACKET_LAST) {
147 stop_sample(SOUND_APPLAUSE);
151 void stop_sample(sound_id id)
153 Mix_FadeOutChannel(CHANNEL_BY_ID(id), FADE_OUT_MS);
156 void pan_sample(sound_id id, float position)
158 if (position == 0.5) {
159 Mix_SetPanning(CHANNEL_BY_ID(id), 255, 255);
161 else {
162 Mix_SetPanning(CHANNEL_BY_ID(id), 255*(1.0-position), 255*(position));
166 void voice_clear()
168 voice_queue_size = 0;
169 Mix_HaltChannel(CHANNEL_VOICE);
172 void voice_enqueue(sound_id id)
174 if (voice_queue_size < VOICE_QUEUE_MAX) {
175 voice_queue[voice_queue_size++] = id;
176 } else {
177 fprintf(stderr, "Voice queue overflow. Skipping: %d\n", id);
181 void voice_say()
183 if (voice_queue_size > 0) {
184 voice_queue_position = 0;
185 voice_finished_flag = 0;
186 Mix_PlayChannel(CHANNEL_VOICE, sounds[voice_queue[voice_queue_position]].data, 0);
187 voice_queue_position++;
191 void voice_say_list(int n, ...)
193 va_list ap;
194 int i;
195 sound_id id;
197 voice_clear();
199 va_start(ap, n);
200 for (i=0; i<n; i++) {
201 id = (sound_id)va_arg(ap, int);
202 voice_enqueue(id);
204 va_end(ap);
205 voice_say();
208 void voice_channel_finished(int channel)
210 if (channel == CHANNEL_VOICE) {
211 if (voice_queue_position < voice_queue_size) {
212 Mix_PlayChannel(CHANNEL_VOICE, sounds[voice_queue[voice_queue_position]].data, 0);
213 voice_queue_position++;
214 } else {
215 voice_finished_flag = 1;
216 voice_queue_position = 0;