File: EST_Wave.h

package info (click to toggle)
speech-tools 1%3A2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,848 kB
  • sloc: cpp: 67,350; ansic: 12,175; sh: 4,047; java: 3,748; makefile: 1,109; lisp: 711; perl: 501; awk: 85; xml: 9
file content (339 lines) | stat: -rw-r--r-- 12,742 bytes parent folder | download | duplicates (3)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
 /*************************************************************************/
 /*                                                                       */
 /*                Centre for Speech Technology Research                  */
 /*                     University of Edinburgh, UK                       */
 /*                         Copyright (c) 1996                            */
 /*                        All Rights Reserved.                           */
 /*                                                                       */
 /*  Permission is hereby granted, free of charge, to use and distribute  */
 /*  this software and its documentation without restriction, including   */
 /*  without limitation the rights to use, copy, modify, merge, publish,  */
 /*  distribute, sublicense, and/or sell copies of this work, and to      */
 /*  permit persons to whom this work is furnished to do so, subject to   */
 /*  the following conditions:                                            */
 /*   1. The code must retain the above copyright notice, this list of    */
 /*      conditions and the following disclaimer.                         */
 /*   2. Any modifications must be clearly marked as such.                */
 /*   3. Original authors' names are not deleted.                         */
 /*   4. The authors' names are not used to endorse or promote products   */
 /*      derived from this software without specific prior written        */
 /*      permission.                                                      */
 /*                                                                       */
 /*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
 /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
 /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
 /*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
 /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
 /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
 /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
 /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
 /*  THIS SOFTWARE.                                                       */
 /*                                                                       */
 /*************************************************************************/
 /*                                                                       */
 /*             Author :  Paul Taylor and Alan W Black                    */
 /*          Rewritten :  Richard Caley                                   */
 /* -------------------------------------------------------------------   */
 /*             EST_Wave Class header file                                */
 /*                                                                       */
 /*************************************************************************/

#ifndef __Wave_H__
#define __Wave_H__

#include <cstdio>
#include "EST_Featured.h"
#include "EST_rw_status.h"
#include "EST_types.h"

class EST_Track;
class EST_String;
class EST_TokenStream;


/** A class for storing digital waveforms. The waveform is stored as
an array of 16 bit shorts. Multiple channels are supported, but if no
channel information is given the 0th channel is accessed.
<p>

The waveforms can be of any sample rate, and can be changed to another
sampling rate using the <tt>resample</tt> function.

*/

class EST_Wave : public EST_Featured
{
protected:
  EST_SMatrix p_values;

  int p_sample_rate;

  void default_vals(int n=0, int c=1);
  void free_wave();
  void copy_data(const EST_Wave &w);
  void copy_setup(const EST_Wave &w);

public:

  static const int default_sample_rate;
  static const int default_num_channels;

  /// default constructor
  EST_Wave();
  /// copy constructor
  EST_Wave(const EST_Wave &a);

  EST_Wave(int n, int c, int sr);

  /// Construct from memory supplied by caller
  EST_Wave(int samps, int chans,
	   short *memory, int offset=0, int sample_rate=default_sample_rate, 
	   int free_when_destroyed=0);
  
  ~EST_Wave();
    

  /**@name Access functions for finding amplitudes of samples */
  //@{

  /** return amplitude of sample <tt>i</tt> from channel <tt>
      channel</tt>.  By default the 0th channel is selected. This
      function can be used for assignment.
  */
  short &a(int i, int channel = 0);
  short a(int i, int channel = 0) const;
  INLINE short &a_no_check(int i, int channel = 0)
        { return p_values.a_no_check(i,channel); }
  INLINE short a_no_check(int i, int channel = 0) const
        { return p_values.a_no_check(i,channel); }
  INLINE short &a_no_check_1(int i, int channel = 0)
        { return p_values.a_no_check_1(i,channel); }
  INLINE short a_no_check_1(int i, int channel = 0) const
        { return p_values.a_no_check_1(i,channel); }

  
  /** explicit set_a, easier to wrap than assignment
   */
  INLINE short set_a(int i, int channel = 0, short val = 0)
  { return a(i,channel) = val; }

