Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello, may I ask how to use the noise reduction module of webrtc #1

Open
wmailn opened this issue Feb 6, 2023 · 2 comments
Open

Hello, may I ask how to use the noise reduction module of webrtc #1

wmailn opened this issue Feb 6, 2023 · 2 comments

Comments

@wmailn
Copy link

wmailn commented Feb 6, 2023

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.

@wmailn
Copy link
Author

wmailn commented Feb 6, 2023

ns.cc

#include "noise_suppression_x.h"
#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/msticker.h"

#define NUMBER_SAMPLE_RATES 4

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
};

@wmailn
Copy link
Author

wmailn commented Feb 6, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant