diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index 08b5668389..81b7d5e614 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -54,7 +54,7 @@ const short JITTER_BUFFER_MSECS = 12; const short JITTER_BUFFER_SAMPLES = JITTER_BUFFER_MSECS * (SAMPLE_RATE / 1000.0); -const unsigned int BUFFER_SEND_INTERVAL_USECS = floorf((BUFFER_LENGTH_SAMPLES_PER_CHANNEL / SAMPLE_RATE) * 1000 * 1000); +const unsigned int BUFFER_SEND_INTERVAL_USECS = floorf((BUFFER_LENGTH_SAMPLES_PER_CHANNEL / (float) SAMPLE_RATE) * 1000 * 1000); const int MAX_SAMPLE_VALUE = std::numeric_limits::max(); const int MIN_SAMPLE_VALUE = std::numeric_limits::min(); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 46be169a56..02160b926e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -4239,7 +4239,8 @@ void* Application::networkReceive(void* args) { break; case PACKET_TYPE_MIXED_AUDIO: - app->_audio.addReceivedAudioToBuffer(app->_incomingPacket, bytesReceived); + QMetaObject::invokeMethod(&app->_audio, "addReceivedAudioToBuffer", Qt::QueuedConnection, + Q_ARG(QByteArray, QByteArray((char*) app->_incomingPacket, bytesReceived))); break; case PACKET_TYPE_VOXEL_DATA: case PACKET_TYPE_VOXEL_ERASE: diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 40a083e604..e4f0677787 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -31,8 +31,7 @@ #include "Util.h" static const float JITTER_BUFFER_LENGTH_MSECS = 12; -static const short JITTER_BUFFER_SAMPLES = JITTER_BUFFER_LENGTH_MSECS * -NUM_AUDIO_CHANNELS * (SAMPLE_RATE / 1000.0); +static const short JITTER_BUFFER_SAMPLES = JITTER_BUFFER_LENGTH_MSECS * NUM_AUDIO_CHANNELS * (SAMPLE_RATE / 1000.0); static const float AUDIO_CALLBACK_MSECS = (float)BUFFER_LENGTH_SAMPLES_PER_CHANNEL / (float)SAMPLE_RATE * 1000.0; @@ -240,8 +239,7 @@ void Audio::handleAudioInput() { // + 12 for 3 floats for position + float for bearing + 1 attenuation byte PACKET_TYPE packetType = Menu::getInstance()->isOptionChecked(MenuOption::EchoServerAudio) - ? PACKET_TYPE_MICROPHONE_AUDIO_WITH_ECHO - : PACKET_TYPE_MICROPHONE_AUDIO_NO_ECHO; + ? PACKET_TYPE_MICROPHONE_AUDIO_WITH_ECHO : PACKET_TYPE_MICROPHONE_AUDIO_NO_ECHO; char* currentPacketPtr = monoAudioDataPacket + populateTypeAndVersion((unsigned char*) monoAudioDataPacket, packetType); @@ -279,6 +277,9 @@ void Audio::handleAudioInput() { nodeList->getNodeSocket().writeDatagram(monoAudioDataPacket, BUFFER_LENGTH_BYTES_PER_CHANNEL + leadingBytes, audioMixer->getActiveSocket()->getAddress(), audioMixer->getActiveSocket()->getPort()); + + Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::AUDIO) + .updateValue(BUFFER_LENGTH_BYTES_PER_CHANNEL + leadingBytes); } else { nodeList->pingPublicAndLocalSocketsForInactiveNode(audioMixer); } @@ -343,6 +344,7 @@ void Audio::handleAudioInput() { _outputDevice->write(stereoOutputBuffer); + // add output (@speakers) data just written to the scope QMetaObject::invokeMethod(_scope, "addStereoSamples", Qt::QueuedConnection, Q_ARG(QByteArray, stereoOutputBuffer), Q_ARG(bool, false)); @@ -352,7 +354,7 @@ void Audio::handleAudioInput() { gettimeofday(&_lastCallbackTime, NULL); } -void Audio::addReceivedAudioToBuffer(unsigned char* receivedData, int receivedBytes) { +void Audio::addReceivedAudioToBuffer(const QByteArray& audioByteArray) { const int NUM_INITIAL_PACKETS_DISCARD = 3; const int STANDARD_DEVIATION_SAMPLE_COUNT = 500; @@ -374,9 +376,7 @@ void Audio::addReceivedAudioToBuffer(unsigned char* receivedData, int receivedBy const int MAX_JITTER_BUFFER_SAMPLES = RING_BUFFER_LENGTH_SAMPLES / 2; const float NUM_STANDARD_DEVIATIONS = 3.f; if (Menu::getInstance()->getAudioJitterBufferSamples() == 0) { - float newJitterBufferSamples = (NUM_STANDARD_DEVIATIONS * _measuredJitter) - / 1000.f - * SAMPLE_RATE; + float newJitterBufferSamples = (NUM_STANDARD_DEVIATIONS * _measuredJitter) / 1000.f * SAMPLE_RATE; setJitterBufferSamples(glm::clamp((int)newJitterBufferSamples, 0, MAX_JITTER_BUFFER_SAMPLES)); } } @@ -393,10 +393,10 @@ void Audio::addReceivedAudioToBuffer(unsigned char* receivedData, int receivedBy } } - _ringBuffer.parseData((unsigned char*) receivedData, receivedBytes); + _ringBuffer.parseData((unsigned char*) audioByteArray.data(), audioByteArray.size()); - Application::getInstance()->getBandwidthMeter()->inputStream(BandwidthMeter::AUDIO) - .updateValue(PACKET_LENGTH_BYTES + sizeof(PACKET_TYPE)); + Application::getInstance()->getBandwidthMeter()->inputStream(BandwidthMeter::AUDIO).updateValue(PACKET_LENGTH_BYTES + + sizeof(PACKET_TYPE)); _lastReceiveTime = currentReceiveTime; } @@ -410,7 +410,7 @@ bool Audio::mousePressEvent(int x, int y) { } void Audio::render(int screenWidth, int screenHeight) { - if (true) { + if (_audioInput) { glLineWidth(2.0); glBegin(GL_LINES); glColor3f(1,1,1); @@ -566,10 +566,13 @@ void Audio::addProceduralSounds(int16_t* inputBuffer, int16_t* stereoOutput, int sample = sinf(t * frequency); sample += ((randFloat() - 0.5f) * NOISE_MAGNITUDE); sample *= _drumSoundVolume * MAX_VOLUME; - inputBuffer[i] += sample; + + int16_t collisionSample = (int16_t) sample; + + inputBuffer[i] += collisionSample; for (int j = (i * 4); j < (i * 4) + 4; j++) { - stereoOutput[j] += sample; + stereoOutput[j] += collisionSample; } _drumSoundVolume *= (1.f - _drumSoundDecay); diff --git a/interface/src/Audio.h b/interface/src/Audio.h index f02b07757f..b92bc08860 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -42,8 +42,6 @@ public: void render(int screenWidth, int screenHeight); - void addReceivedAudioToBuffer(unsigned char* receivedData, int receivedBytes); - float getLastInputLoudness() const { return _lastInputLoudness; } void setLastAcceleration(const glm::vec3 lastAcceleration) { _lastAcceleration = lastAcceleration; } @@ -64,8 +62,9 @@ public: void init(QGLWidget *parent = 0); bool mousePressEvent(int x, int y); - public slots: +public slots: void start(); + void addReceivedAudioToBuffer(const QByteArray& audioByteArray); void handleAudioInput(); void reset();