Edinburgh Speech Tools  2.1-release
nas.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Paul Taylor & Alan Black */
34 /* Date : April 1995 & 96 */
35 /*-----------------------------------------------------------------------*/
36 /* Playback function for NCD's NAS server (formerly called netaudio) */
37 /* */
38 /*=======================================================================*/
39 
40 #include <iostream>
41 #include <cstdio>
42 #include <cstring>
43 #include <cstdlib>
44 #include <cctype>
45 #include <sys/stat.h>
46 #include "EST_Wave.h"
47 #include "EST_Option.h"
48 #include "audioP.h"
49 #include "EST_io_aux.h"
50 
51 using namespace std;
52 
53 
54 #ifdef SUPPORT_NAS
55 #include <audio/audiolib.h>
56 #include <audio/soundlib.h>
57 
58 #define VOL(volume) ((1 << 16) * (volume) / 100)
59 #define au_serverrate 16000
60 static int nas_playing = 0;
61 
62 int nas_supported = TRUE;
63 
64 int endian_int = 1;
65 #define NAS_BIG_ENDIAN (((char *)&endian_int)[0] == 0)
66 
67 static void na_sync_play_cb(AuServer *aud, AuEventHandlerRec *handler,
68  AuEvent *ev, AuPointer data)
69 {
70  int *d = (int *) data;
71  (void)aud; // unused parameter
72  (void)handler; // unused parameter
73  (void)ev; // unused parameter
74 
75  nas_playing = 0;
76  *d = 1;
77  return;
78 }
79 
80 int play_nas_wave(EST_Wave &inwave, EST_Option &al)
81 {
82  AuServer *aud = NULL;
83  /* legal sampling frequencies for Sun dbri device */
84  /* But what about when we are using Linux of FreeBSD ? */
85  int dev_sr[] = {8000, 9600, 11025, 16000, 18900, 22050, 32000,
86  37800, 44100, 48000, -1};
87  Sound in;
88  char *auservername = NULL;
89  int d = 0;
90  int i;
91  int ret;
92  AuEvent ev;
93  AuEventHandlerRec *er;
94  short *waveform;
95  int num_samps, samp_rate;
96 
97  if (al.present("-display"))
98  auservername = wstrdup(al.val("-display"));
99 
100  aud = AuOpenServer(auservername, 0, NULL, 0, NULL, NULL);
101  if (aud == NULL)
102  {
103  cerr << "Can't access NAS server " << auservername << endl;
104  return -1;
105  }
106 
107  /* Check sample rate of server -- should really check the if it */
108  /* only supports individual sample rate of which this wave is not */
109  /* one then we should resample. */
110  samp_rate = inwave.sample_rate();
111  bool samp_rate_ok = FALSE;
112  for (i=0; dev_sr[i] != -1; i++)
113  if (samp_rate == dev_sr[i])
114  samp_rate_ok = TRUE;
115  if (samp_rate_ok == FALSE)
116  {
117  if (samp_rate == 10000)
118  inwave.resample(9600); // just sounds much better than 16000
119  else
120  inwave.resample(16000);
121  }
122 
123  waveform = inwave.values().memory();
124  num_samps = inwave.num_samples();
125  samp_rate = inwave.sample_rate();
126 
127  if (num_samps > 0)
128  {
129  if (inwave.sample_type() == "mulaw")
130  in = SoundCreate(SoundFileFormatNone,
131  AuFormatULAW8,
132  inwave.num_channels(),
133  samp_rate, num_samps, NULL);
134  else
135  in = SoundCreate(SoundFileFormatNone,
136  (NAS_BIG_ENDIAN ?
137  AuFormatLinearSigned16MSB :
138  AuFormatLinearSigned16LSB),
139  inwave.num_channels(),
140  samp_rate, num_samps,NULL);
141 
142  er = AuSoundPlayFromData(aud, in, waveform, AuNone, VOL(100),
143  na_sync_play_cb,
144  (AuPointer) &d,(AuFlowID *) NULL,
145  (int *) NULL, (int *) NULL, &ret);
146  while (1)
147  {
148  AuNextEvent(aud, AuTrue, &ev);
149  AuDispatchEvent(aud, &ev);
150 
151  if (d) break;
152  }
153  }
154 
155  AuCloseServer(aud);
156 
157  return 1;
158 }
159 
160 int record_nas_wave(EST_Wave &wave, EST_Option &al)
161 {
162  (void)wave;
163  (void)al;
164 
165  cerr << "NAS: record not written yet\n";
166  return -1;
167 }
168 
169 #else
171 
173 {
174  (void)inwave;
175  (void)al;
176  cerr << "NAS playback not supported" << endl;
177  return -1;
178 }
179 
180 
182 {
183  (void)wave;
184  (void)al;
185  cerr << "NAS record not supported" << endl;
186  return -1;
187 }
188 
189 #endif
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
char * wstrdup(const char *s)
Definition: walloc.c:117
Utility IO Function header file.
ssize_t num_samples() const
return the number of samples in the waveform
Definition: EST_Wave.h:143
const EST_SMatrix & values() const
Definition: EST_Wave.h:177
#define FALSE
Definition: EST_bool.h:119
NULL
Definition: EST_WFST.cc:55
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
int record_nas_wave(EST_Wave &wave, EST_Option &al)
Definition: nas.cc:181
int play_nas_wave(EST_Wave &inwave, EST_Option &al)
Definition: nas.cc:172
int sample_rate() const
return the sampling rate (frequency)
Definition: EST_Wave.h:147
const T * memory() const
Definition: EST_TVector.h:238
int nas_supported
Definition: nas.cc:170
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
ssize_t num_channels() const
return the number of channels in the waveform
Definition: EST_Wave.h:145
void resample(int rate)
Resample waveform to rate
Definition: EST_Wave.cc:492
EST_String sample_type() const
Definition: EST_Wave.h:163
#define TRUE
Definition: EST_bool.h:118