// Emacs style mode select -*- C++ -*- //----------------------------------------------------------------------------- // // $Id: i_sound.c 196 2005-10-15 16:58:31Z fraggle $ // // Copyright(C) 1993-1996 Id Software, Inc. // Copyright(C) 2005 Simon Howard // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. // // $Log$ // Revision 1.21 2005/10/15 16:58:31 fraggle // Fix MIDI music not pausing when using SDL_mixer's native MIDI playback. // The SDL_mixer native MIDI code does not pause music properly - use // a workaround of setting the volume to 0. // // Revision 1.20 2005/10/02 20:23:04 fraggle // Guard against music lumps containing non-MUS data, document in bugs list // // Revision 1.19 2005/09/11 23:57:08 fraggle // Remove temporary MIDI files generated by sound code. // // Revision 1.18 2005/09/11 20:53:17 fraggle // Fix sounds playing at the wrong volume (too quiet) // // Revision 1.17 2005/09/07 22:24:26 fraggle // Modify the sound effect caching behaviour: sounds which are not playing // are now marked as PU_CACHE; it is otherwise possible to run out of memory. // // Revision 1.16 2005/09/06 22:39:43 fraggle // Restore -nosound, -nosfx, -nomusic // // Revision 1.15 2005/09/06 21:40:28 fraggle // Setting music volume // // Revision 1.14 2005/09/06 21:11:23 fraggle // Working music! // // Revision 1.13 2005/09/05 22:50:56 fraggle // Add mmus2mid code from prboom. Use 'void *' for music handles. Pass // length of data when registering music. // // Revision 1.12 2005/09/05 21:03:43 fraggle // 16-bit sound // // Revision 1.11 2005/09/05 20:32:18 fraggle // Use the system-nonspecific sound code to assign the channel number used // by SDL. Remove handle tagging stuff. // // Revision 1.10 2005/08/19 21:55:51 fraggle // Make sounds louder. Use the correct maximum of 15 when doing sound // calculations. // // Revision 1.9 2005/08/07 19:21:01 fraggle // Cycle round sound channels to stop reuse and conflicts of channel // numbers. Add debug to detect when incorrect sound handles are used. // // Revision 1.8 2005/08/06 17:05:51 fraggle // Remove debug messages, send error messages to stderr // Fix overflow when playing large sound files // // Revision 1.7 2005/08/05 17:53:07 fraggle // More sensible defaults // // Revision 1.6 2005/08/04 21:48:32 fraggle // Turn on compiler optimisation and warning options // Add SDL_mixer sound code // // Revision 1.5 2005/07/23 21:32:47 fraggle // Add missing errno.h, fix crash on startup when no IWAD present // // Revision 1.4 2005/07/23 19:17:11 fraggle // Use ANSI-standard limit constants. Remove LINUX define. // // Revision 1.3 2005/07/23 17:21:35 fraggle // Remove step table (unused, adds dependency on pow function) // // Revision 1.2 2005/07/23 16:44:55 fraggle // Update copyright to GNU GPL // // Revision 1.1.1.1 2005/07/23 16:20:46 fraggle // Initial import // // // DESCRIPTION: // System interface for sound. // //----------------------------------------------------------------------------- static const char rcsid[] = "$Id: i_sound.c 196 2005-10-15 16:58:31Z fraggle $"; #include #include #include #include #ifndef _WIN32 #include #endif #include "mmus2mid.h" #include "z_zone.h" #include "i_system.h" #include "i_sound.h" #include "m_argv.h" #include "m_misc.h" #include "m_swap.h" #include "w_wad.h" #include "doomdef.h" #define NUM_CHANNELS 16 static boolean sound_initialised = false; static Mix_Chunk sound_chunks[NUMSFX]; static int channels_playing[NUM_CHANNELS]; // When a sound stops, check if it is still playing. If it is not, // we can mark the sound data as CACHE to be freed back for other // means. void ReleaseSoundOnChannel(int channel) { int i; int id = channels_playing[channel]; if (!id) return; channels_playing[channel] = sfx_None; for (i=0; iabuf; int i; if (samplerate == 11025) { // need to expand to 2 channels, 11025->22050 and 8->16 bit for (i=0; i> 8) & 0xff; } } else if (samplerate == 22050) { for (i=0; i> 8) & 0xff; } } else { I_Error("Unsupported sample rate %i", samplerate); } } // Load and convert a sound effect static void CacheSFX(int sound) { int lumpnum; int samplerate; int length; int expanded_length; byte *data; // need to load the sound lumpnum = I_GetSfxLumpNum(&S_sfx[sound]); data = W_CacheLumpNum(lumpnum, PU_STATIC); samplerate = (data[3] << 8) | data[2]; length = (data[5] << 8) | data[4]; expanded_length = (length * 4) * (22050 / samplerate); sound_chunks[sound].allocated = 1; sound_chunks[sound].alen = expanded_length; sound_chunks[sound].abuf = Z_Malloc(expanded_length, PU_STATIC, &sound_chunks[sound].abuf); sound_chunks[sound].volume = MIX_MAX_VOLUME; ExpandSoundData(data + 8, samplerate, length, &sound_chunks[sound]); // don't need the original lump any more Z_ChangeTag(data, PU_CACHE); } static Mix_Chunk *getsfx(int sound) { if (sound_chunks[sound].abuf == NULL) { CacheSFX(sound); } else { // don't free the sound while it is playing! Z_ChangeTag(sound_chunks[sound].abuf, PU_STATIC); } return &sound_chunks[sound]; } // // SFX API // Note: this was called by S_Init. // However, whatever they did in the // old DPMS based DOS version, this // were simply dummies in the Linux // version. // See soundserver initdata(). // void I_SetChannels() { } void I_SetSfxVolume(int volume) { // Identical to DOS. // Basically, this should propagate // the menu/config file setting // to the state variable used in // the mixing. snd_SfxVolume = volume; } // // Retrieve the raw data lump index // for a given SFX name. // int I_GetSfxLumpNum(sfxinfo_t* sfx) { char namebuf[9]; sprintf(namebuf, "ds%s", sfx->name); return W_GetNumForName(namebuf); } // // Starting a sound means adding it // to the current list of active sounds // in the internal channels. // As the SFX info struct contains // e.g. a pointer to the raw data, // it is ignored. // As our sound handling does not handle // priority, it is ignored. // Pitching (that is, increased speed of playback) // is set, but currently not used by mixing. // int I_StartSound ( int id, int channel, int vol, int sep, int pitch, int priority ) { Mix_Chunk *chunk; if (!sound_initialised) return 0; // Release a sound effect if there is already one playing // on this channel ReleaseSoundOnChannel(channel); // Get the sound data chunk = getsfx(id); // play sound Mix_PlayChannelTimed(channel, chunk, 0, -1); channels_playing[channel] = id; // set separation, etc. I_UpdateSoundParams(channel, vol, sep, pitch); return channel; } void I_StopSound (int handle) { if (!sound_initialised) return; Mix_HaltChannel(handle); // Sound data is no longer needed; release the // sound data being used for this channel ReleaseSoundOnChannel(handle); } int I_SoundIsPlaying(int handle) { if (!sound_initialised) return false; return Mix_Playing(handle); } // // Periodically called to update the sound system // void I_UpdateSound( void ) { int i; if (!sound_initialised) return; // Check all channels to see if a sound has finished for (i=0; i