Alejandro Acuña
2024-07-30 65a64a81d30f00f1fffd5da6866850e1308e1135
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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;
}