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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
|
/* OpenCP Module Player
* copyright (c) 1994-'10 Niklas Beisert <nbeisert@physik.tu-muenchen.de>
* copyright (c) 2004-'24 Stian Skjelstad <stian.skjelstad@gmail.com>
*
* Audio CD player
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* revision history: (please note changes here)
* -nb980510 Niklas Beisert <nbeisert@physik.tu-muenchen.de>
* -first release
* -ss040907 Stian Skjelstad <stian@nixia.no>
* -complete rewrite for linux
*/
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "types.h"
#include "boot/psetting.h"
#include "cdaudio.h"
#include "cpiface/cpiface.h"
#include "dev/mcp.h"
#include "dev/player.h"
#include "dev/ringbuffer.h"
#include "filesel/cdrom.h"
#include "filesel/filesystem.h"
#include "stuff/err.h"
#include "stuff/imsrtns.h"
static int lba_start; // start of track
static int lba_stop; // end of track
static int lba_next; // next sector to fetch
static int lba_current; // last sector sent to devp
static struct ocpfilehandle_t *fh;
#ifdef CD_FRAMESIZE_RAW
#undef CD_FRAMESIZE_RAW
#endif
#define CD_FRAMESIZE_RAW 2352
struct rip_sector_t
{
uint32_t lba;
const struct plrDevAPI_t *plrDevAPI;
};
#define BUFFER_SLOTS 75*4
#define REQUEST_SLOTS 16
/* cdIdler dumping locations */
static unsigned char cdbufdata[CD_FRAMESIZE_RAW*BUFFER_SLOTS];
static struct ringbuffer_t *cdbufpos;
static uint32_t cdbuffpos; /* fractional part */
static uint32_t cdbufrate; /* re-sampling rate.. fixed point 0x10000 => 1.0 */
static struct rip_sector_t rip_sectors[BUFFER_SLOTS]; /* replace me */
static struct rip_sector_t rip_sectors2[BUFFER_SLOTS]; /* devp space */
static uint32_t cdRate; /* the devp output rate */
static volatile int clipbusy;
static int cda_looped;
static int donotloop;
static unsigned int voll = 256, volr = 256;
static int vol;
static int bal = 0;
static int pan = 64;
static int srnd;
static struct ioctl_cdrom_readaudio_request_t req;
static int req_active = 0;
static int req_pos1;
#define PANPROC \
do { \
float _rs = rs, _ls = ls; \
if(pan==-64) \
{ \
float t=_ls; \
_ls = _rs; \
_rs = t; \
} else if(pan==64) \
{ \
} else if(pan==0) \
_rs=_ls=(_rs+_ls) / 2.0; \
else if(pan<0) \
{ \
_ls = _ls / (-pan/-64.0+2.0) + _rs*(64.0+pan)/128.0; \
_rs = _rs / (-pan/-64.0+2.0) + _ls*(64.0+pan)/128.0; \
} else if(pan<64) \
{ \
_ls = _ls / (pan/-64.0+2.0) + _rs*(64.0-pan)/128.0; \
_rs = _rs / (pan/-64.0+2.0) + _ls*(64.0-pan)/128.0; \
} \
rs = _rs * volr / 256.0; \
ls = _ls * voll / 256.0; \
if (srnd) \
{ \
ls ^= 0xffff; \
} \
} while(0)
static void delay_callback_from_devp (void *arg, int samples_ago)
{
struct rip_sector_t *rip_sector_last = arg;
lba_current = rip_sector_last->lba;
}
static void delay_callback_from_cdbufdata (void *arg, int samples_ago)
{
struct rip_sector_t *cur = arg;
int samples_until = samples_ago * cdbufrate / 65536;
int index = (cur - rip_sectors) / sizeof (rip_sectors[0]);
rip_sectors2[index] = rip_sectors[index];
cur->plrDevAPI->OnBufferCallback (-samples_until, delay_callback_from_devp, &rip_sectors[index]);
}
static void cdIdlerAddBuffer (struct cpifaceSessionAPI_t *cpifaceSession)
{
int temp;
for (temp = 0; temp < req.lba_count; temp++)
{
int index = (req_pos1/CD_FRAMESIZE_RAW)+temp;
rip_sectors[index].lba = lba_next + temp;
rip_sectors[index].plrDevAPI = cpifaceSession->plrDevAPI;
cpifaceSession->ringbufferAPI->add_tail_callback_samples (cdbufpos, - temp * (CD_FRAMESIZE_RAW >> 2) /* stereo + bit16 */, delay_callback_from_cdbufdata, &rip_sectors[index]);
}
#ifdef WORDS_BIGENDIAN
for (temp = 0; temp < req.lba_count * CD_FRAMESIZE_RAW / 2; temp++)
{
((uint16_t *)req.ptr)[temp] = uint16_little(((uint16_t *)req.ptr)[temp]);
}
#endif
cpifaceSession->ringbufferAPI->head_add_bytes (cdbufpos, req.lba_count * CD_FRAMESIZE_RAW);
lba_next += req.lba_count;
}
static void cdIdler (struct cpifaceSessionAPI_t *cpifaceSession)
{
int pos2;
int length1, length2;
int emptyframes;
int temp;
if (req_active)
{
if (fh->ioctl (fh, IOCTL_CDROM_READAUDIO_ASYNC_PULL, &req))
{
return;
}
cdIdlerAddBuffer (cpifaceSession);
req_active = 0;
}
/* first check for EOF */
if (lba_next == lba_stop)
{
if (donotloop)
{
cda_looped |= 1;
return;
} else {
cda_looped &= ~1;
lba_next = lba_start;
}
} else {
cda_looped &= ~1;
}
cpifaceSession->ringbufferAPI->get_head_bytes (cdbufpos, &req_pos1, &length1, &pos2, &length2);
emptyframes = length1 / CD_FRAMESIZE_RAW;
assert (emptyframes >= 0);
if ((!emptyframes) || ((emptyframes < REQUEST_SLOTS) && (!length2)))
{
return;
}
emptyframes = (emptyframes > REQUEST_SLOTS) ? REQUEST_SLOTS : emptyframes;
assert (emptyframes > 0);
/* check against track end */
assert (lba_stop > lba_next);
temp = lba_stop - lba_next;
if (emptyframes > temp)
{
emptyframes = temp;
}
assert (emptyframes);
req.lba_addr = lba_next;
req.lba_count = emptyframes;
req.ptr = cdbufdata + req_pos1;
assert ((req_pos1 + emptyframes * CD_FRAMESIZE_RAW) <= sizeof (cdbufdata));
switch (fh->ioctl (fh, IOCTL_CDROM_READAUDIO_ASYNC_REQUEST, &req))
{
case -1:
return;
case 0:
cdIdlerAddBuffer (cpifaceSession);
return;
case 1:
req_active = 1;
return;
}
}
OCP_INTERNAL void cdIdle (struct cpifaceSessionAPI_t *cpifaceSession)
{
if (clipbusy++)
{
clipbusy--;
return;
}
if (cpifaceSession->InPause || (cda_looped == 3))
{
cpifaceSession->plrDevAPI->Pause (1);
} else {
void *targetbuf;
unsigned int targetlength; /* in samples */
cpifaceSession->plrDevAPI->Pause (0);
cpifaceSession->plrDevAPI->GetBuffer (&targetbuf, &targetlength);
if (targetlength)
{
int16_t *t = targetbuf;
unsigned int accumulated_target = 0;
unsigned int accumulated_source = 0;
int pos1, length1, pos2, length2;
cdIdler (cpifaceSession);
/* how much data is available.. we are using a ringbuffer, so we might receive two fragments */
cpifaceSession->ringbufferAPI->get_tail_samples (cdbufpos, &pos1, &length1, &pos2, &length2);
if (cdbufrate==0x10000)
{
if (targetlength>(length1+length2))
{
targetlength=(length1+length2);
cda_looped |= 2;
} else {
cda_looped &= ~2;
}
// limit source to not overrun target buffer
if (length1 > targetlength)
{
length1 = targetlength;
length2 = 0;
} else if ((length1 + length2) > targetlength)
{
length2 = targetlength - length1;
}
accumulated_source = accumulated_target = length1 + length2;
while (length1)
{
while (length1)
{
int16_t rs, ls;
rs = ((int16_t *)cdbufdata)[(pos1<<1) + 0];
ls = ((int16_t *)cdbufdata)[(pos1<<1) + 1];
PANPROC;
*(t++) = rs;
*(t++) = ls;
pos1++;
length1--;
//accumulated_target++;
}
length1 = length2;
length2 = 0;
pos1 = pos2;
pos2 = 0;
}
//accumulated_source = accumulated_target;
} else {
/* We are going to perform cubic interpolation of rate conversion... this bit is tricky */
cda_looped &= ~2;
while (targetlength && length1)
{
while (targetlength && length1)
{
uint32_t wpm1, wp0, wp1, wp2;
int32_t rc0, rc1, rc2, rc3, rvm1,rv1,rv2;
int32_t lc0, lc1, lc2, lc3, lvm1,lv1,lv2;
unsigned int progress;
int16_t rs, ls;
/* will the interpolation overflow? */
if ((length1+length2) <= 3)
{
cda_looped |= 2;
break;
}
/* will we overflow the wavebuf if we advance? */
if ((length1+length2) < ((cdbufrate+cdbuffpos)>>16))
{
cda_looped |= 2;
break;
}
switch (length1) /* if we are close to the wrap between buffer segment 1 and 2, len1 will grow down to a small number */
{
case 1: wpm1 = pos1; wp0 = pos2; wp1 = pos2 + 1; wp2 = pos2 + 2; break;
case 2: wpm1 = pos1; wp0 = pos1 + 1; wp1 = pos2; wp2 = pos2 + 1; break;
case 3: wpm1 = pos1; wp0 = pos1 + 1; wp1 = pos1 + 2; wp2 = pos2; break;
default: wpm1 = pos1; wp0 = pos1 + 1; wp1 = pos1 + 2; wp2 = pos1 + 3; break;
}
rvm1 = ((uint16_t *)cdbufdata)[(wpm1<<1)+0]^0x8000; /* we temporary need data to be unsigned - hence the ^0x8000 */
lvm1 = ((uint16_t *)cdbufdata)[(wpm1<<1)+1]^0x8000;
rc0 = ((uint16_t *)cdbufdata)[(wp0 <<1)+0]^0x8000;
lc0 = ((uint16_t *)cdbufdata)[(wp0 <<1)+1]^0x8000;
rv1 = ((uint16_t *)cdbufdata)[(wp1 <<1)+0]^0x8000;
lv1 = ((uint16_t *)cdbufdata)[(wp1 <<1)+1]^0x8000;
rv2 = ((uint16_t *)cdbufdata)[(wp2 <<1)+0]^0x8000;
lv2 = ((uint16_t *)cdbufdata)[(wp2 <<1)+1]^0x8000;
rc1 = rv1-rvm1;
rc2 = 2*rvm1-2*rc0+rv1-rv2;
rc3 = rc0-rvm1-rv1+rv2;
rc3 = imulshr16(rc3,cdbuffpos);
rc3 += rc2;
rc3 = imulshr16(rc3,cdbuffpos);
rc3 += rc1;
rc3 = imulshr16(rc3,cdbuffpos);
rc3 += rc0;
if (rc3<0)
rc3=0;
if (rc3>65535)
rc3=65535;
lc1 = lv1-lvm1;
lc2 = 2*lvm1-2*lc0+lv1-lv2;
lc3 = lc0-lvm1-lv1+lv2;
lc3 = imulshr16(lc3,cdbuffpos);
lc3 += lc2;
lc3 = imulshr16(lc3,cdbuffpos);
lc3 += lc1;
lc3 = imulshr16(lc3,cdbuffpos);
lc3 += lc0;
if (lc3<0)
lc3=0;
if (lc3>65535)
lc3=65535;
rs = rc3 ^ 0x8000;
ls = lc3 ^ 0x8000;
PANPROC;
*(t++) = rs;
*(t++) = ls;
cdbuffpos+=cdbufrate;
progress = cdbuffpos>>16;
cdbuffpos &= 0xffff;
accumulated_source+=progress;
pos1+=progress;
length1-=progress;
targetlength--;
if (length1 < 0)
{
length2 += length1;
length1 = 0;
}
accumulated_target++;
} /* while (targetlength && length1) */
length1 = length2;
length2 = 0;
pos1 = pos2;
pos2 = 0;
} /* while (targetlength && length1) */
} /* if (cdbufrate==0x10000) */
cpifaceSession->ringbufferAPI->tail_consume_samples (cdbufpos, accumulated_source);
cpifaceSession->plrDevAPI->CommitBuffer (accumulated_target);
} /* if (targetlength) */
}
cpifaceSession->plrDevAPI->Idle();
clipbusy--;
}
static void cdSetSpeed (unsigned short sp)
{
if (sp < 4)
sp = 4;
cdbufrate=imuldiv(256*sp, 44100, cdRate);
}
static void cdSetVolume()
{
volr=voll=vol*4;
if (bal<0)
voll=(voll*(64+bal))>>6;
else
volr=(volr*(64-bal))>>6;
}
static void cdSet (struct cpifaceSessionAPI_t *cpifaceSession, int ch, int opt, int val)
{
switch (opt)
{
case mcpMasterSpeed:
cdSetSpeed(val);
break;
case mcpMasterPitch:
break;
case mcpMasterSurround:
srnd=val;
break;
case mcpMasterPanning:
pan=val;
cdSetVolume();
break;
case mcpMasterVolume:
vol=val;
cdSetVolume();
break;
case mcpMasterBalance:
bal=val;
cdSetVolume();
break;
}
}
static int cdGet (struct cpifaceSessionAPI_t *cpifaceSession, int ch, int opt)
{
return 0;
}
OCP_INTERNAL void cdClose (struct cpifaceSessionAPI_t *cpifaceSession)
{
if (cpifaceSession->plrDevAPI)
{
cpifaceSession->plrDevAPI->Stop (cpifaceSession);
}
if (cdbufpos)
{
cpifaceSession->ringbufferAPI->free (cdbufpos);
cdbufpos = 0;
}
if (req_active)
{
while (fh->ioctl (fh, IOCTL_CDROM_READAUDIO_ASYNC_PULL, &req) > 1)
{
usleep (1000);
}
}
if (fh)
{
fh->unref (fh);
fh = 0;
}
}
OCP_INTERNAL void cdJump (struct cpifaceSessionAPI_t *cpifaceSession, unsigned long start)
{
int pos1, length1, pos2, length2;
if (start < lba_start) start = lba_start;
if (start > lba_stop) start = lba_stop - 1;
lba_next = start;
cpifaceSession->ringbufferAPI->get_tail_bytes (cdbufpos, &pos1, &length1, &pos2, &length2);
cpifaceSession->ringbufferAPI->tail_consume_bytes (cdbufpos, length1 + length2);
cdbuffpos = 0;
}
OCP_INTERNAL int cdOpen (unsigned long start, unsigned long len, struct ocpfilehandle_t *file, struct cpifaceSessionAPI_t *cpifaceSession)
{
enum plrRequestFormat format;
if (!cpifaceSession->plrDevAPI)
{
return errPlay;
}
lba_next = lba_start = lba_current = start;
lba_stop = start + len;
if (fh)
{
/* should not happen that we already have a file */
cdClose (cpifaceSession);
}
fh = file;
fh->ref (fh);
clipbusy = 0;
cdRate=44100;
format=PLR_STEREO_16BIT_SIGNED;
if (!cpifaceSession->plrDevAPI->Play (&cdRate, &format, file, cpifaceSession))
{
cdClose (cpifaceSession);
return errPlay;
}
cda_looped=0;
donotloop = 1;
cdbufpos = cpifaceSession->ringbufferAPI->new_samples (RINGBUFFER_FLAGS_STEREO | RINGBUFFER_FLAGS_16BIT | RINGBUFFER_FLAGS_SIGNED, sizeof (cdbufdata) / 4);
if (!cdbufpos)
{
cdClose (cpifaceSession);
return errAllocMem;
}
cdbuffpos = 0;
cdSetSpeed(256);
cpifaceSession->mcpSet=cdSet;
cpifaceSession->mcpGet=cdGet;
cpifaceSession->Normalize (cpifaceSession, mcpNormalizeDefaultPlayP);
return errOk;
}
OCP_INTERNAL void cdGetStatus (struct cdStat *stat)
{
stat->error=0;
stat->position=lba_current;
stat->looped=(lba_next==lba_stop)&&(cda_looped==3);
}
OCP_INTERNAL void cdSetLoop (int loop)
{
donotloop=!loop;
}
|