Use Tennix Archive Format instead in-executable data
[tennix.git] / sound.c
blob57b298819808fe5e02571f9f5751ad3c3cee922b
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"
26 #include "archive.h"
28 #include <stdarg.h>
30 static Sound* sounds;
32 static char* samples[] = {
33 "racket1.ogg",
34 "racket2.ogg",
35 "rain.ogg",
36 "ground1.ogg",
37 "ground2.ogg",
38 "audience.ogg",
39 "applause.ogg",
40 "out.ogg",
41 "background.ogg",
42 "mouseover.ogg",
43 "mouseclick.ogg",
45 #ifdef HAVE_VOICE_FILES
46 "to.ogg",
47 "all.ogg",
48 "love-in.ogg",
49 "love-out.ogg",
50 "fifteen-in.ogg",
51 "fifteen-out.ogg",
52 "thirty-in.ogg",
53 "thirty-out.ogg",
54 "forty-in.ogg",
55 "forty-out.ogg",
56 "deuce.ogg",
57 "advantage-player-one.ogg",
58 "advantage-player-two.ogg",
60 "zero-in.ogg",
61 "zero-out.ogg",
62 "one-in.ogg",
63 "one-out.ogg",
64 "two-in.ogg",
65 "two-out.ogg",
66 "three-in.ogg",
67 "three-out.ogg",
68 "four-in.ogg",
69 "four-out.ogg",
70 "five-in.ogg",
71 "five-out.ogg",
72 "six-in.ogg",
73 "six-out.ogg",
74 "seven-in.ogg",
75 "seven-out.ogg",
77 "in-the-first-set.ogg",
78 "in-the-second-set.ogg",
79 "in-the-third-set.ogg",
80 "in-the-fourth-set.ogg",
81 "in-the-fifth-set.ogg",
83 "quit-it1.ogg",
84 "quit-it2.ogg",
85 "quit-it3.ogg",
86 "quit-it4.ogg",
88 "new-game1.ogg",
89 "new-game2.ogg",
90 "new-game3.ogg",
91 "new-game4.ogg",
92 "new-game5.ogg",
93 "new-game6.ogg",
95 "lets-go1.ogg",
96 "lets-go2.ogg",
97 "lets-go3.ogg",
98 "lets-go4.ogg",
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;
111 TennixArchive* tnxar;
113 if( Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 1024) < 0) {
114 fprintf( stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
117 sounds = (Sound*)calloc(SOUND_MAX, sizeof(Sound));
119 tnxar = tnxar_open(ARCHIVE_FILE);
121 for( i=0; i<SOUND_MAX; i++) {
122 if (tnxar_set_current_filename(tnxar, samples[i]) != 0) {
123 data = Mix_LoadWAV_RW(SDL_RWFromMem(tnxar_read_current(tnxar), tnxar_size_current(tnxar)), 1);
124 } else {
125 fprintf(stderr, "Cannot find %s\n", samples[i]);
126 exit(EXIT_FAILURE);
128 if( !data) {
129 fprintf( stderr, "Error: %s\n", SDL_GetError());
130 continue;
133 sounds[i].data = data;
136 tnxar_close(tnxar);
138 /* for Voice Queue processing */
139 Mix_ChannelFinished(voice_channel_finished);
142 void play_sample_n(sound_id id, int n)
144 if (id >= SOUND_MAX) {
145 fprintf(stderr, "Cannot play sound #%d.\n", id);
146 return;
149 if (n == -2) {
150 Mix_FadeInChannel(CHANNEL_BY_ID(id), sounds[id].data, -1, FADE_IN_MS);
151 } else {
152 Mix_PlayChannel(CHANNEL_BY_ID(id), sounds[id].data, n);
155 /* Audience stops clapping when ball is served */
156 if (id >= SOUND_RACKET_FIRST && id <= SOUND_RACKET_LAST) {
157 stop_sample(SOUND_APPLAUSE);
161 void stop_sample(sound_id id)
163 Mix_FadeOutChannel(CHANNEL_BY_ID(id), FADE_OUT_MS);
166 void pan_sample(sound_id id, float position)
168 if (position == 0.5) {
169 Mix_SetPanning(CHANNEL_BY_ID(id), 255, 255);
171 else {
172 Mix_SetPanning(CHANNEL_BY_ID(id), 255*(1.0-position), 255*(position));
176 void voice_clear()
178 voice_queue_size = 0;
179 Mix_HaltChannel(CHANNEL_VOICE);
182 void voice_enqueue(sound_id id)
184 if (voice_queue_size < VOICE_QUEUE_MAX) {
185 voice_queue[voice_queue_size++] = id;
186 } else {
187 fprintf(stderr, "Voice queue overflow. Skipping: %d\n", id);
191 void voice_say()
193 if (voice_queue_size > 0) {
194 voice_queue_position = 0;
195 voice_finished_flag = 0;
196 Mix_PlayChannel(CHANNEL_VOICE, sounds[voice_queue[voice_queue_position]].data, 0);
197 voice_queue_position++;
201 void voice_say_list(int n, ...)
203 va_list ap;
204 int i;
205 sound_id id;
207 voice_clear();
209 va_start(ap, n);
210 for (i=0; i<n; i++) {
211 id = (sound_id)va_arg(ap, int);
212 voice_enqueue(id);
214 va_end(ap);
215 voice_say();
218 void voice_channel_finished(int channel)
220 if (channel == CHANNEL_VOICE) {
221 if (voice_queue_position < voice_queue_size) {
222 Mix_PlayChannel(CHANNEL_VOICE, sounds[voice_queue[voice_queue_position]].data, 0);
223 voice_queue_position++;
224 } else {
225 voice_finished_flag = 1;
226 voice_queue_position = 0;