File: EST_wave_aux.cc

package info (click to toggle)
speech-tools 1:2.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 (329 lines) | stat: -rw-r--r-- 10,997 bytes parent folder | download | duplicates (5)
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
/*************************************************************************/
/*                                                                       */
/*                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 Black                      */
/*             Date   :  June 1996                                       */
/*-----------------------------------------------------------------------*/
/*             more EST_Wave class methods                               */
/*                                                                       */
/*=======================================================================*/

#include <cstring>
#include "EST_unix.h"
#include <cstdlib>
#include "EST_cutils.h"
#include "EST_string_aux.h"
#include "EST_Wave.h"
#include "EST_wave_utils.h"
#include "EST_wave_aux.h"
#include "EST_io_aux.h"
#include "EST_error.h"

void extract(EST_Wave &sig, EST_Option &al);

/* Allow EST_Wave to be used in an EST_Val */
VAL_REGISTER_CLASS(wave,EST_Wave)

static EST_TValuedEnumDefinition<EST_sample_type_t, const char *,NO_INFO> st_names[] = {
{st_unknown,  {"undef"}},
{st_schar, {"schar","byte"}},
{st_uchar, {"uchar"}},
{st_short, {"short"}},
{st_shorten, {"shorten"}},
{st_int, {"int"}},
{st_float, {"float"}},
{st_double, {"double"}},
{st_mulaw, {"mulaw"}},
{st_adpcm, {"adpcm"}},
{st_alaw, {"alaw"}},
{st_ascii, {"ascii"}},
{st_unknown, {0}}
};

EST_TNamedEnum<EST_sample_type_t> EST_sample_type_map(st_names);

void differentiate(EST_Wave &sig)
{
    for (int i = 0; i < sig.num_samples() -1; ++i)
	sig.a(i) = sig.a(i + 1) - sig.a(i);
    sig.resize(sig.num_samples()-1);
}


void extract_channels(EST_Wave &single, const EST_Wave &multi,  
		      EST_IList &ch_list)
{

    if (&single == &multi)
    {
	// some nasty person has passed us the same wave for output and input.
	EST_Wave tmp;
	extract_channels(tmp, multi, ch_list);
	single.copy(tmp);
	return;
    }

    int channel, i;
    int c = multi.num_channels();
    int num_samples = multi.num_samples();

    short *buf = new short [num_samples];
    EST_Litem *p;

    single.resize(num_samples, ch_list.length());
    single.set_sample_rate(multi.sample_rate());
    single.set_file_type(multi.file_type());

    for (i = 0, p = ch_list.head(); p; p = p->next(), ++i)
    {
	channel = ch_list(p);
    
	if (channel < 0 || channel >= c)
	    EST_error("Can't extract channel %d from %d channel waveform\n", 
		      channel, c);

	multi.copy_channel(channel, buf);
	single.set_channel(i, buf);
    }
}

int wave_extract_channel(EST_Wave &single, const EST_Wave &multi, int channel)
{
    if (&single == &multi)
    {
	// some nasty person has passed us the same wave for output and input.
	EST_Wave tmp;
	int v = wave_extract_channel(tmp, multi, channel);
	if(v==0)
	    single.copy(tmp);
	return v;
    }

    int c = multi.num_channels();
    
    if (channel < 0 || channel >= c)
    {
	cerr << "Can't extract channel " << channel << " from " << 
	    c << " channel waveform\n";
	return -1;
    }

    EST_Wave subwave;

    multi.sub_wave(subwave, 0, EST_ALL, channel, 1);

    single.copy(subwave);

    return 0;
}

void extract_channels(EST_Wave &single, const EST_Wave &multi,  int channel)
{
    EST_IList a;
    a.append(channel);
    extract_channels(single, multi, a);
}

void wave_combine_channels(EST_Wave &s,const EST_Wave &m)
{
    if (&s == &m)
    {
	// some nasty person has passed us the same wave for output and input.
	EST_Wave tmp;
	wave_combine_channels(tmp,m);
	s = tmp;
	return;
    }
    s.resize(m.num_samples(), 1, FALSE);
    s.set_sample_rate(m.sample_rate());
    
    for(int i=0; i<m.num_samples(); i++)
    {
	double sum=0.0;
	for(int j=0; j<m.num_channels(); j++)
	    sum += m.a(i,j);
	s.a(i,0) = (int)(sum/m.num_channels() + 0.5);
    }
}

void add_waves(EST_Wave &s, const EST_Wave &m)
{
    int new_samples = Gof(s.num_samples(), m.num_samples());
    int new_channels = Gof(s.num_channels(), m.num_channels());
    
    s.resize(new_samples, new_channels, 1);
    
    for (int i = 0; i < m.num_samples(); i++)
	for (int j = 0; j < m.num_channels(); j++)
	    s.a(i, j) += m.a(i, j);
}

void invert(EST_Wave &sig)
{
    sig.rescale(-1.0);
}

