Edinburgh Speech Tools  2.1-release
pulseaudio.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1997,1998 */
6 /* Red Hat, Inc. */
7 /* Copyright (c) 2008 */
8 /* All Rights Reserved. */
9 /* */
10 /* Permission is hereby granted, free of charge, to use and distribute */
11 /* this software and its documentation without restriction, including */
12 /* without limitation the rights to use, copy, modify, merge, publish, */
13 /* distribute, sublicense, and/or sell copies of this work, and to */
14 /* permit persons to whom this work is furnished to do so, subject to */
15 /* the following conditions: */
16 /* 1. The code must retain the above copyright notice, this list of */
17 /* conditions and the following disclaimer. */
18 /* 2. Any modifications must be clearly marked as such. */
19 /* 3. Original authors' names are not deleted. */
20 /* 4. The authors' names are not used to endorse or promote products */
21 /* derived from this software without specific prior written */
22 /* permission. */
23 /* */
24 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
25 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
26 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
27 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
28 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
29 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
30 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
31 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
32 /* THIS SOFTWARE. */
33 /* */
34 /*************************************************************************/
35 /* Author : Michal Schmidt */
36 /* Date : November 2008 */
37 /*-----------------------------------------------------------------------*/
38 /* Optional support for PulseAudio */
39 /*=======================================================================*/
40 
41 #include "EST_Wave.h"
42 #include "EST_Option.h"
43 #include "audioP.h"
44 
45 using namespace std;
46 
47 #ifdef SUPPORT_PULSE
48 
49 #include <pulse/simple.h>
50 #include <pulse/error.h>
51 
52 int pulse_supported = TRUE;
53 const static char *err_prefix = "Pulseaudio: ";
54 
55 static int transfer_pulse_wave(EST_Wave &inwave, EST_Option &al, int record)
56 {
57  (void) al;
58  short *waveform;
59  int num_samples;
60  int err, pa_ret;
61  int ret = -1;
62  pa_simple *s = NULL;
63  pa_sample_spec ss;
64 
65  ss.format = PA_SAMPLE_S16NE;
66  ss.channels = 1;
67  ss.rate = inwave.sample_rate();
68 
69  waveform = inwave.values().memory();
70  num_samples = inwave.num_samples();
71 
72  /* In case num_samples == 0, don't play. Pulseaudio returns "invalid
73  * argument" if num_samples == 0, so it's better to check now.
74  * I don't expect num_samples < 0, but as we have to check anyway
75  * it doesn't hurt to check.
76  */
77  if (num_samples <= 0) {
78  ret=1;
79  goto finish;
80  }
81 
82  s = pa_simple_new(NULL, // Use the default server.
83  "Festival", // Our application's name.
84  record ? PA_STREAM_RECORD : PA_STREAM_PLAYBACK,
85  NULL, // Use the default device.
86  record ? "Record" : "Speech", // Description of our stream.
87  &ss, // Our sample format.
88  NULL, // Use default channel map
89  NULL, // Use default buffering attributes.
90  &err);
91 
92  if (!s) {
93  cerr << err_prefix << pa_strerror(err) << endl;
94  goto finish;
95  }
96 
97  pa_ret = record ?
98  pa_simple_read (s, waveform, num_samples*sizeof(short), &err) :
99  pa_simple_write(s, waveform, num_samples*sizeof(short), &err);
100 
101  if (pa_ret < 0) {
102  cerr << err_prefix << pa_strerror(err) << endl;
103  goto finish;
104  }
105 
106  if (!record && pa_simple_drain(s, &err) < 0) {
107  cerr << err_prefix << pa_strerror(err) << endl;
108  goto finish;
109  }
110 
111  ret = 1;
112 finish:
113  if (s)
114  pa_simple_free(s);
115  return ret;
116 }
117 
118 int play_pulse_wave(EST_Wave &inwave, EST_Option &al)
119 {
120  return transfer_pulse_wave(inwave, al, 0);
121 }
122 
123 int record_pulse_wave(EST_Wave &inwave, EST_Option &al)
124 {
125  return transfer_pulse_wave(inwave, al, 1);
126 }
127 
128 #else
130 
132 {
133  (void)inwave;
134  (void)al;
135  cerr << "Audio: pulse not compiled in this version" << endl;
136  return -1;
137 }
138 
140 {
141  (void)inwave;
142  (void)al;
143  cerr << "Audio: pulse not compiled in this version" << endl;
144  return -1;
145 }
146 
147 #endif
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
int pulse_supported
Definition: pulseaudio.cc:129
ssize_t num_samples() const
return the number of samples in the waveform
Definition: EST_Wave.h:143
int record_pulse_wave(EST_Wave &inwave, EST_Option &al)
Definition: pulseaudio.cc:139
void err(const char *message, LISP x) EST_NORETURN
Definition: slib.cc:608
const EST_SMatrix & values() const
Definition: EST_Wave.h:177
#define FALSE
Definition: EST_bool.h:119
NULL
Definition: EST_WFST.cc:55
int sample_rate() const
return the sampling rate (frequency)
Definition: EST_Wave.h:147
const T * memory() const
Definition: EST_TVector.h:238
#define TRUE
Definition: EST_bool.h:118
int play_pulse_wave(EST_Wave &inwave, EST_Option &al)
Definition: pulseaudio.cc:131