| 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 |
#include "rplayd.h" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/file.h> |
| 33 |
#include <sys/ioctl.h> |
| 34 |
#include <sys/fcntl.h> |
| 35 |
#include <sys/errno.h> |
| 36 |
#include <sun/audioio.h> |
| 37 |
#include <errno.h> |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
extern char *rplay_audio_device; |
| 43 |
extern int rplay_audio_sample_rate; |
| 44 |
extern int rplay_audio_channels; |
| 45 |
extern int rplay_audio_precision; |
| 46 |
extern int rplay_audio_format; |
| 47 |
extern int rplay_audio_port; |
| 48 |
extern int optional_sample_rate; |
| 49 |
extern int optional_precision; |
| 50 |
extern int optional_channels; |
| 51 |
extern int optional_format; |
| 52 |
extern int optional_port; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static int rplay_audio_fd = -1; |
| 58 |
|
| 59 |
static RPLAY_AUDIO_TABLE amd_table[] = |
| 60 |
{ |
| 61 |
{8000, RPLAY_FORMAT_ULAW, 8, 1}, |
| 62 |
{0, 0, 0, 0} |
| 63 |
}; |
| 64 |
|
| 65 |
static RPLAY_AUDIO_TABLE dbri_table[] = |
| 66 |
{ |
| 67 |
{6615, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 68 |
{6615, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 69 |
{8000, RPLAY_FORMAT_ULAW, 8, 1}, |
| 70 |
{8000, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 71 |
{8000, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 72 |
{9600, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 73 |
{9600, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 74 |
{11025, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 75 |
{11025, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 76 |
{16000, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 77 |
{16000, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 78 |
{18900, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 79 |
{18900, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 80 |
{22050, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 81 |
{22050, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 82 |
{32000, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 83 |
{32000, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 84 |
{33075, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 85 |
{33075, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 86 |
{37800, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 87 |
{37800, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 88 |
{44100, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 89 |
{44100, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 90 |
{48000, RPLAY_FORMAT_LINEAR_16, 16, 1}, |
| 91 |
{48000, RPLAY_FORMAT_LINEAR_16, 16, 2}, |
| 92 |
{0, 0, 0, 0} |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
int |
| 116 |
rplay_audio_init() |
| 117 |
{ |
| 118 |
int d; |
| 119 |
audio_info_t a; |
| 120 |
|
| 121 |
if (rplay_audio_fd == -1) |
| 122 |
{ |
| 123 |
rplay_audio_open(); |
| 124 |
if (rplay_audio_fd == -1) |
| 125 |
{ |
| 126 |
report(REPORT_ERROR, "rplay_audio_init: cannot open %s\n", |
| 127 |
rplay_audio_device); |
| 128 |
return -1; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
#ifdef AUDIO_GETDEV |
| 136 |
#ifdef AUDIO_DEV_SPEAKERBOX |
| 137 |
if (ioctl(rplay_audio_fd, AUDIO_GETDEV, &d) < 0) |
| 138 |
{ |
| 139 |
report(REPORT_ERROR, "rplay_audio_init: AUDIO_GETDEV: %s\n", sys_err_str(errno)); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
if (d == AUDIO_DEV_SPEAKERBOX || d == AUDIO_DEV_CODEC) |
| 144 |
{ |
| 145 |
report(REPORT_DEBUG, "dbri device detected\n"); |
| 146 |
rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 11025; |
| 147 |
rplay_audio_precision = optional_precision ? optional_precision : 16; |
| 148 |
rplay_audio_channels = optional_channels ? optional_channels : 1; |
| 149 |
rplay_audio_format = optional_format ? optional_format : |
| 150 |
rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8; |
| 151 |
rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER; |
| 152 |
rplay_audio_table = dbri_table; |
| 153 |
} |
| 154 |
else if (d == AUDIO_DEV_AMD) |
| 155 |
{ |
| 156 |
report(REPORT_DEBUG, "amd device detected\n"); |
| 157 |
rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 8000; |
| 158 |
rplay_audio_precision = optional_precision ? optional_precision : 8; |
| 159 |
rplay_audio_channels = optional_channels ? optional_channels : 1; |
| 160 |
rplay_audio_format = optional_format ? optional_format : RPLAY_FORMAT_ULAW; |
| 161 |
rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_SPEAKER; |
| 162 |
rplay_audio_table = amd_table; |
| 163 |
} |
| 164 |
|
| 165 |
#ifndef AUDIO_DEV_CS4231 |
| 166 |
#define AUDIO_DEV_CS4231 5 |
| 167 |
#endif |
| 168 |
else if (d == AUDIO_DEV_CS4231) |
| 169 |
{ |
| 170 |
report(REPORT_DEBUG, "cs4231 device detected\n"); |
| 171 |
rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 11025; |
| 172 |
rplay_audio_precision = optional_precision ? optional_precision : 16; |
| 173 |
rplay_audio_channels = optional_channels ? optional_channels : 1; |
| 174 |
rplay_audio_format = optional_format ? optional_format : |
| 175 |
rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8; |
| 176 |
rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_LINEOUT | RPLAY_AUDIO_PORT_SPEAKER; |
| 177 |
rplay_audio_table = dbri_table; |
| 178 |
} |
| 179 |
else |
| 180 |
#endif |
| 181 |
#endif |
| 182 |
{ |
| 183 |
report(REPORT_DEBUG, "assuming amd device\n"); |
| 184 |
rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 8000; |
| 185 |
rplay_audio_precision = optional_precision ? optional_precision : 8; |
| 186 |
rplay_audio_channels = optional_channels ? optional_channels : 1; |
| 187 |
rplay_audio_format = optional_format ? optional_format : RPLAY_FORMAT_ULAW; |
| 188 |
rplay_audio_port = optional_port ? optional_port : RPLAY_AUDIO_PORT_SPEAKER; |
| 189 |
rplay_audio_table = amd_table; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
switch (rplay_audio_precision) |
| 194 |
{ |
| 195 |
case 8: |
| 196 |
if (rplay_audio_format != RPLAY_FORMAT_ULAW |
| 197 |
&& rplay_audio_format != RPLAY_FORMAT_LINEAR_8) |
| 198 |
{ |
| 199 |
report(REPORT_ERROR, "rplay_audio_init: can't use %d bits with format=%d\n", |
| 200 |
rplay_audio_precision, rplay_audio_format); |
| 201 |
return -1; |
| 202 |
} |
| 203 |
break; |
| 204 |
|
| 205 |
case 16: |
| 206 |
if (rplay_audio_format != RPLAY_FORMAT_LINEAR_16) |
| 207 |
{ |
| 208 |
report(REPORT_ERROR, "rplay_audio_init: can't use %d bits with format=%d\n", |
| 209 |
rplay_audio_precision, rplay_audio_format); |
| 210 |
return -1; |
| 211 |
} |
| 212 |
break; |
| 213 |
|
| 214 |
default: |
| 215 |
report(REPORT_ERROR, "rplay_audio_init: `%d' unsupported audio precision\n", |
| 216 |
rplay_audio_precision); |
| 217 |
return -1; |
| 218 |
} |
| 219 |
|
| 220 |
AUDIO_INITINFO(&a); |
| 221 |
|
| 222 |
switch (rplay_audio_format) |
| 223 |
{ |
| 224 |
case RPLAY_FORMAT_ULAW: |
| 225 |
a.play.encoding = AUDIO_ENCODING_ULAW; |
| 226 |
break; |
| 227 |
|
| 228 |
case RPLAY_FORMAT_LINEAR_8: |
| 229 |
case RPLAY_FORMAT_LINEAR_16: |
| 230 |
a.play.encoding = AUDIO_ENCODING_LINEAR; |
| 231 |
break; |
| 232 |
|
| 233 |
default: |
| 234 |
report(REPORT_ERROR, "rplay_audio_init: unsupported audio format `%d'\n", |
| 235 |
rplay_audio_format); |
| 236 |
return -1; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
if (rplay_audio_port == RPLAY_AUDIO_PORT_NONE) |
| 241 |
{ |
| 242 |
a.play.port = ~0; |
| 243 |
} |
| 244 |
else |
| 245 |
{ |
| 246 |
a.play.port = 0; |
| 247 |
if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT)) |
| 248 |
{ |
| 249 |
#ifdef AUDIO_LINE_OUT |
| 250 |
SET_BIT(a.play.port, AUDIO_LINE_OUT); |
| 251 |
#else |
| 252 |
CLR_BIT(rplay_audio_port, RPLAY_AUDIO_PORT_LINEOUT); |
| 253 |
#endif |
| 254 |
} |
| 255 |
if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_HEADPHONE)) |
| 256 |
{ |
| 257 |
#ifdef AUDIO_HEADPHONE |
| 258 |
SET_BIT(a.play.port, AUDIO_HEADPHONE); |
| 259 |
#else |
| 260 |
CLR_BIT(rplay_audio_port, RPLAY_AUDIO_PORT_HEADPHONE); |
| 261 |
#endif |
| 262 |
} |
| 263 |
if (BIT(rplay_audio_port, RPLAY_AUDIO_PORT_SPEAKER)) |
| 264 |
{ |
| 265 |
#ifdef AUDIO_SPEAKER |
| 266 |
SET_BIT(a.play.port, AUDIO_SPEAKER); |
| 267 |
#endif |
| 268 |
|
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
a.play.sample_rate = rplay_audio_sample_rate; |
| 273 |
a.play.precision = rplay_audio_precision; |
| 274 |
a.play.channels = rplay_audio_channels; |
| 275 |
|
| 276 |
if (ioctl(rplay_audio_fd, AUDIO_SETINFO, &a) < 0) |
| 277 |
{ |
| 278 |
report(REPORT_ERROR, "rplay_audio_init: AUDIO_SETINFO: %s\n", sys_err_str(errno)); |
| 279 |
return -1; |
| 280 |
} |
| 281 |
|
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
int |
| 291 |
rplay_audio_open() |
| 292 |
{ |
| 293 |
int flags; |
| 294 |
|
| 295 |
rplay_audio_fd = open(rplay_audio_device, O_WRONLY | O_NDELAY, 0); |
| 296 |
if (rplay_audio_fd < 0) |
| 297 |
{ |
| 298 |
return -1; |
| 299 |
} |
| 300 |
|
| 301 |
if (fcntl(rplay_audio_fd, F_SETFD, 1) < 0) |
| 302 |
{ |
| 303 |
report(REPORT_ERROR, |
| 304 |
"rplay_audio_open: close-on-exec %d\n", |
| 305 |
sys_err_str(errno)); |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
if (rplay_audio_init() < 0) |
| 310 |
{ |
| 311 |
return -1; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
flags = fcntl(rplay_audio_fd, F_GETFL, 0); |
| 318 |
if (flags < 0) |
| 319 |
{ |
| 320 |
return -1; |
| 321 |
} |
| 322 |
flags |= FNDELAY; |
| 323 |
if (fcntl(rplay_audio_fd, F_SETFL, flags) < 0) |
| 324 |
{ |
| 325 |
return -1; |
| 326 |
} |
| 327 |
|
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
int |
| 337 |
rplay_audio_isopen() |
| 338 |
{ |
| 339 |
return rplay_audio_fd != -1; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
int |
| 348 |
rplay_audio_flush() |
| 349 |
{ |
| 350 |
if (rplay_audio_fd != -1) |
| 351 |
{ |
| 352 |
ioctl(rplay_audio_fd, AUDIO_DRAIN, 0); |
| 353 |
} |
| 354 |
|
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
#ifdef __STDC__ |
| 364 |
int |
| 365 |
rplay_audio_write(char *buf, int nbytes) |
| 366 |
#else |
| 367 |
int |
| 368 |
rplay_audio_write(buf, nbytes) |
| 369 |
char *buf; |
| 370 |
int nbytes; |
| 371 |
#endif |
| 372 |
{ |
| 373 |
int n, nleft, nwritten; |
| 374 |
char *p; |
| 375 |
|
| 376 |
nleft = nbytes; |
| 377 |
nwritten = 0; |
| 378 |
|
| 379 |
for (p = buf; nleft > 0; nleft -= n, p += n) |
| 380 |
{ |
| 381 |
n = write(rplay_audio_fd, p, nleft); |
| 382 |
if (n < 0) |
| 383 |
{ |
| 384 |
if (errno == EWOULDBLOCK) |
| 385 |
{ |
| 386 |
return nwritten; |
| 387 |
} |
| 388 |
else if (errno != EINTR) |
| 389 |
{ |
| 390 |
return -1; |
| 391 |
} |
| 392 |
n = 0; |
| 393 |
} |
| 394 |
else |
| 395 |
{ |
| 396 |
nwritten += n; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
return nwritten; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
int |
| 409 |
rplay_audio_close() |
| 410 |
{ |
| 411 |
if (rplay_audio_fd != -1) |
| 412 |
{ |
| 413 |
close(rplay_audio_fd); |
| 414 |
} |
| 415 |
|
| 416 |
rplay_audio_fd = -1; |
| 417 |
|
| 418 |
return 0; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
int |
| 427 |
rplay_audio_get_volume() |
| 428 |
{ |
| 429 |
#ifdef FAKE_VOLUME |
| 430 |
return rplay_audio_volume; |
| 431 |
#else |
| 432 |
audio_info_t a; |
| 433 |
|
| 434 |
if (rplay_audio_fd < 0) |
| 435 |
{ |
| 436 |
rplay_audio_open(); |
| 437 |
} |
| 438 |
if (rplay_audio_fd < 0) |
| 439 |
{ |
| 440 |
return -1; |
| 441 |
} |
| 442 |
if (ioctl(rplay_audio_fd, AUDIO_GETINFO, &a) < 0) |
| 443 |
{ |
| 444 |
return -1; |
| 445 |
} |
| 446 |
else |
| 447 |
{ |
| 448 |
return a.play.gain; |
| 449 |
} |
| 450 |
#endif |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
#ifdef __STDC__ |
| 460 |
int |
| 461 |
rplay_audio_set_volume(int volume) |
| 462 |
#else |
| 463 |
int |
| 464 |
rplay_audio_set_volume(volume) |
| 465 |
int volume; |
| 466 |
#endif |
| 467 |
{ |
| 468 |
#ifdef FAKE_VOLUME |
| 469 |
if (volume < RPLAY_MIN_VOLUME) |
| 470 |
{ |
| 471 |
volume = RPLAY_MIN_VOLUME; |
| 472 |
} |
| 473 |
else if (volume > RPLAY_MAX_VOLUME) |
| 474 |
{ |
| 475 |
volume = RPLAY_MAX_VOLUME; |
| 476 |
} |
| 477 |
rplay_audio_volume = volume; |
| 478 |
|
| 479 |
return rplay_audio_volume; |
| 480 |
#else |
| 481 |
audio_info_t a; |
| 482 |
|
| 483 |
rplay_audio_volume = 0; |
| 484 |
|
| 485 |
if (rplay_audio_fd < 0) |
| 486 |
{ |
| 487 |
rplay_audio_open(); |
| 488 |
} |
| 489 |
if (rplay_audio_fd < 0) |
| 490 |
{ |
| 491 |
return -1; |
| 492 |
} |
| 493 |
|
| 494 |
AUDIO_INITINFO(&a); |
| 495 |
a.play.gain = volume; |
| 496 |
if (ioctl(rplay_audio_fd, AUDIO_SETINFO, &a) < 0) |
| 497 |
{ |
| 498 |
return -1; |
| 499 |
} |
| 500 |
|
| 501 |
rplay_audio_volume = rplay_audio_get_volume(); |
| 502 |
|
| 503 |
return rplay_audio_volume; |
| 504 |
#endif |
| 505 |
} |