Stereo spatialized sound


#1

I was playing with the Audio Injector in order to create a stereo spatial sound.
To be spatial (and positional mixed) the sound must be mono. So I’ve created a script that play two
different mono files (left channel and right channel) each with his position.
The script looks like this:

/* stereoSpeakers.js
 *
 * entity client script for use in High Fidelity Interface.
 * 
 * By George Deac and Piper.Peppercorn
 * 
 * September 2018
 * 
 * To use: Edit the names of two entities so that
 * one is named:   LeftSpeaker
 * and the other:  RightSpeaker
 * exactly as above.
 * 
 * Paste a link to this script in the 'Script URL' slot in the properties of an entity.
 * This entity will act as the trigger,  
 * it could, for example, be a model of a button or switch.
 * 
 * Clicking the 'button' entity will trigger the script and play the sound.
 * The audio of each channel will be emitted from the appropriate speaker.
 * 
 * Enjoy and feel free to modify and distribute your own version.
 * 
 */


(function() {
    var clicked = true;	 
    var leftchannelinjector;
    var rightchannelinjector;
    var leftchannelurl = SoundCache.getSound("https://transmissiongate.com/hifi/stereoL.mp3");
    var rightchannelurl = SoundCache.getSound("https://transmissiongate.com/hifi/stereoR.mp3");
	
    var RANGE = 20; /* Meters. */
    var nearbyEntities = Entities.findEntities(MyAvatar.position, RANGE);  
    var rightSpeakerPosition; 
    var leftSpeakerPosition;  
    var rightSpeakerName = "RightSpeaker"; 
    var leftSpeakerName = "LeftSpeaker";  
    var rightSpeakerFound; 
    var leftSpeakerFound;  
			
    this.clickDownOnEntity = function (entityID, mouseEvent) {
        this.positionEmitters();
        if (clicked) {
            leftchannelinjector = Audio.playSound(leftchannelurl, {
                position: leftSpeakerPosition,
                volume: 0.1,
                loop: true,
                localOnly: true
            });
            rightchannelinjector = Audio.playSound(rightchannelurl, {
                position: rightSpeakerPosition,
                volume: 0.1,
                loop: true,
                localOnly: true
            });
            clicked = false;
        } else {
            leftchannelinjector.stop();
            leftchannelinjector = null;
            rightchannelinjector.stop();
            rightchannelinjector = null;
            clicked = true;
        }
    };
    this.unload = function() {
        if (leftchannelinjector) {
            leftchannelinjector.stop();
            leftchannelinjector = null;
            rightchannelinjector.stop();
            rightchannelinjector = null;
        }
    };

    this.positionEmitters = function() {
        rightSpeakerFound = false;
        leftSpeakerFound = false;
        nearbyEntities.forEach(function(entity) {
            var props = Entities.getEntityProperties(entity);
            if (rightSpeakerFound && leftSpeakerFound) {
                return;
            }        
            if (props.name === rightSpeakerName) {         
                rightSpeakerPosition = Entities.getEntityProperties(entity).position;
                rightSpeakerFound = true;
            }     
            if (props.name === leftSpeakerName) {         
                leftSpeakerPosition = Entities.getEntityProperties(entity).position;
                leftSpeakerFound = true;
            } 
        });
    };
});

I have separated the audio channels of the original audio file in two different mono mp3 files (16 bit 48kHz).

You can run directly this script from:

https://transmissiongate.com/hifi/stereo.js

by placing this script to an entity ScriptURL.

By clicking on this entity the sound will play.

You must create two entities and name them: RightSpeaker and LeftSpeaker.
Place this entities where you want to be the speakers.

Thank you @Piper.Peppercorn for your help on improving the script in order to get the position of the speakers.

What will be nice:
in a future release of HighFidelity to have the AudioInjector improved in order to select from a stereo audio file the audio channel (left or right) or even better to have two different positions for channel left and channel right. Something like:

injector = Audio.playSound(SoundURL {
        positionleft: {x:44.2273, y:-2.7276, z:19.9039},
        positionright: {x:44.2273, y:-2.7276, z:26.5597},
		secondOffset: 0,
        volume: 0.9,
        loop: true
});

Enjoy amazing audiophile music inside HighFidelity!


#2

I’ve managed to play also 5.1 channels.