From bd4d16768440b2c636f48bfca9344078dd6c15af Mon Sep 17 00:00:00 2001 From: David Rowe Date: Thu, 29 May 2014 14:13:02 -0700 Subject: [PATCH] Make JavaScript audio samples able to be looped Add a "loop" property to AudioInjectorOptions and provide an example JavaScript file. --- examples/playSoundLoop.js | 43 ++++++++++++++++++++ libraries/audio/src/AudioInjector.cpp | 5 +++ libraries/audio/src/AudioInjectorOptions.cpp | 2 + libraries/audio/src/AudioInjectorOptions.h | 5 +++ 4 files changed, 55 insertions(+) create mode 100644 examples/playSoundLoop.js diff --git a/examples/playSoundLoop.js b/examples/playSoundLoop.js new file mode 100644 index 0000000000..86226468bc --- /dev/null +++ b/examples/playSoundLoop.js @@ -0,0 +1,43 @@ +// +// playSoundLoop.js +// examples +// +// Created by David Rowe on 5/29/14. +// Copyright 2014 High Fidelity, Inc. +// +// This example script plays a sound in a continuous loop. +// +// 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"); + +var soundPlaying = 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 scriptEnding() { + if (Audio.isInjectorPlaying(soundPlaying)) { + Audio.stopInjector(soundPlaying); + print("Stopped sound loop"); + } +} + +// Connect a call back that happens every frame +Script.scriptEnding.connect(scriptEnding); +Controller.keyPressEvent.connect(keyPressEvent); diff --git a/libraries/audio/src/AudioInjector.cpp b/libraries/audio/src/AudioInjector.cpp index 1fe9f1336f..129dc47bd0 100644 --- a/libraries/audio/src/AudioInjector.cpp +++ b/libraries/audio/src/AudioInjector.cpp @@ -88,6 +88,7 @@ void AudioInjector::injectAudio() { int currentSendPosition = 0; int numPreAudioDataBytes = injectAudioPacket.size(); + bool shouldLoop = _options.getLoop(); // loop to send off our audio in NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL byte chunks while (currentSendPosition < soundByteArray.size() && !_shouldStop) { @@ -120,6 +121,10 @@ void AudioInjector::injectAudio() { usleep(usecToSleep); } } + + if (shouldLoop && currentSendPosition == soundByteArray.size()) { + currentSendPosition = 0; + } } } diff --git a/libraries/audio/src/AudioInjectorOptions.cpp b/libraries/audio/src/AudioInjectorOptions.cpp index cd6b08f6c7..49f1571c98 100644 --- a/libraries/audio/src/AudioInjectorOptions.cpp +++ b/libraries/audio/src/AudioInjectorOptions.cpp @@ -15,6 +15,7 @@ AudioInjectorOptions::AudioInjectorOptions(QObject* parent) : QObject(parent), _position(0.0f, 0.0f, 0.0f), _volume(1.0f), + _loop(false), _orientation(glm::vec3(0.0f, 0.0f, 0.0f)), _loopbackAudioInterface(NULL) { @@ -24,6 +25,7 @@ AudioInjectorOptions::AudioInjectorOptions(QObject* parent) : AudioInjectorOptions::AudioInjectorOptions(const AudioInjectorOptions& other) { _position = other._position; _volume = other._volume; + _loop = other._loop; _orientation = other._orientation; _loopbackAudioInterface = other._loopbackAudioInterface; } diff --git a/libraries/audio/src/AudioInjectorOptions.h b/libraries/audio/src/AudioInjectorOptions.h index bbe3d57b08..b90deb93f1 100644 --- a/libraries/audio/src/AudioInjectorOptions.h +++ b/libraries/audio/src/AudioInjectorOptions.h @@ -26,6 +26,7 @@ class AudioInjectorOptions : public QObject { Q_PROPERTY(glm::vec3 position READ getPosition WRITE setPosition) Q_PROPERTY(float volume READ getVolume WRITE setVolume) + Q_PROPERTY(bool loop READ getLoop WRITE setLoop) public: AudioInjectorOptions(QObject* parent = 0); AudioInjectorOptions(const AudioInjectorOptions& other); @@ -36,6 +37,9 @@ public: float getVolume() const { return _volume; } void setVolume(float volume) { _volume = volume; } + float getLoop() const { return _loop; } + void setLoop(float loop) { _loop = loop; } + const glm::quat& getOrientation() const { return _orientation; } void setOrientation(const glm::quat& orientation) { _orientation = orientation; } @@ -45,6 +49,7 @@ public: private: glm::vec3 _position; float _volume; + bool _loop; glm::quat _orientation; AbstractAudioInterface* _loopbackAudioInterface; };