use glm clamp instead of pleateauAdditionOfSamples in audio mixer

This commit is contained in:
Stephen Birarda 2013-07-11 10:51:56 -07:00
parent beb89e3d2b
commit 5527d4b50f

View file

@ -6,42 +6,45 @@
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. // Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
// //
#include <errno.h>
#include <fcntl.h>
#include <fstream>
#include <iostream> #include <iostream>
#include <limits>
#include <math.h> #include <math.h>
#include <string.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <string.h>
#include <errno.h>
#include <fstream>
#include <limits>
#include <signal.h>
#include <glm/gtx/norm.hpp>
#include <glm/gtx/vector_angle.hpp>
#include <NodeList.h>
#include <Node.h>
#include <NodeTypes.h>
#include <SharedUtil.h>
#include <StdDev.h>
#include <Logstash.h>
#include "InjectedAudioRingBuffer.h"
#include "AvatarAudioRingBuffer.h"
#include <AudioRingBuffer.h>
#include "PacketHeaders.h"
#ifdef _WIN32 #ifdef _WIN32
#include "Syssocket.h" #include "Syssocket.h"
#include "Systime.h" #include "Systime.h"
#include <math.h> #include <math.h>
#else #else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif //_WIN32 #endif //_WIN32
#include <glm/glm.hpp>
#include <glm/gtx/norm.hpp>
#include <glm/gtx/vector_angle.hpp>
#include <Logstash.h>
#include <NodeList.h>
#include <Node.h>
#include <NodeTypes.h>
#include <PacketHeaders.h>
#include <SharedUtil.h>
#include <StdDev.h>
#include <AudioRingBuffer.h>
#include "AvatarAudioRingBuffer.h"
#include "InjectedAudioRingBuffer.h"
const unsigned short MIXER_LISTEN_PORT = 55443; const unsigned short MIXER_LISTEN_PORT = 55443;
const short JITTER_BUFFER_MSECS = 12; const short JITTER_BUFFER_MSECS = 12;
@ -49,17 +52,8 @@ 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) * 1000000); const unsigned int BUFFER_SEND_INTERVAL_USECS = floorf((BUFFER_LENGTH_SAMPLES_PER_CHANNEL / SAMPLE_RATE) * 1000000);
const long MAX_SAMPLE_VALUE = std::numeric_limits<int16_t>::max(); const int MAX_SAMPLE_VALUE = std::numeric_limits<int16_t>::max();
const long MIN_SAMPLE_VALUE = std::numeric_limits<int16_t>::min(); const int MIN_SAMPLE_VALUE = std::numeric_limits<int16_t>::min();
void plateauAdditionOfSamples(int16_t &mixSample, int16_t sampleToAdd) {
long sumSample = sampleToAdd + mixSample;
long normalizedSample = std::min(MAX_SAMPLE_VALUE, sumSample);
normalizedSample = std::max(MIN_SAMPLE_VALUE, sumSample);
mixSample = normalizedSample;
}
void attachNewBufferToNode(Node *newNode) { void attachNewBufferToNode(Node *newNode) {
if (!newNode->getLinkedData()) { if (!newNode->getLinkedData()) {
@ -304,16 +298,23 @@ int main(int argc, const char* argv[]) {
// pull the earlier sample for the delayed channel // pull the earlier sample for the delayed channel
int earlierSample = delaySamplePointer[s] * attenuationCoefficient * weakChannelAmplitudeRatio; int earlierSample = delaySamplePointer[s] * attenuationCoefficient * weakChannelAmplitudeRatio;
plateauAdditionOfSamples(delayedChannel[s], earlierSample); delayedChannel[s] = glm::clamp(delayedChannel[s] + earlierSample,
MIN_SAMPLE_VALUE,
MAX_SAMPLE_VALUE);
} }
int16_t currentSample = stkFrameBuffer[s] * attenuationCoefficient; int16_t currentSample = stkFrameBuffer[s] * attenuationCoefficient;
plateauAdditionOfSamples(goodChannel[s], currentSample); goodChannel[s] = glm::clamp(goodChannel[s] + currentSample,
MIN_SAMPLE_VALUE,
MAX_SAMPLE_VALUE);
if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) { if (s + numSamplesDelay < BUFFER_LENGTH_SAMPLES_PER_CHANNEL) {
plateauAdditionOfSamples(delayedChannel[s + numSamplesDelay], int sumSample = delayedChannel[s + numSamplesDelay]
currentSample * weakChannelAmplitudeRatio); + (currentSample * weakChannelAmplitudeRatio);
delayedChannel[s + numSamplesDelay] = glm::clamp(sumSample,
MIN_SAMPLE_VALUE,
MAX_SAMPLE_VALUE);
} }
if (s >= BUFFER_LENGTH_SAMPLES_PER_CHANNEL - PHASE_DELAY_AT_90) { if (s >= BUFFER_LENGTH_SAMPLES_PER_CHANNEL - PHASE_DELAY_AT_90) {