2004-11-16 01:44:10 +01:00
|
|
|
// Audio functions (eg "pick a random sound from a list", "build a playlist") will go here.
|
|
|
|
// In the meantime, a quick run-down of the basic commands:
|
|
|
|
|
|
|
|
// Save the specified audio file to handle "s".
|
|
|
|
// s = new Sound( "audio/music/menu_track.ogg" );
|
|
|
|
|
|
|
|
// Play the sound stored at handle "s" one time (it'll be automatically freed when the playback ends):
|
|
|
|
// s.play();
|
|
|
|
|
|
|
|
// Play the sound stored at handle "s" continuously:
|
|
|
|
// s.loop();
|
|
|
|
|
|
|
|
// Close "s" and free it from memory (use in conjunction with loop()):
|
|
|
|
// s.free();
|
2004-11-20 18:52:18 +01:00
|
|
|
|
2004-11-25 03:44:38 +01:00
|
|
|
// Adjust the gain (volume) of a sound (floating point range between 0 (silent) and 1 (max volume)).
|
|
|
|
// s.SetGain(value);
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
2004-11-21 02:19:32 +01:00
|
|
|
function newRandomSound(soundType, soundSubType, soundPrePath)
|
2004-11-20 18:52:18 +01:00
|
|
|
{
|
2004-11-25 03:44:38 +01:00
|
|
|
// Return a random audio file by category, to be assigned to a handle.
|
|
|
|
|
2004-11-20 18:52:18 +01:00
|
|
|
switch (soundType)
|
|
|
|
{
|
|
|
|
case "music":
|
|
|
|
randomSoundPath = "audio/music/"
|
|
|
|
break;
|
2004-11-21 02:19:32 +01:00
|
|
|
case "voice":
|
|
|
|
randomSoundPath = soundPrePath + "/";
|
|
|
|
break;
|
2004-11-20 18:52:18 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2005-05-05 04:51:39 +02:00
|
|
|
|
|
|
|
// Get names of sounds (attack, command, select, hit, pain).
|
|
|
|
// or
|
|
|
|
// Get names of "peace", "menu" (theme) and "battle" tracks.
|
|
|
|
soundArray = buildFileList(randomSoundPath, "*" + soundSubType + "*", false);
|
2005-05-09 06:03:26 +02:00
|
|
|
if (soundArray.length == 0)
|
|
|
|
{
|
|
|
|
console.write("Failed to find sounds matching '*"+soundSubType+"*'");
|
|
|
|
return;
|
|
|
|
}
|
2005-05-05 04:51:39 +02:00
|
|
|
// Get a random number within the sound's range.
|
|
|
|
randomSound = getRandom(0, soundArray.length-1);
|
|
|
|
// Set name of track.
|
|
|
|
randomFileName = soundArray[randomSound];
|
|
|
|
|
2004-11-20 18:52:18 +01:00
|
|
|
// Build path to random audio file.
|
2005-05-05 04:51:39 +02:00
|
|
|
randomSoundPath = randomFileName;
|
2004-11-20 18:52:18 +01:00
|
|
|
|
|
|
|
console.write("Playing " + randomSoundPath + " ...");
|
|
|
|
|
|
|
|
return new Sound(randomSoundPath);
|
2004-11-25 03:44:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
|
|
|
function FadeOut (soundHandle, Rate)
|
|
|
|
{
|
|
|
|
// Adjust the gain of a sound until it is zero.
|
|
|
|
|
|
|
|
for (fadeLoop = 1; fadeLoop > 0; fadeLoop = fadeLoop - Rate)
|
|
|
|
{
|
|
|
|
soundHandle.setGain(fadeLoop);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
|
|
|
function FadeIn (soundHandle, Gain, Rate)
|
|
|
|
{
|
|
|
|
// Adjust the gain of a sound from zero up to the given value.
|
|
|
|
|
|
|
|
for (fadeLoop = 0; fadeLoop < Gain; fadeLoop = fadeLoop + Rate)
|
|
|
|
{
|
|
|
|
soundHandle.setGain(fadeLoop);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ====================================================================
|
|
|
|
|
|
|
|
function CrossFade (outHandle, inHandle, Rate)
|
|
|
|
{
|
|
|
|
// Accepts two sound handles. Fades out the first and fades in the second at the specified rate.
|
|
|
|
// Note that it plays the in and frees the out while it's at it.
|
|
|
|
|
2005-05-09 06:03:26 +02:00
|
|
|
if (outHandle)
|
|
|
|
FadeOut(outHandle, Rate);
|
|
|
|
|
|
|
|
if (inHandle)
|
|
|
|
{
|
|
|
|
inHandle.play();
|
|
|
|
FadeIn(inHandle, 1, Rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outHandle)
|
|
|
|
outHandle.free();
|
2004-11-25 03:44:38 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ====================================================================
|