Open wmailn opened 1 year ago
ns.cc
static const int framesize = 80;
// static const int _sample_rates[NUMBER_SAMPLE_RATES] = {8000, 16000, 32000, 48000};
typedef struct MSWebRtcNs { NsxHandle *nsxInst = nullptr; int sample_rate = 8000; int framesize; MSBufferizer echo;
// int enable = 1; // Disable if the sample rate doesn't match webrtc requirement
// int silence_detection_enabled = 0; // silence detection enabled information
// uint64_t silence_duration = 0; // silence threshold duration in ms
// uint64_t last_voice_detection = 0; // last time voice was detected
// int silence_event_send = 0;
} MSWebRtcNs;
static int ns_set_sample_rate(MSFilter f, void arg) { MSWebRtcNs ns = static_cast<MSWebRtcNs >(f->data); ns->sample_rate = (int )arg; ms_message("--wumeng--ns--[M:%s]: sample_rate:%d", FUNCTION, ns->sample_rate);
return 0;
}
static void ns_init(MSFilter f) { ms_message("--wumeng--ns--[M:%s]: ", FUNCTION); MSWebRtcNs ns = new MSWebRtcNs(); ns->nsxInst = WebRtcNsx_Create(); if (WebRtcNsx_Init(ns->nsxInst, ns->sample_rate) == -1){ ns->nsxInst = nullptr; ms_error("--wumeng--ns--[M:%s]: WebRtcNsx_Init fail !!!", FUNCTION); } // 降噪级别 0 ~ 3 if (WebRtcNsx_set_policy(ns->nsxInst, 3) == -1){ ms_warning("--wumeng--ns--[M:%s]: WebRtcNsx_set_policy fail !!!", FUNCTION); } ns->framesize = framesize; ms_bufferizer_init(&ns->echo); f->data = ns; }
static void ns_process(MSFilter f) { MSWebRtcNs ns = static_cast<MSWebRtcNs *>(f->data);
int nbytes = ns->framesize * sizeof(short);
short **outFrame = new short *[2];
short **echo = new short *[2];
echo[0] = (short *)ms_malloc0(nbytes);
outFrame[0] = (short *)ms_malloc0(nbytes);
mblk_t *in = ms_queue_get(f->inputs[0]);
ms_bufferizer_put_from_queue(&ns->echo, f->inputs[0]);
while (ms_bufferizer_read(&ns->echo, (uint8_t *)echo[0], (size_t)nbytes) >= (size_t)nbytes){
WebRtcNsx_Process(ns->nsxInst, echo, 1, outFrame);
mblk_t *refm = allocb(nbytes, 0);
memset(refm->b_wptr, 0, nbytes);
memcpy(refm->b_rptr, outFrame[0], nbytes);
refm->b_wptr += nbytes;
ms_queue_put(f->outputs[0], refm);
}
ms_free(echo[0]);
ms_free(outFrame[0]);
delete outFrame;
delete echo;
}
static void ns_uninit(MSFilter f) { ms_message("--wumeng--ns--[M:%s]: ", FUNCTION); MSWebRtcNs ns = static_cast<MSWebRtcNs *>(f->data); if (ns->nsxInst){ WebRtcNsx_Free(ns->nsxInst); } ns->nsxInst = nullptr; ms_bufferizer_flush(&ns->echo); delete ns; f->data = nullptr; }
static MSFilterMethod ns_methods[]={ { MS_FILTER_SET_SAMPLE_RATE, ns_set_sample_rate}, { 0, nullptr } };
extern "C" MSFilterDesc ms_webrtc_ns_desc = { MS_FILTER_PLUGIN_ID, "MSWebRtcNSDec", "WebRtc's NS", MS_FILTER_OTHER, "NS", 1, // Inputs 1, // Outputs ns_init, nullptr, ns_process, nullptr, ns_uninit, ns_methods, MS_FILTER_IS_PUMP };
Due to network restrictions, I cannot upload the code in the form of attachment. I can only send it in the comment as text. I'm very sorry.
Hello: My linphone-sdk version is 5.2.0 and I found that I could use the AEC and VAD modules of webrtc directly, but not the noise reduction module. I try to write and connect the Filter of noise reduction module in accordance with the AEC module, I see in the log that the WebRtcNsx_Process method has been executed, but there is no effect. May I ask if there is something wrong with my code? Can you help me to enable the noise reduction module? I'll post my code below.