Game Development Community

Music Track Question

by Jon Fernback · in Torque Game Engine · 03/28/2005 (6:11 pm) · 9 replies

I have a question about setting up music tracks for a mission.
Say I have two song files that I want to play. The first one is the "intro.ogg" which I want to play thru once starting at the very beginning of the mission. This part I already know how to do and got working.

The problem is, I also have the second song "loop.ogg" which I want to start right when the intro ends, and keep looping the second song for the rest of the mission.

OR

I am willing to cheat and just queue up a bunch of buffers with the loop.ogg as non-looping but playing a bunch of times in a row after intro plays once. The problem is I have no idea how to do this (or if it can be done in torque)

I do know that OpenAL has a alSourceQueueBuffers() function, which if all else fails I suppose I could try to implement by modifying the engine code, but I'm hoping someone else has figured out a script that would handle song lists?

Thanks,
Jon

#1
03/29/2005 (5:37 am)
Set the intro song to non-looping. Set the loop song to looping.
Schedule it to play the loop song after the intro is done.
#2
03/29/2005 (6:46 am)
Sebastien, Thanks for the reply. The problem however was figuring out how to schedule the song. Your reply did point me to looking up a scheduler tho, which I did not know was available (Yea I'm new with torque).

Anyway, I think I can figure it out now using the scheduler.

Thanks!
#3
03/29/2005 (10:56 am)
Tell us about your results.

I haven't tried such setup, but I think a schedule can result in a small gap between the tracks, due to the small delay when playing an audio track.
#4
03/30/2005 (5:35 am)
Ok, here are the results from last nights attempt at a music soundtrack.

Just as before I was able to get the intro song to play thru once and quit no problem.
I then setup a schedule to run a script just a short time before the intro song was finished.

The script runs and checks to see if the intro song finished playing. If finished then the second song is played (and set to looping). If it is not finished, then i reschedule this script to run again in a 100-200 milliseconds.

However, this did not work. I ended up with a break inbetween the end of the first song and the beginning of the second just like Manoel expected. So I tried a second approach.

Instead of checking to see if the first song was finished, I scheduled to just start playing the second song slightly before the time the intro "should" be finished. Which in my case is about 15 seconds. After toying with the exact timing a little bit (ended up being about 15500 milliseconds in the schedule call) I was able to get rid of a noticable skip between the songs. Unfortunately this approach is just a hack and only possible if you know exactly how long your intro song should be.

The other issue I have not yet worked out, I noticed the frame-rate would spike when the first song ended and the second one began, which is unacceptable. I think however, that I can get rid of this bug by changing a preloaded setting. I'll let you know if this works, I didn't have time to try it last night.

To sum up.

Pros: Seems to work for scheduling a looping song to play after a short intro track IF you know exactly how long your intro is AND the intro is designed to flow into the main loop (which is the case with the songs I'm working with). OR I believe this method would work fine if you have several songs that are to be played as seperate tracks, like a playlist.

Cons: You must know the exact length of the non-looping intro track. Also, there is a noticable frame-rate spike when the second song begins, which I hope can be eliminated by using a preloaded setting.

Anyone else have a good idea on a better way to implement this? At worse I know it can be done with OpenAL functions by modifying the engine as the old engine I worked on could do this, but I'm guessing the functionality is already existing and I'm just too blind to see it.

Thanks,
Jon
#5
03/31/2005 (6:44 pm)
%time = alxGetOggLen(%file);
schedule(%time * 1000, 0, "nextSong");

GetOggLen is a C++ console function I made (as alxGetWavLen has memory issues)

Just stick this in audioFunctions.cc (I resourced it, but no idea what happened to it)

ConsoleFunction(alxGetOggLen, S32, 2, 2, "alxGetOggLen(filename)")
{
OggVorbisFile vf;
ResourceObject * obj = ResourceManager->find(argv[1]);
Stream *stream = ResourceManager->openStream(obj);
if(vf.ov_open(stream, NULL, 0) < 0) {
return false;
}
S32 time = S32(vf.ov_time_total(-1));
vf.ov_clear();
ResourceManager->closeStream(stream);
return time;
}

That'll get you the song length, so your scheduling can be exact.
#6
03/31/2005 (7:00 pm)
We're looking at a similar need: having emotion generating variants on a particular theme that transition based on game events...if the ambient theme is playing because nothing big is going on nearby in the game world, it's a peaceful, non-emotional track playing. When a game event occurs (say, combat), the track transitions to the battle variant of the theme.

What we're looking at right now is just using multiple audio channels, and setting them to overlap slightly, and then fading the first channel out while the second channel fades in. We're also toying with the idea of a transition "spike", a short (2-5 second) fanfare track that covers the transition and blends the two tracks together.
#7
04/01/2005 (8:14 am)
Thanks again Sebastien!

That looks like its exactly what I need to complete the music track functionality.

On another note, setting preloaded = true cleared up the frame-rate spike and slight delay I was experiencing before, as I thought it might.

Stephen, the spike and fading is a good idea for things like fast track changes (due to entering combat, etc) and we will probably try something along those lines for end of level boss combat. This is a great idea because you have no idea when during the song you will need to switch tracks.

For the intro song and loop it was slightly different. We have an intro piece that is nothing more than the song's first 16 seconds or so that needed to immediately flow into a second piece that is designed to loop seamlessly with itself and the end of the intro, which is why I needed the setup we were working out with no breaks.

Anyway, things are working great now and I'll try out Sebastien's getlength code this weekend.
Thanks again for the help,
Jon
#8
04/01/2005 (8:25 am)
I have music playing in the backround of my mission but I have noticed that Torque has some major issues with audio. If you are playing the game some times the music will just stop. I also get issues with other sounds that are not supposed to loop start looping like machine gun sounds and explosions.

I was wondering how any of you added your music in game and if you are having any major sound problems.
#9
04/09/2005 (7:39 am)
Found out that the issue is with OpenAL in TGE... Currently this is being worked on for 1.4 release. Basically what happens is if you have audio profiles that are pointing to nothing. it will try to load the sound and get mixed up with other sounds. I have fixed most of the sound issues with my game but I keep seeing others pop up. One is actually kinda wierd because it will freeze the game when you use a 4x4 vehicle with engine sounds. This problem has been patched by someone in another forum thread.