rockbox/apps/beep.c
Michael Sevakis 286a4c5caa Revise the PCM callback system after adding multichannel audio.
Additional status callback is added to pcm_play/rec_data instead of
using a special function to set it. Status includes DMA error
reporting to the status callback. Playback and recording callback
become more alike except playback uses "const void **addr" (because
the data should not be altered) and recording  uses "void **addr".
"const" is put in place throughout where appropriate.

Most changes are fairly trivial. One that should be checked in
particular because it isn't so much is telechips, if anyone cares to
bother. PP5002 is not so trivial either but that tested as working.

Change-Id: I4928d69b3b3be7fb93e259f81635232df9bd1df2
Reviewed-on: http://gerrit.rockbox.org/166
Reviewed-by: Michael Sevakis <jethead71@rockbox.org>
Tested-by: Michael Sevakis <jethead71@rockbox.org>
2012-03-03 07:23:38 +01:00

100 lines
3.5 KiB
C

/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (c) 2011 Michael Sevakis
*
* 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "config.h"
#include "system.h"
#include "settings.h"
#include "dsp.h"
#include "pcm.h"
#include "pcm_mixer.h"
#include "misc.h"
/** Beep generation, CPU optimized **/
#include "asm/beep.c"
static uint32_t beep_phase; /* Phase of square wave generator */
static uint32_t beep_step; /* Step of square wave generator on each sample */
#ifdef BEEP_GENERIC
static int16_t beep_amplitude; /* Amplitude of square wave generator */
#else
/* Optimized routines do XOR with phase sign bit in both channels at once */
static uint32_t beep_amplitude; /* Amplitude of square wave generator */
#endif
static int beep_count; /* Number of samples remaining to generate */
/* Reserve enough static space for keyclick to fit */
#define BEEP_BUF_COUNT (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
static int16_t beep_buf[BEEP_BUF_COUNT*2] IBSS_ATTR __attribute__((aligned(4)));
/* Callback to generate the beep frames - also don't want inlining of
call below in beep_play */
static void __attribute__((noinline))
beep_get_more(const void **start, size_t *size)
{
int count = beep_count;
if (count > 0)
{
count = MIN(count, BEEP_BUF_COUNT);
beep_count -= count;
*start = (unsigned char *)beep_buf;
*size = count * 2 * sizeof (int16_t);
beep_generate((void *)beep_buf, count, &beep_phase,
beep_step, beep_amplitude);
}
}
/* Generates a constant square wave sound with a given frequency in Hertz for
a duration in milliseconds */
void beep_play(unsigned int frequency, unsigned int duration,
unsigned int amplitude)
{
mixer_channel_stop(PCM_MIXER_CHAN_BEEP);
if (frequency == 0 || duration == 0 || amplitude == 0)
return;
if (amplitude > INT16_MAX)
amplitude = INT16_MAX;
/* Setup the parameters for the square wave generator */
beep_phase = 0;
beep_step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
beep_count = NATIVE_FREQUENCY / 1000 * duration;
#ifdef BEEP_GENERIC
beep_amplitude = amplitude;
#else
/* Optimized routines do XOR with phase sign bit in both channels at once */
beep_amplitude = amplitude | (amplitude << 16); /* Word:|AMP16|AMP16| */
#endif
/* If it fits - avoid cb overhead */
const void *start;
size_t size;
/* Generate first frame here */
beep_get_more(&start, &size);
mixer_channel_set_amplitude(PCM_MIXER_CHAN_BEEP, MIX_AMP_UNITY);
mixer_channel_play_data(PCM_MIXER_CHAN_BEEP,
beep_count ? beep_get_more : NULL,
start, size);
}