| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2012 */ |
| 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 |
/* CARNEGIE MELLON UNIVERSITY 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 CARNEGIE MELLON UNIVERSITY 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: Alan W Black ([email protected]) */ |
| 34 |
/* Date: February 2012 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Native access to pulseaudio on Linux */ |
| 38 |
/* (surprisingly hard to find programming documentation about pa) */ |
| 39 |
/* Sort of got it from here */ |
| 40 |
/* http://pulseaudio.org/ */ |
| 41 |
/* */ |
| 42 |
/*************************************************************************/ |
| 43 |
|
| 44 |
#include "cst_string.h" |
| 45 |
#include "cst_wave.h" |
| 46 |
#include "cst_audio.h" |
| 47 |
|
| 48 |
#include <pulse/simple.h> |
| 49 |
|
| 50 |
cst_audiodev *audio_open_pulseaudio(unsigned int sps, int channels, cst_audiofmt fmt) |
| 51 |
{ |
| 52 |
cst_audiodev *ad; |
| 53 |
int err=0; |
| 54 |
|
| 55 |
/* Pulseaudio specific stuff */ |
| 56 |
pa_sample_spec *ss; |
| 57 |
pa_simple *s; |
| 58 |
|
| 59 |
ss = cst_alloc(pa_sample_spec,1); |
| 60 |
ss->rate = sps; |
| 61 |
ss->channels = channels; |
| 62 |
switch (fmt) |
| 63 |
{ |
| 64 |
case CST_AUDIO_LINEAR16: |
| 65 |
if (CST_LITTLE_ENDIAN) |
| 66 |
ss->format = PA_SAMPLE_S16LE; |
| 67 |
else |
| 68 |
ss->format = PA_SAMPLE_S16BE; |
| 69 |
break; |
| 70 |
case CST_AUDIO_LINEAR8: |
| 71 |
ss->format = PA_SAMPLE_U8; |
| 72 |
break; |
| 73 |
case CST_AUDIO_MULAW: |
| 74 |
ss->format = PA_SAMPLE_ULAW; |
| 75 |
break; |
| 76 |
default: |
| 77 |
return NULL; |
| 78 |
break; |
| 79 |
} |
| 80 |
|
| 81 |
s = pa_simple_new( |
| 82 |
NULL, /* use default server */ |
| 83 |
"flite", |
| 84 |
PA_STREAM_PLAYBACK, |
| 85 |
NULL, /* use default device */ |
| 86 |
"Speech", |
| 87 |
ss, |
| 88 |
NULL, /* default channel map */ |
| 89 |
NULL, /* default buffering attributes */ |
| 90 |
&err); |
| 91 |
if (err < 0) |
| 92 |
return NULL; |
| 93 |
|
| 94 |
/* Write hardware parameters to flite audio device data structure */ |
| 95 |
ad = cst_alloc(cst_audiodev, 1); |
| 96 |
ad->real_sps = ad->sps = sps; |
| 97 |
ad->real_channels = ad->channels = channels; |
| 98 |
ad->real_fmt = ad->fmt = fmt; |
| 99 |
ad->platform_data = (void *) s; |
| 100 |
|
| 101 |
return ad; |
| 102 |
} |
| 103 |
|
| 104 |
int audio_close_pulseaudio(cst_audiodev *ad) |
| 105 |
{ |
| 106 |
int result; |
| 107 |
pa_simple *s; |
| 108 |
|
| 109 |
if (ad == NULL) |
| 110 |
return 0; |
| 111 |
|
| 112 |
s = (pa_simple *) ad->platform_data; |
| 113 |
|
| 114 |
pa_simple_drain(s,&result); |
| 115 |
|
| 116 |
pa_simple_free(s); |
| 117 |
cst_free(ad); |
| 118 |
return result; |
| 119 |
} |
| 120 |
|
| 121 |
int audio_write_pulseaudio(cst_audiodev *ad, void *samples, int num_bytes) |
| 122 |
{ |
| 123 |
pa_simple *s; |
| 124 |
int err; |
| 125 |
|
| 126 |
s = (pa_simple *)ad->platform_data; |
| 127 |
pa_simple_write(s,samples,(size_t)num_bytes,&err); |
| 128 |
|
| 129 |
return num_bytes; |
| 130 |
} |
| 131 |
|
| 132 |
int audio_flush_pulseaudio(cst_audiodev *ad) |
| 133 |
{ |
| 134 |
pa_simple *s; |
| 135 |
int err; |
| 136 |
|
| 137 |
s = (pa_simple *)ad->platform_data; |
| 138 |
pa_simple_drain(s,&err); |
| 139 |
|
| 140 |
return err; |
| 141 |
} |
| 142 |
|
| 143 |
int audio_drain_pulseaudio(cst_audiodev *ad) |
| 144 |
{ |
| 145 |
pa_simple *s; |
| 146 |
int err; |
| 147 |
|
| 148 |
s = (pa_simple *)ad->platform_data; |
| 149 |
pa_simple_drain(s,&err); |
| 150 |
|
| 151 |
return err; |
| 152 |
} |
| 153 |
|