  /** return amplitude of sample <tt>i</tt> from channel <tt>
      channel</tt>.  By default the 0th channel is selected.
  */
  short operator()(int i, int channel) const
    { return a(i,channel); }

  /** return amplitude of sample <tt>i</tt> from channel 0. 
    */
  short operator()(int i) const
    { return a(i,0); }
       
  /** Version of a() that returns zero if index is out of array
      bounds.  This is particularly useful in signal processing when
      you want to have windows going off the end of the waveform.  */
  short &a_safe(int i, int channel = 0);

  /// return the time position in seconds of the ith sample
  float t(int i) const { return (float)i/(float)p_sample_rate; }
  //@}

  /**@name Information functions */
  //@{
  /// return the number of samples in the waveform
  int num_samples() const { return p_values.num_rows();}
  /// return the number of channels in the waveform
  int num_channels() const { return p_values.num_columns(); }
  /// return the sampling rate (frequency)
  int sample_rate() const { return p_sample_rate; }
  /// Set sampling rate to <tt>n</tt>
  void set_sample_rate(const int n){p_sample_rate = n;}
  /// return the size of the waveform, i.e. the number of samples.
  int length() const { return num_samples();}
  /// return the time position of the last sample.
  float end(){ return t(num_samples()-1); }

  /// Can we look N samples to the left?
  bool have_left_context(unsigned int n) const
    { return p_values.have_rows_before(n); }

  /** returns the file format of the file from which the waveform
      was read. If the waveform has not been read from a file, this is set
      to the default type */

  EST_String sample_type() const { return f_String("sample_type","short"); }
  void set_sample_type(const EST_String t) { f_set("sample_type", t); }

  EST_String file_type() const { return f_String("file_type","riff"); }
  void set_file_type(const EST_String t) { f_set("file_type", t); }

  /// A string identifying the waveform, commonly used to store the filename
  EST_String name() const { return f_String("name"); }

  /// Sets name.
  void set_name(const EST_String n){ f_set("name", n); }

  //@}

  const EST_SMatrix &values() const { return p_values; }
  EST_SMatrix &values() { return p_values; }

  /**@name Waveform manipulation functions */
  //@{

  /// resize the waveform 
  void resize(int num_samples, int num_channels = EST_ALL, int set=1) 
    { p_values.resize(num_samples, num_channels, set); }

  /// Resample waveform to <tt>rate</tt>
  void resample(int rate);

  /// Dynamic Range Compression - SaiKrishna May 2017
  void compress(float mu, float limit);

  /** multiply all samples by a factor <tt>gain</tt>. This checks for
      overflows and puts them to the maximum positive or negative value
      as appropriate.
  */
  void rescale(float gain,int normalize=0);

  // multiply samples by a factor contour.  The factor_contour track
  // should contains factor targets at time points throughout the wave,
  // between which linear interpolation is used to calculate the factor
  // for each sample.
  void rescale( const EST_Track &factor_contour );

  /// clear waveform and set size to 0.
  void clear() {resize(0,EST_ALL);}

  void copy(const EST_Wave &from);

  void fill(short v=0, int channel=EST_ALL);

  void empty(int channel=EST_ALL) { fill(0,channel); }

  void sample(EST_TVector<short> &sv, int n)
    { p_values.row(sv, n); }
  void channel(EST_TVector<short> &cv, int n)
    { p_values.column(cv, n); }

  void copy_channel(int n, short *buf, int offset=0, int num=EST_ALL) const
    { p_values.copy_column(n, buf, offset, num); } 
  void copy_sample(int n, short *buf, int offset=0, int num=EST_ALL) const
    {  p_values.copy_row(n, buf, offset, num); } 

