// PCMMixer.cpp: implementation of the PCMMixer class.
|
//
|
//////////////////////////////////////////////////////////////////////
|
|
#include "PCMMixer.h"
|
#include "AudioSample.h"
|
#include "AudioSampleManager.h"
|
|
//////////////////////////////////////////////////////////////////////
|
// Construction/Destruction
|
//////////////////////////////////////////////////////////////////////
|
|
PCMMixer::PCMMixer()
|
{
|
char subFacilityName[100];
|
sprintf(subFacilityName, "PCMMixer:%x", this);
|
tracer.SetSubFacilityName(subFacilityName);
|
SetTraceLevel();
|
}
|
|
PCMMixer::~PCMMixer()
|
{
|
|
}
|
|
int
|
PCMMixer::SetTraceLevel()
|
{
|
long SystemMask = 0;
|
if ((SystemMask = GetRegKeyLong(HKEY_CURRENT_USER, "Software\\Cisco Systems\\MTC\\Tracing", "AllComponents", 0x0)) == 0)
|
{
|
SystemMask = GetRegKeyLong(HKEY_CURRENT_USER, "Software\\Cisco Systems\\MTC\\Tracing", "PCMMixer", 0x100000);
|
}
|
tracer.SetSystemMask(SystemMask);
|
return 0;
|
}
|
|
int
|
PCMMixer::TransformAudioSamples(std::vector<std::pair<AudioSample *, AudioSource *> > &dataIn, AudioSample **ppAudioSample)
|
{
|
tracer.tracef(DET, "TransformAudioSamples\n");
|
int result = 0;
|
AudioSample *outSample = NULL;
|
AudioSample *inSample = NULL;
|
std::vector<std::pair<AudioSample *, AudioSource *> > data;
|
|
// create a new input vector with only the non-null valid audio samples passed in the input
|
for (int i=0; i<dataIn.size(); i++)
|
{
|
if (dataIn[i].first)
|
{
|
data.push_back(std::make_pair(dataIn[i].first, dataIn[i].second));
|
}
|
}
|
|
// make inSample the first (if any) non-null audioSample in the vector. it is used later for audio format stuff
|
|
if (data.size() == 1)
|
{
|
inSample = data[0].first;
|
outSample = inSample;
|
inSample->AddRef(this);
|
}
|
else if (data.size() > 1)
|
{
|
// create new audio sample
|
inSample = data[0].first;
|
result = AudioSampleManager::GetInstance()->GetAudioSample(&outSample, this);
|
WAVEFORMATEX sampleFormat;
|
inSample->GetFormat(&sampleFormat);
|
outSample->SetFormat(sampleFormat);
|
short *dataOut = (short *)(outSample->Data());
|
|
// find out how many need to be mixed
|
int maxInputSize(0);
|
int maxSilenceDuration(0);
|
for (int i=0; i< data.size(); i++)
|
{
|
int sampleDataSize = (data[i].first)->DataSize();
|
if (sampleDataSize > maxInputSize)
|
{
|
maxInputSize = sampleDataSize;
|
}
|
else if (sampleDataSize == 0)
|
{
|
maxSilenceDuration = max(maxSilenceDuration, (data[i].first)->GetSilenceDuration());
|
}
|
}
|
outSample->SetSilenceDuration(maxSilenceDuration); // in case all are silence packets
|
int samplesToMix = (outSample->BufferSize() > maxInputSize ? maxInputSize : outSample->BufferSize()) / 2;
|
|
// mix samples
|
for (i=0; i<samplesToMix; i++)
|
{
|
double total = 0;
|
|
// first add the samples
|
for (int j=0; j<data.size(); j++)
|
{
|
AudioSample *audioSample = data[j].first;
|
short *dataIn = (short *)(audioSample->Data());
|
if (audioSample->DataSize() >= (i+1) * 2) // sample #0 means datasize has to be = (0+1) * 2
|
{
|
total += dataIn[i];
|
}
|
}
|
|
// limit the volume
|
if (total > 32767.0)
|
{
|
total = 32767.0;
|
}
|
else if (total < -32768.0)
|
{
|
total = -32768.0;
|
}
|
|
dataOut[i] = (short)total;
|
}
|
outSample->SetDataSize(samplesToMix * 2);
|
tracer.tracef(DET, "TransformAudioSamples : outSample->DataSize = %d\n", samplesToMix * 2);
|
}
|
*ppAudioSample = outSample;
|
tracer.tracef(DET, "~TransformAudioSamples\n");
|
return 0;
|
}
|