Do the clamping a little better.
[kugel-rb.git] / firmware / pcm_playback.c
blob66a6fdc957286729427320168df65413bdaf2321
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "system.h"
20 #include "kernel.h"
21 #include "logf.h"
22 #include "audio.h"
23 #include "sound.h"
24 #if CONFIG_CPU == PNX0101
25 #include "string.h"
26 #endif
28 /**
29 * APIs implemented in the target-specific portion:
30 * Public -
31 * pcm_init
32 * pcm_get_bytes_waiting
33 * pcm_calculate_peaks
34 * Semi-private -
35 * pcm_play_dma_start
36 * pcm_play_dma_stop
37 * pcm_play_pause_pause
38 * pcm_play_pause_unpause
41 /** These items may be implemented target specifically or need to
42 be shared semi-privately **/
44 /* the registered callback function to ask for more mp3 data */
45 volatile pcm_more_callback_type pcm_callback_for_more = NULL;
46 volatile bool pcm_playing = false;
47 volatile bool pcm_paused = false;
49 void pcm_play_dma_start(const void *addr, size_t size);
50 void pcm_play_dma_stop(void);
51 void pcm_play_pause_pause(void);
52 void pcm_play_pause_unpause(void);
54 /** Functions that require targeted implementation **/
56 #if !defined(CPU_COLDFIRE) && (CONFIG_CPU != S3C2440)
58 #if (CONFIG_CPU == PNX0101)
60 #define DMA_BUF_SAMPLES 0x100
62 short __attribute__((section(".dmabuf"))) dma_buf_left[DMA_BUF_SAMPLES];
63 short __attribute__((section(".dmabuf"))) dma_buf_right[DMA_BUF_SAMPLES];
65 static int pcm_freq = HW_SAMPR_DEFAULT; /* 44.1 is default */
67 unsigned short* p IBSS_ATTR;
68 size_t p_size IBSS_ATTR;
70 void pcm_play_dma_start(const void *addr, size_t size)
72 p = (unsigned short*)addr;
73 p_size = size;
75 pcm_playing = true;
78 void pcm_play_dma_stop(void)
80 pcm_playing = false;
83 void pcm_play_pause_pause(void)
87 void pcm_play_pause_unpause(void)
91 static inline void fill_dma_buf(int offset)
93 short *l, *r, *lend;
95 l = dma_buf_left + offset;
96 lend = l + DMA_BUF_SAMPLES / 2;
97 r = dma_buf_right + offset;
99 if (pcm_playing && !pcm_paused)
103 int count;
104 unsigned short *tmp_p;
105 count = MIN(p_size / 4, (size_t)(lend - l));
106 tmp_p = p;
107 p_size -= count * 4;
109 if ((int)l & 3)
111 *l++ = *tmp_p++;
112 *r++ = *tmp_p++;
113 count--;
115 while (count >= 4)
117 asm("ldmia %0!, {r0, r1, r2, r3}\n\t"
118 "and r4, r0, %3\n\t"
119 "orr r4, r4, r1, lsl #16\n\t"
120 "and r5, r2, %3\n\t"
121 "orr r5, r5, r3, lsl #16\n\t"
122 "stmia %1!, {r4, r5}\n\t"
123 "bic r4, r1, %3\n\t"
124 "orr r4, r4, r0, lsr #16\n\t"
125 "bic r5, r3, %3\n\t"
126 "orr r5, r5, r2, lsr #16\n\t"
127 "stmia %2!, {r4, r5}"
128 : "+r" (tmp_p), "+r" (l), "+r" (r)
129 : "r" (0xffff)
130 : "r0", "r1", "r2", "r3", "r4", "r5", "memory");
131 count -= 4;
133 while (count > 0)
135 *l++ = *tmp_p++;
136 *r++ = *tmp_p++;
137 count--;
139 p = tmp_p;
140 if (l >= lend)
141 return;
142 else if (pcm_callback_for_more)
143 pcm_callback_for_more((unsigned char**)&p,
144 &p_size);
146 while (p_size);
147 pcm_playing = false;
150 if (l < lend)
152 memset(l, 0, sizeof(short) * (lend - l));
153 memset(r, 0, sizeof(short) * (lend - l));
157 static void audio_irq(void)
159 unsigned long st = DMAINTSTAT & ~DMAINTEN;
160 int i;
161 for (i = 0; i < 2; i++)
162 if (st & (1 << i))
164 fill_dma_buf((i == 1) ? 0 : DMA_BUF_SAMPLES / 2);
165 DMAINTSTAT = 1 << i;
169 unsigned long physical_address(void *p)
171 unsigned long adr = (unsigned long)p;
172 return (MMUBLOCK((adr >> 21) & 0xf) << 21) | (adr & ((1 << 21) - 1));
175 void pcm_init(void)
177 int i;
179 pcm_playing = false;
180 pcm_paused = false;
181 pcm_callback_for_more = NULL;
183 memset(dma_buf_left, 0, sizeof(dma_buf_left));
184 memset(dma_buf_right, 0, sizeof(dma_buf_right));
186 for (i = 0; i < 8; i++)
188 DMASRC(i) = 0;
189 DMADEST(i) = 0;
190 DMALEN(i) = 0x1ffff;
191 DMAR0C(i) = 0;
192 DMAR10(i) = 0;
193 DMAR1C(i) = 0;
196 DMAINTSTAT = 0xc000ffff;
197 DMAINTEN = 0xc000ffff;
199 DMASRC(0) = physical_address(dma_buf_left);
200 DMADEST(0) = 0x80200280;
201 DMALEN(0) = 0xff;
202 DMAR1C(0) = 0;
203 DMAR0C(0) = 0x40408;
205 DMASRC(1) = physical_address(dma_buf_right);
206 DMADEST(1) = 0x80200284;
207 DMALEN(1) = 0xff;
208 DMAR1C(1) = 0;
209 DMAR0C(1) = 0x40409;
211 irq_set_int_handler(0x1b, audio_irq);
212 irq_enable_int(0x1b);
214 DMAINTSTAT = 1;
215 DMAINTSTAT = 2;
216 DMAINTEN &= ~3;
217 DMAR10(0) |= 1;
218 DMAR10(1) |= 1;
221 void pcm_postinit(void)
223 audiohw_postinit();
226 void pcm_set_frequency(unsigned int frequency)
228 (void)frequency;
229 pcm_freq = HW_SAMPR_DEFAULT;
231 size_t pcm_get_bytes_waiting(void)
233 return p_size;
235 #endif /* CONFIG_CPU == */
237 /* dummy functions for those not actually supporting all this yet */
238 void pcm_apply_settings(void)
241 /** **/
243 void pcm_mute(bool mute)
245 #if defined(HAVE_WM8975) || defined(HAVE_WM8758) \
246 || defined(HAVE_WM8731) || defined(HAVE_WM8721)
247 audiohw_mute(mute);
248 #endif
249 if (mute)
250 sleep(HZ/16);
252 #if !defined(CPU_PP)
254 * This function goes directly into the DMA buffer to calculate the left and
255 * right peak values. To avoid missing peaks it tries to look forward two full
256 * peek periods (2/HZ sec, 100% overlap), although it's always possible that
257 * the entire period will not be visible. To reduce CPU load it only looks at
258 * every third sample, and this can be reduced even further if needed (even
259 * every tenth sample would still be pretty accurate).
262 /* Check for a peak every PEAK_STRIDE samples */
263 #define PEAK_STRIDE 3
264 /* Up to 1/50th of a second of audio for peak calculation */
265 /* This should use NATIVE_FREQUENCY, or eventually an adjustable freq. value */
266 #define PEAK_SAMPLES (44100/50)
267 void pcm_calculate_peaks(int *left, int *right)
269 #if (CONFIG_CPU == S3C2440)
270 (void)left;
271 (void)right;
272 #else
273 short *addr;
274 short *end;
276 #if CONFIG_CPU == PNX0101
277 size_t samples = p_size / 4;
278 addr = p;
279 #endif
281 if (samples > PEAK_SAMPLES)
282 samples = PEAK_SAMPLES - (PEAK_STRIDE - 1);
283 else
284 samples -= MIN(PEAK_STRIDE - 1, samples);
286 end = &addr[samples * 2];
289 if (left && right) {
290 int left_peak = 0, right_peak = 0;
292 while (addr < end) {
293 int value;
294 if ((value = addr [0]) > left_peak)
295 left_peak = value;
296 else if (-value > left_peak)
297 left_peak = -value;
299 if ((value = addr [PEAK_STRIDE | 1]) > right_peak)
300 right_peak = value;
301 else if (-value > right_peak)
302 right_peak = -value;
304 addr = &addr[PEAK_STRIDE * 2];
307 *left = left_peak;
308 *right = right_peak;
310 else if (left || right) {
311 int peak_value = 0, value;
313 if (right)
314 addr += (PEAK_STRIDE | 1);
316 while (addr < end) {
317 if ((value = addr [0]) > peak_value)
318 peak_value = value;
319 else if (-value > peak_value)
320 peak_value = -value;
322 addr += PEAK_STRIDE * 2;
325 if (left)
326 *left = peak_value;
327 else
328 *right = peak_value;
330 #endif
332 #endif
333 #endif /* CPU_COLDFIRE */
335 /****************************************************************************
336 * Functions that do not require targeted implementation but only a targeted
337 * interface
340 /* Common code to pcm_play_data and pcm_play_pause
341 Returns true if DMA playback was started, else false. */
342 bool pcm_play_data_start(pcm_more_callback_type get_more,
343 unsigned char *start, size_t size)
345 if (!(start && size))
347 size = 0;
348 if (get_more)
349 get_more(&start, &size);
352 if (start && size)
354 pcm_play_dma_start(start, size);
355 return true;
358 return false;
361 void pcm_play_data(pcm_more_callback_type get_more,
362 unsigned char *start, size_t size)
364 pcm_callback_for_more = get_more;
366 if (pcm_play_data_start(get_more, start, size) && pcm_paused)
368 pcm_paused = false;
369 pcm_play_pause(false);
373 void pcm_play_pause(bool play)
375 bool needs_change = pcm_paused == play;
377 /* This needs to be done ahead of the rest to prevent infinite
378 recursion from pcm_play_data */
379 pcm_paused = !play;
381 if (pcm_playing && needs_change)
383 if (play)
385 if (pcm_get_bytes_waiting())
387 logf("unpause");
388 pcm_play_pause_unpause();
390 else
392 logf("unpause, no data waiting");
393 if (!pcm_play_data_start(pcm_callback_for_more, NULL, 0))
395 pcm_play_dma_stop();
396 logf("unpause attempted, no data");
400 else
402 logf("pause");
403 pcm_play_pause_pause();
405 } /* pcm_playing && needs_change */
408 void pcm_play_stop(void)
410 if (pcm_playing)
411 pcm_play_dma_stop();
414 bool pcm_is_playing(void)
416 return pcm_playing;
419 bool pcm_is_paused(void)
421 return pcm_paused;