1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*
rtpulse.c:
Copyright (C) 2008 Victor Lazzarini
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include <csdl.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#include <string.h>
typedef struct _pulse_params {
pa_simple *ps;
pa_sample_spec spec;
float *buf;
} pulse_params;
typedef struct _pulse_globals {
char server[64];
char oname[32];
char iname[32];
} pulse_globals;
PUBLIC int csoundModuleCreate(CSOUND *csound)
{
pulse_globals *p;
int siz = 64;
OPARMS oparms;
csound->GetOParms(csound, &oparms);
if (oparms.msglevel & 0x400)
csound->Message(csound, Str("PulseAudio client RT IO module for Csound"
"by Victor Lazzarini\n"));
if (UNLIKELY(csound->CreateGlobalVariable(csound, "_pulse_globals",
sizeof(pulse_globals)) != 0)) {
csound->ErrorMsg(csound, Str(" *** rtpulse: error allocating globals"));
return -1;
}
p = (pulse_globals*) csound->QueryGlobalVariableNoCheck(csound,
"_pulse_globals");
strcpy(&(p->server[0]), "default");
csound->CreateConfigurationVariable(
csound,"server", (void*) &(p->server[0]),
CSOUNDCFG_STRING, 0, NULL, &siz,
"PulseAudio server name (default: default server)", NULL);
strcpy(&(p->oname[0]), "csound-out");
siz = 32;
csound->CreateConfigurationVariable(
csound,"output_stream", (void*) &(p->oname[0]),
CSOUNDCFG_STRING, 0, NULL, &siz,
"PulseAudio output stream name (default: csound-out)", NULL);
strcpy(&(p->iname[0]), "csound-in");
csound->CreateConfigurationVariable(
csound,"input_stream", (void*) &(p->iname[0]),
CSOUNDCFG_STRING, 0, NULL, &siz,
"PulseAudio input stream name (default: csound-in)", NULL);
return 0;
}
PUBLIC int csoundModuleInfo(void)
{
return ((CS_APIVERSION << 16) + (CS_APISUBVER << 8) + (int) sizeof(MYFLT));
}
static int pulse_playopen(CSOUND *csound, const csRtAudioParams *parm)
{
pulse_params *pulse;
pulse_globals *pg;
const char *server;
/* pa_buffer_attr attr */
int pulserror;
pulse = (pulse_params *) csound->Malloc(csound, sizeof(pulse_params));
*(csound->GetRtPlayUserData(csound)) = (void *) pulse;
pulse->spec.rate = csound->GetSr(csound);
pulse->spec.channels = csound->GetNchnls(csound);
pulse->spec.format = PA_SAMPLE_FLOAT32;
pulse->buf =
(float *) csound->Malloc(csound,
sizeof(float)*parm->bufSamp_SW*pulse->spec.channels);
if(!pa_sample_spec_valid(&(pulse->spec))) {
csound->ErrorMsg(csound,Str("Pulse audio module error: invalid sample spec, "
"check number of output channels (%d)\n"),
csound->GetNchnls(csound));
return -1;
}
/*
attr.maxlength = parm->bufSamp_HW;
attr.prebuf = parm->bufSamp_SW;
attr.tlength = parm->bufSamp_SW;
attr.minreq = parm->bufSamp_SW;
attr.fragsize = parm->bufSamp_SW;
*/
pg = (pulse_globals*) csound->QueryGlobalVariableNoCheck(csound,
"_pulse_globals");
if (!strcmp(pg->server,"default")){
server = NULL;
csound->Message(csound, Str("PulseAudio output server: default\n"));
}
else {
server = pg->server;
csound->Message(csound, Str("PulseAudio output server %s\n"), server);
}
pulse->ps = pa_simple_new (server,
"csound", // client name
PA_STREAM_PLAYBACK, // stream direction
parm->devName, // device name
&(pg->oname[0]), // stream name
&(pulse->spec), // sample spec
NULL, // channel map (NULL=default)
/*&attrib*/ NULL, // buffer attribute
&pulserror
) ;
if (LIKELY(pulse->ps)){
csound->Message(csound, Str("pulseaudio output open\n"));
return 0;
}
else {
csound->ErrorMsg(csound,Str("Pulse audio module error: %s\n"),
pa_strerror(pulserror));
return -1;
}
}
static void pulse_play(CSOUND *csound, const MYFLT *outbuf, int nbytes){
int i, bufsiz, pulserror;
float *buf;
pulse_params *pulse = (pulse_params*) *(csound->GetRtPlayUserData(csound));
//MYFLT norm = csound->e0dbfs;
bufsiz = nbytes/sizeof(MYFLT);
buf = pulse->buf;
for (i=0;i<bufsiz;i++) buf[i] = outbuf[i];
if (UNLIKELY(pa_simple_write(pulse->ps, buf,
bufsiz*sizeof(float), &pulserror) < 0))
csound->ErrorMsg(csound,Str("Pulse audio module error: %s\n"),
pa_strerror(pulserror));
}
static void pulse_close(CSOUND *csound)
{
int error;
pulse_params *pulse = (pulse_params*) *(csound->GetRtPlayUserData(csound));
if (pulse != NULL){
pa_simple_drain(pulse->ps, &error);
pa_simple_free(pulse->ps);
csound->Free(csound,pulse->buf);
}
pulse = (pulse_params*) *(csound->GetRtRecordUserData(csound)) ;
if (pulse != NULL){
pa_simple_free(pulse->ps);
csound->Free(csound,pulse->buf);
}
csound->DestroyGlobalVariable(csound, "pulse_globals");
}
static int pulse_recopen(CSOUND *csound, const csRtAudioParams *parm)
{
pulse_params *pulse;
pulse_globals *pg;
const char *server;
/*pa_buffer_attr attr;*/
int pulserror;
pulse = (pulse_params *) csound->Malloc(csound, sizeof(pulse_params));
*(csound->GetRtRecordUserData(csound)) = (void *) pulse;
pulse->spec.rate = csound->GetSr(csound);
pulse->spec.channels = csound->GetNchnls_i(csound);
pulse->spec.format = PA_SAMPLE_FLOAT32;
pulse->buf =
(float *) csound->Malloc(csound,
sizeof(float)*parm->bufSamp_SW*pulse->spec.channels);
/*
attr.maxlength = parm->bufSamp_HW;
attr.prebuf = 0;
attr.tlength = parm->bufSamp_SW;
attr.minreq = parm->bufSamp_SW;
attr.fragsize = parm->bufSamp_SW;
*/
pg = (pulse_globals*) csound->QueryGlobalVariableNoCheck(csound,
"_pulse_globals");
if (!strcmp(pg->server,"default")){
server = NULL;
csound->Message(csound, Str("PulseAudio input server: default\n"));
}
else {
server = pg->server;
csound->Message(csound, Str("PulseAudio input server %s\n"), server);
}
pulse->ps = pa_simple_new (server,
"csound", // client name
PA_STREAM_RECORD, // stream direction
parm->devName, // device name
&(pg->iname[0]), // stream name
&(pulse->spec), // sample spec
NULL, // channel map (NULL=default)
/*&attr*/ NULL, // buffer attribute
&pulserror );
if (LIKELY(pulse->ps)) return 0;
else {
csound->ErrorMsg(csound,Str("Pulse audio module error: %s\n"),
pa_strerror(pulserror));
return -1;
}
}
static int pulse_record(CSOUND *csound, MYFLT *inbuf, int nbytes)
{
int i, bufsiz,pulserror;
float *buf;
pulse_params *pulse = (pulse_params*) *(csound->GetRtRecordUserData(csound)) ;
//MYFLT norm = csound->e0dbfs;
bufsiz = nbytes/sizeof(MYFLT);
buf = pulse->buf;
if (UNLIKELY(pa_simple_read(pulse->ps, buf,
bufsiz*sizeof(float), &pulserror) < 0)) {
csound->ErrorMsg(csound,Str("Pulse audio module error: %s\n"),
pa_strerror(pulserror));
return -1;
}
else {
for (i=0;i<bufsiz;i++) inbuf[i] = buf[i];
return nbytes;
}
}
PUBLIC int csoundModuleInit(CSOUND *csound)
{
char *s;
int i;
char buf[9];
csound->module_list_add(csound, "pulse", "audio");
s = (char*) csound->QueryGlobalVariable(csound, "_RTAUDIO");
i = 0;
if (s != NULL) {
while (*s != (char) 0 && i < 8)
buf[i++] = *(s++) | (char) 0x20;
}
buf[i] = (char) 0;
if (strcmp(&(buf[0]), "pulse") == 0) {
csound->Message(csound, Str("rtaudio: pulseaudio module enabled\n"));
csound->SetPlayopenCallback(csound, pulse_playopen);
csound->SetRecopenCallback(csound, pulse_recopen);
csound->SetRtplayCallback(csound, pulse_play);
csound->SetRtrecordCallback(csound, pulse_record);
csound->SetRtcloseCallback(csound, pulse_close);
}
return 0;
}
|