shithub: choc

Download patch

ref: d0d179940306ba2c7f20cbe45c794a1f6a282ecf
parent: beab4eb58b667a5883166bd1dd7bc33369a005c7
author: Simon Howard <[email protected]>
date: Sun Jun 17 15:19:37 EDT 2007

Make the music code modular as well, although for the time being there
is only one module. Remove s_dummy.c.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 914

--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -167,8 +167,7 @@
 
 EXTRA_DIST =                               \
         chocolate_doom_icon.c              \
-        chocolate-doom-screensaver.desktop \
-        s_dummy.c
+        chocolate-doom-screensaver.desktop
 
 .rc.o:
 	$(WINDRES) $^ -o $@
--- a/src/i_music.h
+++ /dev/null
@@ -1,76 +1,0 @@
-// Emacs style mode select   -*- C++ -*- 
-//-----------------------------------------------------------------------------
-//
-// 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.
-//
-//
-// DESCRIPTION:
-//	System interface, music.
-//
-//-----------------------------------------------------------------------------
-
-#ifndef __I_SOUND__
-#define __I_SOUND__
-
-#include "doomdef.h"
-
-#include "doomstat.h"
-#include "sounds.h"
-#include "s_sound.h"
-
-//
-//  MUSIC I/O
-//
-
-void I_InitMusic(void);
-void I_ShutdownMusic(void);
-
-// Volume.
-
-void I_SetMusicVolume(int volume);
-
-// PAUSE game handling.
-
-void I_PauseSong(void *handle);
-void I_ResumeSong(void *handle);
-
-// Registers a song handle to song data.
-
-void *I_RegisterSong(void *data, int length);
-
-// Called by anything that wishes to start music.
-//  plays a song, and when the song is done,
-//  starts playing it again in an endless loop.
-// Horrible thing to do, considering.
-
-void I_PlaySong(void *handle, int looping);
-
-// Stops a song over 3 seconds.
-
-void I_StopSong(void *handle);
-
-// See above (register), then think backwards
-
-void I_UnRegisterSong(void *handle);
-
-boolean I_QrySongPlaying(void *handle);
-
-
-#endif
-
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -52,7 +52,9 @@
 static boolean musicpaused = false;
 static int current_music_volume;
 
-void I_ShutdownMusic(void)
+// Shutdown music
+
+static void I_SDL_ShutdownMusic(void)
 {    
     if (music_initialised)
     {
@@ -76,7 +78,9 @@
     return Mix_QuerySpec(&freq, &format, &channels) != 0;
 }
 
-void I_InitMusic()
+// Initialise music subsystem
+
+static boolean I_SDL_InitMusic(void)
 { 
     // When trying to run with music enabled on OSX, display
     // a warning message.
@@ -97,7 +101,7 @@
         if (SDL_Init(SDL_INIT_AUDIO) < 0)
         {
             fprintf(stderr, "Unable to set up sound.\n");
-            return;
+            return false;
         }
 
         if (Mix_OpenAudio(snd_samplerate, AUDIO_S16SYS, 2, 1024) < 0)
@@ -104,7 +108,7 @@
         {
             fprintf(stderr, "Error initialising SDL_mixer: %s\n", Mix_GetError());
             SDL_QuitSubSystem(SDL_INIT_AUDIO);
-            return;
+            return false;
         }
 
         SDL_PauseAudio(0);
@@ -113,6 +117,8 @@
     }
 
     music_initialised = true;
+
+    return true;
 }
 
 //
@@ -136,8 +142,9 @@
     Mix_VolumeMusic(vol);
 }
 
-// MUSIC API - dummy. Some code from DOS version.
-void I_SetMusicVolume(int volume)
+// Set music volume (0 - 127)
+
+static void I_SDL_SetMusicVolume(int volume)
 {
     // Internal state variable.
     current_music_volume = volume;
@@ -145,7 +152,9 @@
     UpdateMusicVolume();
 }
 
-void I_PlaySong(void *handle, int looping)
+// Start playing a mid
+
+static void I_SDL_PlaySong(void *handle, int looping)
 {
     Mix_Music *music = (Mix_Music *) handle;
     int loops;
@@ -172,7 +181,7 @@
     Mix_PlayMusic(music, loops);
 }
 
