Game Development Community

Sound Engine in TGB

by Bruno · in Torque Game Builder · 09/07/2008 (3:52 am) · 4 replies

Hi,

I'm tryng to get sounds to work in my game as it should, but i'm getting some weird results.
Some sounds do not play at all, some others play fine, and after a new sound is played, the sound that before was playing ok, now plays randomly.
All sounds are mono.
All my sounds in the audiodatablocks have preload = false.

I play the sounds like this :

function PlaySound(%this_sound)
{
  %sound_found = 0;
  
  for(%i = 0; %i < 16; %i ++)
  {
        %ret_sound = alxIsPlaying($Available_sound[%i]);   	
   	if(%ret_sound == 0)
        {
	     alxStop($Available_sound[%i]);
 	     if(%sound_found == 0)
	     {
	       $Available_sound[%i] = alxPlay(%this_sound);
	       %sound_found = 1;
	     }
	  
	}   
  }
}

In the above routine, when a sound is not playing anymore, i can track it down to the routine "alxplay", but
no sound is coming out.
Is this a common problem in Torque ?
If so, how are people getting around this ?

thanks,
Bruno

#1
09/07/2008 (1:03 pm)
Shouldn't it be

if(%sound_found == 1)

the it will run the alxPlay statment...

actually change your %ret_sound to %sound_found...

Here you go:

function PlaySound(%this_sound)
{  
     %sound_playing = 0;    // used for checking if sound is playing 0 is false 1 is true
         
     %sound_playing = alxIsPlaying(%this_sound);   	   	
          
     if(%sound_playing == 1) //sound is playing so stop        
     {	     
          alxStop(%this_sound); 	
     } else
     if(%sound_playing == 0)// sound is not playing so start	     
     {	      
          $playSound = alxPlay(%this_sound);	      
          %sound_found = 1;	     
      }	  	
    
}

This code asumes you know which sound you're looking for.
#2
09/07/2008 (2:21 pm)
Hi Steven

Your code won't work if you need the same sound played twice. Imagine you shoot a gun twice., the second shot sound will cancel the first one.
What i did , is create an array of sound handles, that gets a handle when i play a sound.

the array $Available_sound, will be initialized to zero in the begining of the game, so all handles are available to play.
When i play a sound, i assign the audio handle to a position of this array. If i need to play another sound, he moves one position in the array. (that's why
if(%sound_found == 0)

This way, i never delete audio handles, and i can place the same sound several times.
The problem is.., "it's not working lol", and i don't understand why
#3
09/07/2008 (10:17 pm)
Here's what I do to prevent two of the same sound from playing over each other.

if alxIsPlaying($explosion1)
   alxStop($explosion1);

$explosion1 = alxPlay(explosion1SoundEffect);

where 'explosion1SoundEffect' is defined in your audioDataBlocks.cs
#4
09/10/2008 (3:59 pm)
Here my method for playing a sound, that stops two identical sounds from playing *only* if they are both triggered within a very short amount of time within each other, which results in essential an amplification of the sound, rather than multiple distinct sounds being heard.

Not sure if this answers the original poster's question, but it's what I use, for what it's worth.

// Written by Jason McIntosh (posted to GarageGames forum in response to a question of mine)
	// http://www.garagegames.com/mg/forums/result.thread.php?qt=55989
	// The function ignores the request to play a given sound if that same exact sound
	// has already been played within the last 60 milliseconds. (NOTE: It seems the intervals are in 20 milliseconds each.)
	//
	// Returns the handle of the sound if it's played, otherwise returns nothing (null???)
function playSound( %datablock )
{
	// Get current millisecond running count since the game started (use GetRealTime for since system startup)
	%now = GetSimTime();
	
		// Remember that objects are just integers, so we can use them as indexes to an array
		// Also remember that an uninitialized array accessed in a mathematical expression returns 0.
	%diff = %now - $gSoundInfo[%datablock];
	
	if ( %diff > 60 ) // don't play sounds closer than 0.06 seconds apart
	{
			// Store the time that we're playing this sound
		$gSoundInfo[%datablock] = %now;
		
		%handle = alxPlay( %datablock );
		
			// Ensure the sound is played back at the correct volume, as specified in the datablock
	//	if ( %datablock.volume !$= "" )
	//		alxSourcef( %handle, "AL_GAIN", %datablock.volume );
	}
	
	return %handle;
}