Simplify pcmbuf_beep() a little, should be no functional change but it seems to mysteriously change the PortalPlayer 'keyclick beep of death' problem into a 'barely-there keyclick' problem.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@16947 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Steve Bavin 2008-04-03 14:24:37 +00:00
parent 0ab1a3b801
commit b24ed65faf

View file

@ -975,70 +975,59 @@ bool pcmbuf_insert_buffer(char *buf, int count)
in Hertz for a duration in milliseconds. */ in Hertz for a duration in milliseconds. */
void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude) void pcmbuf_beep(unsigned int frequency, size_t duration, int amplitude)
{ {
unsigned int count = 0, i = 0; unsigned int count = 0;
unsigned int i;
unsigned int interval = NATIVE_FREQUENCY / frequency; unsigned int interval = NATIVE_FREQUENCY / frequency;
unsigned int samples = NATIVE_FREQUENCY / 1000 * duration;
int32_t sample; int32_t sample;
int16_t *buf; int16_t *bufstart;
int16_t *bufptr;
int16_t *pcmbuf_end = (int16_t *)fadebuf; int16_t *pcmbuf_end = (int16_t *)fadebuf;
size_t samples = NATIVE_FREQUENCY / 1000 * duration; bool mix = pcmbuf_read != NULL && pcmbuf_read->link != NULL;
if (pcm_is_playing() && pcmbuf_read != NULL) /* Find the insertion point and set bufstart to the start of it */
{ if (mix)
if (pcmbuf_read->link)
{ {
/* Get the next chunk */ /* Get the next chunk */
char *pcmbuf_mix_buf = pcmbuf_read->link->addr; char *pcmbuf_mix_buf = pcmbuf_read->link->addr;
/* Give at least 1/8s clearance. */ /* Give 1/8s clearance. */
buf = (int16_t *)&pcmbuf_mix_buf[NATIVE_FREQUENCY * 4 / 8]; bufstart = (int16_t *)&pcmbuf_mix_buf[NATIVE_FREQUENCY * 4 / 8];
} }
else else
{ {
logf("No place to beep"); /* Use audiobuffer */
return; bufstart = (int16_t *)audiobuffer;
} }
while (i++ < samples) /* Mix square wave into buffer */
bufptr = bufstart;
for (i = 0; i < samples; ++i)
{ {
sample = *buf; sample = mix ? *bufptr : 0;
*buf++ = clip_sample_16(sample + amplitude); *bufptr++ = clip_sample_16(sample + amplitude);
if (buf > pcmbuf_end) if (bufptr > pcmbuf_end)
buf = (int16_t *)audiobuffer; bufptr = (int16_t *)audiobuffer;
sample = *buf; sample = mix ? *bufptr : 0;
*buf++ = clip_sample_16(sample + amplitude); *bufptr++ = clip_sample_16(sample + amplitude);
if (bufptr > pcmbuf_end)
bufptr = (int16_t *)audiobuffer;
/* Toggle square wav side */ /* Toggle square wave edge */
if (++count >= interval) if (++count >= interval)
{ {
count = 0; count = 0;
amplitude = -amplitude; amplitude = -amplitude;
} }
if (buf > pcmbuf_end)
buf = (int16_t *)audiobuffer;
} }
}
else
{
buf = (int16_t *)audiobuffer;
while (i++ < samples)
{
*buf++ = amplitude;
if (buf > pcmbuf_end)
buf = (int16_t *)audiobuffer;
*buf++ = amplitude;
/* Toggle square wav side */ /* Kick off playback if required */
if (++count >= interval) if (!pcm_is_playing())
{ {
count = 0; pcm_play_data(NULL, (unsigned char *)bufstart, samples * 4);
amplitude = -amplitude;
}
if (buf > pcmbuf_end)
buf = (int16_t *)audiobuffer;
}
pcm_play_data(NULL, (unsigned char *)audiobuffer, samples * 4);
} }
} }
/* Returns pcm buffer usage in percents (0 to 100). */ /* Returns pcm buffer usage in percents (0 to 100). */
int pcmbuf_usage(void) int pcmbuf_usage(void)
{ {