-void I_PauseSong (void *handle)
+static void I_SDL_PauseSong(void)
 {
     if (!music_initialised)
     {
@@ -184,7 +193,7 @@
     UpdateMusicVolume();
 }
 
-void I_ResumeSong (void *handle)
+static void I_SDL_ResumeSong(void)
 {
     if (!music_initialised)
     {
@@ -196,7 +205,7 @@
     UpdateMusicVolume();
 }
 
-void I_StopSong(void *handle)
+static void I_SDL_StopSong(void)
 {
     if (!music_initialised)
     {
@@ -206,7 +215,7 @@
     Mix_HaltMusic();
 }
 
-void I_UnRegisterSong(void *handle)
+static void I_SDL_UnRegisterSong(void *handle)
 {
     Mix_Music *music = (Mix_Music *) handle;
 
@@ -256,7 +265,7 @@
     return result;
 }
 
-void *I_RegisterSong(void *data, int len)
+static void *I_SDL_RegisterSong(void *data, int len)
 {
     char *filename;
     Mix_Music *music;
@@ -303,7 +312,7 @@
 }
 
 // Is the song playing?
-boolean I_QrySongPlaying(void *handle)
+static boolean I_SDL_MusicIsPlaying(void)
 {
     if (!music_initialised)
     {
@@ -313,5 +322,32 @@
     return Mix_PlayingMusic();
 }
 
+static snddevice_t music_sdl_devices[] =
+{
+    SNDDEVICE_ADLIB,
+    SNDDEVICE_SB,
+    SNDDEVICE_PAS,
+    SNDDEVICE_GUS,
+    SNDDEVICE_WAVEBLASTER,
+    SNDDEVICE_SOUNDCANVAS,
+    SNDDEVICE_GENMIDI,
+    SNDDEVICE_AWE32,
+};
+
+music_module_t music_sdl_module =
+{
+    music_sdl_devices,
+    sizeof(music_sdl_devices) / sizeof(*music_sdl_devices),
+    I_SDL_InitMusic,
+    I_SDL_ShutdownMusic,
+    I_SDL_SetMusicVolume,
+    I_SDL_PauseSong,
+    I_SDL_ResumeSong,
+    I_SDL_RegisterSong,
+    I_SDL_UnRegisterSong,
+    I_SDL_PlaySong,
+    I_SDL_StopSong,
+    I_SDL_MusicIsPlaying,
+};
 
 
--- a/src/s_dummy.c
+++ /dev/null
@@ -1,127 +1,0 @@
-// Emacs style mode select   -*- C++ -*- 
-//-----------------------------------------------------------------------------
-//
-// 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.
-//
-// DESCRIPTION:  Dummy sound interface for running with FEATURE_SOUND
-//               disabled.
-//
-//-----------------------------------------------------------------------------
-
-#include "doomtype.h"
-#include "s_sound.h"
-#include "p_mobj.h"
-#include "sounds.h"
-
-int snd_musicdevice = SNDDEVICE_NONE;
-int snd_sfxdevice = SNDDEVICE_NONE;
-
-// Maximum volume of a sound effect.
-// Internal default is max out of 0-15.
-int sfxVolume = 8;
-
-// Maximum volume of music. 
-int musicVolume = 8;
-
-// number of channels available
-
-int                        numChannels = 8;
-
-//
-// Initializes sound stuff, including volume
-// Sets channels, SFX and music volume,
-//  allocates channel buffer, sets S_sfx lookup.
-//
-
-void S_Init(int sfxVolume, int musicVolume)
-{  
-}
-
-void S_Shutdown(void)
-{
-}
-
-//
-// Per level startup code.
-// Kills playing sounds at start of level,
-//  determines music if any, changes music.
-//
-
-void S_Start(void)
-{
-}        
-
-void S_StartSound(mobj_t *origin, int sfx_id)
-{
-}
-
-void S_StopSound(mobj_t *origin)
-{
-}
-
-//
-// Stop and resume music, during game PAUSE.
-//
-
-void S_PauseSound(void)
-{
-}
-
-void S_ResumeSound(void)
-{
-}
-
-
-//
-// Updates music & sounds
-//
-
-void S_UpdateSounds(mobj_t *listener)
-{
-}
-
-void S_SetMusicVolume(int volume)
-{
-}
-
-void S_SetSfxVolume(int volume)
-{
-}
-
-//
-// Starts some music with the music id found in sounds.h.
-//
-
-void S_StartMusic(int m_id)
-{
-}
-
-void S_ChangeMusic(int musicnum, int looping)
-{
-}
-
-boolean S_MusicPlaying(void)
-{
-    return false;
-}
-
-void S_StopMusic(void)
-{
-}
-
--- a/src/s_sound.c
+++ b/src/s_sound.c
@@ -26,7 +26,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "i_music.h"
 #include "i_system.h"
 
 #include "doomfeatures.h"
@@ -91,9 +90,10 @@
     
 } channel_t;
 
-// Low-level sound module we are using
+// Low-level sound and music modules we are using
 
 static sound_module_t *sound_module;
+static music_module_t *music_module;
 
 // The set of channels available
 
@@ -131,10 +131,11 @@
 int snd_musicdevice = DEFAULT_MUSIC_DEVICE;
 int snd_sfxdevice = SNDDEVICE_SB;
 
-// Sound effect modules
+// Sound modules
 
 extern sound_module_t sound_sdl_module;
 extern sound_module_t sound_pcsound_module;
+extern music_module_t music_sdl_module;
 
 // Compiled-in sound modules:
 
@@ -144,8 +145,19 @@
     &sound_sdl_module,
     &sound_pcsound_module,
 #endif
+    NULL,
 };
 
+// Compiled-in music modules:
+
+static music_module_t *music_modules[] =
+{
+#ifdef FEATURE_SOUND
+    &music_sdl_module,
+#endif
+    NULL,
+};
+
 // Check if a sound device is in the given list of devices
 
 static boolean SndDeviceInList(snddevice_t device, snddevice_t *list,
@@ -173,7 +185,7 @@
 
     sound_module = NULL;
 
-    for (i=0; i<sizeof(sound_modules) / sizeof(*sound_modules); ++i)
+    for (i=0; sound_modules[i] != NULL; ++i)
     {
         // Is the sfx device in the list of devices supported by
         // this module?
@@ -197,9 +209,27 @@
 
 static void InitMusicModule(void)
 {
-    if (snd_musicdevice >= SNDDEVICE_ADLIB)
+    int i;
+
+    music_module = NULL;
+
+    for (i=0; music_modules[i] != NULL; ++i)
     {
-        I_InitMusic();
+        // Is the music device in the list of devices supported
+        // by this module?
+
+        if (SndDeviceInList(snd_musicdevice, 
+                            music_modules[i]->sound_devices,
+                            music_modules[i]->num_sound_devices))
+        {
+            // Initialise the module
+
+            if (music_modules[i]->Init())
+            {
+                music_module = music_modules[i];
+                return;
+            }
+        }
     }
 }
 
@@ -259,7 +289,10 @@
         sound_module->Shutdown();
     }
 
-    I_ShutdownMusic();
+    if (music_module != NULL)
+    {
+        music_module->Shutdown();
+    }
 }
 
 static void S_StopChannel(int cnum)
@@ -616,7 +649,10 @@
 {
     if (mus_playing && !mus_paused)
     {
-        I_PauseSong(mus_playing->handle);
+        if (music_module != NULL)
+        {
+            music_module->PauseMusic();
+        }
         mus_paused = true;
     }
 }
@@ -625,7 +661,10 @@
 {
     if (mus_playing && mus_paused)
     {
-        I_ResumeSong(mus_playing->handle);
+        if (music_module != NULL)
+        {
+            music_module->ResumeMusic();
+        }
         mus_paused = false;
     }
 }
@@ -707,8 +746,10 @@
                 volume);
     }    
 
