From 987b75d8f7181e6e82ff7929afb18c881d6656db Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 19 Nov 2014 14:52:57 -0800 Subject: [PATCH] switch to next musak when finished --- examples/lobby.js | 55 ++++++++++++++----- libraries/audio/src/AudioInjector.cpp | 5 +- .../audio/src/AudioInjectorLocalBuffer.cpp | 5 ++ .../audio/src/AudioInjectorLocalBuffer.h | 2 + 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/examples/lobby.js b/examples/lobby.js index 3e74cb0136..2e7949a110 100644 --- a/examples/lobby.js +++ b/examples/lobby.js @@ -44,7 +44,8 @@ var currentDrone = null; var latinSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Lobby/latin.stereo.raw") var elevatorSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Lobby/elevator.stereo.raw") -var currentMusak = null; +var currentMusakInjector = null; +var currentSound = null; function reticlePosition() { var RETICLE_DISTANCE = 1; @@ -125,23 +126,42 @@ function changeLobbyTextures() { Overlays.editOverlay(panelWall, textureProp); } +var MUSAK_VOLUME = 0.5; + +function playNextMusak() { + if (panelWall) { + print("PLAYING THE NEXT MUSAK!"); + if (currentSound == latinSound) { + if (elevatorSound.downloaded) { + currentSound = elevatorSound; + } + } else if (currentSound == elevatorSound) { + if (latinSound.downloaded) { + currentSound = latinSound; + } + } + + currentMusakInjector = Audio.playSound(currentSound, { localOnly: true, volume: MUSAK_VOLUME }); + } +} + function playRandomMusak() { - chosenSound = null; + currentSound = null; if (latinSound.downloaded && elevatorSound.downloaded) { - chosenSound = Math.random < 0.5 ? latinSound : elevatorSound; + currentSound = Math.random() < 0.5 ? latinSound : elevatorSound; } else if (latinSound.downloaded) { - chosenSound = latinSound; + currentSound = latinSound; } else if (elevatorSound.downloaded) { - chosenSound = elevatorSound; + currentSound = elevatorSound; } - if (chosenSound) { - // pick a random number of seconds from 0-10 to offset the muzak + if (currentSound) { + // pick a random number of seconds from 0-10 to offset the musak var secondOffset = Math.random() * 10; - currentMusak = Audio.playSound(chosenSound, { localOnly: true, secondOffset: secondOffset, volume: 0.5 }) + currentMusakInjector = Audio.playSound(currentSound, { localOnly: true, secondOffset: secondOffset, volume: MUSAK_VOLUME }); } else { - currentMusak = null; + currentMusakInjector = null; } } @@ -150,16 +170,16 @@ function cleanupLobby() { Overlays.deleteOverlay(orbShell); Overlays.deleteOverlay(reticle); - Audio.stopInjector(currentDrone); - currentDrone = null; - - Audio.stopInjector(currentMusak); - currentMusak = null; - panelWall = false; orbShell = false; reticle = false; + Audio.stopInjector(currentDrone); + currentDrone = null; + + Audio.stopInjector(currentMusakInjector); + currentMusakInjector = null; + locations = {}; toggleEnvironmentRendering(true); @@ -224,6 +244,11 @@ function update(deltaTime) { Overlays.editOverlay(reticle, { position: reticlePosition() }); + + // if the reticle is up then we may need to play the next musak + if (!Audio.isInjectorPlaying(currentMusakInjector)) { + playNextMusak(); + } } } diff --git a/libraries/audio/src/AudioInjector.cpp b/libraries/audio/src/AudioInjector.cpp index d5f82bbdd4..d31ab83976 100644 --- a/libraries/audio/src/AudioInjector.cpp +++ b/libraries/audio/src/AudioInjector.cpp @@ -86,8 +86,6 @@ void AudioInjector::injectAudio() { int byteOffset = (int) floorf(SAMPLE_RATE * _options.secondOffset * (_options.stereo ? 2.0f : 1.0f)); byteOffset *= sizeof(int16_t); - qDebug() << "Changing current send position to" << byteOffset; - _currentSendPosition = byteOffset; } @@ -117,7 +115,8 @@ void AudioInjector::injectLocally() { Q_ARG(qreal, _options.volume), Q_ARG(AudioInjector*, this)); - + // if we're not looping and the buffer tells us it is empty then emit finished + connect(_localBuffer, &AudioInjectorLocalBuffer::bufferEmpty, this, &AudioInjector::stop); if (!success) { qDebug() << "AudioInjector::injectLocally could not output locally via _localAudioInterface"; diff --git a/libraries/audio/src/AudioInjectorLocalBuffer.cpp b/libraries/audio/src/AudioInjectorLocalBuffer.cpp index bdf084091d..a58d686498 100644 --- a/libraries/audio/src/AudioInjectorLocalBuffer.cpp +++ b/libraries/audio/src/AudioInjectorLocalBuffer.cpp @@ -47,6 +47,11 @@ qint64 AudioInjectorLocalBuffer::readData(char* data, qint64 maxSize) { _currentOffset += bytesRead; } + if (!_shouldLoop && bytesRead == bytesToEnd) { + // we hit the end of the buffer, emit a signal + emit bufferEmpty(); + } + return bytesRead; } else { return 0; diff --git a/libraries/audio/src/AudioInjectorLocalBuffer.h b/libraries/audio/src/AudioInjectorLocalBuffer.h index 92409617f6..399c7515ec 100644 --- a/libraries/audio/src/AudioInjectorLocalBuffer.h +++ b/libraries/audio/src/AudioInjectorLocalBuffer.h @@ -27,6 +27,8 @@ public: void setShouldLoop(bool shouldLoop) { _shouldLoop = shouldLoop; } void setCurrentOffset(int currentOffset) { _currentOffset = currentOffset; } +signals: + void bufferEmpty(); private: qint64 recursiveReadFromFront(char* data, qint64 maxSize);