Skip to content

Commit

Permalink
cdda timing
Browse files Browse the repository at this point in the history
  • Loading branch information
xjsxjs197 committed Sep 18, 2022
1 parent 7669390 commit cd5a79a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
49 changes: 28 additions & 21 deletions cdriso.c
Expand Up @@ -178,10 +178,10 @@ static long GetTickCount(void) {

static void *playthread(void *param)
{
usleep(CD_FRAMESIZE_RAW * 10);
usleep(CD_FRAMESIZE_RAW * CDDA_FRAME_COUNT >> 1);
//fseek(cddaHandle, cdda_file_offset, SEEK_SET);

long osleep, d, t, i, s;
long osleep, d, t, i, s, startPos;
unsigned char tmp;
int ret = 0, sector_offs, readSectors;

Expand Down Expand Up @@ -212,30 +212,35 @@ static void *playthread(void *param)
{
sector_offs = 0;
readSectors = CDDA_FRAME_COUNT - (cdda_first_sector - cdda_cur_sector);
s = (cdda_first_sector - cdda_cur_sector) * CD_FRAMESIZE_RAW;
memset(sndbuffer, 0, s);
startPos = (cdda_first_sector - cdda_cur_sector) * CD_FRAMESIZE_RAW;
if (startPos > 0)
{
memset(sndbuffer, 0, startPos);
}
}
else
{
sector_offs = cdda_cur_sector - cdda_first_sector;
readSectors = CDDA_FRAME_COUNT;
s = 0;
startPos = 0;
}
fseek(cddaHandle, cdda_file_offset + sector_offs * CD_FRAMESIZE_RAW, SEEK_SET);
s = fread(sndbuffer + s, 1, readSectors * CD_FRAMESIZE_RAW, cddaHandle);
s = fread(sndbuffer + startPos, 1, readSectors * CD_FRAMESIZE_RAW, cddaHandle);
cdda_cur_sector += CDDA_FRAME_COUNT;

/*if (cdda_cur_sector - cdda_first_sector < 0)
{
memset(sndbuffer, 0, CD_FRAMESIZE_RAW);
s = CD_FRAMESIZE_RAW;
//memset(sndbuffer, 0, CD_FRAMESIZE_RAW);
//s = CD_FRAMESIZE_RAW;
cdda_cur_sector += CDDA_FRAME_COUNT;
continue;
}
else
{
//sector_offs = cdda_cur_sector - cdda_first_sector;
s = fread(sndbuffer, 1, CD_FRAMESIZE_RAW, cddaHandle);
}
cdda_cur_sector += CDDA_FRAME_COUNT;*/
cdda_cur_sector += CDDA_FRAME_COUNT;
}*/

if (s == 0) {
playing = FALSE;
Expand All @@ -248,17 +253,18 @@ static void *playthread(void *param)
}

if (!cdr.Muted && playing) {
unsigned char * newBufPos = sndbuffer + startPos;
if (cddaBigEndian) {
for (i = 0; i < s / 2; i++) {
tmp = sndbuffer[i * 2];
sndbuffer[i * 2] = sndbuffer[i * 2 + 1];
sndbuffer[i * 2 + 1] = tmp;
tmp = newBufPos[i * 2];
newBufPos[i * 2] = newBufPos[i * 2 + 1];
newBufPos[i * 2 + 1] = tmp;
}
}

// pitch cdda 48000
int spos = 0x10000L;
uint32_t *pS = (uint32_t *)sndbuffer;
uint32_t *pS = (uint32_t *)newBufPos;
uint32_t *psPitch = (uint32_t *)sndbufferPitch;
uint32_t l = 0;
int iSize = (PS_SPU_FREQ * (s >> 2)) / 44100;
Expand All @@ -277,12 +283,13 @@ static void *playthread(void *param)

// can't do it yet due to readahead..
//cdrAttenuate((short *)sndbuffer, s / 4, 1);
//p_cdrAttenuate((short *)sndbufferPitch, iSize << 1, 1);
do {
ret = SPU_playCDDAchannel((short *)sndbufferPitch, iSize << 2);
if (ret == 0x7761)
{
{
usleep(6 * 1000);
}
}
} while (ret == 0x7761 && playing); // rearmed_wait
}

Expand Down Expand Up @@ -340,7 +347,7 @@ static void stopCDDA() {
if (stop)
{
// wait pthread stop
usleep(10000);
usleep(5000);
return;
}

Expand All @@ -354,10 +361,10 @@ static void startCDDA(void) {
stopCDDA();
}

#ifdef SHOW_DEBUG
sprintf(txtbuffer, "startCDDA %ld %ld %ld", cdda_first_sector, cdda_cur_sector, cdda_file_offset);
DEBUG_print(txtbuffer, DBG_CDR2);
#endif // DISP_DEBUG
// #ifdef SHOW_DEBUG
// sprintf(txtbuffer, "startCDDA %ld %ld %ld", cdda_first_sector, cdda_cur_sector, cdda_file_offset);
// DEBUG_print(txtbuffer, DBG_CDR2);
// #endif // DISP_DEBUG

playing = TRUE;

Expand Down
43 changes: 17 additions & 26 deletions cdrom.c
Expand Up @@ -181,6 +181,7 @@ int msf2SectS[] = {
// PSXCLK = 1 sec in the ps
// so (PSXCLK / 75) = cdr read time (linuzappz)
#define cdReadTime (PSXCLK / 75) / 2 // OK
//#define playAdpcmTime 178560 // =(PSXCLK * 930 / 4 / 44100) / 2 // OK
#define playAdpcmTime (PSXCLK * 930 / 4 / 44100) / 2 // OK
#define WaitTime1st (0x800)
#define WaitTime1stInit (0x13cce >> 1)
Expand Down Expand Up @@ -528,27 +529,6 @@ static void AddIrqQueue(unsigned short irq, unsigned long ecycle) {
CDR_INT(ecycle);
}

static void cdrPlayDataEnd()
{
#ifdef DISP_DEBUG
sprintf(txtbuffer, "cdrPlayDataEnd cdr.Mode & MODE_AUTOPAUSE %d \n", cdr.Mode & MODE_AUTOPAUSE);
DEBUG_print(txtbuffer, DBG_CDR1);
#endif // DISP_DEBUG
//if (cdr.Mode & MODE_AUTOPAUSE)
{
cdr.Stat = DataEnd;
SetResultSize(1);
cdr.StatP |= STATUS_ROTATING;
cdr.StatP &= ~STATUS_SEEK;
cdr.Result[0] = cdr.StatP;
cdr.Seeked = SEEK_DONE;
psxHu32ref(0x1070) |= SWAP32((u32)0x4);
psxRegs.interrupt|= 0x80000000;

StopCdda();
}
}

static void cdrPlayInterrupt_Autopause(s16* cddaBuf)
{
u32 abs_lev_max = 0;
Expand Down Expand Up @@ -616,14 +596,20 @@ static void cdrPlayCddaData(int timePlus, int isEnd, s16* cddaBuf)
{
if (!cdr.Play) return;

if (isEnd == 1) {
if (*(u32 *)cdr.SetSectorPlay >= *(u32 *)cdr.SetSectorEnd) {
#ifdef SHOW_DEBUG
sprintf(txtbuffer, "cdrPlayCddaData End");
DEBUG_print(txtbuffer, DBG_CDR4);
#endif // DISP_DEBUG
StopCdda();
cdr.TrackChanged = TRUE;
}

if (!cdr.Irq && !cdr.Stat && (cdr.Mode & (MODE_AUTOPAUSE | MODE_REPORT)))
cdrPlayInterrupt_Autopause(cddaBuf);

if (!cdr.Play) return;

cdr.SetSectorPlay[2] += timePlus;
if (cdr.SetSectorPlay[2] >= 75) {
cdr.SetSectorPlay[2] = 0;
Expand Down Expand Up @@ -673,7 +659,12 @@ void cdrPlayInterrupt()
CDR_LOG( "CDDA - %d:%d:%d\n",
cdr.SetSectorPlay[0], cdr.SetSectorPlay[1], cdr.SetSectorPlay[2] );

if (memcmp(cdr.SetSectorPlay, cdr.SetSectorEnd, 3) == 0) {
//if (memcmp(cdr.SetSectorPlay, cdr.SetSectorEnd, 3) == 0) {
if (*(u32 *)cdr.SetSectorPlay >= *(u32 *)cdr.SetSectorEnd) {
#ifdef SHOW_DEBUG
sprintf(txtbuffer, "cdrom check playCDDA End");
DEBUG_print(txtbuffer, DBG_CDR4);
#endif // DISP_DEBUG
StopCdda();
cdr.TrackChanged = TRUE;
}
Expand Down Expand Up @@ -717,12 +708,12 @@ void cdrPlayInterrupt()

if (cdr.m_locationChanged)
{
CDRMISC_INT(cdReadTime * 30 / 2);
CDRMISC_INT(cdReadTime * 30);
cdr.m_locationChanged = FALSE;
}
else
{
CDRMISC_INT(cdReadTime / 2);
CDRMISC_INT(cdReadTime);
}

// update for CdlGetlocP/autopause
Expand Down Expand Up @@ -1819,8 +1810,8 @@ void cdrReset() {
cdr.AttenuatorRightToRight = 0x80;
getCdInfo();

p_cdrPlayDataEnd = cdrPlayDataEnd;
p_cdrPlayCddaData = cdrPlayCddaData;
p_cdrAttenuate = cdrAttenuate;
}

int cdrFreeze(gzFile f, int Mode) {
Expand Down
2 changes: 1 addition & 1 deletion cdrom.h
Expand Up @@ -149,8 +149,8 @@ void cdrWrite3(unsigned char rt);
int cdrFreeze(gzFile f, int Mode);

bool swapIso;
void (*p_cdrPlayDataEnd)();
void (*p_cdrPlayCddaData)(int timePlus, int isEnd, s16* cddaBuf);
void (*p_cdrAttenuate)(s16 *buf, int samples, int stereo);

#ifdef __cplusplus
}
Expand Down

0 comments on commit cd5a79a

Please sign in to comment.