File: gen_audio.cc

package info (click to toggle)
speech-tools 1%3A2.5.0-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,988 kB
  • sloc: cpp: 67,350; ansic: 12,174; sh: 4,055; java: 3,748; makefile: 1,111; lisp: 711; perl: 396; awk: 85; xml: 9
file content (387 lines) | stat: -rw-r--r-- 12,056 bytes parent folder | download | duplicates (4)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*************************************************************************/
/*                                                                       */
/*                Centre for Speech Technology Research                  */
/*                     University of Edinburgh, UK                       */
/*                       Copyright (c) 1995,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                            */
/*                      Date   :  March 95                               */
/*-----------------------------------------------------------------------*/
/*                  Generalised playback function                        */
/*                                                                       */
/*=======================================================================*/

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
#include <fcntl.h>
#include "EST_system.h"
#include "EST_socket.h"
#include "EST_Option.h"
#include "EST_Wave.h"
#include "EST_io_aux.h"
#include "audioP.h"
#include "EST_audio.h"
#include "EST_wave_aux.h"

static int play_sunau_wave(EST_Wave &inwave, EST_Option &al);
static int play_socket_wave(EST_Wave &inwave, EST_Option &al);
static int play_aucomm_wave(EST_Wave &inwave, EST_Option &al);

static int record_sunau_wave(EST_Wave &wave, EST_Option &al);

int play_wave(EST_Wave &inwave, EST_Option &al)
{
    EST_String protocol;
    EST_Wave wtmp;
    EST_Wave *toplay;
    char *quality;
    char *sr;

    if ((sr = getenv("NA_PLAY_HOST")) != NULL)
	if  (!al.present("-display"))
	    al.add_item("-display", sr);
    
    if ((quality = getenv("NA_PLAY_QUALITY")) != NULL)
	if  (!al.present("-quality"))
	    al.add_item("-quality", quality);

    if (al.present("-p"))
	protocol = al.val("-p");
    else if ((sr=getenv("NA_PLAY_PROTOCOL")) != NULL)
	protocol = sr;
    else if (protocol == "")
    {
	if (nas_supported)
	    protocol = "netaudio";  // the default protocol
	else if (pulse_supported)
	    protocol = "pulseaudio";
	else if (esd_supported)
	    protocol = "esdaudio";
	else if (sun16_supported)
	    protocol = "sun16audio";
	else if (freebsd16_supported)
	    protocol = "freebsd16audio";
	else if (linux16_supported)
	    protocol = "linux16audio";
	else if (irix_supported)
	    protocol = "irixaudio";
	else if (macosx_supported)
      protocol = "macosxaudio";
	else if (win32audio_supported)
	    protocol = "win32audio";
	else if (mplayer_supported)
	    protocol = "mplayeraudio";
	else
	    protocol = "sunaudio";
    }

  // OS X can handle multichannel audio, don't know about other systems.
  if (inwave.num_channels() > 1 && upcase(protocol) != "MACOSXAUDIO" )
    {
  	  wave_combine_channels(wtmp,inwave);
  	  toplay = &wtmp;
    }
  else
  	toplay = &inwave;

    if (upcase(protocol) == "NETAUDIO")
	return play_nas_wave(*toplay,al);
    else if (upcase(protocol) == "PULSEAUDIO")
	return play_pulse_wave(*toplay,al);
    else if (upcase(protocol) == "ESDAUDIO")
	return play_esd_wave(*toplay,al);
    else if (upcase(protocol) == "SUNAUDIO")
	return play_sunau_wave(*toplay,al);
    else if (upcase(protocol) == "SUN16AUDIO")
	return play_sun16_wave(*toplay,al);
    else if ((upcase(protocol) == "FREEBSD16AUDIO") ||
	     (upcase(protocol) == "LINUX16AUDIO"))
	return play_linux_wave(*toplay,al);
    else if (upcase(protocol) == "IRIXAUDIO")
	return play_irix_wave(*toplay,al);
    else if (upcase(protocol) == "MACOSXAUDIO")
	return play_macosx_wave(*toplay,al);
    else if (upcase(protocol) == "MPLAYERAUDIO")
	return play_mplayer_wave(*toplay,al);
    else if (upcase(protocol) == "WIN32AUDIO")
	return play_win32audio_wave(*toplay,al);
    else if (upcase(protocol) == "AUDIO_COMMAND")
	return play_aucomm_wave(*toplay,al);
    else if (upcase(protocol) == "SOCKET")
	return play_socket_wave(*toplay,al);
    else
    {
	cerr << "Unknown audio server protocol " << protocol << endl;
	return -1;
    }
}

