5 * Copyright (C) 2003, 2007, 2008, 2009 Thomas Perl <thp@thpinfo.com>
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,
35 static const char* samples
[] = {
48 #ifdef HAVE_VOICE_FILES
60 "advantage-player-one.ogg",
61 "advantage-player-two.ogg",
80 "in-the-first-set.ogg",
81 "in-the-second-set.ogg",
82 "in-the-third-set.ogg",
83 "in-the-fourth-set.ogg",
84 "in-the-fifth-set.ogg",
105 static sound_id voice_queue
[VOICE_QUEUE_MAX
];
106 static int voice_queue_size
= 0;
107 static int voice_queue_position
= 0;
109 int voice_finished_flag
= 1;
110 static int available_channels
= 0;
116 TennixArchive
* tnxar
;
118 if( Mix_OpenAudio( 44100, AUDIO_S16SYS
, 2, 1024) < 0) {
119 fprintf( stderr
, "Error initializing SDL_mixer: %s\n", Mix_GetError());
122 sounds
= (Sound
*)calloc(SOUND_MAX
, sizeof(Sound
));
124 draw_button(40, (HEIGHT
-40)/2, WIDTH
-80, 40, 100, 100, 100, 1);
128 /* +1 because of zero-based indexing AND +1 for voice channel */
129 available_channels
= Mix_AllocateChannels(LAST_SOUNDEFFECT_ID
+1+1);
131 if (available_channels
< LAST_SOUNDEFFECT_ID
+1+1) {
132 fprintf(stderr
, "Warning: Got only %d audio channels (requested %d).\n",
134 LAST_SOUNDEFFECT_ID
+1+1);
137 tnxar
= tnxar_open(ARCHIVE_FILE
);
139 /* not found in cwd - try installed... */
140 tnxar
= tnxar_open(ARCHIVE_FILE_INSTALLED
);
141 assert(tnxar
!= NULL
);
144 for( i
=0; i
<SOUND_MAX
; i
++) {
145 if (tnxar_set_current_filename(tnxar
, samples
[i
]) != 0) {
146 d
= tnxar_read_current(tnxar
);
147 data
= Mix_LoadWAV_RW(SDL_RWFromMem(d
, tnxar_size_current(tnxar
)), 0);
150 fprintf(stderr
, "Cannot find %s\n", samples
[i
]);
154 fprintf( stderr
, "Error: %s\n", SDL_GetError());
158 sounds
[i
].data
= data
;
160 draw_button(40, (HEIGHT
-40)/2, (WIDTH
-80)*(GR_COUNT
+i
)/(SOUND_MAX
+GR_COUNT
), 40, 100, 250, 100, 0);
161 rectangle(40+BUTTON_BORDER
, (HEIGHT
-40)/2+20, (WIDTH
-80)*(GR_COUNT
+i
)/(SOUND_MAX
+GR_COUNT
)-2*BUTTON_BORDER
, 10, 170, 250, 170);
171 /* for Voice Queue processing */
172 Mix_ChannelFinished(voice_channel_finished
);
175 void play_sample_n(sound_id id
, int n
)
177 if (id
>= SOUND_MAX
) {
178 fprintf(stderr
, "Cannot play sound #%d.\n", id
);
183 Mix_FadeInChannel(CHANNEL_BY_ID(id
, available_channels
), sounds
[id
].data
, -1, FADE_IN_MS
);
185 Mix_PlayChannel(CHANNEL_BY_ID(id
, available_channels
), sounds
[id
].data
, n
);
188 /* Audience stops clapping when ball is served */
189 if (id
>= SOUND_RACKET_FIRST
&& id
<= SOUND_RACKET_LAST
) {
190 stop_sample(SOUND_APPLAUSE
);
194 void stop_sample(sound_id id
)
196 Mix_FadeOutChannel(CHANNEL_BY_ID(id
, available_channels
), FADE_OUT_MS
);
199 void sample_volume(sound_id id
, float volume
)
201 Mix_Volume(CHANNEL_BY_ID(id
, available_channels
), volume
*MIX_MAX_VOLUME
);
204 void sample_volume_group(sound_id first
, sound_id last
, float volume
)
208 for (i
=first
; i
<=last
; i
++) {
209 sample_volume(i
, volume
);
212 void pan_sample(sound_id id
, float position
)
214 if (position
== 0.5) {
215 Mix_SetPanning(CHANNEL_BY_ID(id
, available_channels
), 255, 255);
218 Mix_SetPanning(CHANNEL_BY_ID(id
, available_channels
), 255*(1.0-position
), 255*(position
));
222 void pan_sample_group(sound_id first
, sound_id last
, float position
)
226 for (i
=first
; i
<=last
; i
++) {
227 pan_sample(i
, position
);
232 voice_queue_size
= 0;
233 Mix_HaltChannel(CHANNEL_VOICE
);
236 void voice_enqueue(sound_id id
)
238 if (voice_queue_size
< VOICE_QUEUE_MAX
) {
239 voice_queue
[voice_queue_size
++] = id
;
241 fprintf(stderr
, "Voice queue overflow. Skipping: %d\n", id
);
247 if (voice_queue_size
> 0) {
248 voice_queue_position
= 0;
249 voice_finished_flag
= 0;
250 Mix_PlayChannel(CHANNEL_VOICE
, sounds
[voice_queue
[voice_queue_position
]].data
, 0);
251 voice_queue_position
++;
255 void voice_say_list(int n
, ...)
264 for (i
=0; i
<n
; i
++) {
265 id
= (sound_id
)va_arg(ap
, int);
272 void voice_channel_finished(int channel
)
274 if (channel
== CHANNEL_VOICE
) {
275 if (voice_queue_position
< voice_queue_size
) {
276 Mix_PlayChannel(CHANNEL_VOICE
, sounds
[voice_queue
[voice_queue_position
]].data
, 0);
277 voice_queue_position
++;
279 voice_finished_flag
= 1;
280 voice_queue_position
= 0;