rework audioMuteOverlay.js

This commit is contained in:
Seth Alves 2019-02-17 14:21:23 -08:00 committed by Wayne Chen
parent 7780db813d
commit d54d0280c2
3 changed files with 63 additions and 90 deletions

View file

@ -76,6 +76,7 @@ void Audio::setMuted(bool isMuted) {
withWriteLock([&] { withWriteLock([&] {
if (_isMuted != isMuted) { if (_isMuted != isMuted) {
_isMuted = isMuted; _isMuted = isMuted;
mutedSetting.set(_isMuted);
auto client = DependencyManager::get<AudioClient>().data(); auto client = DependencyManager::get<AudioClient>().data();
QMetaObject::invokeMethod(client, "setMuted", Q_ARG(bool, isMuted), Q_ARG(bool, false)); QMetaObject::invokeMethod(client, "setMuted", Q_ARG(bool, isMuted), Q_ARG(bool, false));
changed = true; changed = true;
@ -92,15 +93,6 @@ bool Audio::noiseReductionEnabled() const {
}); });
} }
void Audio::onMutedChanged() {
bool isMuted = DependencyManager::get<AudioClient>()->isMuted();
if (_isMuted != isMuted) {
_isMuted = isMuted;
mutedSetting.set(_isMuted);
emit mutedChanged(_isMuted);
}
}
void Audio::enableNoiseReduction(bool enable) { void Audio::enableNoiseReduction(bool enable) {
bool changed = false; bool changed = false;
withWriteLock([&] { withWriteLock([&] {

View file

@ -32,7 +32,8 @@ var DEFAULT_SCRIPTS_COMBINED = [
"system/firstPersonHMD.js", "system/firstPersonHMD.js",
"system/tablet-ui/tabletUI.js", "system/tablet-ui/tabletUI.js",
"system/emote.js", "system/emote.js",
"system/miniTablet.js" "system/miniTablet.js",
"system/audioMuteOverlay.js"
]; ];
var DEFAULT_SCRIPTS_SEPARATE = [ var DEFAULT_SCRIPTS_SEPARATE = [
"system/controllers/controllerScripts.js", "system/controllers/controllerScripts.js",

View file

@ -1,104 +1,84 @@
"use strict";
/* jslint vars: true, plusplus: true, forin: true*/
/* globals Tablet, Script, AvatarList, Users, Entities, MyAvatar, Camera, Overlays, Vec3, Quat, Controller, print, getControllerWorldLocation */
/* eslint indent: ["error", 4, { "outerIIFEBody": 0 }] */
// //
// audioMuteOverlay.js // audioMuteOverlay.js
// //
// client script that creates an overlay to provide mute feedback // client script that creates an overlay to provide mute feedback
// //
// Created by Triplelexx on 17/03/09 // Created by Triplelexx on 17/03/09
// Reworked by Seth Alves on 2019-2-17
// Copyright 2017 High Fidelity, Inc. // Copyright 2017 High Fidelity, Inc.
// //
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
"use strict";
/* global Audio, Script, Overlays, Quat, MyAvatar */
(function() { // BEGIN LOCAL_SCOPE (function() { // BEGIN LOCAL_SCOPE
var utilsPath = Script.resolvePath('../developer/libraries/utils.js');
Script.include(utilsPath);
var TWEEN_SPEED = 0.025; var lastInputLoudness = 0.0;
var MIX_AMOUNT = 0.25; var sampleRate = 8.0; // Hz
var attackTC = Math.exp(-1.0 / (sampleRate * 0.500)) // 500 milliseconds attack
var releaseTC = Math.exp(-1.0 / (sampleRate * 1.000)) // 1000 milliseconds release
var holdReset = 2.0 * sampleRate; // 2 seconds hold
var holdCount = 0;
var warningOverlayID = null;
var overlayPosition = Vec3.ZERO; function showWarning() {
var tweenPosition = 0; if (warningOverlayID) {
var startColor = { return;
red: 170, }
green: 170, warningOverlayID = Overlays.addOverlay("text3d", {
blue: 170 name: "Muted-Warning",
}; localPosition: { x: 0.2, y: -0.35, z: -1.0 },
var endColor = { localOrientation: Quat.fromVec3Degrees({ x: 0.0, y: 0.0, z: 0.0, w: 1.0 }),
red: 255, text: "Warning: you are muted",
green: 0, textAlpha: 1,
blue: 0 color: { red: 226, green: 51, blue: 77 },
}; backgroundAlpha: 0,
var overlayID; lineHeight: 0.042,
visible: true,
ignoreRayIntersection: true,
drawInFront: true,
grabbable: false,
parentID: MyAvatar.SELF_ID,
parentJointIndex: MyAvatar.getJointIndex("_CAMERA_MATRIX")
});
};
Script.update.connect(update); function hideWarning() {
Script.scriptEnding.connect(cleanup); if (!warningOverlayID) {
return;
}
Overlays.deleteOverlay(warningOverlayID);
warningOverlayID = null;
}
function update(dt) { function cleanup() {
if (!Audio.muted) { Overlays.deleteOverlay(warningOverlayID);
if (hasOverlay()) { }
deleteOverlay();
}
} else if (!hasOverlay()) {
createOverlay();
} else {
updateOverlay();
}
}
function getOffsetPosition() { Script.scriptEnding.connect(cleanup);
return Vec3.sum(Camera.position, Quat.getFront(Camera.orientation));
}
function createOverlay() { Script.setInterval(function() {
overlayPosition = getOffsetPosition();
overlayID = Overlays.addOverlay("sphere", {
position: overlayPosition,
rotation: Camera.orientation,
alpha: 0.9,
dimensions: 0.1,
solid: true,
ignoreRayIntersection: true
});
}
function hasOverlay() { var inputLoudness = Audio.inputLevel;
return Overlays.getProperty(overlayID, "position") !== undefined; var tc = (inputLoudness > lastInputLoudness) ? attackTC : releaseTC;
} inputLoudness += tc * (lastInputLoudness - inputLoudness);
lastInputLoudness = inputLoudness;
function updateOverlay() { if (Audio.muted && inputLoudness > 0.3) {
// increase by TWEEN_SPEED until completion holdCount = holdReset;
if (tweenPosition < 1) { } else {
tweenPosition += TWEEN_SPEED; holdCount = Math.max(holdCount - 1, 0);
} else { }
// after tween completion reset to zero and flip values to ping pong
tweenPosition = 0;
for (var component in startColor) {
var storedColor = startColor[component];
startColor[component] = endColor[component];
endColor[component] = storedColor;
}
}
// mix previous position with new and mix colors
overlayPosition = Vec3.mix(overlayPosition, getOffsetPosition(), MIX_AMOUNT);
Overlays.editOverlay(overlayID, {
color: colorMix(startColor, endColor, easeIn(tweenPosition)),
position: overlayPosition,
rotation: Camera.orientation
});
}
function deleteOverlay() { if (holdCount > 0) {
Overlays.deleteOverlay(overlayID); showWarning();
} } else {
hideWarning();
}
}, 1000.0 / sampleRate);
function cleanup() {
deleteOverlay();
Audio.muted.disconnect(onMuteToggled);
Script.update.disconnect(update);
}
}()); // END LOCAL_SCOPE }()); // END LOCAL_SCOPE