void reverse(EST_Wave &sig)
{
    int i, n;
    short t;
    n = (int)floor((float)(sig.num_samples())/2.0);
    
    for (i = 0; i < n; ++i)
    {
	t = sig.a_no_check(i);
	sig.a_no_check(i) = sig.a_no_check(sig.num_samples() - 1 -i);
	sig.a_no_check(sig.num_samples() - 1 -i) = t;
    }
}

void extract(EST_Wave &sig, EST_Option &al)
{
    int from, to;
    EST_Wave sub_wave, w2;
    
    if (al.present("-start"))
	from = (int)(sig.sample_rate() * al.fval("-start"));
    else if (al.present("-from"))
	from = al.ival("-from");
    else
	from = 0;
    
    if (al.present("-end"))
	to = (int)(sig.sample_rate() * al.fval("-end"));
    else if (al.present("-to"))
	to = al.ival("-to");
    else
	to = sig.num_samples();
    
    sig.sub_wave(sub_wave, from, to - from);
    w2 = sub_wave;
    sig = w2;
}

void wave_info(EST_Wave &w)
{
    EST_String t;
    cout << "Duration: " << 
	ftoString((float)w.num_samples()/(float)w.sample_rate(),4,1) << endl;
    
    cout << "Sample rate: " << w.sample_rate() << endl;
    cout << "Number of samples: " << w.num_samples() << endl;
    cout << "Number of channels: " << w.num_channels() << endl;
    cout << "Header type: " << w.file_type() << endl;
    cout << "Data type: " << w.sample_type() << endl;
}

static EST_String options_wave_filetypes(void)
{
    // Returns list of currently support wave file types
    // Should be extracted from the list in EST_Wave (but that's
    // not very clear :-(
    
    return "nist, est, esps, snd, riff, aiff, audlab, raw, ascii";
}

EST_String options_subwave(void)
{
    return
	EST_String("")+
	"-start <float>  Extract sub-wave starting at this time, specified in \n"
	"    seconds\n\n"
	"-end <float>  Extract sub-wave ending at this time, specified in \n"
	"    seconds\n\n"
	"-from <int> Extract sub-wave starting at this sample point\n\n"
	"-to <int> Extract sub-wave ending at this sample point\n\n";
}
EST_String options_wave_input(void)
{
    // The standard waveform input options 
    return
	EST_String("")+
	"-itype <string>  Input file type (optional).  If set to raw, this \n"
	"    indicates that the input file does not have a header. While \n"
        "    this can be used to specify file types other than raw, this is \n"
	"    rarely used for other purposes\n"
        "    as the file type of all the existing supported \n"
	"    types can be determined automatically from the \n"
	"    file's header. If the input file is unheadered, \n"
	"    files are assumed to be shorts (16bit).  \n"
	"    Supported types are \n"
	"   "+options_wave_filetypes()+"\n\n"
	"-n <int>  Number of channels in an unheadered input file \n\n"
	"-f <int> Sample rate in Hertz for an unheadered input file \n\n"
	"-ibo <string>  Input byte order in an unheadered input file: \n"
	"    possibliities are: MSB , LSB, native or nonnative. \n"
	"    Suns, HP, SGI Mips, M68000 are MSB (big endian) \n"
	"    Intel, Alpha, DEC Mips, Vax are LSB (little \n"
	"    endian)\n\n"
	"-iswap  Swap bytes. (For use on an unheadered input file)\n\n"
	"-istype <string> Sample type in an unheadered input file:\n"
	"     short, alaw, mulaw, byte, ascii\n\n"
	"-c <string>  Select a single channel (starts from 0). \n"
	"    Waveforms can have multiple channels. This option \n"
        "    extracts a single channel for progcessing and \n"
	"    discards the rest. \n\n"+
	options_subwave();

// old option
//	"-ulaw            Assume unheadered input is 8k ulaw\n\n"
// this facility moved into na_play
//	"-r*              Select subrange of file. (ESPS compatible)\n\n"

}

EST_String options_wave_output(void)
{
    return 
	EST_String("")+
	"-o <ofile>       Output filename. If not specified output is\n"
	"    to stdout.\n\n"
	"-otype <string>  Output file type, (optional).  If no type is\n"
	"    Specified the type of the input file is assumed.\n"
	"    Supported types are: \n"
        "   "+options_wave_filetypes()+"\n\n"
	"-F <int>         Output sample rate in Hz. If this is different \n"
	"    from the input sample rate, resampling will occur \n\n"
	"-obo <string>   Output byte order: MSB, LSB, native, or nonnative. \n"
	"    Suns, HP, SGI Mips, M68000 are MSB (big endian) \n"
	"    Intel, Alpha, DEC Mips, Vax are LSB \n"
	"    (little endian)\n\n"
	"-oswap Swap bytes when saving to output\n\n"+
	"-ostype <string> Output sample type: short, alaw, mulaw, byte or ascii\n\n";
}

Declare_TNamedEnum(EST_sample_type_t)
#if defined(INSTANTIATE_TEMPLATES)
#include "../base_class/EST_TNamedEnum.cc"
Instantiate_TNamedEnum(EST_sample_type_t)
#endif