Skip to content

Instantly share code, notes, and snippets.

@JanX2
Created October 5, 2017 11:22
Show Gist options
  • Save JanX2/b4d283b0ab83986b16315cf2817ee50d to your computer and use it in GitHub Desktop.
Save JanX2/b4d283b0ab83986b16315cf2817ee50d to your computer and use it in GitHub Desktop.
/* Copyright (c) 2017 Mozilla */
/* Copyright (c) 2017 Jan Weiß */
/*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include "rnnoise.h"
#define FRAME_SIZE 480
#define SAMPLE_RATE 48000
#define LOG_VAD_PROBABILITY 0 // In Audacity text label format.
int
main(int argc, const char* argv[])
{
int i;
int first = 1;
float x[FRAME_SIZE];
#if LOG_VAD_PROBABILITY
const double frame_length = (double)FRAME_SIZE / (double)SAMPLE_RATE;
#endif
FILE *in_file_handle, *out_file_handle;
#if LOG_VAD_PROBABILITY
FILE* vad_file_handle;
#endif
DenoiseState* st;
st = rnnoise_create();
if (argc != 3) {
fprintf(stderr, "usage: %s <noisy speech> <output denoised>\n", argv[0]);
return EXIT_FAILURE;
}
const char* input_path = argv[1];
const char* output_path = argv[2];
in_file_handle = fopen(input_path, "r");
out_file_handle = fopen(output_path, "w");
#if LOG_VAD_PROBABILITY
const size_t output_path_length = strlen(output_path);
const char* suffix = ".vad.txt";
const size_t suffix_length = strlen(suffix);
const size_t vad_path_length = output_path_length + suffix_length + 1;
char vad_path[vad_path_length];
strncat(vad_path, output_path, vad_path_length);
strncat(vad_path, suffix, vad_path_length);
vad_file_handle = fopen(vad_path, "w");
size_t frame_index = 0;
#endif
while (1) {
if (feof(in_file_handle)) {
break;
}
short tmp[FRAME_SIZE];
size_t samples_read = fread(tmp, sizeof(short), FRAME_SIZE, in_file_handle);
if (samples_read == 0) {
break;
}
for (i = 0; i < samples_read; i += 1) {
x[i] = (float)tmp[i];
}
for (; i < FRAME_SIZE; i += 1) {
x[i] = (float)0;
}
#if LOG_VAD_PROBABILITY
float vad_prob =
#endif
rnnoise_process_frame(st, x, x);
#if LOG_VAD_PROBABILITY
double frame_start = frame_length * frame_index;
double frame_end;
if (samples_read == FRAME_SIZE) {
frame_end = frame_start + frame_length;
} else {
frame_end = frame_start +
(frame_length * (double)samples_read / (double)FRAME_SIZE);
}
fprintf(vad_file_handle, "%0.6f\t%0.6f\t%f\n", frame_start, frame_end,
vad_prob);
#endif
for (i = 0; i < samples_read; i += 1) {
tmp[i] = x[i];
}
if (!first) {
fwrite(tmp, sizeof(short), samples_read, out_file_handle);
}
first = 0;
#if LOG_VAD_PROBABILITY
frame_index += 1;
#endif
}
rnnoise_destroy(st);
fclose(in_file_handle);
fclose(out_file_handle);
#if LOG_VAD_PROBABILITY
fclose(vad_file_handle);
#endif
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment