move audio toolbox into its own class

This commit is contained in:
Stephen Birarda 2014-12-16 15:24:57 -08:00
parent d9ab673442
commit 369fdc5771
7 changed files with 159 additions and 114 deletions

View file

@ -51,9 +51,6 @@
static const int NUMBER_OF_NOISE_SAMPLE_FRAMES = 300;
// Mute icon configration
static const int MUTE_ICON_SIZE = 24;
static const int RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES = 100;
Audio::Audio() :
@ -98,8 +95,6 @@ Audio::Audio() :
_reverbOptions(&_scriptReverbOptions),
_gverbLocal(NULL),
_gverb(NULL),
_iconColor(1.0f),
_iconPulseTimeReference(usecTimestampNow()),
_noiseSourceEnabled(false),
_toneSourceEnabled(true),
_outgoingAvatarAudioSequenceNumber(0),
@ -117,12 +112,6 @@ Audio::Audio() :
initGverb();
}
void Audio::init(QGLWidget *parent) {
_micTextureId = parent->bindTexture(QImage(Application::resourcesPath() + "images/mic.svg"));
_muteTextureId = parent->bindTexture(QImage(Application::resourcesPath() + "images/mic-mute.svg"));
_boxTextureId = parent->bindTexture(QImage(Application::resourcesPath() + "images/audio-box.svg"));
}
void Audio::reset() {
_receivedAudioStream.reset();
_stats.reset();
@ -962,14 +951,6 @@ void Audio::parseAudioEnvironmentData(const QByteArray &packet) {
}
}
bool Audio::mousePressEvent(int x, int y) {
if (_iconBounds.contains(x, y)) {
toggleMute();
return true;
}
return false;
}
void Audio::toggleMute() {
_muted = !_muted;
muteToggled();
@ -1160,85 +1141,6 @@ bool Audio::outputLocalInjector(bool isStereo, qreal volume, AudioInjector* inje
return false;
}
void Audio::renderToolBox(int x, int y, bool boxed) {
glEnable(GL_TEXTURE_2D);
if (boxed) {
bool isClipping = ((getTimeSinceLastClip() > 0.0f) && (getTimeSinceLastClip() < 1.0f));
const int BOX_LEFT_PADDING = 5;
const int BOX_TOP_PADDING = 10;
const int BOX_WIDTH = 266;
const int BOX_HEIGHT = 44;
QRect boxBounds = QRect(x - BOX_LEFT_PADDING, y - BOX_TOP_PADDING, BOX_WIDTH, BOX_HEIGHT);
glBindTexture(GL_TEXTURE_2D, _boxTextureId);
if (isClipping) {
glColor3f(1.0f, 0.0f, 0.0f);
} else {
glColor3f(0.41f, 0.41f, 0.41f);
}
glBegin(GL_QUADS);
glTexCoord2f(1, 1);
glVertex2f(boxBounds.left(), boxBounds.top());
glTexCoord2f(0, 1);
glVertex2f(boxBounds.right(), boxBounds.top());
glTexCoord2f(0, 0);
glVertex2f(boxBounds.right(), boxBounds.bottom());
glTexCoord2f(1, 0);
glVertex2f(boxBounds.left(), boxBounds.bottom());
glEnd();
}
_iconBounds = QRect(x, y, MUTE_ICON_SIZE, MUTE_ICON_SIZE);
if (!_muted) {
glBindTexture(GL_TEXTURE_2D, _micTextureId);
_iconColor = 1.0f;
} else {
glBindTexture(GL_TEXTURE_2D, _muteTextureId);
// Make muted icon pulsate
static const float PULSE_MIN = 0.4f;
static const float PULSE_MAX = 1.0f;
static const float PULSE_FREQUENCY = 1.0f; // in Hz
qint64 now = usecTimestampNow();
if (now - _iconPulseTimeReference > (qint64)USECS_PER_SECOND) {
// Prevents t from getting too big, which would diminish glm::cos precision
_iconPulseTimeReference = now - ((now - _iconPulseTimeReference) % USECS_PER_SECOND);
}
float t = (float)(now - _iconPulseTimeReference) / (float)USECS_PER_SECOND;
float pulseFactor = (glm::cos(t * PULSE_FREQUENCY * 2.0f * PI) + 1.0f) / 2.0f;
_iconColor = PULSE_MIN + (PULSE_MAX - PULSE_MIN) * pulseFactor;
}
glColor3f(_iconColor, _iconColor, _iconColor);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(_iconBounds.left(), _iconBounds.top());
glTexCoord2f(0.0f, 1.0f);
glVertex2f(_iconBounds.right(), _iconBounds.top());
glTexCoord2f(0.0f, 0.0f);
glVertex2f(_iconBounds.right(), _iconBounds.bottom());
glTexCoord2f(1.0f, 0.0f);
glVertex2f(_iconBounds.left(), _iconBounds.bottom());
glEnd();
glDisable(GL_TEXTURE_2D);
}
void Audio::outputFormatChanged() {
int outputFormatChannelCountTimesSampleRate = _outputFormat.channelCount() * _outputFormat.sampleRate();
_outputFrameSize = AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL * outputFormatChannelCountTimesSampleRate / _desiredOutputFormat.sampleRate();

View file

@ -99,12 +99,7 @@ public:
bool getCollisionFlashesScreen() { return _collisionFlashesScreen; }
bool getMuted() { return _muted; }
void init(QGLWidget *parent = 0);
bool mousePressEvent(int x, int y);
void renderToolBox(int x, int y, bool boxed);
bool isMuted() { return _muted; }
const AudioIOStats& getStats() const { return _stats; }
@ -224,12 +219,6 @@ private:
AudioEffectOptions* _reverbOptions;
ty_gverb* _gverbLocal;
ty_gverb* _gverb;
GLuint _micTextureId;
GLuint _muteTextureId;
GLuint _boxTextureId;
QRect _iconBounds;
float _iconColor;
qint64 _iconPulseTimeReference;
// Process procedural audio by
// 1. Echo to the local procedural output device

View file

@ -144,7 +144,7 @@ void DatagramProcessor::processDatagrams() {
}
case PacketTypeNoisyMute:
case PacketTypeMuteEnvironment: {
bool mute = !DependencyManager::get<Audio>()->getMuted();
bool mute = !DependencyManager::get<Audio>()->isMuted();
if (incomingType == PacketTypeMuteEnvironment) {
glm::vec3 position;

View file

@ -0,0 +1,119 @@
//
// AudioToolBox.cpp
// interface/src/audio
//
// Created by Stephen Birarda on 2014-12-16.
// 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
//
#include "InterfaceConfig.h"
#include "Application.h"
#include "Audio.h"
#include "AudioToolBox.h"
// Mute icon configration
const int MUTE_ICON_SIZE = 24;
AudioToolBox::AudioToolBox() :
_iconPulseTimeReference(usecTimestampNow())
{
Application* app = Application::getInstance();
_micTextureId = app->getGLWidget()->bindTexture(QImage(Application::resourcesPath() + "images/mic.svg"));
_muteTextureId = app->getGLWidget()->bindTexture(QImage(Application::resourcesPath() + "images/mic-mute.svg"));
_boxTextureId = app->getGLWidget()->bindTexture(QImage(Application::resourcesPath() + "images/audio-box.svg"));
}
bool AudioToolBox::mousePressEvent(int x, int y) {
if (_iconBounds.contains(x, y)) {
DependencyManager::get<Audio>()->toggleMute();
return true;
}
return false;
}
void AudioToolBox::render(int x, int y, bool boxed) {
glEnable(GL_TEXTURE_2D);
Audio* audioIO = DependencyManager::get<Audio>();
if (boxed) {
bool isClipping = ((audioIO->getTimeSinceLastClip() > 0.0f) && (audioIO->getTimeSinceLastClip() < 1.0f));
const int BOX_LEFT_PADDING = 5;
const int BOX_TOP_PADDING = 10;
const int BOX_WIDTH = 266;
const int BOX_HEIGHT = 44;
QRect boxBounds = QRect(x - BOX_LEFT_PADDING, y - BOX_TOP_PADDING, BOX_WIDTH, BOX_HEIGHT);
glBindTexture(GL_TEXTURE_2D, _boxTextureId);
if (isClipping) {
glColor3f(1.0f, 0.0f, 0.0f);
} else {
glColor3f(0.41f, 0.41f, 0.41f);
}
glBegin(GL_QUADS);
glTexCoord2f(1, 1);
glVertex2f(boxBounds.left(), boxBounds.top());
glTexCoord2f(0, 1);
glVertex2f(boxBounds.right(), boxBounds.top());
glTexCoord2f(0, 0);
glVertex2f(boxBounds.right(), boxBounds.bottom());
glTexCoord2f(1, 0);
glVertex2f(boxBounds.left(), boxBounds.bottom());
glEnd();
}
float iconColor = 1.0f;
_iconBounds = QRect(x, y, MUTE_ICON_SIZE, MUTE_ICON_SIZE);
if (!audioIO->isMuted()) {
glBindTexture(GL_TEXTURE_2D, _micTextureId);
iconColor = 1.0f;
} else {
glBindTexture(GL_TEXTURE_2D, _muteTextureId);
// Make muted icon pulsate
static const float PULSE_MIN = 0.4f;
static const float PULSE_MAX = 1.0f;
static const float PULSE_FREQUENCY = 1.0f; // in Hz
qint64 now = usecTimestampNow();
if (now - _iconPulseTimeReference > (qint64)USECS_PER_SECOND) {
// Prevents t from getting too big, which would diminish glm::cos precision
_iconPulseTimeReference = now - ((now - _iconPulseTimeReference) % USECS_PER_SECOND);
}
float t = (float)(now - _iconPulseTimeReference) / (float)USECS_PER_SECOND;
float pulseFactor = (glm::cos(t * PULSE_FREQUENCY * 2.0f * PI) + 1.0f) / 2.0f;
iconColor = PULSE_MIN + (PULSE_MAX - PULSE_MIN) * pulseFactor;
}
glColor3f(iconColor, iconColor, iconColor);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(_iconBounds.left(), _iconBounds.top());
glTexCoord2f(0.0f, 1.0f);
glVertex2f(_iconBounds.right(), _iconBounds.top());
glTexCoord2f(0.0f, 0.0f);
glVertex2f(_iconBounds.right(), _iconBounds.bottom());
glTexCoord2f(1.0f, 0.0f);
glVertex2f(_iconBounds.left(), _iconBounds.bottom());
glEnd();
glDisable(GL_TEXTURE_2D);
}

View file

@ -0,0 +1,34 @@
//
// AudioToolBox.h
// interface/src/audio
//
// Created by Stephen Birarda on 2014-12-16.
// 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
//
#ifndef hifi_AudioToolBox_h
#define hifi_AudioToolBox_h
#include <DependencyManager.h>
class AudioToolBox : public DependencyManager::Dependency {
public:
void render(int x, int y, bool boxed);
friend class DependencyManager;
bool mousePressEvent(int x, int y);
protected:
AudioToolBox();
private:
GLuint _micTextureId;
GLuint _muteTextureId;
GLuint _boxTextureId;
QRect _iconBounds;
qint64 _iconPulseTimeReference;
};
#endif // hifi_AudioToolBox_h

View file

@ -89,5 +89,5 @@ void AudioDeviceScriptingInterface::toggleMute() {
}
bool AudioDeviceScriptingInterface::getMuted() {
return DependencyManager::get<Audio>()->getMuted();
return DependencyManager::get<Audio>()->isMuted();
}

View file

@ -14,8 +14,9 @@
#include <QOpenGLFramebufferObject>
#include <PerfStat.h>
#include "audio/AudioScope.h"
#include "audio/AudioIOStatsRenderer.h"
#include "audio/AudioScope.h"
#include "audio/AudioToolBox.h"
#include "Application.h"
#include "ApplicationOverlay.h"
#include "devices/OculusManager.h"
@ -828,7 +829,7 @@ void ApplicationOverlay::renderAudioMeter() {
renderCollisionOverlay(glWidget->width(), glWidget->height(), magnitude, 1.0f);
}
audio->renderToolBox(MIRROR_VIEW_LEFT_PADDING + AUDIO_METER_GAP, audioMeterY, boxed);
DependencyManager::get<AudioToolBox>()->render(MIRROR_VIEW_LEFT_PADDING + AUDIO_METER_GAP, audioMeterY, boxed);
DependencyManager::get<AudioScope>()->render(glWidget->width(), glWidget->height());
DependencyManager::get<AudioIOStatsRenderer>()->render(WHITE_TEXT, glWidget->width(), glWidget->height());