From 4dc929eddbaa68c6cdebdeeab715d80aa4f78a06 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Mon, 15 Sep 2014 19:50:38 -0700 Subject: [PATCH] Simpler script with sphere to indicate emission point --- examples/playSoundLoop.js | 51 +++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/examples/playSoundLoop.js b/examples/playSoundLoop.js index 86226468bc..2c684a73a1 100644 --- a/examples/playSoundLoop.js +++ b/examples/playSoundLoop.js @@ -5,39 +5,54 @@ // Created by David Rowe on 5/29/14. // Copyright 2014 High Fidelity, Inc. // -// This example script plays a sound in a continuous loop. +// This example script plays a sound in a continuous loop, and creates a red sphere in front of you at the origin of the sound. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -var sound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Nylon+A.raw"); + +// A few sample files you may want to try: + +//var sound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guitars/Guitar+-+Nylon+A.raw"); +//var sound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/220Sine.wav"); +var sound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Cocktail+Party+Snippets/Bandcamp.wav"); var soundPlaying = false; +var options = new AudioInjectionOptions(); +options.position = Vec3.sum(Camera.getPosition(), Quat.getFront(MyAvatar.orientation)); +options.volume = 0.5; +options.loop = true; +var playing = false; +var ball = false; -function keyPressEvent(event) { - if (event.text === "1") { - if (!Audio.isInjectorPlaying(soundPlaying)) { - var options = new AudioInjectionOptions(); - options.position = MyAvatar.position; - options.volume = 0.5; - options.loop = true; - soundPlaying = Audio.playSound(sound, options); - print("Started sound loop"); - } else { - Audio.stopInjector(soundPlaying); - print("Stopped sound loop"); - } +function maybePlaySound(deltaTime) { + if (sound.downloaded) { + var properties = { + type: "Sphere", + position: options.position, + velocity: { x: 0, y: 0, z: 0}, + gravity: { x: 0, y: 0, z: 0}, + radius: 0.10, + damping: 0.999, + color: { red: 200, green: 0, blue: 0 } + }; + ball = Entities.addEntity(properties); + soundPlaying = Audio.playSound(sound, options); + print("Started sound looping."); + Script.update.disconnect(maybePlaySound); } } - + function scriptEnding() { if (Audio.isInjectorPlaying(soundPlaying)) { Audio.stopInjector(soundPlaying); - print("Stopped sound loop"); + Entities.deleteEntity(ball); + print("Stopped sound."); } } // Connect a call back that happens every frame Script.scriptEnding.connect(scriptEnding); -Controller.keyPressEvent.connect(keyPressEvent); +Script.update.connect(maybePlaySound); +