Edinburgh Speech Tools 2.4-release
 
Loading...
Searching...
No Matches
nas.cc
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 <cstdio>
41#include <cstring>
42#include <cstdlib>
43#include <cctype>
44#include <sys/stat.h>
45#include "EST_Wave.h"
46#include "EST_Option.h"
47#include "audioP.h"
48#include "EST_io_aux.h"
49
50#ifdef SUPPORT_NAS
51#include <audio/audiolib.h>
52#include <audio/soundlib.h>
53
54#define VOL(volume) ((1 << 16) * (volume) / 100)
55#define au_serverrate 16000
56static int nas_playing = 0;
57
58int nas_supported = TRUE;
59
60int endian_int = 1;
61#define NAS_BIG_ENDIAN (((char *)&endian_int)[0] == 0)
62
63static void na_sync_play_cb(AuServer *aud, AuEventHandlerRec *handler,
64 AuEvent *ev, AuPointer data)
65{
66 int *d = (int *) data;
67 (void)aud; // unused parameter
68 (void)handler; // unused parameter
69 (void)ev; // unused parameter
70
71 nas_playing = 0;
72 *d = 1;
73 return;
74}
75
76int play_nas_wave(EST_Wave &inwave, EST_Option &al)
77{
78 AuServer *aud = NULL;
79 /* legal sampling frequencies for Sun dbri device */
80 /* But what about when we are using Linux of FreeBSD ? */
81 int dev_sr[] = {8000, 9600, 11025, 16000, 18900, 22050, 32000,
82 37800, 44100, 48000, -1};
83 Sound in;
84 char *auservername = NULL;
85 int d = 0;
86 int i;
87 int ret;
88 AuEvent ev;
89 AuEventHandlerRec *er;
90 short *waveform;
91 int num_samps, samp_rate;
92
93 if (al.present("-display"))
94 auservername = wstrdup(al.val("-display"));
95
96 aud = AuOpenServer(auservername, 0, NULL, 0, NULL, NULL);
97 if (aud == NULL)
98 {
99 cerr << "Can't access NAS server " << auservername << endl;
100 return -1;
101 }
102
103 /* Check sample rate of server -- should really check the if it */
104 /* only supports individual sample rate of which this wave is not */
105 /* one then we should resample. */
106 samp_rate = inwave.sample_rate();
107 bool samp_rate_ok = FALSE;
108 for (i=0; dev_sr[i] != -1; i++)
109 if (samp_rate == dev_sr[i])
110 samp_rate_ok = TRUE;
111 if (samp_rate_ok == FALSE)
112 {
113 if (samp_rate == 10000)
114 inwave.resample(9600); // just sounds much better than 16000
115 else
116 inwave.resample(16000);
117 }
118
119 waveform = inwave.values().memory();
120 num_samps = inwave.num_samples();
121 samp_rate = inwave.sample_rate();
122
123 if (num_samps > 0)
124 {
125 if (inwave.sample_type() == "mulaw")
126 in = SoundCreate(SoundFileFormatNone,
127 AuFormatULAW8,
128 inwave.num_channels(),
129 samp_rate, num_samps, NULL);
130 else
131 in = SoundCreate(SoundFileFormatNone,
132 (NAS_BIG_ENDIAN ?
133 AuFormatLinearSigned16MSB :
134 AuFormatLinearSigned16LSB),
135 inwave.num_channels(),
136 samp_rate, num_samps,NULL);
137
138 er = AuSoundPlayFromData(aud, in, waveform, AuNone, VOL(100),
139 na_sync_play_cb,
140 (AuPointer) &d,(AuFlowID *) NULL,
141 (int *) NULL, (int *) NULL, &ret);
142 while (1)
143 {
144 AuNextEvent(aud, AuTrue, &ev);
145 AuDispatchEvent(aud, &ev);
146
147 if (d) break;
148 }
149 }
150
151 AuCloseServer(aud);
152
153 return 1;
154}
155
156int record_nas_wave(EST_Wave &wave, EST_Option &al)
157{
158 (void)wave;
159 (void)al;
160
161 cerr << "NAS: record not written yet\n";
162 return -1;
163}
164
165#else
166int nas_supported = FALSE;
167
168int play_nas_wave(EST_Wave &inwave, EST_Option &al)
169{
170 (void)inwave;
171 (void)al;
172 cerr << "NAS playback not supported" << endl;
173 return -1;
174}
175
176
177int record_nas_wave(EST_Wave &wave, EST_Option &al)
178{
179 (void)wave;
180 (void)al;
181 cerr << "NAS record not supported" << endl;
182 return -1;
183}
184
185#endif
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition EST_TKVL.cc:145
const int present(const K &rkey) const
Returns true if key is present.
Definition EST_TKVL.cc:222
const T * memory() const
int num_channels() const
return the number of channels in the waveform
Definition EST_Wave.h:145
int sample_rate() const
return the sampling rate (frequency)
Definition EST_Wave.h:147
void resample(int rate)
Resample waveform to rate
Definition EST_Wave.cc:489
EST_String sample_type() const
Definition EST_Wave.h:163
int num_samples() const
return the number of samples in the waveform
Definition EST_Wave.h:143