SvOlli
Certified Guru
Hello again!
I've got a problem with putting out correct sound, from what I've seen on other topics this ain't uncommon, but I can't seem to pinpoint it.
Here's what I've got:
My Problem now is that the speed is about 10% too slow and crackling. If SNDBUFFERSIZE is big enough and when snd_getbuffer() is waiting for a free buffer, sound is fine. From all that I'm assuming, that my writes to the audio buffer disturb the sound playing. Do my writes have to be in sync with something? If yes, with what? Or where am I going wrong?
As always, any help and clue is appreciated!
Thanks in advanace and greetings from Germany,
SvOlli
I've got a problem with putting out correct sound, from what I've seen on other topics this ain't uncommon, but I can't seem to pinpoint it.
Here's what I've got:
Code:
unsigned short *g_buffer;
unsigned int *g_playpos;
int g_free;
int g_use;
void snd_timer()
{
if( g_free ) return;
if( *g_playpos < (unsigned int)g_buffer + SNDBUFFERSIZE )
{
if( g_use == 0 )
{
g_use = 1;
g_free = 1;
}
}
else /* *g_playpos >= (unsigned int)g_buffer + SNDBUFFERSIZE ) */
{
if( g_use == 1 )
{
g_use = 0;
g_free = 1;
}
}
}
unsigned short* snd_getbuffer()
{
/* wait for a free buffer */
while(!g_free);
g_free = 0;
if(g_use)
{
return g_buffer + SNDBUFFERSIZE;
}
else
{
return g_buffer;
}
}
void snd_start()
{
int i;
unsigned short *p;
g_free = 1;
g_use = 0;
g_buffer = (unsigned short*)gm_malloc(sizeof(unsigned short*) * 2 * SNDBUFFERSIZE);
for(p = g_buffer, i = SNDBUFFERSIZE * 2; i; i--)
{
*(p++) = 0x8000;
}
GpTimerOptSet( 0, 60, 0, snd_timer );
GpTimerSet(0);
GpPcmInit(PCM_S44, PCM_16BIT);
GpPcmPlay(g_buffer, SNDBUFFERSIZE * 2 * sizeof(unsigned short), 1);
GpPcmLock(g_buffer, &i, (unsigned int*)&g_playpos);
}
void snd_stop()
{
while(!g_free);
GpPcmStop();
GpTimerKill(0);
}
the main loop goes something like:
{
unsigned short *buffer;
for(;;)
{
buffer = snd_getbuffer();
fancy_fill_buffer_code( (char*)buffer ); /* this really is ov_read() from OggVorbis/Tremor */
}
}
My Problem now is that the speed is about 10% too slow and crackling. If SNDBUFFERSIZE is big enough and when snd_getbuffer() is waiting for a free buffer, sound is fine. From all that I'm assuming, that my writes to the audio buffer disturb the sound playing. Do my writes have to be in sync with something? If yes, with what? Or where am I going wrong?
As always, any help and clue is appreciated!
Thanks in advanace and greetings from Germany,
SvOlli