-    I_SetMusicVolume(127);
-    I_SetMusicVolume(volume);
+    if (music_module != NULL)
+    {
+        music_module->SetMusicVolume(volume);
+    }
 }
 
 void S_SetSfxVolume(int volume)
@@ -732,8 +773,9 @@
 
 void S_ChangeMusic(int musicnum, int looping)
 {
-    musicinfo_t*        music = NULL;
-    char                namebuf[9];
+    musicinfo_t *music = NULL;
+    char namebuf[9];
+    void *handle;
 
     if (musicnum <= mus_None || musicnum >= NUMMUSIC)
     {
@@ -759,19 +801,33 @@
         music->lumpnum = W_GetNumForName(namebuf);
     }
 
-    // load & register it
-    music->data = W_CacheLumpNum(music->lumpnum, PU_MUSIC);
-    music->handle = I_RegisterSong(music->data, W_LumpLength(music->lumpnum));
+    if (music_module != NULL)
+    {
+        // Load & register it
 
-    // play it
-    I_PlaySong(music->handle, looping);
+        music->data = W_CacheLumpNum(music->lumpnum, PU_MUSIC);
+        handle = music_module->RegisterSong(music->data, 
+                                            W_LumpLength(music->lumpnum));
+        music->handle = handle;
 
+        // Play it
+
+        music_module->PlaySong(handle, looping);
+    }
+
     mus_playing = music;
 }
 
 boolean S_MusicPlaying(void)
 {
-    return I_QrySongPlaying(NULL);
+    if (music_module != NULL)
+    {
+        return music_module->MusicIsPlaying();
+    }
+    else
+    {
+        return false;
+    }
 }
 
 void S_StopMusic(void)
