From 771e5a4f9de7ab08bb5308b7b43d9528d5e00b58 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 20 Oct 2015 11:21:09 -0700 Subject: [PATCH 1/6] add toybox ac scripts, write headers for them, organize --- examples/toys/AC_scripts/arcade_game_sound.js | 52 +++++++++++++++++++ examples/toys/AC_scripts/cat_purr_sound.js | 41 +++++++++++++++ .../toys/AC_scripts/dogs_barking_sound.js | 52 +++++++++++++++++++ examples/toys/AC_scripts/fireplace_sound.js | 38 ++++++++++++++ .../AC_scripts}/flickeringLight.js | 0 examples/toys/AC_scripts/insects_sound.js | 39 ++++++++++++++ examples/toys/AC_scripts/river_water_sound.js | 37 +++++++++++++ examples/toys/AC_scripts/windmill_sound.js | 38 ++++++++++++++ 8 files changed, 297 insertions(+) create mode 100644 examples/toys/AC_scripts/arcade_game_sound.js create mode 100644 examples/toys/AC_scripts/cat_purr_sound.js create mode 100644 examples/toys/AC_scripts/dogs_barking_sound.js create mode 100644 examples/toys/AC_scripts/fireplace_sound.js rename examples/{acScripts => toys/AC_scripts}/flickeringLight.js (100%) create mode 100644 examples/toys/AC_scripts/insects_sound.js create mode 100644 examples/toys/AC_scripts/river_water_sound.js create mode 100644 examples/toys/AC_scripts/windmill_sound.js diff --git a/examples/toys/AC_scripts/arcade_game_sound.js b/examples/toys/AC_scripts/arcade_game_sound.js new file mode 100644 index 0000000000..0f158aa574 --- /dev/null +++ b/examples/toys/AC_scripts/arcade_game_sound.js @@ -0,0 +1,52 @@ +// Adds arcade game noises to Toybox. +// By Ryan Karpf 10/20/2015 +// +// 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_URL = "http://hifi-public.s3.amazonaws.com/ryan/ARCADE_GAMES_VID.L.L.wav"; +var SOUND_POSITION = { x: 543.77, y: 495.07, z: 502.25 }; + +var MINUTE = 60 * 1000; +var PLAY_SOUND_INTERVAL = 1.5 * MINUTE; + +var audioOptions = { + position: SOUND_POSITION, + volume: .01, + loop: false, +}; + +var sound = SoundCache.getSound(SOUND_URL); +var injector = null; + +function playSound() { + print("Playing sound"); + if (injector) { + // try/catch in case the injector QObject has been deleted already + try { + injector.stop(); + } catch (e) { + } + } + injector = Audio.playSound(sound, audioOptions); +} + +function checkDownloaded() { + if (sound.downloaded) { + print("Sound downloaded."); + Script.clearInterval(checkDownloadedTimer); + Script.setInterval(playSound, PLAY_SOUND_INTERVAL); + playSound(); + } +} + +// Check once a second to see if the audio file has been downloaded +var checkDownloadedTimer = Script.setInterval(checkDownloaded, 1000); + +Script.scriptEnding.connect(function() { + if (injector) { + injector.stop(); + } +}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/cat_purr_sound.js b/examples/toys/AC_scripts/cat_purr_sound.js new file mode 100644 index 0000000000..c02b14ebf8 --- /dev/null +++ b/examples/toys/AC_scripts/cat_purr_sound.js @@ -0,0 +1,41 @@ +// +// ambiance.js +// examples +// +// Created by Clément Brisset on 11/18/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/Cat_Purring_Deep_Low_Snor.wav"; +var position = { x: 551.48, y: 495.60, z: 502.08}; +var audioOptions = { + position: position, + volume: .03, + loop: true +}; + +var sound = SoundCache.getSound(soundURL); +var injector = null; +var count = 300; + +Script.update.connect(function() { + if (count > 0) { + count--; + return; + } + + if (sound.downloaded && injector === null) { + print("Sound downloaded."); + injector = Audio.playSound(sound, audioOptions); + print("Playing: " + injector); + } +}); + +Script.scriptEnding.connect(function() { + if (injector !== null) { + injector.stop(); + } +}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/dogs_barking_sound.js b/examples/toys/AC_scripts/dogs_barking_sound.js new file mode 100644 index 0000000000..c362e1ac76 --- /dev/null +++ b/examples/toys/AC_scripts/dogs_barking_sound.js @@ -0,0 +1,52 @@ +// Adds a dogs barking noise to Toybox. +// By Ryan Karpf 10/20/2015 +// +// 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_URL = "http://hifi-public.s3.amazonaws.com/ryan/dogs_barking_1.L.wav"; +var SOUND_POSITION = { x: 523, y: 495, z: 469 }; + +var MINUTE = 60 * 1000; +var PLAY_SOUND_INTERVAL = 1 * MINUTE; + +var audioOptions = { + position: SOUND_POSITION, + volume: .05, + loop: false, +}; + +var sound = SoundCache.getSound(SOUND_URL); +var injector = null; + +function playSound() { + print("Playing sound"); + if (injector) { + // try/catch in case the injector QObject has been deleted already + try { + injector.stop(); + } catch (e) { + } + } + injector = Audio.playSound(sound, audioOptions); +} + +function checkDownloaded() { + if (sound.downloaded) { + print("Sound downloaded."); + Script.clearInterval(checkDownloadedTimer); + Script.setInterval(playSound, PLAY_SOUND_INTERVAL); + playSound(); + } +} + +// Check once a second to see if the audio file has been downloaded +var checkDownloadedTimer = Script.setInterval(checkDownloaded, 1000); + +Script.scriptEnding.connect(function() { + if (injector) { + injector.stop(); + } +}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/fireplace_sound.js b/examples/toys/AC_scripts/fireplace_sound.js new file mode 100644 index 0000000000..76a7153c4b --- /dev/null +++ b/examples/toys/AC_scripts/fireplace_sound.js @@ -0,0 +1,38 @@ +// Adds a fireplace noise to Toybox. +// By Ryan Karpf 10/20/2015 +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + + +var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/demo/0619_Fireplace__Tree_B.L.wav"; +var position = { x: 551.61, y: 494.88, z: 502.00}; +var audioOptions = { + position: position, + volume: .08, + loop: true +}; + +var sound = SoundCache.getSound(soundURL); +var injector = null; +var count = 300; + +Script.update.connect(function() { + if (count > 0) { + count--; + return; + } + + if (sound.downloaded && injector === null) { + print("Sound downloaded."); + injector = Audio.playSound(sound, audioOptions); + print("Playing: " + injector); + } +}); + +Script.scriptEnding.connect(function() { + if (injector !== null) { + injector.stop(); + } +}); \ No newline at end of file diff --git a/examples/acScripts/flickeringLight.js b/examples/toys/AC_scripts/flickeringLight.js similarity index 100% rename from examples/acScripts/flickeringLight.js rename to examples/toys/AC_scripts/flickeringLight.js diff --git a/examples/toys/AC_scripts/insects_sound.js b/examples/toys/AC_scripts/insects_sound.js new file mode 100644 index 0000000000..1783413901 --- /dev/null +++ b/examples/toys/AC_scripts/insects_sound.js @@ -0,0 +1,39 @@ +// Adds an insect noise to Toybox. +// By Ryan Karpf 10/20/2015 +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + + + +var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/insects3.wav"; +var position = { x: 560, y: 495, z: 474}; +var audioOptions = { + position: position, + volume: .25, + loop: true +}; + +var sound = SoundCache.getSound(soundURL); +var injector = null; +var count = 300; + +Script.update.connect(function() { + if (count > 0) { + count--; + return; + } + + if (sound.downloaded && injector === null) { + print("Sound downloaded."); + injector = Audio.playSound(sound, audioOptions); + print("Playing: " + injector); + } +}); + +Script.scriptEnding.connect(function() { + if (injector !== null) { + injector.stop(); + } +}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/river_water_sound.js b/examples/toys/AC_scripts/river_water_sound.js new file mode 100644 index 0000000000..30df67b515 --- /dev/null +++ b/examples/toys/AC_scripts/river_water_sound.js @@ -0,0 +1,37 @@ +// Adds a river water sound to Toybox. +// By Ryan Karpf 10/20/2015 +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/Water_Lap_River_Edge_Gentle.L.wav"; +var position = { x: 580, y: 493, z: 528}; +var audioOptions = { + position: position, + volume: .4, + loop: true +}; + +var sound = SoundCache.getSound(soundURL); +var injector = null; +var count = 300; + +Script.update.connect(function() { + if (count > 0) { + count--; + return; + } + + if (sound.downloaded && injector === null) { + print("Sound downloaded."); + injector = Audio.playSound(sound, audioOptions); + print("Playing: " + injector); + } +}); + +Script.scriptEnding.connect(function() { + if (injector !== null) { + injector.stop(); + } +}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/windmill_sound.js b/examples/toys/AC_scripts/windmill_sound.js new file mode 100644 index 0000000000..2072dbb9dd --- /dev/null +++ b/examples/toys/AC_scripts/windmill_sound.js @@ -0,0 +1,38 @@ +// Adds a windmill noise to toybox. +// By Ryan Karpf 10/20/2015 +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + + +var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/WINDMILL_Mono.wav"; +var position = { x: 530, y: 516, z: 518}; +var audioOptions = { + position: position, + volume: .08, + loop: true +}; + +var sound = SoundCache.getSound(soundURL); +var injector = null; +var count = 300; + +Script.update.connect(function() { + if (count > 0) { + count--; + return; + } + + if (sound.downloaded && injector === null) { + print("Sound downloaded."); + injector = Audio.playSound(sound, audioOptions); + print("Playing: " + injector); + } +}); + +Script.scriptEnding.connect(function() { + if (injector !== null) { + injector.stop(); + } +}); \ No newline at end of file From b0ae55ee63a7161142b300c130089225683fa7da Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 20 Oct 2015 11:28:04 -0700 Subject: [PATCH 2/6] update cat purr header --- examples/toys/AC_scripts/cat_purr_sound.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/examples/toys/AC_scripts/cat_purr_sound.js b/examples/toys/AC_scripts/cat_purr_sound.js index c02b14ebf8..0ac05a1974 100644 --- a/examples/toys/AC_scripts/cat_purr_sound.js +++ b/examples/toys/AC_scripts/cat_purr_sound.js @@ -1,14 +1,11 @@ -// -// ambiance.js -// examples -// -// Created by Clément Brisset on 11/18/14. -// Copyright 2014 High Fidelity, Inc. +// Adds a cat purring noise to Toybox. +// By Ryan Karpf 10/20/2015 // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // + var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/Cat_Purring_Deep_Low_Snor.wav"; var position = { x: 551.48, y: 495.60, z: 502.08}; var audioOptions = { From 1c5dc0374abd075342706f941e0c4450ca1a65d4 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 21 Oct 2015 10:16:31 -0700 Subject: [PATCH 3/6] Added wantsTrigger to user data for a couple items to avoid breakage with new grab script changes --- unpublishedScripts/hiddenEntityReset.js | 14 +++++++++----- unpublishedScripts/masterReset.js | 7 ++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/unpublishedScripts/hiddenEntityReset.js b/unpublishedScripts/hiddenEntityReset.js index fde97beeb5..19a7a5ee5e 100644 --- a/unpublishedScripts/hiddenEntityReset.js +++ b/unpublishedScripts/hiddenEntityReset.js @@ -54,7 +54,6 @@ MasterReset = function() { var resetKey = "resetMe"; - var GRABBABLE_DATA_KEY = "grabbableKey"; var HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; @@ -97,8 +96,6 @@ z: 505.78 }); - - createCombinedArmChair({ x: 549.29, y: 494.9, @@ -487,7 +484,6 @@ grabbableKey: { invertSolidWhileHeld: true } - }) }); @@ -533,6 +529,9 @@ resetMe: true, on: true, type: "Hall Light" + }, + grabbableKey: { + wantsTrigger: true } }) }); @@ -628,6 +627,9 @@ resetMe: true, on: true, type: "Garage Light" + }, + grabbableKey: { + wantsTrigger: true } }) }); @@ -759,6 +761,7 @@ grabbableKey: { invertSolidWhileHeld: true } + }) }; var dice1 = Entities.addEntity(diceProps); @@ -851,6 +854,7 @@ grabbableKey: { invertSolidWhileHeld: true } + }) }); } @@ -1176,7 +1180,7 @@ y: 0.05, z: 0.25 } - } ]; + }]; var modelURL, entity; for (i = 0; i < blockTypes.length; i++) { diff --git a/unpublishedScripts/masterReset.js b/unpublishedScripts/masterReset.js index 7316da57d1..54f29b4259 100644 --- a/unpublishedScripts/masterReset.js +++ b/unpublishedScripts/masterReset.js @@ -27,7 +27,6 @@ var targetsScriptURL = Script.resolvePath('../examples/toys/ping_pong_gun/wallTa MasterReset = function() { var resetKey = "resetMe"; - var GRABBABLE_DATA_KEY = "grabbableKey"; var HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; @@ -505,6 +504,9 @@ MasterReset = function() { resetMe: true, on: true, type: "Hall Light" + }, + grabbableKey: { + wantsTrigger: true } }) }); @@ -600,6 +602,9 @@ MasterReset = function() { resetMe: true, on: true, type: "Garage Light" + }, + grabbableKey: { + wantsTrigger: true } }) }); From 246e46b69a8e6556aea29cdcaf4b678962f481a7 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Wed, 21 Oct 2015 10:46:50 -0700 Subject: [PATCH 4/6] implement improvement in sphere test --- .../src/EntityItemPropertiesDefaults.h | 4 +- libraries/entities/src/EntityTreeElement.cpp | 44 ++++++++++++------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/libraries/entities/src/EntityItemPropertiesDefaults.h b/libraries/entities/src/EntityItemPropertiesDefaults.h index 538d7dd890..06c6565d1b 100644 --- a/libraries/entities/src/EntityItemPropertiesDefaults.h +++ b/libraries/entities/src/EntityItemPropertiesDefaults.h @@ -21,8 +21,8 @@ // There is a minor performance gain when comparing/copying an existing glm::vec3 rather than // creating a new one on the stack so we declare the ZERO_VEC3 constant as an optimization. const glm::vec3 ENTITY_ITEM_ZERO_VEC3 = glm::vec3(0.0f); -const glm::vec3 ENTITY_ITEM_ONE_VEC3 = glm::vec3(1.0f, 1.0f, 1.0f); -const glm::vec3 ENTITY_ITEM_HALF_VEC3 = ENTITY_ITEM_ONE_VEC3 / 2.0f; +const glm::vec3 ENTITY_ITEM_ONE_VEC3 = glm::vec3(1.0f); +const glm::vec3 ENTITY_ITEM_HALF_VEC3 = glm::vec3(0.5f); const bool ENTITY_ITEM_DEFAULT_LOCKED = false; const QString ENTITY_ITEM_DEFAULT_USER_DATA = QString(""); diff --git a/libraries/entities/src/EntityTreeElement.cpp b/libraries/entities/src/EntityTreeElement.cpp index 3ea6cfdbe8..57f49f2354 100644 --- a/libraries/entities/src/EntityTreeElement.cpp +++ b/libraries/entities/src/EntityTreeElement.cpp @@ -648,7 +648,6 @@ EntityItemPointer EntityTreeElement::getClosestEntity(glm::vec3 position) const // TODO: change this to use better bounding shape for entity than sphere void EntityTreeElement::getEntities(const glm::vec3& searchPosition, float searchRadius, QVector& foundEntities) const { - float compareRadius = searchRadius * searchRadius; forEachEntity([&](EntityItemPointer entity) { AABox entityBox = entity->getAABox(); @@ -657,26 +656,41 @@ void EntityTreeElement::getEntities(const glm::vec3& searchPosition, float searc glm::vec3 penetration; if (entityBox.findSpherePenetration(searchPosition, searchRadius, penetration)) { - // FIXME - handle entity->getShapeType() == SHAPE_TYPE_SPHERE case better + glm::vec3 dimensions = entity->getDimensions(); + // FIXME - consider allowing the entity to determine penetration so that // entities could presumably dull actuall hull testing if they wanted to + // FIXME - handle entity->getShapeType() == SHAPE_TYPE_SPHERE case better in particular + // can we handle the ellipsoid case better? We only currently handle perfect spheres + // with centered registration points + if (entity->getShapeType() == SHAPE_TYPE_SPHERE && + (dimensions.x == dimensions.y && dimensions.y == dimensions.z)) { - // determine the worldToEntityMatrix that doesn't include scale because - // we're going to use the registration aware aa box in the entity frame - glm::mat4 rotation = glm::mat4_cast(entity->getRotation()); - glm::mat4 translation = glm::translate(entity->getPosition()); - glm::mat4 entityToWorldMatrix = translation * rotation; - glm::mat4 worldToEntityMatrix = glm::inverse(entityToWorldMatrix); + // NOTE: entity->getRadius() doesn't return the true radius, it returns the radius of the + // maximum bounding sphere, which is actually larger than our actual radius + float entityTrueRadius = dimensions.x / 2.0f; - glm::vec3 dimensions = entity->getDimensions(); - glm::vec3 registrationPoint = entity->getRegistrationPoint(); - glm::vec3 corner = -(dimensions * registrationPoint); + if (findSphereSpherePenetration(searchPosition, searchRadius, + entity->getCenterPosition(), entityTrueRadius, penetration)) { + foundEntities.push_back(entity); + } + } else { + // determine the worldToEntityMatrix that doesn't include scale because + // we're going to use the registration aware aa box in the entity frame + glm::mat4 rotation = glm::mat4_cast(entity->getRotation()); + glm::mat4 translation = glm::translate(entity->getPosition()); + glm::mat4 entityToWorldMatrix = translation * rotation; + glm::mat4 worldToEntityMatrix = glm::inverse(entityToWorldMatrix); - AABox entityFrameBox(corner, dimensions); + glm::vec3 registrationPoint = entity->getRegistrationPoint(); + glm::vec3 corner = -(dimensions * registrationPoint); - glm::vec3 entityFrameSearchPosition = glm::vec3(worldToEntityMatrix * glm::vec4(searchPosition, 1.0f)); - if (entityFrameBox.findSpherePenetration(entityFrameSearchPosition, searchRadius, penetration)) { - foundEntities.push_back(entity); + AABox entityFrameBox(corner, dimensions); + + glm::vec3 entityFrameSearchPosition = glm::vec3(worldToEntityMatrix * glm::vec4(searchPosition, 1.0f)); + if (entityFrameBox.findSpherePenetration(entityFrameSearchPosition, searchRadius, penetration)) { + foundEntities.push_back(entity); + } } } }); From 68d43dc57661ae6aeeb4e8e1d03cfe07060a02c3 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 21 Oct 2015 10:50:37 -0700 Subject: [PATCH 5/6] reduce number of toybox sound scripts --- examples/toys/AC_scripts/arcade_game_sound.js | 52 ------- examples/toys/AC_scripts/cat_purr_sound.js | 38 ----- .../toys/AC_scripts/dogs_barking_sound.js | 52 ------- examples/toys/AC_scripts/fireplace_sound.js | 38 ----- examples/toys/AC_scripts/insects_sound.js | 39 ----- examples/toys/AC_scripts/river_water_sound.js | 37 ----- examples/toys/AC_scripts/toybox_sounds.js | 143 ++++++++++++++++++ examples/toys/AC_scripts/windmill_sound.js | 38 ----- 8 files changed, 143 insertions(+), 294 deletions(-) delete mode 100644 examples/toys/AC_scripts/arcade_game_sound.js delete mode 100644 examples/toys/AC_scripts/cat_purr_sound.js delete mode 100644 examples/toys/AC_scripts/dogs_barking_sound.js delete mode 100644 examples/toys/AC_scripts/fireplace_sound.js delete mode 100644 examples/toys/AC_scripts/insects_sound.js delete mode 100644 examples/toys/AC_scripts/river_water_sound.js create mode 100644 examples/toys/AC_scripts/toybox_sounds.js delete mode 100644 examples/toys/AC_scripts/windmill_sound.js diff --git a/examples/toys/AC_scripts/arcade_game_sound.js b/examples/toys/AC_scripts/arcade_game_sound.js deleted file mode 100644 index 0f158aa574..0000000000 --- a/examples/toys/AC_scripts/arcade_game_sound.js +++ /dev/null @@ -1,52 +0,0 @@ -// Adds arcade game noises to Toybox. -// By Ryan Karpf 10/20/2015 -// -// 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_URL = "http://hifi-public.s3.amazonaws.com/ryan/ARCADE_GAMES_VID.L.L.wav"; -var SOUND_POSITION = { x: 543.77, y: 495.07, z: 502.25 }; - -var MINUTE = 60 * 1000; -var PLAY_SOUND_INTERVAL = 1.5 * MINUTE; - -var audioOptions = { - position: SOUND_POSITION, - volume: .01, - loop: false, -}; - -var sound = SoundCache.getSound(SOUND_URL); -var injector = null; - -function playSound() { - print("Playing sound"); - if (injector) { - // try/catch in case the injector QObject has been deleted already - try { - injector.stop(); - } catch (e) { - } - } - injector = Audio.playSound(sound, audioOptions); -} - -function checkDownloaded() { - if (sound.downloaded) { - print("Sound downloaded."); - Script.clearInterval(checkDownloadedTimer); - Script.setInterval(playSound, PLAY_SOUND_INTERVAL); - playSound(); - } -} - -// Check once a second to see if the audio file has been downloaded -var checkDownloadedTimer = Script.setInterval(checkDownloaded, 1000); - -Script.scriptEnding.connect(function() { - if (injector) { - injector.stop(); - } -}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/cat_purr_sound.js b/examples/toys/AC_scripts/cat_purr_sound.js deleted file mode 100644 index 0ac05a1974..0000000000 --- a/examples/toys/AC_scripts/cat_purr_sound.js +++ /dev/null @@ -1,38 +0,0 @@ -// Adds a cat purring noise to Toybox. -// By Ryan Karpf 10/20/2015 -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - - -var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/Cat_Purring_Deep_Low_Snor.wav"; -var position = { x: 551.48, y: 495.60, z: 502.08}; -var audioOptions = { - position: position, - volume: .03, - loop: true -}; - -var sound = SoundCache.getSound(soundURL); -var injector = null; -var count = 300; - -Script.update.connect(function() { - if (count > 0) { - count--; - return; - } - - if (sound.downloaded && injector === null) { - print("Sound downloaded."); - injector = Audio.playSound(sound, audioOptions); - print("Playing: " + injector); - } -}); - -Script.scriptEnding.connect(function() { - if (injector !== null) { - injector.stop(); - } -}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/dogs_barking_sound.js b/examples/toys/AC_scripts/dogs_barking_sound.js deleted file mode 100644 index c362e1ac76..0000000000 --- a/examples/toys/AC_scripts/dogs_barking_sound.js +++ /dev/null @@ -1,52 +0,0 @@ -// Adds a dogs barking noise to Toybox. -// By Ryan Karpf 10/20/2015 -// -// 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_URL = "http://hifi-public.s3.amazonaws.com/ryan/dogs_barking_1.L.wav"; -var SOUND_POSITION = { x: 523, y: 495, z: 469 }; - -var MINUTE = 60 * 1000; -var PLAY_SOUND_INTERVAL = 1 * MINUTE; - -var audioOptions = { - position: SOUND_POSITION, - volume: .05, - loop: false, -}; - -var sound = SoundCache.getSound(SOUND_URL); -var injector = null; - -function playSound() { - print("Playing sound"); - if (injector) { - // try/catch in case the injector QObject has been deleted already - try { - injector.stop(); - } catch (e) { - } - } - injector = Audio.playSound(sound, audioOptions); -} - -function checkDownloaded() { - if (sound.downloaded) { - print("Sound downloaded."); - Script.clearInterval(checkDownloadedTimer); - Script.setInterval(playSound, PLAY_SOUND_INTERVAL); - playSound(); - } -} - -// Check once a second to see if the audio file has been downloaded -var checkDownloadedTimer = Script.setInterval(checkDownloaded, 1000); - -Script.scriptEnding.connect(function() { - if (injector) { - injector.stop(); - } -}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/fireplace_sound.js b/examples/toys/AC_scripts/fireplace_sound.js deleted file mode 100644 index 76a7153c4b..0000000000 --- a/examples/toys/AC_scripts/fireplace_sound.js +++ /dev/null @@ -1,38 +0,0 @@ -// Adds a fireplace noise to Toybox. -// By Ryan Karpf 10/20/2015 -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - - -var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/demo/0619_Fireplace__Tree_B.L.wav"; -var position = { x: 551.61, y: 494.88, z: 502.00}; -var audioOptions = { - position: position, - volume: .08, - loop: true -}; - -var sound = SoundCache.getSound(soundURL); -var injector = null; -var count = 300; - -Script.update.connect(function() { - if (count > 0) { - count--; - return; - } - - if (sound.downloaded && injector === null) { - print("Sound downloaded."); - injector = Audio.playSound(sound, audioOptions); - print("Playing: " + injector); - } -}); - -Script.scriptEnding.connect(function() { - if (injector !== null) { - injector.stop(); - } -}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/insects_sound.js b/examples/toys/AC_scripts/insects_sound.js deleted file mode 100644 index 1783413901..0000000000 --- a/examples/toys/AC_scripts/insects_sound.js +++ /dev/null @@ -1,39 +0,0 @@ -// Adds an insect noise to Toybox. -// By Ryan Karpf 10/20/2015 -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - - - -var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/insects3.wav"; -var position = { x: 560, y: 495, z: 474}; -var audioOptions = { - position: position, - volume: .25, - loop: true -}; - -var sound = SoundCache.getSound(soundURL); -var injector = null; -var count = 300; - -Script.update.connect(function() { - if (count > 0) { - count--; - return; - } - - if (sound.downloaded && injector === null) { - print("Sound downloaded."); - injector = Audio.playSound(sound, audioOptions); - print("Playing: " + injector); - } -}); - -Script.scriptEnding.connect(function() { - if (injector !== null) { - injector.stop(); - } -}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/river_water_sound.js b/examples/toys/AC_scripts/river_water_sound.js deleted file mode 100644 index 30df67b515..0000000000 --- a/examples/toys/AC_scripts/river_water_sound.js +++ /dev/null @@ -1,37 +0,0 @@ -// Adds a river water sound to Toybox. -// By Ryan Karpf 10/20/2015 -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/Water_Lap_River_Edge_Gentle.L.wav"; -var position = { x: 580, y: 493, z: 528}; -var audioOptions = { - position: position, - volume: .4, - loop: true -}; - -var sound = SoundCache.getSound(soundURL); -var injector = null; -var count = 300; - -Script.update.connect(function() { - if (count > 0) { - count--; - return; - } - - if (sound.downloaded && injector === null) { - print("Sound downloaded."); - injector = Audio.playSound(sound, audioOptions); - print("Playing: " + injector); - } -}); - -Script.scriptEnding.connect(function() { - if (injector !== null) { - injector.stop(); - } -}); \ No newline at end of file diff --git a/examples/toys/AC_scripts/toybox_sounds.js b/examples/toys/AC_scripts/toybox_sounds.js new file mode 100644 index 0000000000..87d2bcc75a --- /dev/null +++ b/examples/toys/AC_scripts/toybox_sounds.js @@ -0,0 +1,143 @@ +// +// toys/AC_scripts/toybox_sounds.js +// +// This script adds several sounds to the correct locations for toybox. +// By James B. Pollack @imgntn 10/21/2015 +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + + +var soundMap = [{ + name: 'river water', + url: "http://hifi-public.s3.amazonaws.com/ryan/Water_Lap_River_Edge_Gentle.L.wav", + audioOptions: { + position: { + x: 580, + y: 493, + z: 528 + }, + volume: 0.4, + loop: true + } +}, { + name: 'windmill', + url: "http://hifi-public.s3.amazonaws.com/ryan/WINDMILL_Mono.wav", + audioOptions: { + position: { + x: 530, + y: 516, + z: 518 + }, + volume: 0.08, + loop: true + } +}, { + name: 'insects', + url: "http://hifi-public.s3.amazonaws.com/ryan/insects3.wav", + audioOptions: { + position: { + x: 560, + y: 495, + z: 474 + }, + volume: 0.25, + loop: true + } +}, { + name: 'fireplace', + url: "http://hifi-public.s3.amazonaws.com/ryan/demo/0619_Fireplace__Tree_B.L.wav", + audioOptions: { + position: { + x: 551.61, + y: 494.88, + z: 502.00 + }, + volume: 0.25, + loop: true + } +}, { + name: 'cat purring', + url: "http://hifi-public.s3.amazonaws.com/ryan/Cat_Purring_Deep_Low_Snor.wav", + audioOptions: { + position: { + x: 551.48, + y: 495.60, + z: 502.08 + }, + volume: 0.25, + loop: true + } +}, { + name: 'dogs barking', + url: "http://hifi-public.s3.amazonaws.com/ryan/dogs_barking_1.L.wav", + audioOptions: { + position: { + x: 551.61, + y: 494.88, + z: 502.00 + }, + volume: 0.15, + loop: false + }, + playAtInterval: 60 * 1000 +}]; + +function loadSounds() { + soundMap.forEach(function(soundData) { + soundData.sound = SoundCache.getSound(soundData.url); + }); +} + + +function playSound(soundData) { + if (soundData.injector) { + // try/catch in case the injector QObject has been deleted already + try { + soundData.injector.stop(); + } catch (e) {} + } + soundData.injector = Audio.playSound(soundData.sound, soundData.audioOptions); +} + +function checkDownloaded(soundData) { + if (soundData.sound.downloaded) { + Script.clearInterval(soundData.downloadTimer); + if (soundData.hasOwnProperty('playAtInterval')) { + soundData.playingInterval = Script.setInterval(function() { + playSound(soundData); + }, soundData.playAtInterval); + } else { + playSound(soundData); + } + + } +} + +function startCheckDownloadedTimers() { + soundMap.forEach(function(soundData) { + soundData.downloadTimer = Script.setInterval(function() { + checkDownloaded(soundData) + }, 1000); + }); +} + +Script.scriptEnding.connect(function() { + soundMap.forEach(function(soundData) { + if (soundData.hasOwnProperty("injector")) { + soundData.injector.stop(); + } + if (soundData.hasOwnProperty("downloadTimer")) { + Script.clearInterval(soundData.downloadTimer); + + } + if (soundData.hasOwnProperty("playingInterval")) { + Script.clearInterval(soundData.playingInterval); + } + }); + +}); + +loadSounds(); +startCheckDownloadedTimers(); \ No newline at end of file diff --git a/examples/toys/AC_scripts/windmill_sound.js b/examples/toys/AC_scripts/windmill_sound.js deleted file mode 100644 index 2072dbb9dd..0000000000 --- a/examples/toys/AC_scripts/windmill_sound.js +++ /dev/null @@ -1,38 +0,0 @@ -// Adds a windmill noise to toybox. -// By Ryan Karpf 10/20/2015 -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - - -var soundURL = "http://hifi-public.s3.amazonaws.com/ryan/WINDMILL_Mono.wav"; -var position = { x: 530, y: 516, z: 518}; -var audioOptions = { - position: position, - volume: .08, - loop: true -}; - -var sound = SoundCache.getSound(soundURL); -var injector = null; -var count = 300; - -Script.update.connect(function() { - if (count > 0) { - count--; - return; - } - - if (sound.downloaded && injector === null) { - print("Sound downloaded."); - injector = Audio.playSound(sound, audioOptions); - print("Playing: " + injector); - } -}); - -Script.scriptEnding.connect(function() { - if (injector !== null) { - injector.stop(); - } -}); \ No newline at end of file From 0b06cf1dfbf990b1e6c786906b6f9189e9a1e21b Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 21 Oct 2015 10:53:32 -0700 Subject: [PATCH 6/6] cleanup --- examples/toys/AC_scripts/toybox_sounds.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/toys/AC_scripts/toybox_sounds.js b/examples/toys/AC_scripts/toybox_sounds.js index 87d2bcc75a..67985a5938 100644 --- a/examples/toys/AC_scripts/toybox_sounds.js +++ b/examples/toys/AC_scripts/toybox_sounds.js @@ -8,7 +8,6 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // - var soundMap = [{ name: 'river water', url: "http://hifi-public.s3.amazonaws.com/ryan/Water_Lap_River_Edge_Gentle.L.wav", @@ -90,7 +89,6 @@ function loadSounds() { }); } - function playSound(soundData) { if (soundData.injector) { // try/catch in case the injector QObject has been deleted already @@ -103,10 +101,12 @@ function playSound(soundData) { function checkDownloaded(soundData) { if (soundData.sound.downloaded) { + Script.clearInterval(soundData.downloadTimer); + if (soundData.hasOwnProperty('playAtInterval')) { soundData.playingInterval = Script.setInterval(function() { - playSound(soundData); + playSound(soundData) }, soundData.playAtInterval); } else { playSound(soundData); @@ -118,23 +118,26 @@ function checkDownloaded(soundData) { function startCheckDownloadedTimers() { soundMap.forEach(function(soundData) { soundData.downloadTimer = Script.setInterval(function() { - checkDownloaded(soundData) + checkDownloaded(soundData); }, 1000); }); } Script.scriptEnding.connect(function() { soundMap.forEach(function(soundData) { + if (soundData.hasOwnProperty("injector")) { soundData.injector.stop(); } + if (soundData.hasOwnProperty("downloadTimer")) { Script.clearInterval(soundData.downloadTimer); - } + if (soundData.hasOwnProperty("playingInterval")) { Script.clearInterval(soundData.playingInterval); } + }); });