Game Development Community

TGB audio not working [Resolved]

by Siobhan · in Torque Game Builder · 02/17/2011 (2:37 pm) · 9 replies

I was hoping someone would be able to help me with this. I've been trying for several hours now to work it out on my own but to no avail (only crashing TGB about 1294875 times!).
Audio in TGB refuses to work for me. I've put my music file here: common/data/audio/NatureSketch.wav (i've also tried putting it in game/data/audio/). I've created my own Audio Descriptions and I've used the already esisting ones. I've tried Looping and NonLooping. I've tried some nicely written behaviours which I found on TDN, but they work either. :(

Below I have 2 audios descriptions and 2 audio profiles. and some output from the console which I thought might be of some use.

In my consosle output I noticed that $fish has a negative value and I do believe that alxPlay looks for a positive number from a handle. But $other seems to have a positive number (which increases every time I called print(), lol). But anwyays, I am completely stumped. Anything I do either results in nothing happening or TGB crashing.

If anyone could please point me in the right direction or tell me I'm wasting my time and should cut my losses I would be soooo grateful! ( By the by I don't have the source code so can't modify the engine).


The Profiles & Descriptions
$effectsAudioType = 2;
new AudioDescription(AudioChannel2)
{
   volume = 1.0;
   isLooping = false;
   is3D = false;
   type = 2;
};

new AudioDescription(AudioChannel3)
{
   volume = 2.0;
   isLooping = true;
   is3D = false;
   type = 3;
};


new AudioProfile(Music_MainMenu)
{
   filename = "~/data/audio/NatureSketch.wav";
   description = "AudioChannel3";
   preload = true;
};

new AudioProfile(Music_2)
{
	filename = "~/data/audio/NatureSketch.wav";
   description = "AudioChannel2";
   preload = true;
};


I also put a function in one of my scripts to print out some info and the following is it:
function print(){
	$fish = alxCreateSource("Music_MainMenu");
	echo("$fish:" SPC $fish);
	echo("filename:" SPC Music_MainMenu.fileName);
	echo("Music_MainMenu:" SPC Music_MainMenu);
	echo("is playing?:" SPC alxIsPlaying($fish));
	
	$other = alxCreateSource("Music_2");
	echo("$other:" SPC $other);
	echo("filename:" SPC Music_2.fileName);
	echo("Music_2:" SPC Music_2);
	echo("is playing?:" SPC alxIsPlaying($other));
}


and here's some console stuff:
==>print();
$fish: -2147483647
filename: common/data/audio/NatureSketch.wav
Music_MainMenu: Music_MainMenu
is playing?: 0

$other: 2
filename: common/data/audio/NatureSketch.wav
Music_2: Music_2
is playing?: 0


==>alxplay($other);
==>alxplay(2);
==>alxplay("2");


==>print();
$fish: -2147483645
filename: common/data/audio/NatureSketch.wav
Music_MainMenu: Music_MainMenu
is playing?: 0

$other: 4
filename: common/data/audio/NatureSketch.wav
Music_2: Music_2
is playing?: 0


==>alxplay(Music_2);


==>print();
$fish: -2147483642
filename: common/data/audio/NatureSketch.wav
Music_MainMenu: Music_MainMenu
is playing?: 0

$other: 7
filename: common/data/audio/NatureSketch.wav
Music_2: Music_2
is playing?: 0

#1
02/17/2011 (8:10 pm)
@Siobhan - Your setup looks a little complex. Here is how I have my audio set up for a single sound:

new AudioDescription(SoundOnce)
{
   volume		= 1.0;
   type		   = 0;
   isLooping	= false;
   is3D		   = false;
};

new AudioProfile(fireSound)
{
   filename	   = "game/data/audio/shoot.wav";
   description = "SoundOnce";
   preload 	   = true;
};

function playAudio()
{
   alxPlay("fireSound");
}

That will always work, as long as the script containing the code is executed. Carefully compare your syntax to mine, such as how I'm using file paths "game/data/audio/shoot.wav", not "~/data".
#2
02/18/2011 (1:35 am)
Thank you so much for that Michael.
My sound at least worked for some of the simple sound files like fire! But when I went to play my longer one (2.09 minutes), it wouldn't play. So I shortened it (1 minute) and now it plays! Hurray! :)
Is there a max length a song could be? I know 2minutes is probably quite long.. But just curious. I can always check it out for myself later.
Thanks again! :)
#3
02/18/2011 (6:16 am)
Try OGG instead of WAV. I had no trouble with long OGGs.
#4
02/18/2011 (6:44 am)
Here is the audio code I use. It has some built in stopping etc. since sometimes you want to end your audio early...

Notes: there isn't a stop single because I never need to do it (and you can do it with alxStop ...

It also has music fading so you can fade your music in or out.

Basically you just call playMusic(); and it picks a track at random from your 3 tracks and fades it in. You could set up scheduling to make it loop and fade songs in and out as needed.

new AudioDescription(MusicDesc)
{
   volume = 1.0;
   isLooping = true;
   is3D = false;
   type = 1;
};

new AudioDescription(SFXDesc)
{
   volume = 1.0;
   isLooping = false;
   is3D = false;
   type = 2;
};

new AudioProfile(mySound)
{
   filename    = "game/data/audio/soundFile.wav";
   description = SFXDesc;
   preload     = true;
};

new AudioProfile(playMusic1Audio)
{
   filename    = "game/data/audio/music1.ogg";
   description = musicDesc;
   preload     = true;
};