@@ -778,17 +834,21 @@
 {
     if (mus_playing)
     {
-        if (mus_paused)
+        if (music_module != NULL)
         {
-            I_ResumeSong(mus_playing->handle);
+            if (mus_paused)
+            {
+                music_module->ResumeMusic();
+            }
+
+            music_module->StopSong();
+            music_module->UnRegisterSong(mus_playing->handle);
+            Z_ChangeTag(mus_playing->data, PU_CACHE);
+            
+            mus_playing->data = NULL;
         }
 
-        I_StopSong(mus_playing->handle);
-        I_UnRegisterSong(mus_playing->handle);
-        Z_ChangeTag(mus_playing->data, PU_CACHE);
-        
-        mus_playing->data = 0;
-        mus_playing = 0;
+        mus_playing = NULL;
     }
 }
 
--- a/src/s_sound.h
+++ b/src/s_sound.h
@@ -45,8 +45,12 @@
     SNDDEVICE_AWE32 = 9,
 } snddevice_t;
 
+// Interface for sound modules
+
 typedef struct
 {
+    // List of sound devices that this sound module is used for.
+
     snddevice_t *sound_devices;
     int num_sound_devices;
 
@@ -85,6 +89,57 @@
     boolean (*SoundIsPlaying)(int channel);
 
 } sound_module_t;
+
+// Interface for music modules
+
+typedef struct
+{
+    // List of sound devices that this music module is used for.
+
+    snddevice_t *sound_devices;
+    int num_sound_devices;
+
+    // Initialise the music subsystem
+
+    boolean (*Init)(void);
+
+    // Shutdown the music subsystem
+
+    void (*Shutdown)(void);
+
+    // Set music volume - range 0-127
+
+    void (*SetMusicVolume)(int volume);
+
+    // Pause music
+
+    void (*PauseMusic)(void);
+
+    // Un-pause music
+
+    void (*ResumeMusic)(void);
+
+    // Register a song handle from data
+    // Returns a handle that can be used to play the song
+
+    void *(*RegisterSong)(void *data, int len);
+
+    // Un-register (free) song data
+
+    void (*UnRegisterSong)(void *handle);
+
+    // Play the song
+
+    void (*PlaySong)(void *handle, int looping);
+
+    // Stop playing the current song.
+
+    void (*StopSong)(void);
+
+    // Query if music is playing.
+
+    boolean (*MusicIsPlaying)(void);
+} music_module_t;
 
 extern int snd_sfxdevice;
 extern int snd_musicdevice;