shithub: choc

Download patch

ref: 0ff16d8ff1d13bb7793aa5fbfee6b6a364397a1d
parent: fb5c593052d24a3583bd71d517972ba0f59b14ac
author: Simon Howard <fraggle@gmail.com>
date: Sun Aug 7 15:21:01 EDT 2005

Cycle round sound channels to stop reuse and conflicts of channel
numbers. Add debug to detect when incorrect sound handles are used.

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

--- a/src/i_sound.c
+++ b/src/i_sound.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: i_sound.c 43 2005-08-06 17:05:51Z fraggle $
+// $Id: i_sound.c 48 2005-08-07 19:21:01Z fraggle $
 //
 // Copyright(C) 1993-1996 Id Software, Inc.
 // Copyright(C) 2005 Simon Howard
@@ -22,6 +22,10 @@
 // 02111-1307, USA.
 //
 // $Log$
+// 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
@@ -55,7 +59,7 @@
 //-----------------------------------------------------------------------------
 
 static const char
-rcsid[] = "$Id: i_sound.c 43 2005-08-06 17:05:51Z fraggle $";
+rcsid[] = "$Id: i_sound.c 48 2005-08-07 19:21:01Z fraggle $";
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -73,11 +77,14 @@
 
 #include "doomdef.h"
 
-#define NUM_CHANNELS		8
+#define NUM_CHANNELS		16
 
 static int sound_initialised = 0;
 static Mix_Chunk sound_chunks[NUMSFX];
 
+static int sounds_in_use[256];
+static int nextchannel = 0;
+
 static byte *expand_sound_data(byte *data, int samplerate, int length)
 {
     byte *result = data;
@@ -185,6 +192,8 @@
     return W_GetNumForName(namebuf);
 }
 
+static int soundtag = 0;
+
 //
 // Starting a sound means adding it
 //  to the current list of active sounds
@@ -208,8 +217,34 @@
     Mix_Chunk *chunk = getsfx(id);
     int channel;
 
-    channel = Mix_PlayChannelTimed(-1, chunk, 0, -1);
+    // find a free channel, starting from the first after
+    // the last channel we used
+ 
+    channel = nextchannel;
 
+    do
+    {
+        channel = (channel + 1) % NUM_CHANNELS;
+
+        if (channel == nextchannel)
+        {
+            fprintf(stderr, "No free sound channels left.\n");
+            return -1;
+        }
+    } while (Mix_Playing(channel));
+
+    nextchannel = channel;
+
+    // play sound
+
+    Mix_PlayChannelTimed(channel, chunk, 0, -1);
+
+    sounds_in_use[channel] = soundtag;
+    channel |= soundtag << 8;
+    soundtag++;
+
+    // set separation, etc.
+ 
     I_UpdateSoundParams(channel, vol, sep, pitch);
 
     return channel;
@@ -219,6 +254,20 @@
 
 void I_StopSound (int handle)
 {
+    int tag = handle >> 8;
+
+    handle &= 0xff;
+    
+    if (sounds_in_use[handle] != tag)
+    {
+        fprintf(stderr,
+                "stopping wrong sound: %i != %i (%i)\n",
+                sounds_in_use[handle], tag,
+                handle);
+    }
+
+    sounds_in_use[handle] = -1;
+
     Mix_HaltChannel(handle);
 }
 
@@ -225,6 +274,7 @@
 
 int I_SoundIsPlaying(int handle)
 {
+    handle &= 0xff;
     return Mix_Playing(handle);
 }
 
@@ -271,6 +321,16 @@
   int	sep,
   int	pitch)
 {
+    int tag = handle >> 8;
+
+    handle &= 0xff;
+    
+    if (sounds_in_use[handle] != tag)
+    {
+        fprintf(stderr,
+                "tag is wrong for playing sound: %i, %i\n", tag, handle);
+    }
+    
     Mix_SetPanning(handle, 
                    ((254 - sep) * vol) / 8, 
                    ((sep) * vol) / 8);
@@ -307,7 +367,7 @@
         fprintf(stderr, "Error initialising SDL_mixer: %s\n", SDL_GetError());
     }
 
-    Mix_AllocateChannels(16);
+    Mix_AllocateChannels(NUM_CHANNELS);
     
     sound_initialised = 1;