| 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 |
#ifndef __alsa_dsp__ |
| 38 |
#define __alsa_dsp__ |
| 39 |
|
| 40 |
#include <stdio.h> |
| 41 |
#include <pthread.h> |
| 42 |
#include <sys/types.h> |
| 43 |
#include <pwd.h> |
| 44 |
|
| 45 |
#include <alsa/asoundlib.h> |
| 46 |
#include "faust/audio/audio.h" |
| 47 |
#include "faust/audio/dsp.h" |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
using namespace std; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#ifdef __x86_64__ |
| 66 |
|
| 67 |
#define uint32 unsigned int |
| 68 |
#define uint64 unsigned long int |
| 69 |
|
| 70 |
#define int32 int |
| 71 |
#define int64 long int |
| 72 |
|
| 73 |
#else |
| 74 |
|
| 75 |
#define uint32 unsigned int |
| 76 |
#define uint64 unsigned long long int |
| 77 |
|
| 78 |
#define int32 int |
| 79 |
#define int64 long long int |
| 80 |
#endif |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#define check_error(err) if (err) { printf("%s:%d, alsa error %d : %s\n", __FILE__, __LINE__, err, snd_strerror(err)); exit(1); } |
| 85 |
#define check_error_msg(err,msg) if (err) { fprintf(stderr, "%s:%d, %s : %s(%d)\n", __FILE__, __LINE__, msg, snd_strerror(err), err); exit(1); } |
| 86 |
#define display_error_msg(err,msg) if (err) { fprintf(stderr, "%s:%d, %s : %s(%d)\n", __FILE__, __LINE__, msg, snd_strerror(err), err); } |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
static bool setRealtimePriority () |
| 95 |
{ |
| 96 |
struct passwd * pw; |
| 97 |
int err; |
| 98 |
uid_t uid; |
| 99 |
struct sched_param param; |
| 100 |
|
| 101 |
uid = getuid (); |
| 102 |
pw = getpwnam ("root"); |
| 103 |
setuid (pw->pw_uid); |
| 104 |
param.sched_priority = 50; |
| 105 |
err = sched_setscheduler(0, SCHED_RR, ¶m); |
| 106 |
setuid (uid); |
| 107 |
return (err != -1); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
enum { kRead = 1, kWrite = 2, kReadWrite = 3 }; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
class AudioParam |
| 124 |
{ |
| 125 |
public: |
| 126 |
|
| 127 |
const char* fCardName; |
| 128 |
unsigned int fFrequency; |
| 129 |
unsigned int fBuffering; |
| 130 |
unsigned int fPeriods; |
| 131 |
|
| 132 |
unsigned int fSoftInputs; |
| 133 |
unsigned int fSoftOutputs; |
| 134 |
|
| 135 |
public : |
| 136 |
AudioParam() : |
| 137 |
fCardName("hw:0"), |
| 138 |
fFrequency(44100), |
| 139 |
fBuffering(512), |
| 140 |
fPeriods(2), |
| 141 |
fSoftInputs(2), |
| 142 |
fSoftOutputs(2) |
| 143 |
{} |
| 144 |
|
| 145 |
AudioParam& cardName(const char* n) { fCardName = n; return *this; } |
| 146 |
AudioParam& frequency(int f) { fFrequency = f; return *this; } |
| 147 |
AudioParam& buffering(int fpb) { fBuffering = fpb; return *this; } |
| 148 |
AudioParam& periods(int p) { fPeriods = p; return *this; } |
| 149 |
AudioParam& inputs(int n) { fSoftInputs = n; return *this; } |
| 150 |
AudioParam& outputs(int n) { fSoftOutputs = n; return *this; } |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
class AudioInterface : public AudioParam |
| 157 |
{ |
| 158 |
public : |
| 159 |
snd_pcm_t* fOutputDevice ; |
| 160 |
snd_pcm_t* fInputDevice ; |
| 161 |
snd_pcm_hw_params_t* fInputParams; |
| 162 |
snd_pcm_hw_params_t* fOutputParams; |
| 163 |
|
| 164 |
snd_pcm_format_t fSampleFormat; |
| 165 |
snd_pcm_access_t fSampleAccess; |
| 166 |
|
| 167 |
unsigned int fCardInputs; |
| 168 |
unsigned int fCardOutputs; |
| 169 |
|
| 170 |
unsigned int fChanInputs; |
| 171 |
unsigned int fChanOutputs; |
| 172 |
|
| 173 |
|
| 174 |
void* fInputCardBuffer; |
| 175 |
void* fOutputCardBuffer; |
| 176 |
|
| 177 |
|
| 178 |
void* fInputCardChannels[256]; |
| 179 |
void* fOutputCardChannels[256]; |
| 180 |
|
| 181 |
|
| 182 |
float* fInputSoftChannels[256]; |
| 183 |
float* fOutputSoftChannels[256]; |
| 184 |
|
| 185 |
public : |
| 186 |
|
| 187 |
const char* cardName() { return fCardName; } |
| 188 |
int frequency() { return fFrequency; } |
| 189 |
int buffering() { return fBuffering; } |
| 190 |
int periods() { return fPeriods; } |
| 191 |
|
| 192 |
float** inputSoftChannels() { return fInputSoftChannels; } |
| 193 |
float** outputSoftChannels() { return fOutputSoftChannels; } |
| 194 |
|
| 195 |
|
| 196 |
AudioInterface(const AudioParam& ap = AudioParam()) : AudioParam(ap) |
| 197 |
{ |
| 198 |
|
| 199 |
fInputDevice = 0; |
| 200 |
fOutputDevice = 0; |
| 201 |
fInputParams = 0; |
| 202 |
fOutputParams = 0; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
void open() |
| 209 |
{ |
| 210 |
int err; |
| 211 |
|
| 212 |
|
| 213 |
err = snd_pcm_open( &fInputDevice, fCardName, SND_PCM_STREAM_CAPTURE, 0 ); check_error(err) |
| 214 |
err = snd_pcm_open( &fOutputDevice, fCardName, SND_PCM_STREAM_PLAYBACK, 0 ); check_error(err) |
| 215 |
|
| 216 |
|
| 217 |
err = snd_pcm_hw_params_malloc ( &fInputParams ); check_error(err); |
| 218 |
setAudioParams(fInputDevice, fInputParams); |
| 219 |
|
| 220 |
|
| 221 |
err = snd_pcm_hw_params_malloc ( &fOutputParams ); check_error(err) |
| 222 |
setAudioParams(fOutputDevice, fOutputParams); |
| 223 |
|
| 224 |
|
| 225 |
fCardInputs = fSoftInputs; |
| 226 |
fCardOutputs = fSoftOutputs; |
| 227 |
|
| 228 |
snd_pcm_hw_params_set_channels_near(fInputDevice, fInputParams, &fCardInputs); |
| 229 |
snd_pcm_hw_params_set_channels_near(fOutputDevice, fOutputParams, &fCardOutputs); |
| 230 |
|
| 231 |
printf("inputs : %u, outputs : %u\n", fCardInputs, fCardOutputs); |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
err = snd_pcm_hw_params (fInputDevice, fInputParams ); check_error (err); |
| 236 |
err = snd_pcm_hw_params (fOutputDevice, fOutputParams ); check_error (err); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
if (fSampleAccess == SND_PCM_ACCESS_RW_INTERLEAVED) { |
| 242 |
fInputCardBuffer = calloc(interleavedBufferSize(fInputParams), 1); |
| 243 |
fOutputCardBuffer = calloc(interleavedBufferSize(fOutputParams), 1); |
| 244 |
|
| 245 |
} else { |
| 246 |
for (unsigned int i = 0; i < fCardInputs; i++) { |
| 247 |
fInputCardChannels[i] = calloc(noninterleavedBufferSize(fInputParams), 1); |
| 248 |
} |
| 249 |
for (unsigned int i = 0; i < fCardOutputs; i++) { |
| 250 |
fOutputCardChannels[i] = calloc(noninterleavedBufferSize(fOutputParams), 1); |
| 251 |
} |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
fChanInputs = max(fSoftInputs, fCardInputs); assert (fChanInputs < 256); |
| 258 |
fChanOutputs = max(fSoftOutputs, fCardOutputs); assert (fChanOutputs < 256); |
| 259 |
|
| 260 |
for (unsigned int i = 0; i < fChanInputs; i++) { |
| 261 |
fInputSoftChannels[i] = (float*) calloc (fBuffering, sizeof(float)); |
| 262 |
for (unsigned int j = 0; j < fBuffering; j++) { |
| 263 |
fInputSoftChannels[i][j] = 0.0; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
for (unsigned int i = 0; i < fChanOutputs; i++) { |
| 268 |
fOutputSoftChannels[i] = (float*) calloc (fBuffering, sizeof(float)); |
| 269 |
for (unsigned int j = 0; j < fBuffering; j++) { |
| 270 |
fOutputSoftChannels[i][j] = 0.0; |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
void setAudioParams(snd_pcm_t* stream, snd_pcm_hw_params_t* params) |
| 276 |
{ |
| 277 |
int err; |
| 278 |
|
| 279 |
|
| 280 |
err = snd_pcm_hw_params_any ( stream, params ); |
| 281 |
check_error_msg(err, "unable to init parameters") |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
err = snd_pcm_hw_params_set_access (stream, params, SND_PCM_ACCESS_RW_NONINTERLEAVED ); |
| 286 |
if (err) { |
| 287 |
err = snd_pcm_hw_params_set_access (stream, params, SND_PCM_ACCESS_RW_INTERLEAVED ); |
| 288 |
check_error_msg(err, "unable to set access mode neither to non-interleaved or to interleaved"); |
| 289 |
} |
| 290 |
snd_pcm_hw_params_get_access(params, &fSampleAccess); |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
err = snd_pcm_hw_params_set_format (stream, params, SND_PCM_FORMAT_S32); |
| 295 |
if (err) { |
| 296 |
err = snd_pcm_hw_params_set_format (stream, params, SND_PCM_FORMAT_S16); |
| 297 |
check_error_msg(err, "unable to set format to either 32-bits or 16-bits"); |
| 298 |
} |
| 299 |
snd_pcm_hw_params_get_format(params, &fSampleFormat); |
| 300 |
|
| 301 |
snd_pcm_hw_params_set_rate_near (stream, params, &fFrequency, 0); |
| 302 |
|
| 303 |
|
| 304 |
err = snd_pcm_hw_params_set_period_size (stream, params, fBuffering, 0); |
| 305 |
check_error_msg(err, "period size not available"); |
| 306 |
|
| 307 |
err = snd_pcm_hw_params_set_periods (stream, params, fPeriods, 0); |
| 308 |
check_error_msg(err, "number of periods not available"); |
| 309 |
} |
| 310 |
|
| 311 |
ssize_t interleavedBufferSize (snd_pcm_hw_params_t* params) |
| 312 |
{ |
| 313 |
_snd_pcm_format format; snd_pcm_hw_params_get_format(params, &format); |
| 314 |
snd_pcm_uframes_t psize; snd_pcm_hw_params_get_period_size(params, &psize, NULL); |
| 315 |
unsigned int channels; snd_pcm_hw_params_get_channels(params, &channels); |
| 316 |
ssize_t bsize = snd_pcm_format_size (format, psize * channels); |
| 317 |
return bsize; |
| 318 |
} |
| 319 |
|
| 320 |
ssize_t noninterleavedBufferSize (snd_pcm_hw_params_t* params) |
| 321 |
{ |
| 322 |
_snd_pcm_format format; snd_pcm_hw_params_get_format(params, &format); |
| 323 |
snd_pcm_uframes_t psize; snd_pcm_hw_params_get_period_size(params, &psize, NULL); |
| 324 |
ssize_t bsize = snd_pcm_format_size (format, psize); |
| 325 |
return bsize; |
| 326 |
} |
| 327 |
|
| 328 |
void close() |
| 329 |
{} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
void read() |
| 336 |
{ |
| 337 |
if (fSampleAccess == SND_PCM_ACCESS_RW_INTERLEAVED) { |
| 338 |
|
| 339 |
int count = snd_pcm_readi(fInputDevice, fInputCardBuffer, fBuffering); |
| 340 |
if (count<0) { |
| 341 |
display_error_msg(count, "reading samples"); |
| 342 |
int err = snd_pcm_prepare(fInputDevice); |
| 343 |
check_error_msg(err, "preparing input stream"); |
| 344 |
} |
| 345 |
|
| 346 |
if (fSampleFormat == SND_PCM_FORMAT_S16) { |
| 347 |
|
| 348 |
short* buffer16b = (short*) fInputCardBuffer; |
| 349 |
for (unsigned int s = 0; s < fBuffering; s++) { |
| 350 |
for (unsigned int c = 0; c < fCardInputs; c++) { |
| 351 |
fInputSoftChannels[c][s] = float(buffer16b[c + s*fCardInputs])*(1.0/float(SHRT_MAX)); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
} else if (fSampleFormat == SND_PCM_FORMAT_S32) { |
| 356 |
|
| 357 |
int32* buffer32b = (int32*) fInputCardBuffer; |
| 358 |
for (unsigned int s = 0; s < fBuffering; s++) { |
| 359 |
for (unsigned int c = 0; c < fCardInputs; c++) { |
| 360 |
fInputSoftChannels[c][s] = float(buffer32b[c + s*fCardInputs])*(1.0/float(INT_MAX)); |
| 361 |
} |
| 362 |
} |
| 363 |
} else { |
| 364 |
|
| 365 |
printf("unrecognized input sample format : %u\n", fSampleFormat); |
| 366 |
exit(1); |
| 367 |
} |
| 368 |
|
| 369 |
} else if (fSampleAccess == SND_PCM_ACCESS_RW_NONINTERLEAVED) { |
| 370 |
|
| 371 |
int count = snd_pcm_readn(fInputDevice, fInputCardChannels, fBuffering); |
| 372 |
if (count<0) { |
| 373 |
display_error_msg(count, "reading samples"); |
| 374 |
int err = snd_pcm_prepare(fInputDevice); |
| 375 |
check_error_msg(err, "preparing input stream"); |
| 376 |
} |
| 377 |
|
| 378 |
if (fSampleFormat == SND_PCM_FORMAT_S16) { |
| 379 |
|
| 380 |
for (unsigned int c = 0; c < fCardInputs; c++) { |
| 381 |
short* chan16b = (short*) fInputCardChannels[c]; |
| 382 |
for (unsigned int s = 0; s < fBuffering; s++) { |
| 383 |
fInputSoftChannels[c][s] = float(chan16b[s])*(1.0/float(SHRT_MAX)); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
} else if (fSampleFormat == SND_PCM_FORMAT_S32) { |
| 388 |
|
| 389 |
for (unsigned int c = 0; c < fCardInputs; c++) { |
| 390 |
int32* chan32b = (int32*) fInputCardChannels[c]; |
| 391 |
for (unsigned int s = 0; s < fBuffering; s++) { |
| 392 |
fInputSoftChannels[c][s] = float(chan32b[s])*(1.0/float(INT_MAX)); |
| 393 |
} |
| 394 |
} |
| 395 |
} else { |
| 396 |
|
| 397 |
printf("unrecognized input sample format : %u\n", fSampleFormat); |
| 398 |
exit(1); |
| 399 |
} |
| 400 |
|
| 401 |
} else { |
| 402 |
check_error_msg(-10000, "unknow access mode"); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
void write() |
| 411 |
{ |
| 412 |
recovery : |
| 413 |
|
| 414 |
if (fSampleAccess == SND_PCM_ACCESS_RW_INTERLEAVED) { |
| 415 |
|
| 416 |
if (fSampleFormat == SND_PCM_FORMAT_S16) { |
| 417 |
|
| 418 |
short* buffer16b = (short*) fOutputCardBuffer; |
| 419 |
for (unsigned int f = 0; f < fBuffering; f++) { |
| 420 |
for (unsigned int c = 0; c < fCardOutputs; c++) { |
| 421 |
float x = fOutputSoftChannels[c][f]; |
| 422 |
buffer16b[c + f*fCardOutputs] = short( max(min(x,1.0f),-1.0f) * float(SHRT_MAX) ) ; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
} else if (fSampleFormat == SND_PCM_FORMAT_S32) { |
| 427 |
|
| 428 |
int32* buffer32b = (int32*) fOutputCardBuffer; |
| 429 |
for (unsigned int f = 0; f < fBuffering; f++) { |
| 430 |
for (unsigned int c = 0; c < fCardOutputs; c++) { |
| 431 |
float x = fOutputSoftChannels[c][f]; |
| 432 |
buffer32b[c + f*fCardOutputs] = int( max(min(x,1.0f),-1.0f) * float(INT_MAX) ) ; |
| 433 |
} |
| 434 |
} |
| 435 |
} else { |
| 436 |
|
| 437 |
printf("unrecognized output sample format : %u\n", fSampleFormat); |
| 438 |
exit(1); |
| 439 |
} |
| 440 |
|
| 441 |
int count = snd_pcm_writei(fOutputDevice, fOutputCardBuffer, fBuffering); |
| 442 |
if (count<0) { |
| 443 |
display_error_msg(count, "w3"); |
| 444 |
int err = snd_pcm_prepare(fOutputDevice); |
| 445 |
check_error_msg(err, "preparing output stream"); |
| 446 |
goto recovery; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
} else if (fSampleAccess == SND_PCM_ACCESS_RW_NONINTERLEAVED) { |
| 451 |
|
| 452 |
if (fSampleFormat == SND_PCM_FORMAT_S16) { |
| 453 |
|
| 454 |
for (unsigned int c = 0; c < fCardOutputs; c++) { |
| 455 |
short* chan16b = (short*) fOutputCardChannels[c]; |
| 456 |
for (unsigned int f = 0; f < fBuffering; f++) { |
| 457 |
float x = fOutputSoftChannels[c][f]; |
| 458 |
chan16b[f] = short( max(min(x,1.0f),-1.0f) * float(SHRT_MAX) ) ; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
} else if (fSampleFormat == SND_PCM_FORMAT_S32) { |
| 463 |
|
| 464 |
for (unsigned int c = 0; c < fCardOutputs; c++) { |
| 465 |
int32* chan32b = (int32*) fOutputCardChannels[c]; |
| 466 |
for (unsigned int f = 0; f < fBuffering; f++) { |
| 467 |
float x = fOutputSoftChannels[c][f]; |
| 468 |
chan32b[f] = int( max(min(x,1.0f),-1.0f) * float(INT_MAX) ) ; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
} else { |
| 473 |
|
| 474 |
printf("unrecognized output sample format : %u\n", fSampleFormat); |
| 475 |
exit(1); |
| 476 |
} |
| 477 |
|
| 478 |
int count = snd_pcm_writen(fOutputDevice, fOutputCardChannels, fBuffering); |
| 479 |
if (count<0) { |
| 480 |
display_error_msg(count, "w3"); |
| 481 |
int err = snd_pcm_prepare(fOutputDevice); |
| 482 |
check_error_msg(err, "preparing output stream"); |
| 483 |
goto recovery; |
| 484 |
} |
| 485 |
|
| 486 |
} else { |
| 487 |
check_error_msg(-10000, "unknow access mode"); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
void shortinfo() |
| 495 |
{ |
| 496 |
int err; |
| 497 |
snd_ctl_card_info_t* card_info; |
| 498 |
snd_ctl_t* ctl_handle; |
| 499 |
err = snd_ctl_open (&ctl_handle, fCardName, 0); check_error(err); |
| 500 |
snd_ctl_card_info_alloca (&card_info); |
| 501 |
err = snd_ctl_card_info(ctl_handle, card_info); check_error(err); |
| 502 |
printf("%s|%d|%d|%d|%d|%s\n", |
| 503 |
snd_ctl_card_info_get_driver(card_info), |
| 504 |
fCardInputs, fCardOutputs, |
| 505 |
fFrequency, fBuffering, |
| 506 |
snd_pcm_format_name((_snd_pcm_format)fSampleFormat)); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
void longinfo() |
| 513 |
{ |
| 514 |
int err; |
| 515 |
snd_ctl_card_info_t* card_info; |
| 516 |
snd_ctl_t* ctl_handle; |
| 517 |
|
| 518 |
printf("Audio Interface Description :\n"); |
| 519 |
printf("Sampling Frequency : %d, Sample Format : %s, buffering : %d\n", |
| 520 |
fFrequency, snd_pcm_format_name((_snd_pcm_format)fSampleFormat), fBuffering); |
| 521 |
printf("Software inputs : %2d, Software outputs : %2d\n", fSoftInputs, fSoftOutputs); |
| 522 |
printf("Hardware inputs : %2d, Hardware outputs : %2d\n", fCardInputs, fCardOutputs); |
| 523 |
printf("Channel inputs : %2d, Channel outputs : %2d\n", fChanInputs, fChanOutputs); |
| 524 |
|
| 525 |
|
| 526 |
err = snd_ctl_open (&ctl_handle, fCardName, 0); check_error(err); |
| 527 |
snd_ctl_card_info_alloca (&card_info); |
| 528 |
err = snd_ctl_card_info(ctl_handle, card_info); check_error(err); |
| 529 |
printCardInfo(card_info); |
| 530 |
|
| 531 |
|
| 532 |
if (fSoftInputs > 0) printHWParams(fInputParams); |
| 533 |
if (fSoftOutputs > 0) printHWParams(fOutputParams); |
| 534 |
} |
| 535 |
|
| 536 |
void printCardInfo(snd_ctl_card_info_t* ci) |
| 537 |
{ |
| 538 |
printf("Card info (address : %p)\n", ci); |
| 539 |
printf("\tID = %s\n", snd_ctl_card_info_get_id(ci)); |
| 540 |
printf("\tDriver = %s\n", snd_ctl_card_info_get_driver(ci)); |
| 541 |
printf("\tName = %s\n", snd_ctl_card_info_get_name(ci)); |
| 542 |
printf("\tLongName = %s\n", snd_ctl_card_info_get_longname(ci)); |
| 543 |
printf("\tMixerName = %s\n", snd_ctl_card_info_get_mixername(ci)); |
| 544 |
printf("\tComponents = %s\n", snd_ctl_card_info_get_components(ci)); |
| 545 |
printf("--------------\n"); |
| 546 |
} |
| 547 |
|
| 548 |
void printHWParams( snd_pcm_hw_params_t* params ) |
| 549 |
{ |
| 550 |
printf("HW Params info (address : %p)\n", params); |
| 551 |
#if 0 |
| 552 |
printf("\tChannels = %d\n", snd_pcm_hw_params_get_channels(params)); |
| 553 |
printf("\tFormat = %s\n", snd_pcm_format_name((_snd_pcm_format)snd_pcm_hw_params_get_format(params))); |
| 554 |
printf("\tAccess = %s\n", snd_pcm_access_name((_snd_pcm_access)snd_pcm_hw_params_get_access(params))); |
| 555 |
printf("\tRate = %d\n", snd_pcm_hw_params_get_rate(params, NULL)); |
| 556 |
printf("\tPeriods = %d\n", snd_pcm_hw_params_get_periods(params, NULL)); |
| 557 |
printf("\tPeriod size = %d\n", (int)snd_pcm_hw_params_get_period_size(params, NULL)); |
| 558 |
printf("\tPeriod time = %d\n", snd_pcm_hw_params_get_period_time(params, NULL)); |
| 559 |
printf("\tBuffer size = %d\n", (int)snd_pcm_hw_params_get_buffer_size(params)); |
| 560 |
printf("\tBuffer time = %d\n", snd_pcm_hw_params_get_buffer_time(params, NULL)); |
| 561 |
#endif |
| 562 |
printf("--------------\n"); |
| 563 |
} |
| 564 |
|
| 565 |
}; |
| 566 |
|
| 567 |
|
| 568 |
long lopt(int argc, char *argv[], const char* longname, const char* shortname, long def) |
| 569 |
{ |
| 570 |
for (int i=2; i<argc; i++) |
| 571 |
if ( strcmp(argv[i-1], shortname) == 0 || strcmp(argv[i-1], longname) == 0 ) |
| 572 |
return atoi(argv[i]); |
| 573 |
return def; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
const char* sopt(int argc, char *argv[], const char* longname, const char* shortname, const char* def) |
| 578 |
{ |
| 579 |
for (int i=2; i<argc; i++) |
| 580 |
if ( strcmp(argv[i-1], shortname) == 0 || strcmp(argv[i-1], longname) == 0 ) |
| 581 |
return argv[i]; |
| 582 |
return def; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
bool fopt(int argc, char *argv[], const char* longname, const char* shortname) |
| 587 |
{ |
| 588 |
for (int i=1; i<argc; i++) |
| 589 |
if ( strcmp(argv[i], shortname) == 0 || strcmp(argv[i], longname) == 0 ) |
| 590 |
return true; |
| 591 |
return false; |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
static int getDefaultEnv(const char* name, int defval) |
| 598 |
{ |
| 599 |
const char* str = getenv(name); |
| 600 |
if (str) { |
| 601 |
return atoi(str); |
| 602 |
} else { |
| 603 |
return defval; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
static const char* getDefaultEnv(const char* name, const char* defval) |
| 611 |
{ |
| 612 |
const char* str = getenv(name); |
| 613 |
if (str) { |
| 614 |
return str; |
| 615 |
} else { |
| 616 |
return defval; |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
void* __run(void* ptr); |
| 628 |
|
| 629 |
class alsaaudio : public audio { |
| 630 |
AudioInterface* fAudio; |
| 631 |
dsp* fDSP; |
| 632 |
pthread_t fAudioThread; |
| 633 |
bool fRunning; |
| 634 |
|
| 635 |
public: |
| 636 |
alsaaudio(int argc, char *argv[], dsp* DSP) : fAudio(0), fDSP(DSP), fRunning(false) { |
| 637 |
fAudio = new AudioInterface ( |
| 638 |
AudioParam().cardName( sopt(argc, argv, "--device", "-d", getDefaultEnv("FAUST2ALSA_DEVICE", "hw:0") ) ) |
| 639 |
.frequency( lopt(argc, argv, "--frequency", "-f", getDefaultEnv("FAUST2ALSA_FREQUENCY",44100) ) ) |
| 640 |
.buffering( lopt(argc, argv, "--buffer", "-b", getDefaultEnv("FAUST2ALSA_BUFFER",1024) ) ) |
| 641 |
.periods( lopt(argc, argv, "--periods", "-p", getDefaultEnv("FAUST2ALSA_PERIODS",2) ) ) |
| 642 |
.inputs(DSP->getNumInputs()) |
| 643 |
.outputs(DSP->getNumOutputs())); |
| 644 |
} |
| 645 |
virtual ~alsaaudio() { stop(); delete fAudio; } |
| 646 |
|
| 647 |
virtual bool init(const char *, dsp* DSP) { |
| 648 |
AVOIDDENORMALS; |
| 649 |
fAudio->open(); |
| 650 |
DSP->init(fAudio->frequency()); |
| 651 |
return true; |
| 652 |
} |
| 653 |
|
| 654 |
virtual bool start() { |
| 655 |
fRunning = true; |
| 656 |
if (pthread_create( &fAudioThread, 0, __run, this)) |
| 657 |
fRunning = false; |
| 658 |
return fRunning; |
| 659 |
} |
| 660 |
|
| 661 |
virtual void stop() { |
| 662 |
if (fRunning) { |
| 663 |
fRunning = false; |
| 664 |
pthread_join (fAudioThread, 0); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
virtual void run() { |
| 669 |
bool rt = setRealtimePriority(); |
| 670 |
printf(rt ? "RT : ":"NRT: "); fAudio->shortinfo(); |
| 671 |
fAudio->write(); |
| 672 |
fAudio->write(); |
| 673 |
while(fRunning) { |
| 674 |
fAudio->read(); |
| 675 |
fDSP->compute(fAudio->buffering(), fAudio->inputSoftChannels(), fAudio->outputSoftChannels()); |
| 676 |
fAudio->write(); |
| 677 |
} |
| 678 |
} |
| 679 |
}; |
| 680 |
|
| 681 |
void* __run (void* ptr) |
| 682 |
{ |
| 683 |
alsaaudio * alsa = (alsaaudio*)ptr; |
| 684 |
alsa->run(); |
| 685 |
return 0; |
| 686 |
} |
| 687 |
|
| 688 |
#endif |
| 689 |
|
| 690 |
|
| 691 |
|