File: tnlp.c

package info (click to toggle)
codec2 0.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 113,072 kB
  • sloc: ansic: 412,877; python: 4,004; sh: 1,540; objc: 817; asm: 683; makefile: 588
file content (164 lines) | stat: -rw-r--r-- 4,243 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*---------------------------------------------------------------------------*\

  FILE........: tnlp.c
  AUTHOR......: David Rowe
  DATE CREATED: 23/3/93

  Test program for non linear pitch estimation functions.

\*---------------------------------------------------------------------------*/

/*
  Copyright (C) 2009 David Rowe

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "defines.h"
#include "dump.h"
#include "sine.h"
#include "nlp.h"
#include "kiss_fft.h"

int   frames;

/*---------------------------------------------------------------------------*\

   switch_present()

   Searches the command line arguments for a "switch".  If the switch is
   found, returns the command line argument where it ws found, else returns
   NULL.

\*---------------------------------------------------------------------------*/

int switch_present(sw,argc,argv)
  char sw[];     /* switch in string form */
  int argc;      /* number of command line arguments */
  char *argv[];  /* array of command line arguments in string form */
{
  int i;       /* loop variable */

  for(i=1; i<argc; i++)
    if (!strcmp(sw,argv[i]))
      return(i);

  return 0;
}

/*---------------------------------------------------------------------------*\

                                    MAIN

\*---------------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
    if (argc < 3) {
	printf("\nusage: tnlp InputRawSpeechFile Outputf0PitchTextFile "
	       "[--dump DumpFile] [--Fs SampleRateHz]\n");
        exit(1);
    }
    
    int Fs = 8000;
    if (switch_present("--Fs",argc,argv)) {
        Fs = atoi(argv[argc+1]);
    }

    C2CONST c2const = c2const_create(Fs, N_S);
    int   n = c2const.n_samp;
    int   m = c2const.m_pitch;
    FILE *fin,*fout;
    short buf[n];
    float Sn[m];	        /* float input speech samples */
    kiss_fft_cfg  fft_fwd_cfg;
    COMP  Sw[FFT_ENC];	        /* DFT of Sn[] */
    float w[m];	                /* time domain hamming window */
    float W[FFT_ENC];	        /* DFT of w[] */
    float pitch_samples;
    int   i;
    float f0, prev_f0;
    void  *nlp_states;
    #ifdef DUMP
    int   dump;
    #endif

    /* Input file */

    if ((fin = fopen(argv[1],"rb")) == NULL) {
      printf("Error opening input speech file: %s\n",argv[1]);
      exit(1);
    }

    /* Output file */

    if ((fout = fopen(argv[2],"wt")) == NULL) {
      printf("Error opening output text file: %s\n",argv[2]);
      exit(1);
    }

    #ifdef DUMP
    dump = switch_present("--dump",argc,argv);
    if (dump)
      dump_on(argv[dump+1]);
    #else
    /// TODO
    /// #warning "Compile with -DDUMP if you expect to dump anything."
    #endif

    for(i=0; i<m; i++) {
      Sn[i] = 0.0;
    }

    nlp_states = nlp_create(&c2const);
    fft_fwd_cfg = kiss_fft_alloc(FFT_ENC, 0, NULL, NULL);
    make_analysis_window(&c2const, fft_fwd_cfg, w, W);

    frames = 0;
    prev_f0 = 1/P_MAX_S;
    while(fread(buf, sizeof(short), n, fin)) {
      /* Update input speech buffers */

      for(i=0; i<m-n; i++)
        Sn[i] = Sn[i+n];
      for(i=0; i<n; i++)
        Sn[i+m-n] = buf[i];
      dft_speech(&c2const, fft_fwd_cfg, Sw, Sn, w);
      #ifdef DUMP
      dump_Sn(m, Sn); dump_Sw(Sw);
      #endif

      f0 = nlp(nlp_states, Sn, n, &pitch_samples, Sw, W, &prev_f0);

      fprintf(stderr,"%d %f %f\n", frames++, f0, pitch_samples);
      fprintf(fout,"%f %f\n", f0, pitch_samples);
    }

    fclose(fin);
    fclose(fout);
    #ifdef DUMP
    if (dump) dump_off();
    #endif
    nlp_destroy(nlp_states);

    return 0;
}