示例#1
0
AudioDevice* AudioDevice::create(){
	int ret;
	AudioDeviceImpl *impl = new AudioDeviceImpl();
	
	impl->audio = webrtc::CreateAudioDeviceModule(0, webrtc::AudioDeviceModule::kPlatformDefaultAudio);
	if(!impl->audio){
		goto err;
	}
	ret = impl->audio->Init();
	if(ret == -1){
		goto err;
	}
	
	WebRtcVad_Create(&impl->vad);
	if(!impl->vad){
		goto err;
	}
	ret = WebRtcVad_Init(impl->vad);
	if(ret == -1){
		goto err;
	}

	{	
		uint32_t device_rate = 0;
		ret = impl->audio->RecordingSampleRate(&device_rate);
		if(ret == -1){
			goto err;
		}
		impl->set_input_sample_rate(device_rate);
	}

	{
		uint32_t device_rate = 0;
		ret = impl->audio->PlayoutSampleRate(&device_rate);
		if(ret == -1){
			goto err;
		}
		impl->set_output_sample_rate(device_rate);
	}
	ret = impl->audio->RegisterAudioCallback(impl);
	if(ret == -1){
		goto err;
	}

	return impl;
err:
	delete impl;
	return NULL;
}
示例#2
0
RTC::ReturnCode_t WebRTCVAD::onInitialize()
{
  RTC_DEBUG(("onInitialize start"));
  RTC_INFO(("WebRTCVAD : WebRTC based noise reduction component"));
  RTC_INFO((" Copyright (C) 2010-2011 Yosuke Matsusaka and AIST-OpenHRI development team"));
  RTC_INFO((" Version %s", VERSION));

  // Registration: InPort/OutPort/Service
  // <rtc-template block="registration">
  // Set InPort buffers
  addInPort("AudioDataIn", m_ninIn);
  m_ninIn.setDescription(_("Audio data input."));

  /* setiing datalistener event */
  m_ninIn.addConnectorDataListener(ON_BUFFER_WRITE,
                        new DataListener("ON_BUFFER_WRITE", this));

  // Set OutPort buffer
  addOutPort("AudioDataOut", m_foutOut);
  m_foutOut.setDescription(_("Audio data output."));

  // Set service provider to Ports

  // Set service consumers to Ports

  // Set CORBA Service Ports

  // </rtc-template>
  bindParameter("FilterLength", m_bufferlen, "5");//ADDED

  WebRtcVad_Create(&handle);
  WebRtcVad_Init(handle);
  WebRtcVad_set_mode(handle, 2); // agressive adaptation mode

  RTC_DEBUG(("onInitialize finish"));
  return RTC::RTC_OK;
}
示例#3
0
Filter_Audio *new_filter_audio(uint32_t fs)
{
    if (fs == 0) {
        return NULL;
    }

    Filter_Audio *f_a = calloc(sizeof(Filter_Audio), 1);

    if (!f_a) {
        return NULL;
    }

    f_a->fs = fs;

    if (fs != 16000)
        fs = 32000;

    init_highpass_filter_zam(&f_a->hpfa, 100, (float) f_a->fs);
    init_highpass_filter_zam(&f_a->hpfb, 100, (float) f_a->fs);

    unsigned int lowpass_filter_frequency = 12000;
    if (f_a->fs > (lowpass_filter_frequency * 2)) {
        init_lowpass_filter_zam(&f_a->lpfa, lowpass_filter_frequency, (float) f_a->fs);
        init_lowpass_filter_zam(&f_a->lpfb, lowpass_filter_frequency, (float) f_a->fs);
        f_a->lowpass_enabled = 1;
    }

    if (WebRtcAgc_Create(&f_a->gain_control) == -1) {
        free(f_a);
        return NULL;
    }

    if (WebRtcNsx_Create(&f_a->noise_sup_x) == -1) {
        WebRtcAgc_Free(f_a->gain_control);
        free(f_a);
        return NULL;
    }

    if (WebRtcAec_Create(&f_a->echo_cancellation) == -1) {
        WebRtcAgc_Free(f_a->gain_control);
        WebRtcNsx_Free(f_a->noise_sup_x);
        free(f_a);
        return NULL;
    }

    if (WebRtcVad_Create(&f_a->Vad_handle) == -1){
        WebRtcAec_Free(f_a->echo_cancellation);
        WebRtcAgc_Free(f_a->gain_control);
        WebRtcNsx_Free(f_a->noise_sup_x);
        free(f_a);
        return NULL;
    }

    WebRtcAgc_config_t gain_config;

    gain_config.targetLevelDbfs = 1;
    gain_config.compressionGaindB = 20;
    gain_config.limiterEnable = kAgcTrue;

    if (WebRtcAgc_Init(f_a->gain_control, 0, 255, kAgcModeAdaptiveDigital, fs) == -1 || WebRtcAgc_set_config(f_a->gain_control, gain_config) == -1) {
        kill_filter_audio(f_a);
        return NULL;
    }


    if (WebRtcNsx_Init(f_a->noise_sup_x, fs) == -1 || WebRtcNsx_set_policy(f_a->noise_sup_x, 2) == -1) {
        kill_filter_audio(f_a);
        return NULL;
    }

    AecConfig echo_config;

    echo_config.nlpMode = kAecNlpAggressive;
    echo_config.skewMode = kAecFalse;
    echo_config.metricsMode = kAecFalse;
    echo_config.delay_logging = kAecFalse;

    if (WebRtcAec_Init(f_a->echo_cancellation, fs, f_a->fs) == -1 || WebRtcAec_set_config(f_a->echo_cancellation, echo_config) == -1) {
        kill_filter_audio(f_a);
        return NULL;
    }

    int vad_mode = 1;  //Aggressiveness mode (0, 1, 2, or 3).
    if (WebRtcVad_Init(f_a->Vad_handle) == -1 || WebRtcVad_set_mode(f_a->Vad_handle,vad_mode) == -1){
        kill_filter_audio(f_a);
        return NULL;
    }

    f_a->echo_enabled = 1;
    f_a->gain_enabled = 1;
    f_a->noise_enabled = 1;
    f_a->vad_enabled = 1;

    int quality = 4;
    if (f_a->fs != 16000) {
        f_a->downsampler = speex_resampler_init(1, f_a->fs, 32000, quality, 0);
        f_a->upsampler = speex_resampler_init(1, 32000, f_a->fs, quality, 0);

         /* quality doesn't need to be high for this one. */
        quality = 0;
        f_a->downsampler_echo = speex_resampler_init(1, f_a->fs, 16000, quality, 0);

        if (!f_a->upsampler || !f_a->downsampler || !f_a->downsampler_echo) {
            kill_filter_audio(f_a);
            return NULL;
        }
    }


    return f_a;
}
示例#4
0
int main(int argc, char* argv[])
{
    FILE * infile, *outfile, *statefile;
    short res=0,errtype;
    /*float time=0.0;*/
    
    WebRtcVad_Create(&vinst);
    WebRtcVad_Init(vinst);
    
    short size;
    int samps=0;
    
    if (argc < 5){
        printf("Usage:\n CNG.exe infile outfile samplingfreq(Hz) interval(ms) order\n\n");
        return(0);
    }
    
    infile=fopen(argv[1],"rb");
    if (infile==NULL){
        printf("file %s does not exist\n",argv[1]);
        return(0);
    }
    outfile=fopen(argv[2],"wb");
    statefile=fopen("CNGVAD.d","wb");
    if (outfile==NULL){
        printf("file %s could not be created\n",argv[2]);
        return(0); 
    }
    
    unsigned int fs=16000;
    short frameLen=fs/50;
    
#ifndef ASSIGN
    res=WebRtcCng_CreateEnc(&e_inst);
    if (res < 0) {
        /* exit if returned with error */
        errtype=WebRtcCng_GetErrorCodeEnc(e_inst);
        fprintf(stderr,"\n\n Error in initialization: %d.\n\n", errtype);
        exit(EXIT_FAILURE);
    }
    res=WebRtcCng_CreateDec(&d_inst);
    if (res < 0) {
        /* exit if returned with error */
        errtype=WebRtcCng_GetErrorCodeDec(d_inst);
        fprintf(stderr,"\n\n Error in initialization: %d.\n\n", errtype);
        exit(EXIT_FAILURE);
    }
    
#else
    
    // Test the Assign-functions
    int Esize, Dsize;    
    void *Eaddr, *Daddr;
    
    res=WebRtcCng_AssignSizeEnc(&Esize);
    res=WebRtcCng_AssignSizeDec(&Dsize);
    Eaddr=malloc(Esize);
    Daddr=malloc(Dsize);
    
    res=WebRtcCng_AssignEnc(&e_inst, Eaddr);
    if (res < 0) {
        /* exit if returned with error */
        errtype=WebRtcCng_GetErrorCodeEnc(e_inst);
        fprintf(stderr,"\n\n Error in initialization: %d.\n\n", errtype);
        exit(EXIT_FAILURE);
    }
    
    res=WebRtcCng_AssignDec(&d_inst, Daddr);
    if (res < 0) {
        /* exit if returned with error */
        errtype=WebRtcCng_GetErrorCodeDec(d_inst);
        fprintf(stderr,"\n\n Error in initialization: %d.\n\n", errtype);
        exit(EXIT_FAILURE);
    }
    
#endif
    
    res=WebRtcCng_InitEnc(e_inst,atoi(argv[3]),atoi(argv[4]),atoi(argv[5])); 
    if (res < 0) {
        /* exit if returned with error */
        errtype=WebRtcCng_GetErrorCodeEnc(e_inst);
        fprintf(stderr,"\n\n Error in initialization: %d.\n\n", errtype);
        exit(EXIT_FAILURE);
    }
    
    res=WebRtcCng_InitDec(d_inst); 
    if (res < 0) {
        /* exit if returned with error */
        errtype=WebRtcCng_GetErrorCodeDec(d_inst);
        fprintf(stderr,"\n\n Error in initialization: %d.\n\n", errtype);
        exit(EXIT_FAILURE);
    }
    
    
    static bool firstSilent=true;
    
    int numSamp=0;
    int speech=0;
    int silent=0;
    long cnt=0;
    
    while(fread(anaSpeech,2,frameLen,infile)==frameLen){
        
        cnt++;
        if (cnt==60){
            cnt=60;
        }
        /*  time+=(float)frameLen/fs;
        numSamp+=frameLen;
        float temp[640];
        for(unsigned int j=0;j<frameLen;j++)
        temp[j]=(float)anaSpeech[j]; */
        
        //        if(!WebRtcVad_Process(vinst, fs, anaSpeech, frameLen)){
        

        if(1){ // Do CNG coding of entire file

            //        if(!((anaSpeech[0]==0)&&(anaSpeech[1]==0)&&(anaSpeech[2]==0))){
            if(firstSilent){
                res = WebRtcCng_Encode(e_inst, anaSpeech, frameLen/2, SIDpkt,&size,1);
                if (res < 0) {
                    /* exit if returned with error */
                    errtype=WebRtcCng_GetErrorCodeEnc(e_inst);
                    fprintf(stderr,"\n\n Error in encoder: %d.\n\n", errtype);
                    exit(EXIT_FAILURE);
                }
                
                
                firstSilent=false;
                
                res=WebRtcCng_Encode(e_inst, &anaSpeech[frameLen/2], frameLen/2, SIDpkt,&size,1);
                if (res < 0) {
                    /* exit if returned with error */
                    errtype=WebRtcCng_GetErrorCodeEnc(e_inst);
                    fprintf(stderr,"\n\n Error in encoder: %d.\n\n", errtype);
                    exit(EXIT_FAILURE);
                }
                
            }
            else{
                res=WebRtcCng_Encode(e_inst, anaSpeech, frameLen/2, SIDpkt,&size,0);
                if (res < 0) {
                    /* exit if returned with error */
                    errtype=WebRtcCng_GetErrorCodeEnc(e_inst);
                    fprintf(stderr,"\n\n Error in encoder: %d.\n\n", errtype);
                    exit(EXIT_FAILURE);
                }
                res=WebRtcCng_Encode(e_inst, &anaSpeech[frameLen/2], frameLen/2, SIDpkt,&size,0);
                if (res < 0) {
                    /* exit if returned with error */
                    errtype=WebRtcCng_GetErrorCodeEnc(e_inst);
                    fprintf(stderr,"\n\n Error in encoder: %d.\n\n", errtype);
                    exit(EXIT_FAILURE);
                }
            }
            
            if(size>0){
                res=WebRtcCng_UpdateSid(d_inst,SIDpkt, size);
                if (res < 0) {
                    /* exit if returned with error */
                    errtype=WebRtcCng_GetErrorCodeDec(d_inst);
                    fprintf(stderr,"\n\n Error in decoder: %d.\n\n", errtype);
                    exit(EXIT_FAILURE);
                }
            }
            res=WebRtcCng_Generate(d_inst,genSpeech, frameLen,0);
                if (res < 0) {
                    /* exit if returned with error */
                    errtype=WebRtcCng_GetErrorCodeDec(d_inst);
                    fprintf(stderr,"\n\n Error in decoder: %d.\n\n", errtype);
                    exit(EXIT_FAILURE);
                }
            memcpy(state,anaSpeech,2*frameLen);
        }
        else{
            firstSilent=true;
            memcpy(genSpeech,anaSpeech,2*frameLen);
            
            memset(anaSpeech,0,frameLen*2);
            memset(state,0,frameLen*2);
            
        }
        fwrite(genSpeech,2,frameLen,outfile);
        fwrite(state,2,frameLen,statefile);
        
    }
    
    fclose(infile);
    fclose(outfile);
    fclose(statefile);
    return 0;
}