Need help with Script: How can I play a sound?

Discussion and information relevant to creating special missions, new ships, skins etc.

Moderators: winston, another_commander

DarkSoul
Average
Average
Posts: 11
Joined: Sat Jun 01, 2013 1:59 am

Need help with Script: How can I play a sound?

Post 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
User avatar
Diziet Sma
---- E L I T E ----
---- E L I T E ----
Posts: 6312
Joined: Mon Apr 06, 2009 12:20 pm
Location: Aboard the Pitviper S.E. "Blackwidow"

Re: Need help with Script: How can I play a sound?

Post 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:

Code: Select all

{
    "[test]" = "testsound.ogg";
}
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'. :wink:

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.
Most games have some sort of paddling-pool-and-water-wings beginning to ease you in: Oolite takes the rather more Darwinian approach of heaving you straight into the ocean, often with a brick or two in your pockets for luck. ~ Disembodied
User avatar
Eric Walch
Slightly Grand Rear Admiral
Slightly Grand Rear Admiral
Posts: 5536
Joined: Sat Jun 16, 2007 3:48 pm
Location: Netherlands

Re: Need help with Script: How can I play a sound?

Post 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:

Code: Select all

this.mySound.play();
But you could also use:

Code: Select all

this.mySound.play(10);
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. :lol:
User avatar
Diziet Sma
---- E L I T E ----
---- E L I T E ----
Posts: 6312
Joined: Mon Apr 06, 2009 12:20 pm
Location: Aboard the Pitviper S.E. "Blackwidow"

Re: Need help with Script: How can I play a sound?

Post 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:

Code: Select all

this.mySound.play();
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?
Most games have some sort of paddling-pool-and-water-wings beginning to ease you in: Oolite takes the rather more Darwinian approach of heaving you straight into the ocean, often with a brick or two in your pockets for luck. ~ Disembodied
User avatar
Thargoid
Thargoid
Thargoid
Posts: 5528
Joined: Thu Jun 12, 2008 6:55 pm

Re: Need help with Script: How can I play a sound?

Post 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.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Need help with Script: How can I play a sound?

Post 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?
DarkSoul
Average
Average
Posts: 11
Joined: Sat Jun 01, 2013 1:59 am

Re: Need help with Script: How can I play a sound?

Post by DarkSoul »

Thanks guys so much. I will try that codes and post here a feedback, see you soon...
I hope it works.
User avatar
Diziet Sma
---- E L I T E ----
---- E L I T E ----
Posts: 6312
Joined: Mon Apr 06, 2009 12:20 pm
Location: Aboard the Pitviper S.E. "Blackwidow"

Re: Need help with Script: How can I play a sound?

Post 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.. :lol:

(on the other hand, maybe Thargoid's trick might overcome SDL's laziness..)

You're very welcome, DarkSoul.. looking forward to your feedback. 8)
Most games have some sort of paddling-pool-and-water-wings beginning to ease you in: Oolite takes the rather more Darwinian approach of heaving you straight into the ocean, often with a brick or two in your pockets for luck. ~ Disembodied
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6683
Joined: Wed Feb 28, 2007 7:54 am

Re: Need help with Script: How can I play a sound?

Post 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.
User avatar
Diziet Sma
---- E L I T E ----
---- E L I T E ----
Posts: 6312
Joined: Mon Apr 06, 2009 12:20 pm
Location: Aboard the Pitviper S.E. "Blackwidow"

Re: Need help with Script: How can I play a sound?

Post 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)
Most games have some sort of paddling-pool-and-water-wings beginning to ease you in: Oolite takes the rather more Darwinian approach of heaving you straight into the ocean, often with a brick or two in your pockets for luck. ~ Disembodied
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Re: Need help with Script: How can I play a sound?

Post 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.
User avatar
Diziet Sma
---- E L I T E ----
---- E L I T E ----
Posts: 6312
Joined: Mon Apr 06, 2009 12:20 pm
Location: Aboard the Pitviper S.E. "Blackwidow"

Re: Need help with Script: How can I play a sound?

Post by Diziet Sma »

That makes a lot of sense..
Most games have some sort of paddling-pool-and-water-wings beginning to ease you in: Oolite takes the rather more Darwinian approach of heaving you straight into the ocean, often with a brick or two in your pockets for luck. ~ Disembodied
DarkSoul
Average
Average
Posts: 11
Joined: Sat Jun 01, 2013 1:59 am

Re: Need help with Script: How can I play a sound?

Post by DarkSoul »

Well folks sorry I am delayed but not yet can pllay any sound. I dont understand it. Help please.
User avatar
Smivs
Retired Assassin
Retired Assassin
Posts: 8408
Joined: Tue Feb 09, 2010 11:31 am
Location: Lost in space
Contact:

Re: Need help with Script: How can I play a sound?

Post 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
Commander Smivs, the friendliest Gourd this side of Riedquat.
DarkSoul
Average
Average
Posts: 11
Joined: Sat Jun 01, 2013 1:59 am

Re: Need help with Script: How can I play a sound?

Post 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()
}
}
Post Reply