static int play_socket_wave(EST_Wave &inwave, EST_Option &al)
{
    // Send inwave down the given fd (a socket)
    SOCKET_FD fd;
    EST_String otype;
    EST_String tmpfile = make_tmp_filename();

    if (al.present("socket_fd"))
	fd = al.ival("socket_fd");
    else
    {
	cerr << "Socket audio mode: no socket_fd specified" << endl;
	return -1;
    }

    if (al.present("socket_otype"))
	otype = al.val("socket_otype");  // file type to send to client
    else
	otype = "riff";
    
    inwave.save(tmpfile,otype);
    
    // Because the client may receive many different types of file
    // I send WV\n to it before the file itself
    send(fd,"WV\n",3,0);
    socket_send_file(fd,tmpfile);
    unlink(tmpfile);

    return 0;
}

static int play_aucomm_wave(EST_Wave &inwave, EST_Option &al)
{
    // Play wave by specified command 
    EST_String usrcommand, otype;
    char tmpfile[2048];
    char pref[2048];

    if (al.present("-command"))
	usrcommand = al.val("-command");
    else if (getenv("NA_PLAY_COMMAND") != NULL)
	usrcommand = getenv("NA_PLAY_COMMAND");
    else 
    {
	cerr << "Audio protocol set to COMMAND but no command specified\n";
	return -1;
    }

    sprintf(tmpfile,"/tmp/audiofile_%05ld",(long)getpid());

    if (al.present("-rate"))
	inwave.resample(al.ival("-rate"));
    if (al.present("-otype"))
	otype = al.val("-otype");
    else
	otype = "raw";

    if (inwave.save(tmpfile,otype) != write_ok)
    {
	cerr << "Audio writing file \"" << tmpfile << "\" in type \"" <<
	    otype << " failed " << endl;
	return -1;
    }

    sprintf(pref,"FILE=%s;SR=%d;",tmpfile,inwave.sample_rate());

    system((EST_String)pref+usrcommand.unquote('"'));

    unlink(tmpfile);  // so we don't fill up /tmp

    return 0;
}

static int play_sunau_wave(EST_Wave &inwave, EST_Option &al)
{
    // Play wave through /dev/audio using 8K ulaw encoding
    // works for Suns as well as Linux and FreeBSD machines
    int rcode;
    const char *audiodevice;

    inwave.resample(8000);

    if (al.present("-audiodevice"))
	audiodevice = al.val("-audiodevice");
    else
	audiodevice = "/dev/audio";

    // Should really do something cute about checking if /dev/audio
    // is not in use
    rcode = inwave.save(audiodevice,"ulaw");

    return rcode;

}

EST_String options_supported_audio(void)
{
    // returns list of supported audio types
    EST_String audios = "";

    audios += "sunaudio";  // we always support this in spite of the hardware

    audios += " audio_command";
    if (nas_supported)
	audios += " netaudio";
    else if (esd_supported)
	audios += " esdaudio";
    if (sun16_supported)
	audios += " sun16audio";
    if (pulse_supported)
	audios += " pulseaudio";
    if (freebsd16_supported)
	audios += " freebsd16audio";
    if (linux16_supported)
	audios += " linux16audio";
    if (irix_supported)
	audios += " irixaudio";
    if (mplayer_supported)
	audios += " mplayeraudio";
    if (macosx_supported)
	audios += "macosxaudio";
    if (win32audio_supported)
	audios += " win32audio";
    if (os2audio_supported)
	audios += " os2audio";

    return audios;
}