new AudioProfile(playMusic2Audio)
{
   filename    = "game/data/audio/music2.ogg";
   description = musicDesc;
   preload     = true;
};

new AudioProfile(playMusic3Audio)
{
   filename    = "game/data/audio/music3.ogg";
   description = musicDesc;
   preload     = true;
};

$numAudioPlaying = 0;

function playAudio(%profile)
{
	$audioPlaying[$numAudioPlaying] = alxPlay(%profile);
	$numAudioPlaying++;
	
	return $audioPlaying[$numAudioPlaying - 1];
}

function killAudio()
{
	cancel($musicSchedule);
	cancel($fadeSchedule);
	
	for(%c = 0; %c < $numAudioPlaying; %c++)
	{
		alxStop($audioPlaying[%c]);
	}
	$numAudioPlaying = 0;
}

$lastMusic = -1;
$musicSwapTime = 5 * 60 * 1000;
$musicFadeTime = 5000;
$musicFullVolume = 0.8;
$musicFadeIncTime = 100;

function playMusic()
{
	audio_startTrack();
	%iters = $musicFadeTime / $musicFadeIncTime;
	%volChange = $musicFullVolume / %iters;
	audio_performFade(true, 0, %volChange, %iters);
}

function audio_swapMusic()
{
	%iters = $musicFadeTime / $musicFadeIncTime;
	%volChange = $musicFullVolume / %iters;
	audio_performFade(false, $musicFullVolume, %volChange, %iters);
}

function audio_startTrack()
{
	alxStop($musicHandle);
	
	%num = $lastMusic;
	while(%num == $lastMusic)
	{
		%num = getRandom(1,3);
	}
	$lastMusic = %num;
	
	$musicHandle = playAudio("playMusic" @ %num @ "audio");
	alxSetChannelVolume(1, 0);
}

function audio_PerformFade(%isIN, %vol, %volChange, %iters)
{
	if(%iters == 0)
	{
		if(%isIN)
		{
			alxSetChannelVolume(1, $musicFullVolume);
			$musicSchedule = schedule($musicSwapTime, 0, audio_swapMusic);
			return;
		}
		else
		{
			alxStop($musicHandle);
			audio_startTrack();
			%iters = $musicFadeTime / $musicFadeIncTime;
			%volChange = $musicFullVolume / %iters;
			audio_performFade(true, 0, %volChange, %iters);
			return;
		}
	}
	
	if(%isIN)
	{
		%vol += %volChange;
	}
	else
	{
		%vol -= %volChange;
	}
	
	alxSetChannelVolume(1, %vol);
	
	$fadeSchedule = schedule($musicFadeIncTime, 0, audio_PerformFade, %isIn, %vol, %volChange, %iters - 1);
}
#5
02/18/2011 (6:50 am)
Just realized... it already does auto fade out and in your music... Currently set to do it every 5 minutes and picks the new track at random from your 3. All of my 3 music tracks are exactly 5 minutes.

Put this in a file called audio.cs (make a new one of your own, call it something different if you have to) in game/gameScripts and exec it.
#6
02/18/2011 (6:55 am)
That's Ogg/Vorbis, to be specific - Ogg is just the container, Vorbis is the encoder ;)

And an important note is that *some* encoders in the past didn't produce files which work with T2D. Compiling the engine with LLVM on Mac also makes it not play compressed audio (probably a failing of the version included - will have to test by updating). Success is guaranteed when encoding with the Xiph command line tools.

Not being able to play WAV files over one minute seems like it's very wrong, though. I have decoded 5-6 minutes long tracks for testing myself, and there's no trouble at all playing them. Even when preloading you shouldn't have an issue, since a sound effect should rarely be stereo, therefore it's going to fit in RAM on a modern system.

44.1kHz stereo 16-bit audio = about 10MB/minute. Two minutes of speech should be mono, played through both channels, and take up about 10MB. Not a strain on any system in the past five years, at least. Vorbis at q5-6 will do really well with most sound, although T2D has the weakness that it doesn't stream anything. Everything has to be decoded into RAM, and it seems that process is unusually slow.
#7
02/18/2011 (7:40 am)
Thanks for all the replies everyone. For pig iron I decided to resave my music file with Audacity (exporting it to .wav) and the damned thing plays just fine now! I'm guessing there was just something funky with my file that TGG didn't like. I can only image how daft that sounds but I think I just attract the oddest of problems sometimes.

@Chris Lombabard: I'll have to check out that code of yours. The fading will be super handy.

I'm not going to have much music in my game (time just won't allow me to be overly fancy because it's my final year project for college). So I'm just going to have some music at the start while people are setting up and then some during gameplay and something at the end depending on whether you've lost or won.

@Ronny Bangsund: That's quite the bit of info there. Quite interesting though and always good to know a bit extra about something. :)

Now back to exciting stuff for a while. Finding some more music to play with! xDDD
#8
02/18/2011 (7:50 am)
@Siobhan - Great! Glad we got this resolved. My tip for future problems is to post about them early, rather than get burnt out on the same problem. I've been there and it is very frustrating + a productivity drain. If you want immediate support, hop into IRC to ask questions. If it is more troublesome or complex, post in the forums as soon as you are stuck. The faster we can resolve your issues, the faster you can release the game =)
#9
02/18/2011 (9:48 am)
@Michael Perry: I didn't know there was an IRC chat! And thanks for the tip. I'll be doing that in future. :)