From 20d1ba11c3c342a776826b84c7a6840b69236e10 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 2 Feb 2016 10:37:23 -0800 Subject: [PATCH] Basic ac sound playing and entity searching Injecting sounds from entity --- .../ACAudioSearchAndInject.js | 59 +++++++++++++++++++ .../acAudioSearchCompatibleEntitySpawner.js | 36 +++++++++++ 2 files changed, 95 insertions(+) create mode 100644 examples/audioExamples/acAudioSearching/ACAudioSearchAndInject.js create mode 100644 examples/audioExamples/acAudioSearching/acAudioSearchCompatibleEntitySpawner.js diff --git a/examples/audioExamples/acAudioSearching/ACAudioSearchAndInject.js b/examples/audioExamples/acAudioSearching/ACAudioSearchAndInject.js new file mode 100644 index 0000000000..dee5408589 --- /dev/null +++ b/examples/audioExamples/acAudioSearching/ACAudioSearchAndInject.js @@ -0,0 +1,59 @@ +// +// ACAudioSearchAndInject.js +// audio +// +// Created by Eric Levin 2/1/2016 +// Copyright 2016 High Fidelity, Inc. + +// This AC script constantly searches for entities with a special userData field that specifies audio settings, and then +// injects the sound with the specified URL with other specified settings (playback volume) or playback at interval, or random interval +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +Script.include("https://rawgit.com/highfidelity/hifi/master/examples/libraries/utils.js"); +var SOUND_DATA_KEY = "soundKey"; + + +EntityViewer.setPosition({ + x: 0, + y: 0, + z: 0 +}); +EntityViewer.setKeyholeRadius(60000); +EntityViewer.queryOctree(); +Entities.setPacketsPerSecond(6000); + +Script.setInterval(function() { + var entities = Entities.findEntities({ + x: 0, + y: 0, + z: 0 + }, 16000) + print("EBL ENTITIES FOUND " + entities.length); + entities.forEach(function(entity) { + var soundData = getEntityCustomData(SOUND_KEY, entity); + if (soundData && soundData.url) { + playSound(soundData); + } + }); + +}, 1000); + +function playSound(soundData) { + var sound = SoundCache.getSound(soundData.url); + sound.ready.connect(function() { + print("EBL PLAY SOUND") + Audio.playSound(sound, { + position: { + x: 0, + y: 0, + z: 0 + }, + volume: 1.0, + loop: true + }); + }) + +} \ No newline at end of file diff --git a/examples/audioExamples/acAudioSearching/acAudioSearchCompatibleEntitySpawner.js b/examples/audioExamples/acAudioSearching/acAudioSearchCompatibleEntitySpawner.js new file mode 100644 index 0000000000..e25e114ba5 --- /dev/null +++ b/examples/audioExamples/acAudioSearching/acAudioSearchCompatibleEntitySpawner.js @@ -0,0 +1,36 @@ +// +// acAudioSearchCompatibleEntitySpawner.js +// audio/acAudioSearching +// +// Created by Eric Levin 2/2/2016 +// Copyright 2016 High Fidelity, Inc. + +// This is a client script which spawns entities with a field in userdata compatible with the AcAudioSearchAndInject script +// These entities specify data about the sound they want to play, such as url, volume, and whether to loop or not +// The position of the entity determines the position from which the sound plays from +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +Script.include("../../libraries/utils.js"); + +var SOUND_DATA_KEY = "soundKey"; + +var soundEntity = Entities.addEntity({ + type: "Box", + position: {x: 0, y: 0, z: 0}, + color: {red: 200, green: 10, blue: 200}, + dimensions: {x: .1, y: .1, z: .1}, + userData: JSON.stringify({ + soundKey: { + url: "http://hifi-public.s3.amazonaws.com/ryan/demo/0619_Fireplace__Tree_B.L.wav" + } + }) +}); + +function cleanup() { + Entities.deleteEntity(soundEntity); +} + +Script.scriptEnding.connect(cleanup); \ No newline at end of file