void gp_clearRingbuffer() {
int i;
u16 *SEGMENT0 = (u16*)SAMPLEBUFFER1;
for (i=0;i<(BSIZE)/2;i++) *SEGMENT0++ = 0;
}
// Init Sound, starts a Ringbuffer with 2 segments
// Ringbuffer is BSIZE (in bytes)
// One segement is BSIZE/2
void gp_initSound( int freq, int bit, int ringsize) {
int sysfs=0;
if (bit == 8) bit=0; else bit=1;
if (ringsize > 1024*16) ringsize = 1024*16;
BSIZE = ringsize; SEGMENTSIZE = ringsize/2;
GpPcmStop();
sysfs=InitIIS (gp_getPCLK(),freq,bit);
Init1330( sysfs, iisbus,0);
gp_clearRingbuffer();
//gp_setMMU( SAMPLEBUFFER1, (SAMPLEBUFFER1+16384)-1, 0xFFA ); //no cache for samplebuffer
gp_setMMU( SAMPLEBUFFER1, (SAMPLEBUFFER1+8192)-1, 0xFFA ); //no cache for samplebuffer
gp_setMMU( UNCACHED4KB0 , (UNCACHED4KB0 +16384)-1, 0xFFA ); //no cache for extra_ram
GpPcmPlay( (u16*)SAMPLEBUFFER1, BSIZE, 0, bit); // Loop endless
}
void gp_addRingsegment( u16 *add_buffer ) {
int i;
u16 *SEGMENT0 = (u16*)SAMPLEBUFFER1;
u16 *SEGMENT1 = ((u16*)SAMPLEBUFFER1) + SEGMENTSIZE/2; // ((short*)0xf000)+4 is exactly the same as ((short*)0xf000)[4]
// detect playing segment
//gp_debug( 1, "addRS: get sample pos" );
if ( gp_getSamplepos() > (u32)SEGMENT1 ) { // we are in upper segment
//gp_debug( 1, "addRS: upper segment. fill buffer %d", SEGMENT0 );
for (i=0;i<SEGMENTSIZE/2;i++) SEGMENT0[i] = add_buffer[i]; // fill lower segment
//gp_debug( 1, "addRS: wait for lower segment" );
while (1) { if (gp_getSamplepos() < (u32)SEGMENT1) break; } // wait reaches lower segment
}
else {
//gp_debug( 1, "addRS: lower segment. fill buffer %d", SEGMENT1 );
for (i=0;i<SEGMENTSIZE/2;i++) SEGMENT1[i] = add_buffer[i]; // we are in lower segment, fill upper segment
//gp_debug( 1, "addRS: wait for upper segment" );
while (1) { if (gp_getSamplepos() > (u32)SEGMENT1) break; } // wait reaches upper segment
}
}