Page 1 of 2
Need help with Script: How can I play a sound?
Posted: Thu Jun 06, 2013 10:53 pm
by DarkSoul
Hello guy I want to play some sounds when my condition changes, like :
condition 1= I can use witchdrive(key "J")
condition 2= Mass lock (key "J")
condition 3= when attacked
I´ve tryed this:
this.alertConditionChanged = function(newCondition, oldCondition)
{
switch(newCondition)
{
case 0: // we're docked
{
notifySound.sound = "[test]";
notifySound.play();
}
case 1: // we're at green alert
case 2: // or we're at yellow alert
{
if(player.ship.energy > 108)
{
notifySound.sound = "[test]";
notifySound.play();
}
break;
But this dont works with me and I am beggining to scripts so help me please
Re: Need help with Script: How can I play a sound?
Posted: Fri Jun 07, 2013 3:12 am
by Diziet Sma
You forgot to create a new SoundSource first.. (see the
Oolite JavaScript Reference: SoundSource for details)
This:
Code: Select all
{
notifySound.sound = "[test]";
notifySound.play();
}
should look something like:
Code: Select all
{
var notifySound = new SoundSource;
notifySound.sound = "[test]";
notifySound.play();
}
Also, the above requires that
[test]
be declared in a
customsounds.plist
. This would look something like:
Rather than using a
customsounds.plist
(recommended), you could supply the filename of the sound directly, in a string, as in:
Code: Select all
{
var notifySound = new SoundSource;
notifySound.sound = "testsound.ogg";
notifySound.play();
}
Further, don't forget that
testsound.ogg
needs to be in a folder called 'Sounds'.
For one of the most stripped-down-to-basics examples of how to play a sound, take a look at my
Q-Bomb Detector OXP. All it does is play a sound when a certain condition is detected.
Re: Need help with Script: How can I play a sound?
Posted: Fri Jun 07, 2013 1:10 pm
by Eric Walch
Similar as above, i first define the sound in startup:
Code: Select all
this.startUp = function ()
{
this.mySound = new SoundSource;
this.mySound.sound = "mySound.ogg";
this.mySound.loop = false; // not needed, as it is false by default
}
And than every time I need to play it, I use:
But you could also use:
to play the sound 10 times. Or you can set the loop to true, in which case you must stop it later with a new command.
EDIT: the method by Diziet Sma with a local 'var' is probably more memory friendly.
Re: Need help with Script: How can I play a sound?
Posted: Fri Jun 07, 2013 5:19 pm
by Diziet Sma
Eric Walch wrote:Similar as above, i first define the sound in startup:
Code: Select all
this.startUp = function ()
{
this.mySound = new SoundSource;
this.mySound.sound = "mySound.ogg";
this.mySound.loop = false; // not needed, as it is false by default
}
And than every time I need to play it, I use:
Interesting.. as I've been considering doing something like the above.
The only problem the Q-Bomb Detector has at present is that the first time it's triggered in a gaming session (at least I'm pretty sure it's only the first time) there can be a few seconds delay in the siren playing. I'm guessing the delay is caused by the game needing to load in the audio file from disk. Of course, with a Q-bomb in the vicinity, that few seconds delay can be fatal.. would your method above amount to a pre-loading of the audio? And if not, is there a way that I can pre-load it, so as to eliminate that delay?
Re: Need help with Script: How can I play a sound?
Posted: Fri Jun 07, 2013 6:06 pm
by Thargoid
You could probably set it to load, play and then immediately stop (ie so it doesn't actually play anything) on start-up or somewhere suitably early. That way if it is a loading/caching delay, then it should be done sometime before it'd be a problem.
Re: Need help with Script: How can I play a sound?
Posted: Fri Jun 07, 2013 9:08 pm
by JensAyton
Oolite preloads all sounds in customsounds.plist, and the q-mine alarm plays instantly for me. Perhaps SDL confounds our efforts by loading sounds lazily?
Re: Need help with Script: How can I play a sound?
Posted: Sat Jun 08, 2013 3:06 am
by DarkSoul
Thanks guys so much. I will try that codes and post here a feedback, see you soon...
I hope it works.
Re: Need help with Script: How can I play a sound?
Posted: Sat Jun 08, 2013 3:44 am
by Diziet Sma
Thanks for that info, Jens.. looks as if there's nothing that can be done, then.. it's only an occasional thing, fortunately, but has happened to me once or twice, and it recently helped
Capt. Reynolds pick up a Darwin Award..
(on the other hand, maybe Thargoid's trick might overcome SDL's laziness..)
You're very welcome, DarkSoul.. looking forward to your feedback.
Re: Need help with Script: How can I play a sound?
Posted: Sat Jun 08, 2013 7:43 am
by another_commander
It is unlikely that the sound delay thing is SDL related. I am getting the qbomb alert sound playing at the same time as the message warning appears on screen with Q-mine Detector, same as Jens.
Re: Need help with Script: How can I play a sound?
Posted: Sat Jun 08, 2013 9:13 am
by Diziet Sma
Hmm.. I wonder if maybe it's related to running on a lower-spec computer? Mine has only a 1.1GHz CPU. Could be if it has a lot to keep track of at the time, it's bogging down a little..
Good thing it's a rare event, even on this machine.
Edit:
another_commander wrote:I am getting the qbomb alert sound playing at the same time as the message warning appears on screen with Q-mine Detector, same as Jens.
Well, that's as it should be anyway.. the message and sound are part of the same subroutine.
What I'm wondering about is, why is it that occasionally, from the q-bomb being launched to the message/siren routine getting triggered. there can be a delay of around 3 or so seconds? It's almost as if
this.cascadeWeaponDetected
is -just once in a while- taking a long time to catch on to what's happening.. (not that I'm saying that's where the problem is)
Re: Need help with Script: How can I play a sound?
Posted: Sat Jun 08, 2013 10:22 am
by JensAyton
Diziet Sma wrote:Hmm.. I wonder if maybe it's related to running on a lower-spec computer? Mine has only a 1.1GHz CPU. Could be if it has a lot to keep track of at the time, it's bogging down a little..
It could be a memory thing – the sound, or information about it, is paged out and it takes the virtual memory system a while to bring it back in. If SDL is waiting for it on a background thread, the game will continue to run in the meantime.
Re: Need help with Script: How can I play a sound?
Posted: Sun Jun 09, 2013 12:02 am
by Diziet Sma
That makes a lot of sense..
Re: Need help with Script: How can I play a sound?
Posted: Thu Jun 20, 2013 12:57 am
by DarkSoul
Well folks sorry I am delayed but not yet can pllay any sound. I dont understand it. Help please.
Re: Need help with Script: How can I play a sound?
Posted: Thu Jun 20, 2013 6:33 am
by Smivs
Can you give us some more details?
Maybe post the script here so we can check it (c&p it between 'code' tags), and tell us how you are putting it into the game.
Smivs
Re: Need help with Script: How can I play a sound?
Posted: Fri Jun 21, 2013 1:13 am
by DarkSoul
OK I just try use some part of GENERIC HUD script:
this.name = "test";
this.author = "DarkSoul";
this.copyright = "Do what you want with it";
this.description = "test";
this.version = "1.0";
this.alertConditionChanged = function(newCondition, oldCondition)
{
switch(newCondition)
{
case 0: // we're docked
{
var testSound = new SoundSource;
testSound.sound = "[test_01]";
testSound.play();
}
case 1: // we're at green alert
case 2: // or we're at yellow alert
{
if(player.ship.energy > 108) // if we're not using the damaged HUD
{
testSound.play();
}
//break;
}
case 3: // we're at red alert
{
if(player.alertHostiles && player.ship.energy > 108) // and under attack and not using the damaged HUD
{
testSound.play();
}
//break;
}
}
}
this.shipLaunchedFromStation = function()
{
if(this.energyCheckTimer)
{
this.energyCheckTimer.start()
}
else
{
this.energyCheckTimer = new Timer(this, this.energyCheck,0,2) // use a timer to keep an eye on the HUD state
}
}
this.energyCheck = function()
{
if(player.ship.docked)
{
this.energyCheckTimer.stop();
}
else
{
if(player.ship.energy < 109 )
{
testSound.play();
}
if(player.ship.energy > 108 && player.ship.hud == "kwsnafuhud.plist")
{
this.alertConditionChanged(player.alertCondition,0); // if energy is >49 and we're still displaying the damaged HUD, use other code to repair
}
}
}
this.shipDied = function()
{
if(this.energyCheckTimer)
{
this.energyCheckTimer.stop()
}
}