• webrtc-AudioprcessingModule 3A算法demo


    以2019年代码为例;


    // // Created by yizhimao on 2020. // #import "ViewController.h" #import "ViewController.h" #include "audio_processing.h" #include"module_common_types.h" @interface ViewController () @property webrtc::AudioProcessing* apm; @end /////////////////////////////////// bool is_echo_cancel = false; FILE *echo_in = NULL; int delay_ms = -1; ////////////////////////////////////// @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [ self setparam:2 NAME:"AEC"]; [self createAudio]; // Do any additional setup after loading the view. } -(int)setparam:(int) level NAME:(char *)arg{ // Usage example, omitting error checking: _apm = webrtc::AudioProcessing::Create(); _apm->high_pass_filter()->Enable(true); if(arg == "ANR") { switch (level) { case 0: _apm->noise_suppression()->set_level(webrtc::NoiseSuppression::kLow); break; case 1: _apm->noise_suppression()->set_level(webrtc::NoiseSuppression::kModerate); break; case 2: _apm->noise_suppression()->set_level(webrtc::NoiseSuppression::kHigh); break; case 3: _apm->noise_suppression()->set_level(webrtc::NoiseSuppression::kVeryHigh); break; default: _apm->noise_suppression()->set_level(webrtc::NoiseSuppression::kVeryHigh); } _apm->noise_suppression()->Enable(true);//噪声抑制组件,client必须启用 //---------------------------------------------------------------------------------- }else if (arg == "AGC") { _apm->gain_control()->set_analog_level_limits(0, 100); _apm->gain_control()->set_mode(webrtc::GainControl::kAdaptiveDigital);//模拟信号字自适应调节 //apm->gain_control()->set_mode(webrtc::GainControl::kAdaptiveAnalog); //Default Gain control mode is kAdaptiveAnalog, so if mode is not set, //that means kAdaptiveAnalog is being used. _apm->gain_control()->Enable(true); } else if (arg == "AEC") { webrtc::EchoCancellation *echo_cancell = _apm->echo_cancellation(); echo_cancell->enable_drift_compensation(false);//d是否启用补偿机制 switch (level) {//回声抑制增强等级 case 0: echo_cancell->set_suppression_level(webrtc::EchoCancellation::kLowSuppression); break; case 1: echo_cancell->set_suppression_level(webrtc::EchoCancellation::kModerateSuppression); break; case 2: echo_cancell->set_suppression_level(webrtc::EchoCancellation::kHighSuppression); } is_echo_cancel = true; delay_ms = 0; //////////////////////////// NSArray *dicts = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [dicts objectAtIndex:0]; NSLog(@"the path is %@", path); echo_in = fopen([[NSString stringWithFormat:@"%@/Captureccref.pcm", path] UTF8String], "rb");//参考信号 if(echo_in == NULL) { return -1; } //////////////////////////////////////// echo_cancell->Enable(true); _apm->gain_control()->Enable(false); _apm->noise_suppression()->Enable(false); }else { delete _apm; } // _apm->level_estimator()->Enable(true);//启用重试次数估计组件 // _apm->echo_cancellation()->Enable(true);//启用回声消除组件 // _apm->echo_cancellation()->enable_metrics(true);//允许计算回波指标 // _apm->echo_cancellation()->enable_drift_compensation(true); //启用时钟补偿模块(声音捕捉设备的时钟频率和播放设备的时钟频率可能不一样) // _apm->gain_control()->Enable(true);//启用增益控制组件,client必须启用哦! // _apm->high_pass_filter()->Enable(true);//高通过滤器组件,过滤DC偏移和低频噪音,client必须启用 // _apm->voice_detection()->Enable(true);//启用语音检测组件,检测是否有说话声 // _apm->voice_detection()->set_likelihood( webrtc::VoiceDetection::kModerateLikelihood);//设置语音检测的阀值,阀值越大,语音越不容易被忽略,同样一些噪音可能被当成语音。 _apm->Initialize();//保留所有用户设置的情况下重新初始化apm的内部状态,用于开始处理一个新的音频流。第一个流创建之后不一定需要调用此方法。 return 0; } bool ReadFrame(FILE* file, webrtc::AudioFrame* frame) { // The files always contain stereo audio. size_t frame_size = frame->samples_per_channel_; size_t read_count = fread(frame->data_, sizeof(int16_t),//以short的形式读取,16位精度 frame_size, file); if (read_count != frame_size) { // Check that the file really ended. return false; // This is expected. } return true; } // bool WriteFrame(FILE* file, webrtc::AudioFrame* frame) { // The files always contain stereo audio. size_t frame_size = frame->samples_per_channel_; size_t read_count = fwrite (frame->data_, sizeof(int16_t), frame_size, file); if (read_count != frame_size) { return false; // This is expected. } return true; } -(void)createAudio{ webrtc::AudioFrame *frame = new webrtc::AudioFrame(); float frame_step = 10; // ms frame->sample_rate_hz_ = 8000; frame->samples_per_channel_ = frame->sample_rate_hz_ * frame_step / 1000.0;//10毫秒采集多少采样点(后边读数据会按照精度转成short来读) frame->num_channels_ = 1; webrtc::AudioFrame *echo_frame = NULL; if (is_echo_cancel) { echo_frame = new webrtc::AudioFrame(); echo_frame->sample_rate_hz_ = 8000; echo_frame->samples_per_channel_ = echo_frame->sample_rate_hz_ * frame_step / 1000.0; echo_frame->num_channels_ = 1; } //------- static FILE *wav_in = NULL; static FILE *wav_out = NULL; if (wav_in == NULL ||wav_out == NULL ) { NSArray *dicts = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [dicts objectAtIndex:0]; NSLog(@"the path is %@", path); wav_in = fopen([[NSString stringWithFormat:@"%@/Capturecc.pcm", path] UTF8String], "rb"); wav_out = fopen([[NSString stringWithFormat:@"%@/waveoutAEC.pcm", path] UTF8String], "wb"); } //---------- if (wav_in == NULL ||wav_out == NULL ) { return ; } int num_frame = 0; while (ReadFrame(wav_in, frame)) { num_frame += 1; if (is_echo_cancel) { if (ReadFrame(echo_in, echo_frame)) { _apm->ProcessReverseStream(echo_frame);//分析处理远端信号(参考信号,渲染信号) _apm->set_stream_delay_ms(10);//apm->set_stream_delay_ms(0);设置延时值 } } // if(_apm->gain_control()->is_enabled()) {// //apm->gain_control()->set_stream_analog_level(level); // //apm->gain_control()->set_target_level_dbfs(level); // } _apm->ProcessStream(frame);//处理近端(采集)数据;如果需要启用的功能 任何带有set_stream_tag标记的函数必须在处理当前帧之前调用;包括各个环节的处理。(如增益调节、回声消除、噪声抑制、语音检测、高通过率等,没有解码!是针对pcm数据做处理的) WriteFrame(wav_out, frame); } fclose(wav_in); fclose(wav_out); if(is_echo_cancel && echo_in) fclose(echo_in); if(frame) { delete frame; frame = NULL; } if (is_echo_cancel) delete echo_frame; if(_apm) { delete _apm; _apm = NULL; } } @end

     

     我们扣一些文件来测测它的3A算法

  • 相关阅读:
    ACM: POJ 1401 Factorial-数论专题-水题
    ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法
    ACM: HDU 1028 Ignatius and the Princess III-DP
    ACM: HDU 2563 统计问题-DFS+打表
    ACM: How many integers can you find-数论专题-容斥原理的简单应用+GCD
    ACM: Happy 2004-数论专题-因子求和-快速幂
    ACM:a^b%p-数论-快速幂-快速乘
    ACM: 强化训练-Beautiful People-最长递增子序列变形-DP
    POJ 1472 Instant Complexity 应该叫它编程题。。
    POJ 3393 Lucky and Good Months by Gregorian Calendar 模拟题
  • 原文地址:https://www.cnblogs.com/8335IT/p/14915522.html
Copyright © 2020-2023  润新知