This commit is contained in:
Philip Rosedale 2014-11-17 17:19:08 -08:00
commit ce6464eaf0
11 changed files with 136 additions and 61 deletions

View file

@ -776,25 +776,8 @@ void AudioMixer::run() {
// if the stream should be muted, send mute packet
if (nodeData->getAvatarAudioStream()
&& shouldMute(nodeData->getAvatarAudioStream()->getQuietestFrameLoudness())) {
static const int TIME_BETWEEN_MUTES = 5; // in secs
if (usecTimestampNow() - nodeData->getAvatarAudioStream()->getLastMuted() >
TIME_BETWEEN_MUTES * USECS_PER_SECOND) {
int headerSize = numBytesForPacketHeaderGivenPacketType(PacketTypeMuteEnvironment);
int packetSize = headerSize + sizeof(glm::vec3) + sizeof(float);
// Fake data to force mute
glm::vec3 position = nodeData->getAvatarAudioStream()->getPosition();
float radius = 1.0f;
char* packet = (char*)malloc(packetSize);
populatePacketHeader(packet, PacketTypeMuteEnvironment);
memcpy(packet + headerSize, &position, sizeof(glm::vec3));
memcpy(packet + headerSize + sizeof(glm::vec3), &radius, sizeof(float));
nodeList->writeDatagram(packet, packetSize, node);
nodeData->getAvatarAudioStream()->setLastMutedNow();
free(packet);
}
QByteArray packet = byteArrayWithPopulatedHeader(PacketTypeNoisyMute);
nodeList->writeDatagram(packet, node);
}
if (node->getType() == NodeType::Agent && node->getActiveSocket()

View file

@ -14,8 +14,7 @@
#include "AvatarAudioStream.h"
AvatarAudioStream::AvatarAudioStream(bool isStereo, const InboundAudioStream::Settings& settings) :
PositionalAudioStream(PositionalAudioStream::Microphone, isStereo, settings),
_lastMuted(usecTimestampNow())
PositionalAudioStream(PositionalAudioStream::Microphone, isStereo, settings)
{
}

View file

@ -20,17 +20,12 @@ class AvatarAudioStream : public PositionalAudioStream {
public:
AvatarAudioStream(bool isStereo, const InboundAudioStream::Settings& settings);
qint64 getLastMuted() const { return _lastMuted; }
void setLastMutedNow() { _lastMuted = usecTimestampNow(); }
private:
// disallow copying of AvatarAudioStream objects
AvatarAudioStream(const AvatarAudioStream&);
AvatarAudioStream& operator= (const AvatarAudioStream&);
int parseStreamProperties(PacketType type, const QByteArray& packetAfterSeqNum, int& numAudioSamples);
qint64 _lastMuted;
};
#endif // hifi_AvatarAudioStream_h

View file

@ -2479,7 +2479,7 @@ function Tooltip() {
margin: this.margin,
text: "",
color: { red: 228, green: 228, blue: 228 },
alpha: 0.5,
alpha: 0.8,
visible: false
});
this.show = function (doShow) {

View file

@ -0,0 +1,80 @@
//
// movable.js
// examples/entityScripts
//
// Created by Brad Hefta-Gaub on 11/17/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
//
(function(){
this.entityID = null;
this.properties = null;
this.graboffset = null;
// Pr, Vr are respectively the Ray's Point of origin and Vector director
// Pp, Np are respectively the Plane's Point of origin and Normal vector
this.rayPlaneIntersection = function(Pr, Vr, Pp, Np) {
var d = -Vec3.dot(Pp, Np);
var t = -(Vec3.dot(Pr, Np) + d) / Vec3.dot(Vr, Np);
return Vec3.sum(Pr, Vec3.multiply(t, Vr));
};
// updates the piece position based on mouse input
this.updatePosition = function(mouseEvent) {
var pickRay = Camera.computePickRay(mouseEvent.x, mouseEvent.y)
var upVector = { x: 0, y: 1, z: 0 };
var intersection = this.rayPlaneIntersection(pickRay.origin, pickRay.direction,
this.properties.position, upVector);
var newPosition = Vec3.sum(intersection, this.graboffset);
Entities.editEntity(this.entityID, { position: newPosition });
};
this.grab = function(mouseEvent) {
// first calculate the offset
var pickRay = Camera.computePickRay(mouseEvent.x, mouseEvent.y)
var upVector = { x: 0, y: 1, z: 0 };
var intersection = this.rayPlaneIntersection(pickRay.origin, pickRay.direction,
this.properties.position, upVector);
this.graboffset = Vec3.subtract(this.properties.position, intersection);
this.updatePosition(mouseEvent);
};
this.move = function(mouseEvent) {
this.updatePosition(mouseEvent);
};
this.release = function(mouseEvent) {
this.updatePosition(mouseEvent);
};
// All callbacks start by updating the properties
this.updateProperties = function(entityID) {
if (this.entityID === null || !this.entityID.isKnownID) {
this.entityID = Entities.identifyEntity(entityID);
}
this.properties = Entities.getEntityProperties(this.entityID);
};
this.preload = function(entityID) {
this.updateProperties(entityID); // All callbacks start by updating the properties
};
this.clickDownOnEntity = function(entityID, mouseEvent) {
this.updateProperties(entityID); // All callbacks start by updating the properties
this.grab(mouseEvent);
};
this.holdingClickOnEntity = function(entityID, mouseEvent) {
this.updateProperties(entityID); // All callbacks start by updating the properties
this.move(mouseEvent);
};
this.clickReleaseOnEntity = function(entityID, mouseEvent) {
this.updateProperties(entityID); // All callbacks start by updating the properties
this.release(mouseEvent);
};
})

View file

@ -115,15 +115,15 @@ static const float NODE_KILLED_BLUE = 0.0f;
static const QString SNAPSHOT_EXTENSION = ".jpg";
static const float BILLBOARD_FIELD_OF_VIEW = 30.0f; // degrees
static const float BILLBOARD_DISTANCE = 5.0f; // meters
static const float BILLBOARD_DISTANCE = 5.56f; // meters
static const int MIRROR_VIEW_TOP_PADDING = 5;
static const int MIRROR_VIEW_LEFT_PADDING = 10;
static const int MIRROR_VIEW_WIDTH = 265;
static const int MIRROR_VIEW_HEIGHT = 215;
static const float MIRROR_FULLSCREEN_DISTANCE = 0.35f;
static const float MIRROR_REARVIEW_DISTANCE = 0.65f;
static const float MIRROR_REARVIEW_BODY_DISTANCE = 2.3f;
static const float MIRROR_FULLSCREEN_DISTANCE = 0.389f;
static const float MIRROR_REARVIEW_DISTANCE = 0.722f;
static const float MIRROR_REARVIEW_BODY_DISTANCE = 2.56f;
static const float MIRROR_FIELD_OF_VIEW = 30.0f;
static const quint64 TOO_LONG_SINCE_LAST_SEND_DOWNSTREAM_AUDIO_STATS = 1 * USECS_PER_SECOND;

View file

@ -99,6 +99,7 @@ Audio::Audio(QObject* parent) :
_muted(false),
_reverb(false),
_reverbOptions(&_scriptReverbOptions),
_gverbLocal(NULL),
_gverb(NULL),
_iconColor(1.0f),
_iconPulseTimeReference(usecTimestampNow()),
@ -504,12 +505,23 @@ bool Audio::switchOutputToAudioDevice(const QString& outputDeviceName) {
void Audio::initGverb() {
// Initialize a new gverb instance
_gverbLocal = gverb_new(_outputFormat.sampleRate(), _reverbOptions->getMaxRoomSize(), _reverbOptions->getRoomSize(),
_reverbOptions->getReverbTime(), _reverbOptions->getDamping(), _reverbOptions->getSpread(),
_reverbOptions->getInputBandwidth(), _reverbOptions->getEarlyLevel(),
_reverbOptions->getTailLevel());
_gverb = gverb_new(_outputFormat.sampleRate(), _reverbOptions->getMaxRoomSize(), _reverbOptions->getRoomSize(),
_reverbOptions->getReverbTime(), _reverbOptions->getDamping(), _reverbOptions->getSpread(),
_reverbOptions->getInputBandwidth(), _reverbOptions->getEarlyLevel(),
_reverbOptions->getTailLevel());
// Configure the instance (these functions are not super well named - they actually set several internal variables)
gverb_set_roomsize(_gverbLocal, _reverbOptions->getRoomSize());
gverb_set_revtime(_gverbLocal, _reverbOptions->getReverbTime());
gverb_set_damping(_gverbLocal, _reverbOptions->getDamping());
gverb_set_inputbandwidth(_gverbLocal, _reverbOptions->getInputBandwidth());
gverb_set_earlylevel(_gverbLocal, DB_CO(_reverbOptions->getEarlyLevel()));
gverb_set_taillevel(_gverbLocal, DB_CO(_reverbOptions->getTailLevel()));
gverb_set_roomsize(_gverb, _reverbOptions->getRoomSize());
gverb_set_revtime(_gverb, _reverbOptions->getReverbTime());
gverb_set_damping(_gverb, _reverbOptions->getDamping());
@ -565,25 +577,27 @@ void Audio::setReverbOptions(const AudioEffectOptions* options) {
}
}
void Audio::addReverb(int16_t* samplesData, int numSamples, QAudioFormat& audioFormat) {
float dryFraction = DB_CO(_reverbOptions->getDryLevel());
void Audio::addReverb(ty_gverb* gverb, int16_t* samplesData, int numSamples, QAudioFormat& audioFormat, bool noEcho) {
float wetFraction = DB_CO(_reverbOptions->getWetLevel());
float dryFraction = (noEcho) ? 0.0f : (1.0f - wetFraction);
float lValue,rValue;
for (int sample = 0; sample < numSamples; sample += audioFormat.channelCount()) {
// Run GVerb
float value = (float)samplesData[sample];
gverb_do(_gverb, value, &lValue, &rValue);
gverb_do(gverb, value, &lValue, &rValue);
// Mix, accounting for clipping, the left and right channels. Ignore the rest.
for (int j = sample; j < sample + audioFormat.channelCount(); j++) {
if (j == sample) {
// left channel
int lResult = glm::clamp((int)(samplesData[j] * dryFraction + lValue * wetFraction), MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
int lResult = glm::clamp((int)(samplesData[j] * dryFraction + lValue * wetFraction),
MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
samplesData[j] = (int16_t)lResult;
} else if (j == (sample + 1)) {
// right channel
int rResult = glm::clamp((int)(samplesData[j] * dryFraction + rValue * wetFraction), MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
int rResult = glm::clamp((int)(samplesData[j] * dryFraction + rValue * wetFraction),
MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
samplesData[j] = (int16_t)rResult;
} else {
// ignore channels above 2
@ -622,23 +636,10 @@ void Audio::handleLocalEchoAndReverb(QByteArray& inputByteArray) {
}
if (hasLocalReverb) {
QByteArray loopbackCopy;
if (!hasEcho) {
loopbackCopy = loopBackByteArray;
}
int16_t* loopbackSamples = reinterpret_cast<int16_t*>(loopBackByteArray.data());
int numLoopbackSamples = loopBackByteArray.size() / sizeof(int16_t);
updateGverbOptions();
addReverb(loopbackSamples, numLoopbackSamples, _outputFormat);
if (!hasEcho) {
int16_t* loopbackCopySamples = reinterpret_cast<int16_t*>(loopbackCopy.data());
for (int i = 0; i < numLoopbackSamples; ++i) {
loopbackSamples[i] = glm::clamp((int)loopbackSamples[i] - loopbackCopySamples[i],
MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
}
}
addReverb(_gverbLocal, loopbackSamples, numLoopbackSamples, _outputFormat, !hasEcho);
}
if (_loopbackOutputDevice) {
@ -1029,7 +1030,7 @@ void Audio::processReceivedSamples(const QByteArray& inputBuffer, QByteArray& ou
if(_reverb || _receivedAudioStream.hasReverb()) {
updateGverbOptions();
addReverb((int16_t*)outputBuffer.data(), numDeviceOutputSamples, _outputFormat);
addReverb(_gverb, (int16_t*)outputBuffer.data(), numDeviceOutputSamples, _outputFormat);
}
}

View file

@ -248,6 +248,7 @@ private:
AudioEffectOptions _scriptReverbOptions;
AudioEffectOptions _zoneReverbOptions;
AudioEffectOptions* _reverbOptions;
ty_gverb* _gverbLocal;
ty_gverb* _gverb;
GLuint _micTextureId;
GLuint _muteTextureId;
@ -269,7 +270,7 @@ private:
// Adds Reverb
void initGverb();
void updateGverbOptions();
void addReverb(int16_t* samples, int numSamples, QAudioFormat& format);
void addReverb(ty_gverb* gverb, int16_t* samples, int numSamples, QAudioFormat& format, bool noEcho = false);
void handleLocalEchoAndReverb(QByteArray& inputByteArray);

View file

@ -141,17 +141,29 @@ void DatagramProcessor::processDatagrams() {
AccountManager::getInstance().checkAndSignalForAccessToken();
break;
}
case PacketTypeNoisyMute:
case PacketTypeMuteEnvironment: {
glm::vec3 position;
float radius;
bool mute = !Application::getInstance()->getAudio()->getMuted();
int headerSize = numBytesForPacketHeaderGivenPacketType(PacketTypeMuteEnvironment);
memcpy(&position, incomingPacket.constData() + headerSize, sizeof(glm::vec3));
memcpy(&radius, incomingPacket.constData() + headerSize + sizeof(glm::vec3), sizeof(float));
if (incomingType == PacketTypeMuteEnvironment) {
glm::vec3 position;
float radius, distance;
int headerSize = numBytesForPacketHeaderGivenPacketType(PacketTypeMuteEnvironment);
memcpy(&position, incomingPacket.constData() + headerSize, sizeof(glm::vec3));
memcpy(&radius, incomingPacket.constData() + headerSize + sizeof(glm::vec3), sizeof(float));
distance = glm::distance(Application::getInstance()->getAvatar()->getPosition(), position);
mute = mute && (distance < radius);
}
if (glm::distance(Application::getInstance()->getAvatar()->getPosition(), position) < radius
&& !Application::getInstance()->getAudio()->getMuted()) {
if (mute) {
Application::getInstance()->getAudio()->toggleMute();
if (incomingType == PacketTypeMuteEnvironment) {
AudioScriptingInterface::getInstance().environmentMuted();
} else {
AudioScriptingInterface::getInstance().mutedByMixer();
}
}
break;
}

View file

@ -37,6 +37,10 @@ public slots:
void injectorStopped();
signals:
void mutedByMixer();
void environmentMuted();
private:
AudioScriptingInterface();
QList< QPointer<AudioInjector> > _activeInjectors;

View file

@ -54,7 +54,7 @@ enum PacketType {
UNUSED_2,
UNUSED_3,
UNUSED_4,
UNUSED_5,
PacketTypeNoisyMute,
PacketTypeMetavoxelData,
PacketTypeAvatarIdentity,
PacketTypeAvatarBillboard,