3 * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
5 * Copyright (C) 2001 Chuck Mason <cemason@users.sourceforge.net>
7 * Copyright (C) 2002 Florian Schulze <crow@icculus.org>
9 * This file is part of Jump'n'Bump.
11 * Jump'n'Bump is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * Jump'n'Bump is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "SDL_mixer.h"
36 static Mix_Music
*current_music
= (Mix_Music
*) NULL
;
39 sfx_data sounds
[NUM_SFX
];
41 static int SAMPLECOUNT
= 512;
43 #define MAX_CHANNELS 32
48 /* The channel step amount... */
50 /* ... and a 0.16 bit remainder of last step. */
51 unsigned int stepremainder
;
52 unsigned int samplerate
;
53 /* The channel data pointers, start and end. */
55 signed short* startdata
;
56 signed short* enddata
;
57 /* Hardware left and right channel volume lookup. */
62 channel_info_t channelinfo
[MAX_CHANNELS
];
64 /* Sample rate in samples/second */
65 int audio_rate
= 44100;
66 int global_sfx_volume
= 0;
68 // This function loops all active (internal) sound
69 // channels, retrieves a given number of samples
70 // from the raw sound data, modifies it according
71 // to the current (internal) channel parameters,
72 // mixes the per channel samples into the given
73 // mixing buffer, and clamping it to the allowed
76 // This function currently supports only 16bit.
79 static void stopchan(int i
)
81 if (channelinfo
[i
].data
) {
82 memset(&channelinfo
[i
], 0, sizeof(channel_info_t
));
88 // This function adds a sound to the
89 // list of currently active sounds,
90 // which is maintained as a given number
91 // (eight, usually) of internal channels.
94 int addsfx(signed short *data
, int len
, int loop
, int samplerate
, int channel
)
98 /* We will handle the new SFX. */
99 /* Set pointer to raw data. */
100 channelinfo
[channel
].data
= data
;
101 channelinfo
[channel
].startdata
= data
;
103 /* Set pointer to end of raw data. */
104 channelinfo
[channel
].enddata
= channelinfo
[channel
].data
+ len
- 1;
105 channelinfo
[channel
].samplerate
= samplerate
;
107 channelinfo
[channel
].loop
= loop
;
108 channelinfo
[channel
].stepremainder
= 0;
114 static void updateSoundParams(int slot
, int volume
)
121 // MWM 2000-12-24: Calculates proportion of channel samplerate
122 // to global samplerate for mixing purposes.
123 // Patched to shift left *then* divide, to minimize roundoff errors
124 // as well as to use SAMPLERATE as defined above, not to assume 11025 Hz
126 channelinfo
[slot
].step
= ((channelinfo
[slot
].samplerate
<<16)/audio_rate
);
131 /* Sanity check, clamp volume. */
142 channelinfo
[slot
].leftvol
= leftvol
;
143 channelinfo
[slot
].rightvol
= rightvol
;
147 void mix_sound(void *unused
, Uint8
*stream
, int len
)
149 /* Mix current sound data. */
150 /* Data, from raw sound, for right and left. */
155 /* Pointers in audio stream, left, right, end. */
156 signed short* leftout
;
157 signed short* rightout
;
158 signed short* leftend
;
159 /* Step in stream, left and right, thus two. */
162 /* Mixing channel index. */
165 /* Left and right channel */
166 /* are in audio stream, alternating. */
167 leftout
= (signed short *)stream
;
168 rightout
= ((signed short *)stream
)+1;
171 /* Determine end, for left channel only */
172 /* (right channel is implicit). */
173 leftend
= leftout
+ (len
/4)*step
;
175 /* Mix sounds into the mixing buffer. */
176 /* Loop over step*SAMPLECOUNT, */
177 /* that is 512 values for two channels. */
178 while (leftout
!= leftend
) {
179 /* Reset left/right value. */
181 dr
= *rightout
* 256;
183 /* Love thy L2 chache - made this a loop. */
184 /* Now more channels could be set at compile time */
185 /* as well. Thus loop those channels. */
186 for ( chan
= 0; chan
< MAX_CHANNELS
; chan
++ ) {
187 /* Check channel, if active. */
188 if (channelinfo
[chan
].data
) {
189 /* Get the raw data from the channel. */
191 /* sample = *channelinfo[chan].data; */
192 /* linear filtering */
193 sample
= (int)(((int)channelinfo
[chan
].data
[0] * (int)(0x10000 - channelinfo
[chan
].stepremainder
))
194 + ((int)channelinfo
[chan
].data
[1] * (int)(channelinfo
[chan
].stepremainder
))) >> 16;
196 /* Add left and right part */
197 /* for this channel (sound) */
198 /* to the current data. */
199 /* Adjust volume accordingly. */
200 dl
+= sample
* (channelinfo
[chan
].leftvol
* global_sfx_volume
) / 128;
201 dr
+= sample
* (channelinfo
[chan
].rightvol
* global_sfx_volume
) / 128;
202 /* Increment index ??? */
203 channelinfo
[chan
].stepremainder
+= channelinfo
[chan
].step
;
204 /* MSB is next sample??? */
205 channelinfo
[chan
].data
+= channelinfo
[chan
].stepremainder
>> 16;
206 /* Limit to LSB??? */
207 channelinfo
[chan
].stepremainder
&= 0xffff;
209 /* Check whether we are done. */
210 if (channelinfo
[chan
].data
>= channelinfo
[chan
].enddata
) {
211 if (channelinfo
[chan
].loop
) {
212 channelinfo
[chan
].data
= channelinfo
[chan
].startdata
;
220 /* Clamp to range. Left hardware channel. */
221 /* Has been char instead of short. */
222 /* if (dl > 127) *leftout = 127; */
223 /* else if (dl < -128) *leftout = -128; */
224 /* else *leftout = dl; */
231 else if (dl
< SHRT_MIN
)
234 *leftout
= (signed short)dl
;
236 /* Same for right hardware channel. */
238 *rightout
= SHRT_MAX
;
239 else if (dr
< SHRT_MIN
)
240 *rightout
= SHRT_MIN
;
242 *rightout
= (signed short)dr
;
244 /* Increment current pointers in stream */
254 Uint16 audio_format
= MIX_DEFAULT_FORMAT
;
255 int audio_channels
= 2;
256 int audio_buffers
= 4096;
260 if (main_info
.no_sound
)
263 audio_buffers
= SAMPLECOUNT
*audio_rate
/11025;
265 memset(channelinfo
, 0, sizeof(channelinfo
));
266 memset(sounds
, 0, sizeof(sounds
));
269 if (Mix_OpenAudio(audio_rate
, audio_format
, audio_channels
, audio_buffers
) < 0) {
270 fprintf(stderr
, "Couldn't open audio: %s\n", SDL_GetError());
271 main_info
.no_sound
= 1;
275 Mix_QuerySpec(&audio_rate
, &audio_format
, &audio_channels
);
276 printf("Opened audio at %dHz %dbit %s, %d bytes audio buffer\n", audio_rate
, (audio_format
& 0xFF), (audio_channels
> 1) ? "stereo" : "mono", audio_buffers
);
278 Mix_SetMusicCMD(getenv("MUSIC_CMD"));
280 Mix_SetPostMix(mix_sound
, NULL
);
282 main_info
.no_sound
= 1;
291 if (main_info
.no_sound
)
297 Mix_FreeMusic(current_music
);
298 current_music
= NULL
;
314 char dj_autodetect_sd(void)
319 char dj_set_stereo(char flag
)
324 void dj_set_auto_mix(char flag
)
328 unsigned short dj_set_mixing_freq(unsigned short freq
)
333 void dj_set_dma_time(unsigned short time
)
337 void dj_set_nosound(char flag
)
349 char dj_set_num_sfx_channels(char num_channels
)
354 void dj_set_sfx_volume(char volume
)
356 if (main_info
.no_sound
)
360 global_sfx_volume
= volume
*2;
364 void dj_play_sfx(unsigned char sfx_num
, unsigned short freq
, char volume
, char panning
, unsigned short delay
, char channel
)
368 if (main_info
.music_no_sound
|| main_info
.no_sound
)
372 for (slot
=0; slot
<MAX_CHANNELS
; slot
++)
373 if (channelinfo
[slot
].data
==NULL
)
375 if (slot
>=MAX_CHANNELS
)
381 addsfx((short *)sounds
[sfx_num
].buf
, sounds
[sfx_num
].length
, sounds
[sfx_num
].loop
, freq
, slot
);
382 updateSoundParams(slot
, volume
*2);
386 char dj_get_sfx_settings(unsigned char sfx_num
, sfx_data
*data
)
388 if (main_info
.no_sound
)
391 memcpy(data
, &sounds
[sfx_num
], sizeof(sfx_data
));
395 char dj_set_sfx_settings(unsigned char sfx_num
, sfx_data
*data
)
397 if (main_info
.no_sound
)
400 memcpy(&sounds
[sfx_num
], data
, sizeof(sfx_data
));
404 void dj_set_sfx_channel_volume(char channel_num
, char volume
)
406 if (main_info
.no_sound
)
410 updateSoundParams(channel_num
, volume
*2);
414 void dj_stop_sfx_channel(char channel_num
)
416 if (main_info
.no_sound
)
420 stopchan(channel_num
);
424 char dj_load_sfx(unsigned char * file_handle
, char *filename
, int file_length
, char sfx_type
, unsigned char sfx_num
)
428 unsigned short *dest
;
430 if (main_info
.no_sound
)
433 sounds
[sfx_num
].buf
= malloc(file_length
);
435 memcpy(sounds
[sfx_num
].buf
, file_handle
, file_length
);
437 sounds
[sfx_num
].length
= file_length
/ 2;
438 src
= sounds
[sfx_num
].buf
;
439 dest
= (unsigned short *)sounds
[sfx_num
].buf
;
440 for (i
=0; i
<sounds
[sfx_num
].length
; i
++)
443 temp
= src
[0] + (src
[1] << 8);
451 void dj_free_sfx(unsigned char sfx_num
)
453 if (main_info
.no_sound
)
456 free(sounds
[sfx_num
].buf
);
457 memset(&sounds
[sfx_num
], 0, sizeof(sfx_data
));
462 char dj_ready_mod(char mod_num
)
466 # if ((defined _MSC_VER) || (defined __MINGW32__))
467 char filename
[] = "jnb.tmpmusic.mod";
469 char filename
[] = "/tmp/jnb.tmpmusic.mod";
474 if (main_info
.no_sound
)
479 fp
= dat_open("jump.mod");
480 len
= dat_filelen("jump.mod");
483 fp
= dat_open("bump.mod");
484 len
= dat_filelen("bump.mod");
487 fp
= dat_open("scores.mod");
488 len
= dat_filelen("scores.mod");
491 fprintf(stderr
, "bogus parameter to dj_ready_mod()\n");
497 if (Mix_PlayingMusic())
498 Mix_FadeOutMusic(1500);
501 Mix_FreeMusic(current_music
);
502 current_music
= NULL
;
509 tmp
= fopen(filename
, "wb");
511 fwrite(fp
, len
, 1, tmp
);
516 current_music
= Mix_LoadMUS(filename
);
518 if (current_music
== NULL
) {
519 fprintf(stderr
, "Couldn't load music: %s\n", SDL_GetError());
528 char dj_start_mod(void)
531 if (main_info
.no_sound
)
535 Mix_PlayMusic(current_music
, -1);
541 void dj_stop_mod(void)
544 if (main_info
.no_sound
)
551 void dj_set_mod_volume(char volume
)
554 if (main_info
.no_sound
)
557 Mix_VolumeMusic(volume
);
561 char dj_load_mod(unsigned char * file_handle
, char *filename
, char mod_num
)
566 void dj_free_mod(char mod_num
)