  void set_channel(int n, const short *buf, int offset=0, int num=EST_ALL)
    { p_values.set_column(n, buf, offset, num); }
  void set_sample(int n, const short *buf, int offset=0, int num=EST_ALL)
    { p_values.set_row(n, buf, offset, num); }


  void sub_wave(EST_Wave &sw, 
		int offset=0, int num=EST_ALL,
		int start_c=0, int nchan=EST_ALL);

  void sub_wave(EST_Wave &sw, 
		int offset=0, int num=EST_ALL,
		int start_c=0, int nchan=EST_ALL) const
    { ((EST_Wave *)this)->sub_wave(sw, offset, num, start_c, nchan); }

  //@}

  /**@name File i/o functions */
  //@{

  /** Load a file into the waveform. The load routine attempts to
      automatically determine which file type is being loaded.  A
      portion of the waveform can be loaded by setting <tt>
      offset</tt> to the sample position from the beginning and
      <length> to the number of required samples after this.  */

  EST_read_status load(const EST_String filename, 
		       int offset=0, 
		       int length = 0,
		       int rate = default_sample_rate);

  EST_read_status load(EST_TokenStream &ts,
		       int offset=0, 
		       int length = 0,
		       int rate = default_sample_rate);

  EST_read_status load(const EST_String filename, 
		       const EST_String filetype,
		       int offset=0, 
		       int length = 0,
		       int rate = default_sample_rate);

  EST_read_status load(EST_TokenStream &ts,
		       const EST_String filetype,
		       int offset=0, 
		       int length = 0,
		       int rate = default_sample_rate);

  /** Load a file of type <tt>filetype</tt> into the waveform. This
      can be used to load unheadered files, in which case the fields
      <tt>sample_rate, sample_type, bo</tt> and <tt>nc</tt> are used
      to specify the sample rate, type, byte order and number of
      channels.  A portion of the waveform can be loaded by setting
      <tt> offset</tt> to the sample position from the beginning and
      <length> to the number of required samples after this.
  */

  EST_read_status load_file(const EST_String filename, 
			    const EST_String filetype, int sample_rate, 
			    const EST_String sample_type, int bo, int nc,
			    int offset = 0, int length = 0);
  EST_read_status load_file(EST_TokenStream &ts,
			    const EST_String filetype, int sample_rate, 
			    const EST_String sample_type, int bo, int nc,
			    int offset = 0, int length = 0);

  /* Save waveform to a file called <tt>filename</tt> of file
     format <tt>EST_filetype</tt>.
  */
  EST_write_status save(const  EST_String filename, 
			const EST_String EST_filetype = "");

  EST_write_status save(FILE *fp,
			const EST_String EST_filetype = "");

  EST_write_status save_file(const EST_String filename, 
			     EST_String filetype,
			     EST_String sample_type, int bo, const char *mode = "wb");

  EST_write_status save_file(FILE *fp,
			     EST_String filetype,
			     EST_String sample_type, int bo);

  EST_write_status save_file_header(FILE *fp,
				     EST_String ftype,
				     EST_String stype, int obo);
  EST_write_status save_file_data(FILE *fp,
				     EST_String ftype,
				     EST_String stype, int obo);
  //@}

  /// Assignment operator
  EST_Wave& operator = (const EST_Wave& w);
  /** Add to existing wave in serial. Waveforms must have the same
      number of channels.
  */
  EST_Wave& operator +=(const EST_Wave &a);
  /** Add wave in parallel, i.e. make wave <tt>a</tt> become new
      channels in existing waveform.
  */
  EST_Wave& operator |=(const EST_Wave &a);

  /// print waveform
  friend ostream& operator << (ostream& p_values, const EST_Wave &sig);

  // integrity check *** debug
  void integrity() const { p_values.integrity() ; }

};

typedef EST_TList<EST_Wave> EST_WaveList;

int operator != (EST_Wave a, EST_Wave b);
int operator == (EST_Wave a, EST_Wave b);

#endif /* __Wave_H__ */