From aa47043d5752c16a8685ba48314ac2d2b9909145 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Tue, 27 May 2014 11:24:48 -0700 Subject: [PATCH 1/2] Fix WAV file support The previous implementation assumed the file only contained RIFF, WAVE, fmt, and data chunks. It is valid for other chunks to appear, so I updated it to skip any chunks until it finds the "data" chunk. --- libraries/audio/src/Sound.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/libraries/audio/src/Sound.cpp b/libraries/audio/src/Sound.cpp index 5bd63b7959..9bc78f2303 100644 --- a/libraries/audio/src/Sound.cpp +++ b/libraries/audio/src/Sound.cpp @@ -237,27 +237,30 @@ void Sound::interpretAsWav(const QByteArray& inputAudioByteArray, QByteArray& ou return; } + // Skip any extra data in the WAVE chunk + waveStream.skipRawData(fileHeader.wave.descriptor.size - (sizeof(WAVEHeader) - sizeof(chunk))); + // Read off remaining header information DATAHeader dataHeader; - if (waveStream.readRawData(reinterpret_cast(&dataHeader), sizeof(DATAHeader)) == sizeof(DATAHeader)) { - if (strncmp(dataHeader.descriptor.id, "data", 4) != 0) { - qDebug() << "Invalid wav audio data header."; + while (true) { + // Read chunks until the "data" chunk is found + if (waveStream.readRawData(reinterpret_cast(&dataHeader), sizeof(DATAHeader)) == sizeof(DATAHeader)) { + if (strncmp(dataHeader.descriptor.id, "data", 4) == 0) { + break; + } + waveStream.skipRawData(dataHeader.descriptor.size); + } else { + qDebug() << "Could not read wav audio data header."; return; } - } else { - qDebug() << "Could not read wav audio data header."; - return; - } - - if (qFromLittleEndian(fileHeader.riff.descriptor.size) != qFromLittleEndian(dataHeader.descriptor.size) + 36) { - qDebug() << "Did not read audio file chank headers correctly."; - return; } // Now pull out the data quint32 outputAudioByteArraySize = qFromLittleEndian(dataHeader.descriptor.size); outputAudioByteArray.resize(outputAudioByteArraySize); - waveStream.readRawData(outputAudioByteArray.data(), outputAudioByteArraySize); + if (waveStream.readRawData(outputAudioByteArray.data(), outputAudioByteArraySize) != outputAudioByteArraySize) { + qDebug() << "Error reading WAV file"; + } } else { qDebug() << "Could not read wav audio file header."; From ca6cc86ca124122dcfda7909b0d4cdd9e605fb89 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Tue, 27 May 2014 11:35:00 -0700 Subject: [PATCH 2/2] Add playSoundWave.js --- examples/playSoundWave.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 examples/playSoundWave.js diff --git a/examples/playSoundWave.js b/examples/playSoundWave.js new file mode 100644 index 0000000000..419fd80cd2 --- /dev/null +++ b/examples/playSoundWave.js @@ -0,0 +1,22 @@ +// +// playSoundWave.js +// examples +// +// Created by Ryan Huffman on 05/27/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 soundClip = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Cocktail%20Party%20Snippets/Walken1.wav"); + +function playSound() { + var options = new AudioInjectionOptions(); + var position = MyAvatar.position; + options.position = position; + options.volume = 0.5; + Audio.playSound(soundClip, options); +} + +Script.setInterval(playSound, 10000);