int record_wave(EST_Wave &wave, EST_Option &al)
{
    // Record wave from audio device 
    char *sr;
    EST_String protocol;

    // For archaic reasons, if you are using NAS use DISPLAY or
    // AUDIOSERVER 
    if ((sr = getenv("NA_PLAY_HOST")) != NULL)
	if  (!al.present("-display"))
	    al.add_item("-display", sr);

    if (al.present("-p"))
	protocol = al.val("-p");
    else if ((sr=getenv("NA_PLAY_PROTOCOL")) != NULL)
	protocol = sr;
    else if (protocol == "")
    {
    if (pulse_supported)
        protocol = "pulseaudio";
	else if (nas_supported)
	    protocol = "netaudio";  // the default protocol
	else if (esd_supported)
	    protocol = "esdaudio";  // the default protocol
	else if (sun16_supported)
	    protocol = "sun16audio";
	else if (freebsd16_supported)
	    protocol = "freebsd16audio";
	else if (linux16_supported)
	    protocol = "linux16audio";
	else if (irix_supported)
	    protocol = "irixaudio";
	else if (win32audio_supported)
	    protocol = "win32audio";
	else if (mplayer_supported)
	    protocol = "mplayeraudio";
	else
	    protocol = "sunaudio";
    }

    if (upcase(protocol) == "NETAUDIO")
	return record_nas_wave(wave,al);
    else if (upcase(protocol) == "PULSEAUDIO")
	return record_pulse_wave(wave,al);
    else if (upcase(protocol) == "ESDAUDIO")
        return record_esd_wave(wave,al);
    else if (upcase(protocol) == "SUN16AUDIO")
	return record_sun16_wave(wave,al);
    else if ((upcase(protocol) == "FREEBSD16AUDIO") ||
	     (upcase(protocol) == "LINUX16AUDIO"))
	return record_linux_wave(wave,al);
    else if (upcase(protocol) == "SUNAUDIO")
	return record_sunau_wave(wave,al);
    else
    {
	cerr << "NA_RECORD: \"" << protocol << 
	    "\" EST current has no record support" << endl;
	return -1;
    }
}

static int record_sunau_wave(EST_Wave &wave, EST_Option &al)
{
    int num_samples,i,r,n;
    int audio;
    unsigned char *ulawwave;
    short *waveform;
    const int AUDIOBUFFSIZE = 256;
    const char *audiodevice;

    if (al.present("-audiodevice"))
	audiodevice = al.val("-audiodevice");
    else
	audiodevice = "/dev/audio";

    if ((audio = open(audiodevice, O_RDONLY)) == -1)
    {
	cerr << "SUN16: can't open " << audiodevice << " for reading" << endl;
	return -1;
    }

    num_samples = (int)(8000*al.fval("-time"));
    ulawwave = walloc(unsigned char,num_samples);
    
    for (r=i=0; i < num_samples; i+= r)
    {
	if (num_samples > i+AUDIOBUFFSIZE)
	    n = AUDIOBUFFSIZE;
	else
	    n = num_samples-i;
	r = read(audio,&ulawwave[i], n);
	if (r <= 0)
	{
	    cerr << "sunaudio: failed to read from audio device" << endl;
	    close(audio);
	    wfree(ulawwave);
	    return -1;
	}
    }

    wave.resize(num_samples);
    wave.set_sample_rate(8000);
    waveform = wave.values().memory();

    ulaw_to_short(ulawwave,waveform,num_samples);
    wave.resample(al.ival("-sample_rate"));

    close(audio);
    wfree(ulawwave);
    return 0;
}