2011-06-29 06:37:04 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* 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 "pcm.h"
|
|
|
|
#include "pcm_mixer.h"
|
|
|
|
#include "misc.h"
|
2013-05-23 17:58:51 +00:00
|
|
|
#include "fixedpoint.h"
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2012-02-18 09:50:33 +00:00
|
|
|
/** Beep generation, CPU optimized **/
|
2012-02-19 05:33:04 +00:00
|
|
|
#include "asm/beep.c"
|
2012-02-18 09:50:33 +00:00
|
|
|
|
|
|
|
static uint32_t beep_phase; /* Phase of square wave generator */
|
2011-06-29 06:37:04 +00:00
|
|
|
static uint32_t beep_step; /* Step of square wave generator on each sample */
|
2012-02-18 09:50:33 +00:00
|
|
|
#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 */
|
2011-06-29 06:37:04 +00:00
|
|
|
static uint32_t beep_amplitude; /* Amplitude of square wave generator */
|
2012-02-18 09:50:33 +00:00
|
|
|
#endif
|
2011-06-29 06:37:04 +00:00
|
|
|
static int beep_count; /* Number of samples remaining to generate */
|
|
|
|
|
2013-05-23 17:58:51 +00:00
|
|
|
#define BEEP_COUNT(fs, duration) ((fs) / 1000 * (duration))
|
|
|
|
|
|
|
|
/* Reserve enough static space for keyclick to fit in worst case */
|
|
|
|
#define BEEP_BUF_COUNT BEEP_COUNT(PLAY_SAMPR_MAX, KEYCLICK_DURATION)
|
2012-02-18 09:50:33 +00:00
|
|
|
static int16_t beep_buf[BEEP_BUF_COUNT*2] IBSS_ATTR __attribute__((aligned(4)));
|
2011-06-29 06:37:04 +00:00
|
|
|
|
|
|
|
/* Callback to generate the beep frames - also don't want inlining of
|
|
|
|
call below in beep_play */
|
2011-06-29 09:39:13 +00:00
|
|
|
static void __attribute__((noinline))
|
2012-02-23 13:14:46 +00:00
|
|
|
beep_get_more(const void **start, size_t *size)
|
2011-06-29 06:37:04 +00:00
|
|
|
{
|
|
|
|
int count = beep_count;
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
count = MIN(count, BEEP_BUF_COUNT);
|
|
|
|
beep_count -= count;
|
2012-03-04 19:44:43 +00:00
|
|
|
*start = beep_buf;
|
2012-02-18 09:50:33 +00:00
|
|
|
*size = count * 2 * sizeof (int16_t);
|
|
|
|
beep_generate((void *)beep_buf, count, &beep_phase,
|
|
|
|
beep_step, beep_amplitude);
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
2013-05-23 17:58:51 +00:00
|
|
|
uint32_t fout = mixer_get_frequency();
|
2011-06-29 06:37:04 +00:00
|
|
|
beep_phase = 0;
|
2013-05-23 17:58:51 +00:00
|
|
|
beep_step = fp_div(frequency, fout, 32);
|
|
|
|
beep_count = BEEP_COUNT(fout, duration);
|
2012-02-18 09:50:33 +00:00
|
|
|
|
|
|
|
#ifdef BEEP_GENERIC
|
|
|
|
beep_amplitude = amplitude;
|
|
|
|
#else
|
|
|
|
/* Optimized routines do XOR with phase sign bit in both channels at once */
|
2011-06-29 06:37:04 +00:00
|
|
|
beep_amplitude = amplitude | (amplitude << 16); /* Word:|AMP16|AMP16| */
|
2012-02-18 09:50:33 +00:00
|
|
|
#endif
|
2011-06-29 06:37:04 +00:00
|
|
|
|
|
|
|
/* If it fits - avoid cb overhead */
|
2012-02-23 13:14:46 +00:00
|
|
|
const void *start;
|
2011-06-29 06:37:04 +00:00
|
|
|
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);
|
|
|
|
}
|