Fixing raw sound playback and air guitar

This commit is contained in:
Brad Davis 2015-10-22 22:37:18 -07:00
parent 91804fbc04
commit 63df9fb959
2 changed files with 41 additions and 39 deletions

View file

@ -90,7 +90,7 @@ var audioInjector = null;
var selectorPressed = false; var selectorPressed = false;
var position; var position;
MyAvatar.attach(guitarModel, "Hips", {x: -0.2, y: 0.0, z: 0.1}, Quat.fromPitchYawRollDegrees(90, 00, 90), 1.0); MyAvatar.attach(guitarModel, "Hips", {x: leftHanded ? -0.2 : 0.2, y: 0.0, z: 0.1}, Quat.fromPitchYawRollDegrees(90, 00, leftHanded ? 75 : -75), 1.0);
function checkHands(deltaTime) { function checkHands(deltaTime) {
var strumVelocity = Controller.getPoseValue(strumHand).velocity; var strumVelocity = Controller.getPoseValue(strumHand).velocity;
@ -114,27 +114,32 @@ function checkHands(deltaTime) {
// Change guitars if button FWD (5) pressed // Change guitars if button FWD (5) pressed
if (Controller.getValue(changeGuitar)) { if (Controller.getValue(changeGuitar)) {
print("changeGuitar:" + changeGuitar);
if (!selectorPressed) { if (!selectorPressed) {
print("changeGuitar:" + changeGuitar);
guitarSelector += NUM_CHORDS; guitarSelector += NUM_CHORDS;
if (guitarSelector >= NUM_CHORDS * NUM_GUITARS) { if (guitarSelector >= NUM_CHORDS * NUM_GUITARS) {
guitarSelector = 0; guitarSelector = 0;
} }
print("new guitarBase: " + guitarSelector);
stopAudio(true);
selectorPressed = true; selectorPressed = true;
} }
} else { } else {
selectorPressed = false; selectorPressed = false;
} }
//print("selectorPressed:" + selectorPressed);
if (Controller.getValue(chord1)) { if (Controller.getValue(chord1)) {
whichChord = 1; whichChord = 1;
stopAudio(true);
} else if (Controller.getValue(chord2)) { } else if (Controller.getValue(chord2)) {
whichChord = 2; whichChord = 2;
stopAudio(true);
} else if (Controller.getValue(chord3)) { } else if (Controller.getValue(chord3)) {
whichChord = 3; whichChord = 3;
stopAudio(true);
} else if (Controller.getValue(chord4)) { } else if (Controller.getValue(chord4)) {
whichChord = 4; whichChord = 4;
stopAudio(true);
} }
var STRUM_HEIGHT_ABOVE_PELVIS = 0.10; var STRUM_HEIGHT_ABOVE_PELVIS = 0.10;
@ -154,26 +159,27 @@ function checkHands(deltaTime) {
lastPosition = strumHandPosition; lastPosition = strumHandPosition;
} }
function playChord(position, volume) { function stopAudio(killInjector) {
if (audioInjector && audioInjector.isPlaying) { if (audioInjector && audioInjector.isPlaying) {
print("stopped sound"); print("stopped sound");
audioInjector.stop(); audioInjector.stop();
} }
if (killInjector) {
audioInjector = null;
}
}
function playChord(position, volume) {
stopAudio();
print("Played sound: " + whichChord + " at volume " + volume); print("Played sound: " + whichChord + " at volume " + volume);
if (!audioInjector) { if (!audioInjector) {
var index = guitarSelector + whichChord;
// FIXME - we apparenlty broke RAW file playback, so we need WAV files for all these chords. In the mean var chord = chords[guitarSelector + whichChord];
// time, we will just play the heyMan wave file for all chords
var chord = heyManWave; // chords[guitarSelector + whichChord];
audioInjector = Audio.playSound(chord, { position: position, volume: volume }); audioInjector = Audio.playSound(chord, { position: position, volume: volume });
print("audioInjector: " + JSON.stringify(audioInjector));
} else { } else {
print("audioInjector: " + JSON.stringify(audioInjector));
audioInjector.restart(); audioInjector.restart();
} }
} }
function keyPressEvent(event) { function keyPressEvent(event) {
@ -181,15 +187,19 @@ function keyPressEvent(event) {
keyVolume = 0.4; keyVolume = 0.4;
if (event.text == "1") { if (event.text == "1") {
whichChord = 1; whichChord = 1;
stopAudio(true);
playChord(MyAvatar.position, keyVolume); playChord(MyAvatar.position, keyVolume);
} else if (event.text == "2") { } else if (event.text == "2") {
whichChord = 2; whichChord = 2;
stopAudio(true);
playChord(MyAvatar.position, keyVolume); playChord(MyAvatar.position, keyVolume);
} else if (event.text == "3") { } else if (event.text == "3") {
whichChord = 3; whichChord = 3;
stopAudio(true);
playChord(MyAvatar.position, keyVolume); playChord(MyAvatar.position, keyVolume);
} else if (event.text == "4") { } else if (event.text == "4") {
whichChord = 4; whichChord = 4;
stopAudio(true);
playChord(MyAvatar.position, keyVolume); playChord(MyAvatar.position, keyVolume);
} }
} }
@ -197,6 +207,7 @@ function keyPressEvent(event) {
function scriptEnding() { function scriptEnding() {
MyAvatar.detachOne(guitarModel); MyAvatar.detachOne(guitarModel);
} }
// Connect a call back that happens every frame // Connect a call back that happens every frame
Script.update.connect(checkHands); Script.update.connect(checkHands);
Script.scriptEnding.connect(scriptEnding); Script.scriptEnding.connect(scriptEnding);

View file

@ -59,39 +59,30 @@ Sound::Sound(const QUrl& url, bool isStereo) :
void Sound::downloadFinished(const QByteArray& data) { void Sound::downloadFinished(const QByteArray& data) {
// replace our byte array with the downloaded data // replace our byte array with the downloaded data
QByteArray rawAudioByteArray = QByteArray(data); QByteArray rawAudioByteArray = QByteArray(data);
QString fileName = getURL().fileName(); QString fileName = getURL().fileName().toLower();
const QString WAV_EXTENSION = ".wav";
static const QString WAV_EXTENSION = ".wav";
static const QString RAW_EXTENSION = ".raw";
if (fileName.endsWith(WAV_EXTENSION)) { if (fileName.endsWith(WAV_EXTENSION)) {
QString headerContentType = "audio/x-wav"; QByteArray outputAudioByteArray;
//QByteArray headerContentType = reply->rawHeader("Content-Type");
// WAV audio file encountered interpretAsWav(rawAudioByteArray, outputAudioByteArray);
if (headerContentType == "audio/x-wav" downSample(outputAudioByteArray);
|| headerContentType == "audio/wav" trimFrames();
|| headerContentType == "audio/wave" } else if (fileName.endsWith(RAW_EXTENSION)) {
|| fileName.endsWith(WAV_EXTENSION)) { // check if this was a stereo raw file
// since it's raw the only way for us to know that is if the file was called .stereo.raw
QByteArray outputAudioByteArray; if (fileName.toLower().endsWith("stereo.raw")) {
_isStereo = true;
interpretAsWav(rawAudioByteArray, outputAudioByteArray); qCDebug(audio) << "Processing sound of" << rawAudioByteArray.size() << "bytes from" << getURL() << "as stereo audio file.";
downSample(outputAudioByteArray);
} else {
// check if this was a stereo raw file
// since it's raw the only way for us to know that is if the file was called .stereo.raw
if (fileName.toLower().endsWith("stereo.raw")) {
_isStereo = true;
qCDebug(audio) << "Processing sound of" << rawAudioByteArray.size() << "bytes from" << getURL() << "as stereo audio file.";
}
// Process as RAW file
downSample(rawAudioByteArray);
} }
// Process as RAW file
downSample(rawAudioByteArray);
trimFrames(); trimFrames();
} else { } else {
qCDebug(audio) << "Network reply without 'Content-Type'."; qCDebug(audio) << "Unknown sound file type";
} }
_isReady = true; _isReady = true;