xref: /aosp_15_r20/external/rnnoise/examples/rnnoise_demo.c (revision 1295d6828459cc82c3c29cc5d7d297215250a74b)
1*1295d682SXin Li /* Copyright (c) 2018 Gregor Richards
2*1295d682SXin Li  * Copyright (c) 2017 Mozilla */
3*1295d682SXin Li /*
4*1295d682SXin Li    Redistribution and use in source and binary forms, with or without
5*1295d682SXin Li    modification, are permitted provided that the following conditions
6*1295d682SXin Li    are met:
7*1295d682SXin Li 
8*1295d682SXin Li    - Redistributions of source code must retain the above copyright
9*1295d682SXin Li    notice, this list of conditions and the following disclaimer.
10*1295d682SXin Li 
11*1295d682SXin Li    - Redistributions in binary form must reproduce the above copyright
12*1295d682SXin Li    notice, this list of conditions and the following disclaimer in the
13*1295d682SXin Li    documentation and/or other materials provided with the distribution.
14*1295d682SXin Li 
15*1295d682SXin Li    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*1295d682SXin Li    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*1295d682SXin Li    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18*1295d682SXin Li    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
19*1295d682SXin Li    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20*1295d682SXin Li    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21*1295d682SXin Li    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22*1295d682SXin Li    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23*1295d682SXin Li    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24*1295d682SXin Li    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25*1295d682SXin Li    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*1295d682SXin Li */
27*1295d682SXin Li 
28*1295d682SXin Li #include <stdio.h>
29*1295d682SXin Li #include "rnnoise.h"
30*1295d682SXin Li 
31*1295d682SXin Li #define FRAME_SIZE 480
32*1295d682SXin Li 
main(int argc,char ** argv)33*1295d682SXin Li int main(int argc, char **argv) {
34*1295d682SXin Li   int i;
35*1295d682SXin Li   int first = 1;
36*1295d682SXin Li   float x[FRAME_SIZE];
37*1295d682SXin Li   FILE *f1, *fout;
38*1295d682SXin Li   DenoiseState *st;
39*1295d682SXin Li   st = rnnoise_create(NULL);
40*1295d682SXin Li   if (argc!=3) {
41*1295d682SXin Li     fprintf(stderr, "usage: %s <noisy speech> <output denoised>\n", argv[0]);
42*1295d682SXin Li     return 1;
43*1295d682SXin Li   }
44*1295d682SXin Li   f1 = fopen(argv[1], "rb");
45*1295d682SXin Li   fout = fopen(argv[2], "wb");
46*1295d682SXin Li   while (1) {
47*1295d682SXin Li     short tmp[FRAME_SIZE];
48*1295d682SXin Li     fread(tmp, sizeof(short), FRAME_SIZE, f1);
49*1295d682SXin Li     if (feof(f1)) break;
50*1295d682SXin Li     for (i=0;i<FRAME_SIZE;i++) x[i] = tmp[i];
51*1295d682SXin Li     rnnoise_process_frame(st, x, x);
52*1295d682SXin Li     for (i=0;i<FRAME_SIZE;i++) tmp[i] = x[i];
53*1295d682SXin Li     if (!first) fwrite(tmp, sizeof(short), FRAME_SIZE, fout);
54*1295d682SXin Li     first = 0;
55*1295d682SXin Li   }
56*1295d682SXin Li   rnnoise_destroy(st);
57*1295d682SXin Li   fclose(f1);
58*1295d682SXin Li   fclose(fout);
59*1295d682SXin Li   return 0;
60*1295d682SXin Li }
61