Ambient sound script - looking for


#1

I’m looking for a script it can be used for playing ambient sounds. Or if it would play one sound file, so I could set up a few of them - like the sound sources on Playa. I’am not experienced in scripting, but maybe someone has an idea or hint for me? Thank you :slight_smile:


#2

Here’s example i just made. Only thing you need to click the entity once to start the sound. The script need to be placed inside entity on the script URL line.

(function()
{ 
    this.clickDownOnEntity = function(entityID, mouseEvent) 
    {     
        var soundurl = SoundCache.getSound("http://market.simsquaremetaverse.nl/sounds/dooropen_CCBY30_249573.wav");
        var soundvolume = 1.0;

        Ent = entityID;    
        var copyEnt = Entities.getEntityProperties(Ent, ["position"]);
        var jsonEnt = JSON.stringify(copyEnt);
        var data = JSON.parse(jsonEnt);    
        var basepos = data.position;
        
        Audio.playSound(soundurl, { loop: true, position: basepos ,volume: soundvolume });
    }
}
)

Edit soundurl so it points to the url of the sound you want to play.
soundvolume does not need any explenantion it’s between 0.0 and 1.0
Audio.playSound , loop: true for loop playback. Or loop: false for single time play.


#3

That’s awesome Richardus, thank you :slight_smile:


#4

@Richardus.Raymaker, it works nice, but maybe you have an idea, how I can make it play permanently. Right now, it stops when I relog. Just thought, if someone is on the domain, it would be cool if it is playing without any interaction.

I wonder, if it could possible to play a wav randomly (between 5-60 sec) in a loop?


#5

I wanted to test and try something. But my domain got on black, try to see where the content did go first.

Ill want to try something soon.


#6

For a while I used to use this script to play audio in my domain. It didn’t play the audio directly but instead launched a QML window that was responsible for the audio. The QML window had the added advantage of allowing the client to stop the audio and adjust the volume, although you don’t have to actually display the QML window at all.

The biggest drawbacks for using the QML audio mechanism are:

  • The sound is not positional.
  • The audio will play over the default output device as specified in Windows / OSX. It will ignore any changes made inside Interface to target another audio device. In particular, if you’re using an Oculus Rift, when you switch to the HMD, all audio switches over to the Rift headphones, but the QML audio will not.

These constraints apply right now to any QML audio, including audio from browser windows / web entities, so it’s something we want to fix eventually.


#7

Sorry, Skimi. I cannot do much. every time i try something. I get hugh problems right now.

@Skimi Edit: I think i know why i did get the problem. i go look tomorrow again at the script. maby i have a working one soon.


#8

if you want to loop sound on the server, there’s an example of that in the cellscience folder in the github repository

add a script like that as a persistent script on your domain settings page – when it runs it will wait for sounds to download and then play them


#9

Thanks for your great support, @Richardus.Raymaker, @Jherico and @james_hifi! I very appreciated it :smiley:


#10

Here’s a ambience sound script with random playtime.
Just put the script in entity box, and place it where you want the sound.
Note: URL works only from inside High Fidelity.

http://market.simsquaremetaverse.nl/scripts/RiRa_PlayAmbiancesoundRandom.js

//----------------------------
//
//  RiRa_PlayAmbiancesoundRandom.js
//
//  Created by Richardus Raymaker. 17-06-2016
//
//  Creative Common Attribution 4.0 International (CC BY 4.0)
//  http://creativecommons.org/licenses/by/4.0/
//
//    soundfile,  
//    https://www.freesound.org/people/tdes/sounds/155767/
//    http://creativecommons.org/licenses/by-nc/3.0/
//
//----------------------------

(function()
{ 
    var soundurl = SoundCache.getSound("http://market.simsquaremetaverse.nl/sounds/ambiance/181574__mings__tui-new-zealand-bird.wav");
    var soundvolume = 1.0;

    var Ent;
    var timer;
    this.preload=function(entityID)
    { 
        Ent = entityID;
        timer=Script.setInterval(play, getRandomInt(10000,60000));
    }
    
    function getRandomInt(min, max) 
    {
        return Math.floor(Math.random() * (max - min)) + min;
    }    
    
    function play()
    {     
        var copyEnt = Entities.getEntityProperties(Ent, ["position"]);
        var jsonEnt = JSON.stringify(copyEnt);
        var data = JSON.parse(jsonEnt);    
        var entitypos = data.position;
        
        Audio.playSound(soundurl, { loop: false, position: entitypos ,volume: soundvolume });
        Script.clearInterval(timer);    
        timer=Script.setInterval(play, getRandomInt(15000,60000));
    }    
}
)