GSoC/Buflib: Enable compaction in buflib.
[kugel-rb.git] / apps / beep.c
bloba6244d932c764d16deea3c10b3ce8aa547b6717e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2011 Michael Sevakis
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
22 #include "system.h"
23 #include "settings.h"
24 #include "dsp.h"
25 #include "pcm.h"
26 #include "pcm_mixer.h"
27 #include "misc.h"
29 static int32_t beep_phase; /* Phase of square wave generator */
30 static uint32_t beep_step; /* Step of square wave generator on each sample */
31 static uint32_t beep_amplitude; /* Amplitude of square wave generator */
32 static int beep_count; /* Number of samples remaining to generate */
34 /* Reserve enough static space for keyclick to fit */
35 #define BEEP_BUF_COUNT (NATIVE_FREQUENCY / 1000 * KEYCLICK_DURATION)
36 static uint32_t beep_buf[BEEP_BUF_COUNT] IBSS_ATTR;
38 /* Actually output samples into beep_buf */
39 #if defined(CPU_ARM)
40 static FORCE_INLINE void beep_generate(int count)
42 uint32_t *out = beep_buf;
43 uint32_t s;
45 asm volatile (
46 "1: \n"
47 "eor %3, %5, %1, asr #31 \n"
48 "subs %2, %2, #1 \n"
49 "str %3, [%0], #4 \n"
50 "add %1, %1, %4 \n"
51 "bgt 1b \n"
52 : "+r"(out), "+r"(beep_phase), "+r"(count),
53 "=&r"(s)
54 : "r"(beep_step), "r"(beep_amplitude));
56 #elif defined (CPU_COLDFIRE)
57 static FORCE_INLINE void beep_generate(int count)
59 uint32_t *out = beep_buf;
60 uint32_t s;
62 asm volatile (
63 "1: \n"
64 "move.l %1, %3 \n"
65 "add.l %4, %1 \n"
66 "add.l %3, %3 \n"
67 "subx.l %3, %3 \n"
68 "eor.l %5, %3 \n"
69 "move.l %3, (%0)+ \n"
70 "subq.l #1, %2 \n"
71 "bgt.b 1b \n"
72 : "+a"(out), "+d"(beep_phase), "+d"(count),
73 "=&d"(s)
74 : "r"(beep_step), "d"(beep_amplitude));
76 #else
77 static FORCE_INLINE void beep_generate(int count)
79 uint32_t *out = beep_buf;
80 uint32_t amplitude = beep_amplitude;
81 uint32_t step = beep_step;
82 int32_t phase = beep_phase;
86 *out++ = (phase >> 31) ^ amplitude;
87 phase += step;
89 while (--count > 0);
91 beep_phase = phase;
93 #endif
95 /* Callback to generate the beep frames - also don't want inlining of
96 call below in beep_play */
97 static void __attribute__((noinline))
98 beep_get_more(unsigned char **start, size_t *size)
100 int count = beep_count;
102 if (count > 0)
104 count = MIN(count, BEEP_BUF_COUNT);
105 beep_count -= count;
106 *start = (unsigned char *)beep_buf;
107 *size = count * sizeof(uint32_t);
108 beep_generate(count);
112 /* Generates a constant square wave sound with a given frequency in Hertz for
113 a duration in milliseconds */
114 void beep_play(unsigned int frequency, unsigned int duration,
115 unsigned int amplitude)
117 mixer_channel_stop(PCM_MIXER_CHAN_BEEP);
119 if (frequency == 0 || duration == 0 || amplitude == 0)
120 return;
122 if (amplitude > INT16_MAX)
123 amplitude = INT16_MAX;
125 /* Setup the parameters for the square wave generator */
126 beep_phase = 0;
127 beep_step = 0xffffffffu / NATIVE_FREQUENCY * frequency;
128 beep_count = NATIVE_FREQUENCY / 1000 * duration;
129 beep_amplitude = amplitude | (amplitude << 16); /* Word:|AMP16|AMP16| */
131 /* If it fits - avoid cb overhead */
132 unsigned char *start;
133 size_t size;
135 /* Generate first frame here */
136 beep_get_more(&start, &size);
138 mixer_channel_set_amplitude(PCM_MIXER_CHAN_BEEP, MIX_AMP_UNITY);
139 mixer_channel_play_data(PCM_MIXER_CHAN_BEEP,
140 beep_count ? beep_get_more : NULL,
141 start, size);