Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pulseaudio.cc
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 #ifdef SUPPORT_PULSE
46 
47 #include <pulse/simple.h>
48 #include <pulse/error.h>
49 
50 int pulse_supported = TRUE;
51 const static char *err_prefix = "Pulseaudio: ";
52 
53 static int transfer_pulse_wave(EST_Wave &inwave, EST_Option &al, int record)
54 {
55  short *waveform;
56  int num_samples;
57  int err, pa_ret;
58  int ret = -1;
59  pa_simple *s = NULL;
60  pa_sample_spec ss;
61 
62  ss.format = PA_SAMPLE_S16NE;
63  ss.channels = 1;
64  ss.rate = inwave.sample_rate();
65 
66  waveform = inwave.values().memory();
67  num_samples = inwave.num_samples();
68 
69  /* In case num_samples == 0, don't play. Pulseaudio returns "invalid
70  * argument" if num_samples == 0, so it's better to check now.
71  * I don't expect num_samples < 0, but as we have to check anyway
72  * it doesn't hurt to check.
73  */
74  if (num_samples <= 0) {
75  ret=1;
76  goto finish;
77  }
78 
79  s = pa_simple_new(NULL, // Use the default server.
80  "Festival", // Our application's name.
81  record ? PA_STREAM_RECORD : PA_STREAM_PLAYBACK,
82  NULL, // Use the default device.
83  record ? "Record" : "Speech", // Description of our stream.
84  &ss, // Our sample format.
85  NULL, // Use default channel map
86  NULL, // Use default buffering attributes.
87  &err);
88 
89  if (!s) {
90  cerr << err_prefix << pa_strerror(err) << endl;
91  goto finish;
92  }
93 
94  pa_ret = record ?
95  pa_simple_read (s, waveform, num_samples*sizeof(short), &err) :
96  pa_simple_write(s, waveform, num_samples*sizeof(short), &err);
97 
98  if (pa_ret < 0) {
99  cerr << err_prefix << pa_strerror(err) << endl;
100  goto finish;
101  }
102 
103  if (!record && pa_simple_drain(s, &err) < 0) {
104  cerr << err_prefix << pa_strerror(err) << endl;
105  goto finish;
106  }
107 
108  ret = 1;
109 finish:
110  if (s)
111  pa_simple_free(s);
112  return ret;
113 }
114 
115 int play_pulse_wave(EST_Wave &inwave, EST_Option &al)
116 {
117  return transfer_pulse_wave(inwave, al, 0);
118 }
119 
120 int record_pulse_wave(EST_Wave &inwave, EST_Option &al)
121 {
122  return transfer_pulse_wave(inwave, al, 1);
123 }
124 
125 #else
126 int pulse_supported = FALSE;
127 
128 int play_pulse_wave(EST_Wave &inwave, EST_Option &al)
129 {
130  (void)inwave;
131  (void)al;
132  cerr << "Audio: pulse not compiled in this version" << endl;
133  return -1;
134 }
135 
136 int record_pulse_wave(EST_Wave &inwave, EST_Option &al)
137 {
138  (void)inwave;
139  (void)al;
140  cerr << "Audio: pulse not compiled in this version" << endl;
141  return -1;
142 }
143 
144 #endif
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
int num_samples() const
return the number of samples in the waveform
Definition: EST_Wave.h:143
int sample_rate() const
return the sampling rate (frequency)
Definition: EST_Wave.h:147
const T * memory() const
Definition: EST_TVector.h:239