pure sdk for main
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
#include "aec_process.h"
|
||||
#include "typesdef.h"
|
||||
#include "osal/string.h"
|
||||
#include "osal/task.h"
|
||||
#include "custom_mem.h"
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
||||
#endif
|
||||
|
||||
void *aecmInst = NULL;
|
||||
|
||||
int aec_init(uint32_t sampleRate, int16_t nMode)
|
||||
{
|
||||
AecmConfig config;
|
||||
config.cngMode = AecmTrue;
|
||||
config.echoMode = 4;// 0, 1, 2, 3 (default), 4
|
||||
aecmInst = WebRtcAecm_Create();
|
||||
if(!aecmInst) {
|
||||
printf("\n******create aecm err\n");
|
||||
return -1;
|
||||
}
|
||||
WebRtcAecm_Init(aecmInst, sampleRate);
|
||||
WebRtcAecm_set_config(aecmInst, config);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int push_farbuf(short *data, unsigned int len, uint32_t sampleRate)
|
||||
{
|
||||
uint32_t samples = MIN(160, sampleRate / 100);
|
||||
if (samples == 0)
|
||||
return -1;
|
||||
uint32_t nCount = (len / samples);
|
||||
int16_t *far_input = data;
|
||||
for (uint32_t i = 0; i < nCount; i++) {
|
||||
WebRtcAecm_BufferFarend(aecmInst, far_input, samples);
|
||||
far_input += samples;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int aec_process(int16_t *near_frame, uint32_t inSampleCount, int16_t msInSndCardBuf, uint32_t sampleRate)
|
||||
{
|
||||
if (!near_frame) return -1;
|
||||
if (inSampleCount == 0) return -1;
|
||||
|
||||
uint32_t samples = MIN(160, sampleRate / 100);
|
||||
if (samples == 0)
|
||||
return -1;
|
||||
const int maxSamples = 160;
|
||||
int16_t *near_input = near_frame;
|
||||
uint32_t nCount = (inSampleCount / samples);
|
||||
if (aecmInst == NULL) return -1;
|
||||
|
||||
int16_t out_buffer[maxSamples];
|
||||
for (uint32_t i = 0; i < nCount; i++) {
|
||||
int nRet = WebRtcAecm_Process(aecmInst, near_input, NULL, out_buffer, samples, msInSndCardBuf);
|
||||
|
||||
if (nRet != 0) {
|
||||
printf("failed in WebRtcAecm_Process\n");
|
||||
WebRtcAecm_Free(aecmInst);
|
||||
return -1;
|
||||
}
|
||||
memcpy(near_input, out_buffer, samples * sizeof(int16_t));
|
||||
near_input += samples;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
//char near_buf[1024],far_buf1[1024];
|
||||
//void aec_test(void)
|
||||
//{
|
||||
// FILE *near_fp = osal_fopen("near.pcm","rb");
|
||||
// FILE *far_fp = osal_fopen("far.pcm","rb");
|
||||
// FILE *output_fp = osal_fopen("out.pcm","wb");
|
||||
//
|
||||
// int near_read,far_read;
|
||||
//
|
||||
//// char near_buf[1024],far_buf[1024];
|
||||
// AecmConfig config;
|
||||
// config.cngMode = AecmTrue;
|
||||
// config.echoMode = 4;// 0, 1, 2, 3 (default), 4
|
||||
// void *aecmInst = WebRtcAecm_Create();
|
||||
// printf("aecm:%X\n",aecmInst);
|
||||
// WebRtcAecm_Init(aecmInst, 8000);
|
||||
// WebRtcAecm_set_config(aecmInst, config);
|
||||
// int ret;
|
||||
// while(1)
|
||||
// {
|
||||
// near_read = osal_fread(near_buf,1,1016,near_fp); //下一次播放的数据
|
||||
// far_read = osal_fread(far_buf1,1,1016,far_fp); //上一次
|
||||
// if(near_read == 0)
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
// ret = mcu_AECM(aecmInst,(int16_t *)near_buf, (int16_t *)far_buf1, 8000,near_read/2);
|
||||
// if(ret<0)
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
// osal_fwrite(near_buf,1,near_read,output_fp);
|
||||
//
|
||||
// }
|
||||
// if(ret>0)
|
||||
// {
|
||||
// WebRtcAecm_Free(aecmInst);
|
||||
// }
|
||||
// osal_fclose(near_fp);
|
||||
// osal_fclose(far_fp);
|
||||
// osal_fclose(output_fp);
|
||||
// printf("\n*******press any key to exit. \n");
|
||||
//}
|
||||
//struct os_task aec_test_task;
|
||||
//void aec_test_thread(void)
|
||||
//{
|
||||
// printf("\n**********aec_test_thread*************\n");
|
||||
// OS_TASK_INIT("aec_test",&aec_test_task,aec_test,0,OS_TASK_PRIORITY_NORMAL,10240);
|
||||
//}
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef _AEC_PROCESS_H_
|
||||
#define _AEC_PROCESS_H_
|
||||
|
||||
#include "../modules/audio_processing/aecm/echo_control_mobile.h"
|
||||
|
||||
extern void *aecmInst;
|
||||
|
||||
extern int aec_init(uint32_t sampleRate, int16_t nMode);
|
||||
extern int push_farbuf(short *data, unsigned int len, uint32_t sampleRate);
|
||||
extern int aec_process(int16_t *near_frame, uint32_t inSampleCount, int16_t msInSndCardBuf, uint32_t sampleRate);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,119 @@
|
||||
#include "agc_process.h"
|
||||
#include "typesdef.h"
|
||||
#include "osal/string.h"
|
||||
#include "osal/task.h"
|
||||
#include "osal_file.h"
|
||||
#include "custom_mem.h"
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
||||
#endif
|
||||
|
||||
static void *agcInst = NULL;
|
||||
|
||||
int agc_init(int16_t agcMode, uint32_t sampleRate)
|
||||
{
|
||||
agcInst = WebRtcAgc_Create();
|
||||
if (agcInst == NULL) return -1;
|
||||
|
||||
int minLevel = 0;
|
||||
int maxLevel = 1024;
|
||||
|
||||
WebRtcAgcConfig agcConfig;
|
||||
agcConfig.compressionGaindB = 50; // default 9 dB
|
||||
agcConfig.limiterEnable = 1; // default kAgcTrue (on)
|
||||
agcConfig.targetLevelDbfs = 12; // default 3 (-3 dBOv)
|
||||
|
||||
int status = WebRtcAgc_Init(agcInst, minLevel, maxLevel, agcMode, sampleRate);
|
||||
if (status != 0) {
|
||||
printf("WebRtcAgc_Init fail\n");
|
||||
WebRtcAgc_Free(agcInst);
|
||||
return -1;
|
||||
}
|
||||
status = WebRtcAgc_set_config(agcInst, agcConfig);
|
||||
if (status != 0) {
|
||||
printf("WebRtcAgc_set_config fail\n");
|
||||
WebRtcAgc_Free(agcInst);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int agc_process(int16_t *buffer, uint32_t sampleRate, size_t samplesCount) {
|
||||
if (!buffer) return -1;
|
||||
if (samplesCount == 0) return -1;
|
||||
|
||||
size_t samples = MIN(160, sampleRate / 100);
|
||||
if (samples == 0) return -1;
|
||||
int16_t *input = buffer;
|
||||
size_t nTotal = (samplesCount / samples);
|
||||
|
||||
size_t num_bands = 1;
|
||||
int inMicLevel, outMicLevel = -1;
|
||||
int16_t out_buffer[samples];
|
||||
int16_t *out16 = out_buffer;
|
||||
uint8_t saturationWarning = 1; //是否有溢出发生,增益放大以后的最大值超过了65536
|
||||
int16_t echo = 0; //增益放大是否考虑回声影响
|
||||
for (int i = 0; i < nTotal; i++) {
|
||||
inMicLevel = 0;
|
||||
int nAgcRet = WebRtcAgc_Process(agcInst, (const int16_t *const *) &input, num_bands, samples,
|
||||
(int16_t *const *) &out16, inMicLevel, &outMicLevel, echo,
|
||||
&saturationWarning);
|
||||
if (nAgcRet != 0) {
|
||||
os_printf("failed in WebRtcAgc_Process\n");
|
||||
WebRtcAgc_Free(agcInst);
|
||||
return -1;
|
||||
}
|
||||
os_memcpy(input, out_buffer, samples * sizeof(int16_t));
|
||||
input += samples;
|
||||
}
|
||||
|
||||
const size_t remainedSamples = samplesCount - nTotal * samples;
|
||||
if (remainedSamples > 0) {
|
||||
if (nTotal > 0) {
|
||||
input = input - samples + remainedSamples;
|
||||
}
|
||||
|
||||
inMicLevel = 0;
|
||||
int nAgcRet = WebRtcAgc_Process(agcInst, (const int16_t *const *) &input, num_bands, samples,
|
||||
(int16_t *const *) &out16, inMicLevel, &outMicLevel, echo,
|
||||
&saturationWarning);
|
||||
|
||||
if (nAgcRet != 0) {
|
||||
os_printf("failed in WebRtcAgc_Process during filtering the last chunk\n");
|
||||
WebRtcAgc_Free(agcInst);
|
||||
return -1;
|
||||
}
|
||||
os_memcpy(&input[samples-remainedSamples], &out_buffer[samples-remainedSamples], remainedSamples * sizeof(int16_t));
|
||||
input += samples;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
//static int16 read_data[240];
|
||||
//void agc_test(void)
|
||||
//{
|
||||
// void *in_fp = osal_fopen("infile.pcm","rb");
|
||||
// void *out_fp = osal_fopen("outfile.pcm","wb");
|
||||
//
|
||||
// int read_len = -1;
|
||||
// while(1)
|
||||
// {
|
||||
// read_len = osal_fread(read_data, 2, 240, in_fp);
|
||||
// if(read_len < 480)
|
||||
// break;
|
||||
// agc_process(read_data, 8000, 240);
|
||||
// osal_fwrite(read_data, 2, 240, out_fp);
|
||||
// }
|
||||
// osal_fclose(in_fp);
|
||||
// osal_fclose(out_fp);
|
||||
// printf("\n*******agc test finish********\n");
|
||||
//}
|
||||
//struct os_task agc_test_task;
|
||||
//void agc_test_thread(void)
|
||||
//{
|
||||
// printf("\n**********agc_test_thread*************\n");
|
||||
// agc_init(kAgcModeAdaptiveDigital, 8000);
|
||||
// OS_TASK_INIT("agc_test",&agc_test_task,agc_test,0,OS_TASK_PRIORITY_NORMAL,2048);
|
||||
//}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef _AGC_PROCESS_H_
|
||||
#define _AGC_PROCESS_H_
|
||||
|
||||
#include "../modules/audio_processing/agc/agc.h"
|
||||
|
||||
extern int agc_init(int16_t agcMode, uint32_t sampleRate);
|
||||
extern int agc_process(int16_t *buffer, uint32_t sampleRate, size_t samplesCount);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,74 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "noise_suppression_x.h"
|
||||
#include "nsx_defines.h"
|
||||
#include "typesdef.h"
|
||||
#include "osal/string.h"
|
||||
#include "osal/task.h"
|
||||
#include "osal_file.h"
|
||||
#include "custom_mem.h"
|
||||
|
||||
NsxHandle *nsInst = NULL;
|
||||
|
||||
int ns_init(uint32 samplerate)
|
||||
{
|
||||
nsInst = WebRtcNsx_Create();
|
||||
if(nsInst == NULL)
|
||||
{
|
||||
printf("WebRtcNs_Create err\n");
|
||||
return -1;
|
||||
}
|
||||
if (WebRtcNsx_Init(nsInst, samplerate) == -1) {
|
||||
printf("WebRtcNs_Init err\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ns_process(int16 *inbuffer, uint32 samplerate, uint32 samplecount)
|
||||
{
|
||||
int nBands = 1;
|
||||
uint32 samples = 80;
|
||||
uint32 frames = samplecount / samples;
|
||||
|
||||
for (int i = 0; i < frames; i++)
|
||||
{
|
||||
WebRtcNsx_Process(nsInst, inbuffer, nBands, inbuffer);
|
||||
inbuffer += samples;
|
||||
}
|
||||
}
|
||||
|
||||
//static int16_t read_data[240];
|
||||
//void ns_test(void)
|
||||
//{
|
||||
// os_sleep_ms(2000);
|
||||
// void *in_fp = osal_fopen("infile.pcm","rb");
|
||||
// void *out_fp = osal_fopen("outfile.pcm","wb");
|
||||
// if((!in_fp) || (!out_fp))
|
||||
// return;
|
||||
// int read_len = -1;
|
||||
// uint32 time = 0;
|
||||
// ns_init(8000);
|
||||
// while(1)
|
||||
// {
|
||||
// read_len = osal_fread(read_data, 2, 240, in_fp);
|
||||
// if(read_len < 480)
|
||||
// break;
|
||||
// time = os_jiffies();
|
||||
// ns_process(read_data, 8000, 240);
|
||||
// printf("t:%d\n",(os_jiffies()-time));
|
||||
// osal_fwrite(read_data, 2, 240, out_fp);
|
||||
// os_sleep_ms(1);
|
||||
// }
|
||||
//
|
||||
// printf("\n*******ns test finish********\n");
|
||||
// osal_fclose(in_fp);
|
||||
// osal_fclose(out_fp);
|
||||
//}
|
||||
//
|
||||
//struct os_task ns_test_task;
|
||||
//void ns_test_thread(void)
|
||||
//{
|
||||
// printf("\n**********ns_test_thread*************\n");
|
||||
// OS_TASK_INIT("ns_test",&ns_test_task,ns_test,0,OS_TASK_PRIORITY_NORMAL,5120);
|
||||
//}
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef _NS_PROCESS_H_
|
||||
#define _NS_PROCESS_H_
|
||||
|
||||
#include "../modules/audio_processing/ns/noise_suppression_x.h"
|
||||
extern int ns_init(uint32 samplerate);
|
||||
extern int ns_process(int16_t *buffer, uint32_t sampleRate, uint64_t samplesCount);
|
||||
#endif
|
||||
@@ -0,0 +1,99 @@
|
||||
#include "vad_process.h"
|
||||
#include "typesdef.h"
|
||||
#include "osal/string.h"
|
||||
#include "osal/task.h"
|
||||
#include "osal_file.h"
|
||||
#include "custom_mem.h"
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(A, B) ((A) < (B) ? (A) : (B))
|
||||
#endif
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(A, B) ((A) > (B) ? (A) : (B))
|
||||
#endif
|
||||
|
||||
void *vadInst =NULL;
|
||||
|
||||
int vad_init(int16_t vad_mode)
|
||||
{
|
||||
vadInst = WebRtcVad_Create();
|
||||
if (vadInst == NULL) return -1;
|
||||
int status = WebRtcVad_Init(vadInst);
|
||||
if (status != 0)
|
||||
{
|
||||
printf("WebRtcVad_Init fail\n");
|
||||
WebRtcVad_Free(vadInst);
|
||||
return -1;
|
||||
}
|
||||
status = WebRtcVad_set_mode(vadInst, vad_mode);
|
||||
if (status != 0)
|
||||
{
|
||||
printf("WebRtcVad_set_mode fail\n");
|
||||
WebRtcVad_Free(vadInst);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vad_process(int16_t *buffer, uint32_t sampleRate, size_t samplesCount, int per_ms_frames)
|
||||
{
|
||||
if (!buffer) return -1;
|
||||
if (samplesCount == 0) return -1;
|
||||
// kValidRates : 8000, 16000, 32000, 48000
|
||||
// 10, 20 or 30 ms frames
|
||||
per_ms_frames = MAX(MIN(30, per_ms_frames), 10);
|
||||
size_t samples = sampleRate * per_ms_frames / 1000;
|
||||
if (samples == 0) return -1;
|
||||
int16_t *input = buffer;
|
||||
size_t nTotal = (samplesCount / samples);
|
||||
uint32_t totalVadRet = 0;
|
||||
|
||||
for (int i = 0; i < nTotal; i++)
|
||||
{
|
||||
int keep_weight = 0;
|
||||
int nVadRet = WebRtcVad_Process(vadInst, sampleRate, input, samples, keep_weight);
|
||||
if (nVadRet == -1)
|
||||
{
|
||||
printf("failed in WebRtcVad_Process\n");
|
||||
WebRtcVad_Free(vadInst);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
totalVadRet += nVadRet;
|
||||
}
|
||||
input += samples;
|
||||
}
|
||||
return totalVadRet;
|
||||
}
|
||||
|
||||
//static int16 read_data[240];
|
||||
//void vad_test(void)
|
||||
//{
|
||||
// void *in_fp = osal_fopen("infile.pcm","rb");
|
||||
// void *out_fp = osal_fopen("outfile.pcm","wb");
|
||||
//
|
||||
// int read_len = -1;
|
||||
// int32 vad_ret = 0;
|
||||
//
|
||||
// while(1)
|
||||
// {
|
||||
// read_len = osal_fread(read_data, 2, 240, in_fp);
|
||||
// if(read_len < 480)
|
||||
// break;
|
||||
// vad_ret = vad_process(read_data, 8000, 240, 30);
|
||||
// if(vad_ret != 0)
|
||||
// osal_fwrite(read_data, 2, 240, out_fp);
|
||||
// }
|
||||
// osal_fclose(in_fp);
|
||||
// osal_fclose(out_fp);
|
||||
// printf("\n*******vad test finish********\n");
|
||||
//}
|
||||
//struct os_task vad_test_task;
|
||||
//void vad_test_thread(void)
|
||||
//{
|
||||
// printf("\n**********vad_test_thread*************\n");
|
||||
// vad_init(kVadNormal);
|
||||
// OS_TASK_INIT("vad_test",&vad_test_task,vad_test,0,OS_TASK_PRIORITY_NORMAL,2048);
|
||||
//}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef _VAD_PROCESS_H_
|
||||
#define _VAD_PROCESS_H_
|
||||
|
||||
#include "../modules/audio_processing/vad/vad.h"
|
||||
|
||||
extern int vad_init(int16_t vad_mode);
|
||||
extern int vad_process(int16_t *buffer, uint32_t sampleRate, size_t samplesCount, int per_ms_frames);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user