Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions tools/plugin/modules/ov_noise_suppression/noise_suppression.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,60 @@ static int ns_init(struct processing_module *mod)

static int
ns_process(struct processing_module *mod,
struct input_stream_buffer *input_buffers, int num_input_buffers,
struct output_stream_buffer *output_buffers, int num_output_buffers)
struct sof_source **sources, int num_of_sources,
struct sof_sink **sinks, int num_of_sinks)
Comment on lines 52 to +54
{
ns_handle handle = module_get_private_data(mod);
struct sof_source *source = sources[0];
struct sof_sink *sink = sinks[0];
const size_t frame_bytes = source_get_frame_bytes(source);
struct cir_buf_source src_desc;
struct cir_buf_sink snk_desc;
size_t buf_size;
size_t copy_bytes;
size_t frames;
int ret;

ret = ov_ns_process(handle, input_buffers, num_input_buffers,
output_buffers, num_output_buffers);
if (ret < 0)
frames = source_get_data_frames_available(source);
frames = MIN(frames, sink_get_free_frames(sink));
Comment on lines +67 to +68

/* Noise suppression keeps the stream format and channel count, so the
* source and sink consume and produce the same number of bytes.
*/
copy_bytes = frames * frame_bytes;
if (copy_bytes == 0)
return 0;

ret = source_get_data(source, copy_bytes, &src_desc.ptr, &src_desc.buf_start, &buf_size);
if (ret)
return ret;

src_desc.buf_end = (const char *)src_desc.buf_start + buf_size;

ret = sink_get_buffer(sink, copy_bytes, &snk_desc.ptr, &snk_desc.buf_start, &buf_size);
if (ret) {
source_release_data(source, 0);
return ret;
}

snk_desc.buf_end = (char *)snk_desc.buf_start + buf_size;

ret = ov_ns_process(handle, &src_desc, &snk_desc, frames);
if (ret < 0) {
source_release_data(source, 0);
sink_commit_buffer(sink, 0);
return ret;
}

module_update_buffer_position(&input_buffers[0], &output_buffers[0], ret);
source_release_data(source, ret * frame_bytes);
sink_commit_buffer(sink, ret * frame_bytes);

return 0;
}

static const struct module_interface ns_interface = {
.init = ns_init,
.process_audio_stream = ns_process,
.process = ns_process,
.free = ns_free
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,16 @@ extern "C" {
}

int ov_ns_process(ns_handle handle,
struct input_stream_buffer *input_buffers, int num_input_buffers,
struct output_stream_buffer *output_buffers, int num_output_buffers)
struct cir_buf_source *source,
struct cir_buf_sink *sink,
size_t frame_count)
{
struct audio_stream *source = (struct audio_stream *)input_buffers[0].data;
struct audio_stream *sink = (struct audio_stream *)output_buffers[0].data;
struct ns_data *nd = (struct ns_data *)handle;
std::vector<float> inp_wave_fp32, out_wave_fp32;
/* only 16-bit supported for now */
int16_t *input_data = (int16_t *)audio_stream_get_rptr(source);
int16_t *output_data = (int16_t *)audio_stream_get_wptr(sink);
uint32_t frame_count = input_buffers[0].size;
const int16_t *input_data = static_cast<const int16_t >(source->ptr);
int16_t *output_data = static_cast<int16_t *>(sink->ptr);
float scale = 1.0f / std::numeric_limits<int16_t>::max();
int i, j, ch;

/*
* The noise suppression model only supports mono, so process each channel
Expand All @@ -121,17 +118,17 @@ extern "C" {
inp_wave_fp32.resize(frame_count, 0);
out_wave_fp32.resize(frame_count, 0);

for (ch = 0; ch < NS_MAX_SOURCE_CHANNELS; ch++) {
/* split each channel samples and convert to floating point */
for (i = ch, j = 0; j < frame_count; i+=2,j++) {
void *inp = &input_data[i];
for (unsigned int ch = 0; ch < NS_MAX_SOURCE_CHANNELS; ch++) {
const int16_t *inp = input_data + ch;

/* wrap if needed */
if (inp >= source->end_addr)
inp = (char *)source->addr +
((char *)inp - (char *)source->end_addr);
/* split each channel samples and convert to floating point */
for (size_t i = 0, i < frame_count; i++) {
inp_wave_fp32[i] = static_cast<float>(*inp) * scale;

inp_wave_fp32[j] = (float)(*(int16_t *)inp) * scale;
inp = static_cast<const int16_t *>(
source_cir_buf_wrap(inp + NS_MAX_SOURCE_CHANNELS,
source->buf_start,
source->buf_end));
}

ov::Tensor input_tensor(ov::element::f32, nd->inp_shape,
Expand Down Expand Up @@ -170,17 +167,16 @@ extern "C" {
std::memcpy(dst, src, frame_count * sizeof(float));

/* convert back to int and write back to output buffer */
for (i = 0, j = ch; i < frame_count; i++,j+=2) {
float v = out_wave_fp32[i];
void *out = &output_data[j];
int16_t *out = output_data + ch;
for (size_t i = 0; i < frame_count; i++) {
float v = std::clamp(out_wave_fp32[i], -1.0f, +1.0f);

/* wrap if needed */
if (out >= sink->end_addr)
out = (char *)sink->addr +
((char *)out - (char *)sink->end_addr);
*out = static_cast<int16_t>(v * std::numeric_limits<int16_t>::max());

v = std::clamp(v, -1.0f, +1.0f);
*(int16_t *)out = (int16_t)(v * std::numeric_limits<int16_t>::max());
out = static_cast<int16_t *>(
cir_buf_wrap(out + NS_MAX_SOURCE_CHANNELS,
sink->buf_start,
sink->buf_end));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
#ifndef _NOISE_SUPPRESSION_INTERFACE_H
#define _NOISE_SUPPRESSION_INTERFACE_H

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif
struct cir_buf_source;
struct cir_buf_sink;
typedef void *ns_handle;
int ov_ns_init(ns_handle *handle);
void ov_ns_free(ns_handle handle);
int ov_ns_process(ns_handle handle,
struct input_stream_buffer *input_buffers, int num_input_buffers,
struct output_stream_buffer *output_buffers, int num_output_buffers);
struct cir_buf_source *source,
struct cir_buf_sink *sink,
size_t frame_count);

#ifdef __cplusplus
}
Expand Down
Loading