// originally based on createSoundEntity.js // Copyright 2016 High Fidelity, Inc. // // updated with positional audio tracking and userdata support // copyright 2018 wade watts and humbletim // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // /* globals Entities, SoundCache, Audio, Script */ (function(){ var VERSION = '0.0.0a'; var ringSound; var entityID; var interval; var injector = null; var DEFAULT_OPTIONS = { url: 'https://hifi-content.s3.amazonaws.com/wadewatts/motorcycle-rev-03.wav', volume: 0.1, loop: false, stoppable: true, pitch: 1.0, }; var options = {}; function recalculateSoundOptions(entityID, defaultOptions) { var props = Entities.getEntityProperties(entityID); try { var data = Object(JSON.parse(props.userData)); } catch(e) { data = {}; } return { __proto__: { url: data.url || defaultOptions.url, stoppable: typeof data.stoppable !== 'undefined' ? data.stoppable : defaultOptions.stoppable, }, position: props.position || defaultOptions.url, orientation: props.rotation || defaultOptions.orientation, volume: isFinite(data.volume) ? parseFloat(data.volume) : defaultOptions.volume, loop: typeof data.loop !== 'undefined' ? data.loop : defaultOptions.loop, pitch: isFinite(data.pitch) ? parseFloat(data.pitch) : defaultOptions.pitch, }; } function updateSoundOptions() { options = recalculateSoundOptions(entityID, DEFAULT_OPTIONS); if (injector) { injector.setOptions(options); } } function startSound() { var wasPlaying = injector !== null; stopSound(); updateSoundOptions(); if (wasPlaying && options.stoppable) { return console.info('>>>>>>>>>> sound set stoppable; not restarting until next click'); } if (!ringSound || ringSound.url !== options.url) { console.info('loading sound...', options.url); ringSound = SoundCache.getSound(options.url); } if (!injector) { console.info(entityID, "startSound...", wasPlaying, options.stoppable); injector = Audio.playSound(ringSound, options); } else { console.info(entityID, "restartSound...", wasPlaying, options.stoppable); injector.restart(); } } function stopSound() { if (injector) { console.info('stopping sound'); injector.setOptions({ volume: 0 }); injector.stop(); injector = null; } } // entity methods this.preload = function(id) { entityID = id; console.info("preload("+id+")", 'movingEntitySound v'+VERSION); try { var data = Object(JSON.parse(Entities.getEntityProperties(entityID).userData)); } catch(e) { data = {}; } if (!data.url) { console.info('populating default userData values...', JSON.stringify(DEFAULT_OPTIONS,0,2)); for (var p in DEFAULT_OPTIONS) { data[p] = data[p] || DEFAULT_OPTIONS[p]; } Entities.editEntity(entityID, { userData: JSON.stringify(data) }); } interval = Script.setInterval(updateSoundOptions, 1000/24); } this.unload = function(id) { stopSound(); interval = Script.clearInterval(interval); }; this.clickDownOnEntity = function(entityID, mouseEvent) { if (mouseEvent.isLeftButton) { startSound(); } }; this.startFarTrigger = function(entityID, asdf) { startSound(); }; })