forked from quanwstone/AudioMix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
394 lines (336 loc) · 7.8 KB
/
main.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
int g_SampleRate = 44100;
int g_channels = 2;
#define FMT s16le
#define DEF_MAX 32767
#define DEF_MIN -32768
typedef struct STR_WEBRTC
{
int iEnergy;//能量值
bool bMix;//是否进行mix
short *Data;//真实数据
}STR_WEBRTC;
std::vector<STR_WEBRTC>g_mixList;
std::vector<STR_WEBRTC>g_rampOutList;
//80
const float rampArray[] = {
0.0000f, 0.0127f, 0.0253f, 0.0380f,
0.0506f, 0.0633f, 0.0759f, 0.0886f,
0.1013f, 0.1139f, 0.1266f, 0.1392f,
0.1519f, 0.1646f, 0.1772f, 0.1899f,
0.2025f, 0.2152f, 0.2278f, 0.2405f,
0.2532f, 0.2658f, 0.2785f, 0.2911f,
0.3038f, 0.3165f, 0.3291f, 0.3418f,
0.3544f, 0.3671f, 0.3797f, 0.3924f,
0.4051f, 0.4177f, 0.4304f, 0.4430f,
0.4557f, 0.4684f, 0.4810f, 0.4937f,
0.5063f, 0.5190f, 0.5316f, 0.5443f,
0.5570f, 0.5696f, 0.5823f, 0.5949f,
0.6076f, 0.6203f, 0.6329f, 0.6456f,
0.6582f, 0.6709f, 0.6835f, 0.6962f,
0.7089f, 0.7215f, 0.7342f, 0.7468f,
0.7595f, 0.7722f, 0.7848f, 0.7975f,
0.8101f, 0.8228f, 0.8354f, 0.8481f,
0.8608f, 0.8734f, 0.8861f, 0.8987f,
0.9114f, 0.9241f, 0.9367f, 0.9494f,
0.9620f, 0.9747f, 0.9873f, 1.0000f };
void pcm_mix_webrtc_update(STR_WEBRTC &p,int T)
{
//获取能量最大的三路
//判断能量最大的三路是否需要进行RampIn
if (p.bMix == false)
{
for (int i = 0; i < 80; i++)
{
p.Data[i] = p.Data[i] * rampArray[i];
}
}
for (int i = 0; i < T; i++)
{
p.iEnergy += p.Data[i] * p.Data[i];
}
if (g_mixList.size() >= 3)
{
for (std::vector<STR_WEBRTC>::iterator iter = g_mixList.begin(); iter != g_mixList.end(); iter++)
{
if (p.iEnergy > iter->iEnergy)
{
if (iter->bMix == true)
{
for (int i = 0; i < 80; i++)
{
iter->Data[i] = iter->Data[i] * rampArray[80 - i - 1];
}
g_rampOutList.push_back(*iter);
}
g_mixList.erase(iter);
g_mixList.push_back(p);
break;
}
}
}
else
{
g_mixList.push_back(p);
}
}
void pcm_mix_webrtc_list(short *bufMix,int T)
{
for (int i = 0; i < T; i++)
{
int iT = 0;
if (!g_rampOutList.empty())
{
iT = g_mixList.at(0).Data[i] + g_mixList.at(1).Data[i] + g_mixList.at(2).Data[i] + g_rampOutList.at(0).Data[i];
iT /= 2;
}
else
{
iT = g_mixList.at(0).Data[i] + g_mixList.at(1).Data[i] + g_mixList.at(2).Data[i];
iT /= 2;
}
if (iT > DEF_MAX)
{
printf("pcm_mix_webrtc_list DEF_MAX\n");
iT = DEF_MAX;
}
if (iT < DEF_MIN)
{
printf("pcm_mix_webrtc_list DEF_MIN\n");
iT = DEF_MIN;
}
bufMix[i] = iT;
}
}
//利用webRtc原理实现mix
void pcm_mix_webrtc(short *bufMix,STR_WEBRTC &p1, STR_WEBRTC &p2, STR_WEBRTC &p3, STR_WEBRTC &p4,int T)
{
g_mixList.clear();
g_rampOutList.clear();
pcm_mix_webrtc_update(p1, T);
pcm_mix_webrtc_update(p2, T);
pcm_mix_webrtc_update(p3, T);
pcm_mix_webrtc_update(p4, T);
for (auto iter = g_mixList.begin(); iter != g_mixList.end(); iter++)
{
if (iter->Data == p1.Data)
{
p1.bMix = true;
}
if (iter->Data == p2.Data)
{
p2.bMix = true;
}
if (iter->Data == p3.Data)
{
p3.bMix = true;
}
if (iter->Data == p4.Data)
{
p4.bMix = true;
}
}
for (auto iter = g_rampOutList.begin(); iter != g_rampOutList.end(); iter++)
{
if (iter->Data == p1.Data)
{
p1.bMix = false;
}
if (iter->Data == p2.Data)
{
p2.bMix = false;
}
if (iter->Data == p3.Data)
{
p3.bMix = false;
}
if (iter->Data == p4.Data)
{
p4.bMix = false;
}
}
pcm_mix_webrtc_list(bufMix, T);
}
//
void pcm_mix_auto_newlc(short *bufMix,short *buf,short *buf2,int T)
{
for (int i = 0; i < T; i++)
{
int iT = 0;
if (buf[i] < 0 && buf2[i] < 0)
{
iT = buf[i] * buf2[i] / -(pow(2, 16 - 1) - 1);
}
else
{
iT = buf[i] * buf2[i] / (pow(2, 16 - 1) - 1);
}
int iMax = buf[i] + buf2[i] - iT;
if (iMax > DEF_MAX)
{
printf("pcm_mix_auto_newlc DEF_MAX\n");
}
if (iMax < DEF_MIN)
{
printf("pcm_mix_auto_newlc DEF_MIN\n");
}
bufMix[i] = iMax;
}
}
//自动对齐算法,以自身的比例为权重.
//a的权重为a/(a+b+c),b的权重为b/(a+b+c),则最终a的权重乘以a的值,加上b的权重乘以b的值.
//(a*|a| + |b|*b + |c|*c)/(|a|+|b|+|c|)该方法导致高频数据损失
int get_mix_auto_align_sgn(short p)
{
int isgn = 0;
if (p > 0)
{
isgn = 1;
}
else if (p < 0)
{
isgn = -1;
}
else {
isgn = 0;
}
int ibuf = abs(p * p);
return isgn * ibuf;
}
void pcm_mix_auto_align(short *bufMix,short *buf,short *buf2,int T)
{
for (int i = 0; i < T; i++)
{
//int ibuf = get_mix_auto_align_sgn(buf[i]);
//int ibuf2 = get_mix_auto_align_sgn(buf2[i]);
if (buf[i]==0 && buf2[i] == 0)
{
bufMix[i] = 0;
continue;
}
int iT = (abs(buf[i])*buf[i] +abs(buf2[i])*buf2[i]) / (abs(buf[i]) + abs(buf2[i]));
if (iT > DEF_MAX)
{
printf("pcm_mix_auto_align DEF_MAX\n");
}
if (iT < DEF_MIN)
{
printf("pcm_mix_auto_align DEF_MIN\n");
}
bufMix[i] = iT;
}
}
//线性叠加求平均.不会产生溢出,噪音较小。但是衰减过大,影响通话质量。
void pcm_mix_avg(short *bufMix,short *buf,short *buf2 ,int T)
{
for (int i = 0; i < T; i++)
{
int iT = buf[i] + buf2[i];
bufMix[i] = (short)(iT / 2);
if (bufMix[i] > DEF_MAX)
{
bufMix[i] = DEF_MAX;
printf("pcm_mix_avg DEF_MAX\n");
}
if (bufMix[i] < DEF_MIN)
{
bufMix[i] = DEF_MIN;
printf("pcm_mix_avg DEF_MIN\n");
}
}
}
//自适应混音加权,使用可变的衰减因子对语音进行衰减,该衰减因子代表了语音的权重,
//衰减因子随着音频数据的变化而变化,当溢出时,衰减因子变小,使得后续的数据在衰减后处于临界值以内,
//没有溢出时,又让衰减因子慢慢增大,是数据较为平缓的变化.
void pcm_mix_Normalization(short *bufMix, short *buf, short *buf2, int T)
{
double f = 1;
int ioutput = 0;
for (int i = 0; i < T; i++)
{
int iT = buf[i] + buf2[i];
ioutput = iT * f;
if (ioutput > DEF_MAX)
{
f = (double)DEF_MAX / (double)(ioutput);
ioutput = DEF_MAX;
printf("pcm_mix_Normalization DEF_MAX f=%f\n",f);
}
if (ioutput < DEF_MIN)
{
f = (double)DEF_MIN / (double)(ioutput);
ioutput = DEF_MIN;
printf("pcm_mix_Normalization DEF_MIN f=%f\n", f);
}
if (f < 1)
{
f += (double)(1 - f) / (double)32;
}
bufMix[i] = (short)ioutput;
}
}
//
int main(int argc, char *argv[])
{
FILE *pPcm1 = nullptr, *pPcm2 = nullptr,*pPcm3 = nullptr,*pPcm4 = nullptr,*pPcmMix = nullptr;
errno_t er = fopen_s(&pPcm1,"../Pcm/1.pcm","rb+");
if (er != 0)
{
printf("");
}
er = fopen_s(&pPcm2,"../Pcm/2.pcm","rb+");
if (er != 0)
{
printf("");
}
er = fopen_s(&pPcm3,"../Pcm/3.pcm","rb+");
if(er != 0)
{
printf("");
}
er = fopen_s(&pPcm4, "../Pcm/4.pcm", "rb+");
if (er = 0)
{
printf("");
}
er = fopen_s(&pPcmMix, "Mix.pcm", "wb+");
if (er != 0)
{
printf("");
}
//10ms
short bufMix[1920] = { 0};
short buf1[1920] = { 0 };
short buf2[1920] = { 0 };
short buf3[1920] = { 0 };
short buf4[1920] = { 0 };
int ir1 = fread(buf1, 2, 1920, pPcm1);
int ir2 = fread(buf2, 2, 1920, pPcm2);
int ir3 = fread(buf3, 2, 1920, pPcm3);
int ir4 = fread(buf4, 2, 1920, pPcm4);
STR_WEBRTC s1{0,0,buf1 }, s2{ 0,0,buf2 }, s3{ 0,0,buf3 }, s4{ 0,0,buf4 };
while (ir1 > 0 && ir2 > 0 && ir3 > 0 && ir4 > 0 )
{
//pcm_mix_avg(buf3, buf, buf2, 1024);
//pcm_mix_Normalization(buf3,buf,buf2,1024);
//pcm_mix_auto_align(buf3,buf,buf2,1024);
//pcm_mix_auto_newlc(buf3, buf, buf2, 1024);
pcm_mix_webrtc(bufMix, s1, s2, s3, s4,1920);
fwrite(bufMix, 2, 1920, pPcmMix);
ir1 = fread(buf1, 2, 1920, pPcm1);
ir2 = fread(buf2, 2, 1920, pPcm2);
ir3 = fread(buf3, 2, 1920, pPcm3);
ir4 = fread(buf4, 2, 1920, pPcm4);
}
fclose(pPcm1);
fclose(pPcm2);
fclose(pPcm3);
fclose(pPcm4);
fclose(pPcmMix);
printf("end.\n");
for (;;);
return 0;
}