OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include <math.h> | 22 #include <math.h> |
23 #include <stddef.h> // size_t | 23 #include <stddef.h> // size_t |
24 #include <stdlib.h> | 24 #include <stdlib.h> |
25 #include <string.h> | 25 #include <string.h> |
26 | 26 |
27 #include "webrtc/common_audio/ring_buffer.h" | 27 #include "webrtc/common_audio/ring_buffer.h" |
28 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" | 28 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" |
29 #include "webrtc/modules/audio_processing/aec/aec_common.h" | 29 #include "webrtc/modules/audio_processing/aec/aec_common.h" |
30 #include "webrtc/modules/audio_processing/aec/aec_core_internal.h" | 30 #include "webrtc/modules/audio_processing/aec/aec_core_internal.h" |
31 #include "webrtc/modules/audio_processing/aec/aec_rdft.h" | 31 #include "webrtc/modules/audio_processing/aec/aec_rdft.h" |
| 32 #include "webrtc/modules/audio_processing/logging/aec_logging.h" |
32 #include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h" | 33 #include "webrtc/modules/audio_processing/utility/delay_estimator_wrapper.h" |
33 #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h" | 34 #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h" |
34 #include "webrtc/typedefs.h" | 35 #include "webrtc/typedefs.h" |
35 | 36 |
| 37 |
36 // Buffer size (samples) | 38 // Buffer size (samples) |
37 static const size_t kBufSizePartitions = 250; // 1 second of audio in 16 kHz. | 39 static const size_t kBufSizePartitions = 250; // 1 second of audio in 16 kHz. |
38 | 40 |
39 // Metrics | 41 // Metrics |
40 static const int subCountLen = 4; | 42 static const int subCountLen = 4; |
41 static const int countLen = 50; | 43 static const int countLen = 50; |
42 static const int kDelayMetricsAggregationWindow = 1250; // 5 seconds at 16 kHz. | 44 static const int kDelayMetricsAggregationWindow = 1250; // 5 seconds at 16 kHz. |
43 | 45 |
44 // Quantities to control H band scaling for SWB input | 46 // Quantities to control H band scaling for SWB input |
45 static const int flagHbandCn = 1; // flag for adding comfort noise in H band | 47 static const int flagHbandCn = 1; // flag for adding comfort noise in H band |
(...skipping 1166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1212 WebRtc_ReadBuffer(aec->nearFrBuf, (void**)&nearend_ptr, nearend, PART_LEN); | 1214 WebRtc_ReadBuffer(aec->nearFrBuf, (void**)&nearend_ptr, nearend, PART_LEN); |
1213 memcpy(aec->dBuf + PART_LEN, nearend_ptr, sizeof(nearend)); | 1215 memcpy(aec->dBuf + PART_LEN, nearend_ptr, sizeof(nearend)); |
1214 | 1216 |
1215 // ---------- Ooura fft ---------- | 1217 // ---------- Ooura fft ---------- |
1216 | 1218 |
1217 #ifdef WEBRTC_AEC_DEBUG_DUMP | 1219 #ifdef WEBRTC_AEC_DEBUG_DUMP |
1218 { | 1220 { |
1219 float farend[PART_LEN]; | 1221 float farend[PART_LEN]; |
1220 float* farend_ptr = NULL; | 1222 float* farend_ptr = NULL; |
1221 WebRtc_ReadBuffer(aec->far_time_buf, (void**)&farend_ptr, farend, 1); | 1223 WebRtc_ReadBuffer(aec->far_time_buf, (void**)&farend_ptr, farend, 1); |
1222 rtc_WavWriteSamples(aec->farFile, farend_ptr, PART_LEN); | 1224 RTC_AEC_DEBUG_WAV_WRITE(aec->farFile, farend_ptr, PART_LEN); |
1223 rtc_WavWriteSamples(aec->nearFile, nearend_ptr, PART_LEN); | 1225 RTC_AEC_DEBUG_WAV_WRITE(aec->nearFile, nearend_ptr, PART_LEN); |
1224 } | 1226 } |
1225 #endif | 1227 #endif |
1226 | 1228 |
1227 // We should always have at least one element stored in |far_buf|. | 1229 // We should always have at least one element stored in |far_buf|. |
1228 assert(WebRtc_available_read(aec->far_buf) > 0); | 1230 assert(WebRtc_available_read(aec->far_buf) > 0); |
1229 WebRtc_ReadBuffer(aec->far_buf, (void**)&xf_ptr, &xf[0][0], 1); | 1231 WebRtc_ReadBuffer(aec->far_buf, (void**)&xf_ptr, &xf[0][0], 1); |
1230 | 1232 |
1231 // Near fft | 1233 // Near fft |
1232 memcpy(fft, aec->dBuf, sizeof(float) * PART_LEN2); | 1234 memcpy(fft, aec->dBuf, sizeof(float) * PART_LEN2); |
1233 TimeToFrequency(fft, df, 0); | 1235 TimeToFrequency(fft, df, 0); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1340 | 1342 |
1341 ef[1][0] = 0; | 1343 ef[1][0] = 0; |
1342 ef[1][PART_LEN] = 0; | 1344 ef[1][PART_LEN] = 0; |
1343 ef[0][0] = fft[0]; | 1345 ef[0][0] = fft[0]; |
1344 ef[0][PART_LEN] = fft[1]; | 1346 ef[0][PART_LEN] = fft[1]; |
1345 for (i = 1; i < PART_LEN; i++) { | 1347 for (i = 1; i < PART_LEN; i++) { |
1346 ef[0][i] = fft[2 * i]; | 1348 ef[0][i] = fft[2 * i]; |
1347 ef[1][i] = fft[2 * i + 1]; | 1349 ef[1][i] = fft[2 * i + 1]; |
1348 } | 1350 } |
1349 | 1351 |
| 1352 RTC_AEC_DEBUG_RAW_WRITE(aec->e_fft_file, |
| 1353 &ef[0][0], |
| 1354 sizeof(ef[0][0]) * PART_LEN1 * 2); |
| 1355 |
1350 if (aec->metricsMode == 1) { | 1356 if (aec->metricsMode == 1) { |
1351 // Note that the first PART_LEN samples in fft (before transformation) are | 1357 // Note that the first PART_LEN samples in fft (before transformation) are |
1352 // zero. Hence, the scaling by two in UpdateLevel() should not be | 1358 // zero. Hence, the scaling by two in UpdateLevel() should not be |
1353 // performed. That scaling is taken care of in UpdateMetrics() instead. | 1359 // performed. That scaling is taken care of in UpdateMetrics() instead. |
1354 UpdateLevel(&aec->linoutlevel, ef); | 1360 UpdateLevel(&aec->linoutlevel, ef); |
1355 } | 1361 } |
1356 | 1362 |
1357 // Scale error signal inversely with far power. | 1363 // Scale error signal inversely with far power. |
1358 WebRtcAec_ScaleErrorSignal(aec, ef); | 1364 WebRtcAec_ScaleErrorSignal(aec, ef); |
1359 WebRtcAec_FilterAdaptation(aec, fft, ef); | 1365 WebRtcAec_FilterAdaptation(aec, fft, ef); |
1360 NonLinearProcessing(aec, output, outputH_ptr); | 1366 NonLinearProcessing(aec, output, outputH_ptr); |
1361 | 1367 |
1362 if (aec->metricsMode == 1) { | 1368 if (aec->metricsMode == 1) { |
1363 // Update power levels and echo metrics | 1369 // Update power levels and echo metrics |
1364 UpdateLevel(&aec->farlevel, (float(*)[PART_LEN1])xf_ptr); | 1370 UpdateLevel(&aec->farlevel, (float(*)[PART_LEN1])xf_ptr); |
1365 UpdateLevel(&aec->nearlevel, df); | 1371 UpdateLevel(&aec->nearlevel, df); |
1366 UpdateMetrics(aec); | 1372 UpdateMetrics(aec); |
1367 } | 1373 } |
1368 | 1374 |
1369 // Store the output block. | 1375 // Store the output block. |
1370 WebRtc_WriteBuffer(aec->outFrBuf, output, PART_LEN); | 1376 WebRtc_WriteBuffer(aec->outFrBuf, output, PART_LEN); |
1371 // For high bands | 1377 // For high bands |
1372 for (i = 0; i < aec->num_bands - 1; ++i) { | 1378 for (i = 0; i < aec->num_bands - 1; ++i) { |
1373 WebRtc_WriteBuffer(aec->outFrBufH[i], outputH[i], PART_LEN); | 1379 WebRtc_WriteBuffer(aec->outFrBufH[i], outputH[i], PART_LEN); |
1374 } | 1380 } |
1375 | 1381 |
1376 #ifdef WEBRTC_AEC_DEBUG_DUMP | 1382 RTC_AEC_DEBUG_WAV_WRITE(aec->outLinearFile, e, PART_LEN); |
1377 rtc_WavWriteSamples(aec->outLinearFile, e, PART_LEN); | 1383 RTC_AEC_DEBUG_WAV_WRITE(aec->outFile, output, PART_LEN); |
1378 rtc_WavWriteSamples(aec->outFile, output, PART_LEN); | |
1379 #endif | |
1380 } | 1384 } |
1381 | 1385 |
1382 AecCore* WebRtcAec_CreateAec() { | 1386 AecCore* WebRtcAec_CreateAec() { |
1383 int i; | 1387 int i; |
1384 AecCore* aec = malloc(sizeof(AecCore)); | 1388 AecCore* aec = malloc(sizeof(AecCore)); |
1385 if (!aec) { | 1389 if (!aec) { |
1386 return NULL; | 1390 return NULL; |
1387 } | 1391 } |
1388 | 1392 |
1389 aec->nearFrBuf = WebRtc_CreateBuffer(FRAME_LEN + PART_LEN, sizeof(float)); | 1393 aec->nearFrBuf = WebRtc_CreateBuffer(FRAME_LEN + PART_LEN, sizeof(float)); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1504 | 1508 |
1505 for (i = 0; i < NUM_HIGH_BANDS_MAX; ++i) { | 1509 for (i = 0; i < NUM_HIGH_BANDS_MAX; ++i) { |
1506 WebRtc_FreeBuffer(aec->nearFrBufH[i]); | 1510 WebRtc_FreeBuffer(aec->nearFrBufH[i]); |
1507 WebRtc_FreeBuffer(aec->outFrBufH[i]); | 1511 WebRtc_FreeBuffer(aec->outFrBufH[i]); |
1508 } | 1512 } |
1509 | 1513 |
1510 WebRtc_FreeBuffer(aec->far_buf); | 1514 WebRtc_FreeBuffer(aec->far_buf); |
1511 WebRtc_FreeBuffer(aec->far_buf_windowed); | 1515 WebRtc_FreeBuffer(aec->far_buf_windowed); |
1512 #ifdef WEBRTC_AEC_DEBUG_DUMP | 1516 #ifdef WEBRTC_AEC_DEBUG_DUMP |
1513 WebRtc_FreeBuffer(aec->far_time_buf); | 1517 WebRtc_FreeBuffer(aec->far_time_buf); |
1514 rtc_WavClose(aec->farFile); | |
1515 rtc_WavClose(aec->nearFile); | |
1516 rtc_WavClose(aec->outFile); | |
1517 rtc_WavClose(aec->outLinearFile); | |
1518 #endif | 1518 #endif |
| 1519 RTC_AEC_DEBUG_WAV_CLOSE(aec->farFile); |
| 1520 RTC_AEC_DEBUG_WAV_CLOSE(aec->nearFile); |
| 1521 RTC_AEC_DEBUG_WAV_CLOSE(aec->outFile); |
| 1522 RTC_AEC_DEBUG_WAV_CLOSE(aec->outLinearFile); |
| 1523 RTC_AEC_DEBUG_RAW_CLOSE(aec->e_fft_file); |
| 1524 |
1519 WebRtc_FreeDelayEstimator(aec->delay_estimator); | 1525 WebRtc_FreeDelayEstimator(aec->delay_estimator); |
1520 WebRtc_FreeDelayEstimatorFarend(aec->delay_estimator_farend); | 1526 WebRtc_FreeDelayEstimatorFarend(aec->delay_estimator_farend); |
1521 | 1527 |
1522 free(aec); | 1528 free(aec); |
1523 } | 1529 } |
1524 | 1530 |
1525 #ifdef WEBRTC_AEC_DEBUG_DUMP | |
1526 // Open a new Wav file for writing. If it was already open with a different | |
1527 // sample frequency, close it first. | |
1528 static void ReopenWav(rtc_WavWriter** wav_file, | |
1529 const char* name, | |
1530 int seq1, | |
1531 int seq2, | |
1532 int sample_rate) { | |
1533 int written ATTRIBUTE_UNUSED; | |
1534 char filename[64]; | |
1535 if (*wav_file) { | |
1536 if (rtc_WavSampleRate(*wav_file) == sample_rate) | |
1537 return; | |
1538 rtc_WavClose(*wav_file); | |
1539 } | |
1540 written = snprintf(filename, sizeof(filename), "%s%d-%d.wav", | |
1541 name, seq1, seq2); | |
1542 assert(written >= 0); // no output error | |
1543 assert((size_t)written < sizeof(filename)); // buffer was large enough | |
1544 *wav_file = rtc_WavOpen(filename, sample_rate, 1); | |
1545 } | |
1546 #endif // WEBRTC_AEC_DEBUG_DUMP | |
1547 | |
1548 int WebRtcAec_InitAec(AecCore* aec, int sampFreq) { | 1531 int WebRtcAec_InitAec(AecCore* aec, int sampFreq) { |
1549 int i; | 1532 int i; |
1550 | 1533 |
1551 aec->sampFreq = sampFreq; | 1534 aec->sampFreq = sampFreq; |
1552 | 1535 |
1553 if (sampFreq == 8000) { | 1536 if (sampFreq == 8000) { |
1554 aec->normal_mu = 0.6f; | 1537 aec->normal_mu = 0.6f; |
1555 aec->normal_error_threshold = 2e-6f; | 1538 aec->normal_error_threshold = 2e-6f; |
1556 aec->num_bands = 1; | 1539 aec->num_bands = 1; |
1557 } else { | 1540 } else { |
1558 aec->normal_mu = 0.5f; | 1541 aec->normal_mu = 0.5f; |
1559 aec->normal_error_threshold = 1.5e-6f; | 1542 aec->normal_error_threshold = 1.5e-6f; |
1560 aec->num_bands = sampFreq / 16000; | 1543 aec->num_bands = sampFreq / 16000; |
1561 } | 1544 } |
1562 | 1545 |
1563 WebRtc_InitBuffer(aec->nearFrBuf); | 1546 WebRtc_InitBuffer(aec->nearFrBuf); |
1564 WebRtc_InitBuffer(aec->outFrBuf); | 1547 WebRtc_InitBuffer(aec->outFrBuf); |
1565 for (i = 0; i < NUM_HIGH_BANDS_MAX; ++i) { | 1548 for (i = 0; i < NUM_HIGH_BANDS_MAX; ++i) { |
1566 WebRtc_InitBuffer(aec->nearFrBufH[i]); | 1549 WebRtc_InitBuffer(aec->nearFrBufH[i]); |
1567 WebRtc_InitBuffer(aec->outFrBufH[i]); | 1550 WebRtc_InitBuffer(aec->outFrBufH[i]); |
1568 } | 1551 } |
1569 | 1552 |
1570 // Initialize far-end buffers. | 1553 // Initialize far-end buffers. |
1571 WebRtc_InitBuffer(aec->far_buf); | 1554 WebRtc_InitBuffer(aec->far_buf); |
1572 WebRtc_InitBuffer(aec->far_buf_windowed); | 1555 WebRtc_InitBuffer(aec->far_buf_windowed); |
1573 #ifdef WEBRTC_AEC_DEBUG_DUMP | 1556 #ifdef WEBRTC_AEC_DEBUG_DUMP |
1574 WebRtc_InitBuffer(aec->far_time_buf); | 1557 WebRtc_InitBuffer(aec->far_time_buf); |
1575 { | 1558 { |
1576 int process_rate = sampFreq > 16000 ? 16000 : sampFreq; | 1559 int process_rate = sampFreq > 16000 ? 16000 : sampFreq; |
1577 ReopenWav(&aec->farFile, "aec_far", | 1560 RTC_AEC_DEBUG_WAV_REOPEN("aec_far", aec->instance_index, |
1578 aec->instance_index, aec->debug_dump_count, process_rate); | 1561 aec->debug_dump_count, process_rate, |
1579 ReopenWav(&aec->nearFile, "aec_near", | 1562 &aec->farFile ); |
1580 aec->instance_index, aec->debug_dump_count, process_rate); | 1563 RTC_AEC_DEBUG_WAV_REOPEN("aec_near", aec->instance_index, |
1581 ReopenWav(&aec->outFile, "aec_out", | 1564 aec->debug_dump_count, process_rate, |
1582 aec->instance_index, aec->debug_dump_count, process_rate); | 1565 &aec->nearFile); |
1583 ReopenWav(&aec->outLinearFile, "aec_out_linear", | 1566 RTC_AEC_DEBUG_WAV_REOPEN("aec_out", aec->instance_index, |
1584 aec->instance_index, aec->debug_dump_count, process_rate); | 1567 aec->debug_dump_count, process_rate, |
| 1568 &aec->outFile ); |
| 1569 RTC_AEC_DEBUG_WAV_REOPEN("aec_out_linear", aec->instance_index, |
| 1570 aec->debug_dump_count, process_rate, |
| 1571 &aec->outLinearFile); |
1585 } | 1572 } |
| 1573 |
| 1574 RTC_AEC_DEBUG_RAW_OPEN("aec_e_fft", |
| 1575 aec->debug_dump_count, |
| 1576 &aec->e_fft_file); |
| 1577 |
1586 ++aec->debug_dump_count; | 1578 ++aec->debug_dump_count; |
1587 #endif | 1579 #endif |
1588 aec->system_delay = 0; | 1580 aec->system_delay = 0; |
1589 | 1581 |
1590 if (WebRtc_InitDelayEstimatorFarend(aec->delay_estimator_farend) != 0) { | 1582 if (WebRtc_InitDelayEstimatorFarend(aec->delay_estimator_farend) != 0) { |
1591 return -1; | 1583 return -1; |
1592 } | 1584 } |
1593 if (WebRtc_InitDelayEstimator(aec->delay_estimator) != 0) { | 1585 if (WebRtc_InitDelayEstimator(aec->delay_estimator) != 0) { |
1594 return -1; | 1586 return -1; |
1595 } | 1587 } |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1924 int WebRtcAec_extended_filter_enabled(AecCore* self) { | 1916 int WebRtcAec_extended_filter_enabled(AecCore* self) { |
1925 return self->extended_filter_enabled; | 1917 return self->extended_filter_enabled; |
1926 } | 1918 } |
1927 | 1919 |
1928 int WebRtcAec_system_delay(AecCore* self) { return self->system_delay; } | 1920 int WebRtcAec_system_delay(AecCore* self) { return self->system_delay; } |
1929 | 1921 |
1930 void WebRtcAec_SetSystemDelay(AecCore* self, int delay) { | 1922 void WebRtcAec_SetSystemDelay(AecCore* self, int delay) { |
1931 assert(delay >= 0); | 1923 assert(delay >= 0); |
1932 self->system_delay = delay; | 1924 self->system_delay = delay; |
1933 } | 1925 } |
OLD | NEW |