From 41e9017953a2261774c8ec50ee1fe12fbb355c85 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Thu, 27 Mar 2014 15:14:38 -0700 Subject: [PATCH] more audio level meter work, improved gun --- examples/gun.js | 36 ++++++++++++++++++++++++++--------- interface/src/Application.cpp | 12 ++++++++++-- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/examples/gun.js b/examples/gun.js index 94f3fd4ee3..e358e6b391 100644 --- a/examples/gun.js +++ b/examples/gun.js @@ -27,6 +27,9 @@ var BULLET_VELOCITY = 5.0; var MIN_THROWER_DELAY = 1000; var MAX_THROWER_DELAY = 1000; var LEFT_BUTTON_3 = 3; +var RELOAD_INTERVAL = 9; + +var showScore = false; // Load some sound to use for loading and firing var fireSound = new Sound("https://s3-us-west-1.amazonaws.com/highfidelity-public/sounds/Guns/GUN-SHOT2.raw"); @@ -38,6 +41,8 @@ var targetLaunchSound = new Sound("http://highfidelity-public.s3-us-west-1.amazo var audioOptions = new AudioInjectionOptions(); audioOptions.volume = 0.9; +var shotsFired = 0; + var shotTime = new Date(); // initialize our triggers @@ -63,7 +68,8 @@ var reticle = Overlays.addOverlay("image", { alpha: 1 }); -var text = Overlays.addOverlay("text", { +if (showScore) { + var text = Overlays.addOverlay("text", { x: screenSize.x / 2 - 100, y: screenSize.y / 2 - 50, width: 150, @@ -74,6 +80,8 @@ var text = Overlays.addOverlay("text", { leftMargin: 4, text: "Score: " + score }); +} + function printVector(string, vector) { @@ -94,6 +102,10 @@ function shootBullet(position, velocity) { // Play firing sounds audioOptions.position = position; Audio.playSound(fireSound, audioOptions); + shotsFired++; + if ((shotsFired % RELOAD_INTERVAL) == 0) { + Audio.playSound(loadSound, audioOptions); + } } function shootTarget() { @@ -147,12 +159,15 @@ function particleCollisionWithVoxel(particle, voxel, penetration) { Voxels.eraseVoxel(position.x, position.y, position.z, HOLE_SIZE); //audioOptions.position = position; audioOptions.position = Vec3.sum(Camera.getPosition(), Quat.getFront(Camera.getOrientation())); - Audio.playSound(targetHitSound, audioOptions); + Audio.playSound(impactSound, audioOptions); } function particleCollisionWithParticle(particle1, particle2) { score++; - Overlays.editOverlay(text, { text: "Score: " + score } ); + if (showScore) { + Overlays.editOverlay(text, { text: "Score: " + score } ); + } + // Sort out which particle is which // Record shot time @@ -171,12 +186,12 @@ function keyPressEvent(event) { if (event.text == "t") { var time = MIN_THROWER_DELAY + Math.random() * MAX_THROWER_DELAY; Script.setTimeout(shootTarget, time); + } if (event.text == ".") { + shootFromMouse(); } } function update(deltaTime) { - - // Check for mouseLook movement, update rotation // rotate body yaw for yaw received from mouse var newOrientation = Quat.multiply(MyAvatar.orientation, Quat.fromVec3Radians( { x: 0, y: yawFromMouse, z: 0 } )); @@ -257,18 +272,21 @@ function mousePressEvent(event) { isMouseDown = true; lastX = event.x; lastY = event.y; - audioOptions.position = Vec3.sum(Camera.getPosition(), Quat.getFront(Camera.getOrientation())); - Audio.playSound(loadSound, audioOptions); + //audioOptions.position = Vec3.sum(Camera.getPosition(), Quat.getFront(Camera.getOrientation())); + //Audio.playSound(loadSound, audioOptions); } -function mouseReleaseEvent(event) { - // position +function shootFromMouse() { var DISTANCE_FROM_CAMERA = 2.0; var camera = Camera.getPosition(); var forwardVector = Quat.getFront(Camera.getOrientation()); var newPosition = Vec3.sum(camera, Vec3.multiply(forwardVector, DISTANCE_FROM_CAMERA)); var velocity = Vec3.multiply(forwardVector, BULLET_VELOCITY); shootBullet(newPosition, velocity); +} + +function mouseReleaseEvent(event) { + // position isMouseDown = false; } diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 864be460e7..c67f992536 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2503,14 +2503,22 @@ void Application::displayOverlay() { } } - const int AUDIO_METER_WIDTH = 300; + const int AUDIO_METER_WIDTH = 200; const int AUDIO_METER_INSET = 2; const int AUDIO_METER_SCALE_WIDTH = AUDIO_METER_WIDTH - 2 * AUDIO_METER_INSET; const int AUDIO_METER_HEIGHT = 8; const int AUDIO_METER_Y = _glWidget->height() - 40; const int AUDIO_METER_X = 25; const float CLIPPING_INDICATOR_TIME = 1.0f; - float audioLevel = log10(_audio.getLastInputLoudness() + 1.0) / log10(32767.0) * (float)AUDIO_METER_SCALE_WIDTH; + const float LOG2 = log(2.f); + float audioLevel = 0.f; + float loudness = _audio.getLastInputLoudness() + 1.f; + float log2loudness = log(loudness) / LOG2; + if (loudness < 2048) { + audioLevel = (log2loudness / 11.f) * (AUDIO_METER_SCALE_WIDTH / 5.f); + } else { + audioLevel = (log2loudness - 10.f) * (AUDIO_METER_SCALE_WIDTH / 5.f); + } bool isClipping = ((_audio.getTimeSinceLastClip() > 0.f) && (_audio.getTimeSinceLastClip() < CLIPPING_INDICATOR_TIME)); if (isClipping) {