rbutil caches no the preview images of the themes.
[Rockbox.git] / firmware / pcm_playback.c
blobd81853e1cb0ff7601bc61e04122a6bbbf530b797
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 /* CONFIG_CPU == PNX0101 */
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;
81 if (!audio_status())
82 pcm_paused = false;
85 void pcm_play_pause_pause(void)
89 void pcm_play_pause_unpause(void)
93 static inline void fill_dma_buf(int offset)
95 short *l, *r, *lend;
97 l = dma_buf_left + offset;
98 lend = l + DMA_BUF_SAMPLES / 2;
99 r = dma_buf_right + offset;
101 if (pcm_playing && !pcm_paused)
105 int count;
106 unsigned short *tmp_p;
107 count = MIN(p_size / 4, (size_t)(lend - l));
108 tmp_p = p;
109 p_size -= count * 4;
111 if ((int)l & 3)
113 *l++ = *tmp_p++;
114 *r++ = *tmp_p++;
115 count--;
117 while (count >= 4)
119 asm("ldmia %0!, {r0, r1, r2, r3}\n\t"
120 "and r4, r0, %3\n\t"
121 "orr r4, r4, r1, lsl #16\n\t"
122 "and r5, r2, %3\n\t"
123 "orr r5, r5, r3, lsl #16\n\t"
124 "stmia %1!, {r4, r5}\n\t"
125 "bic r4, r1, %3\n\t"
126 "orr r4, r4, r0, lsr #16\n\t"
127 "bic r5, r3, %3\n\t"
128 "orr r5, r5, r2, lsr #16\n\t"
129 "stmia %2!, {r4, r5}"
130 : "+r" (tmp_p), "+r" (l), "+r" (r)
131 : "r" (0xffff)
132 : "r0", "r1", "r2", "r3", "r4", "r5", "memory");
133 count -= 4;
135 while (count > 0)
137 *l++ = *tmp_p++;
138 *r++ = *tmp_p++;
139 count--;
141 p = tmp_p;
142 if (l >= lend)
143 return;
144 else if (pcm_callback_for_more)
145 pcm_callback_for_more((unsigned char**)&p,
146 &p_size);
148 while (p_size);
149 pcm_playing = false;
152 if (l < lend)
154 memset(l, 0, sizeof(short) * (lend - l));
155 memset(r, 0, sizeof(short) * (lend - l));
159 static void audio_irq(void)
161 unsigned long st = DMAINTSTAT & ~DMAINTEN;
162 int i;
163 for (i = 0; i < 2; i++)
164 if (st & (1 << i))
166 fill_dma_buf((i == 1) ? 0 : DMA_BUF_SAMPLES / 2);
167 DMAINTSTAT = 1 << i;
171 unsigned long physical_address(void *p)
173 unsigned long adr = (unsigned long)p;
174 return (MMUBLOCK((adr >> 21) & 0xf) << 21) | (adr & ((1 << 21) - 1));
177 void pcm_init(void)
179 int i;
181 pcm_playing = false;
182 pcm_paused = false;
183 pcm_callback_for_more = NULL;
185 memset(dma_buf_left, 0, sizeof(dma_buf_left));
186 memset(dma_buf_right, 0, sizeof(dma_buf_right));
188 for (i = 0; i < 8; i++)
190 DMASRC(i) = 0;
191 DMADEST(i) = 0;
192 DMALEN(i) = 0x1ffff;
193 DMAR0C(i) = 0;
194 DMAR10(i) = 0;
195 DMAR1C(i) = 0;
198 DMAINTSTAT = 0xc000ffff;
199 DMAINTEN = 0xc000ffff;
201 DMASRC(0) = physical_address(dma_buf_left);
202 DMADEST(0) = 0x80200280;
203 DMALEN(0) = 0xff;
204 DMAR1C(0) = 0;
205 DMAR0C(0) = 0x40408;
207 DMASRC(1) = physical_address(dma_buf_right);
208 DMADEST(1) = 0x80200284;
209 DMALEN(1) = 0xff;
210 DMAR1C(1) = 0;
211 DMAR0C(1) = 0x40409;
213 irq_set_int_handler(0x1b, audio_irq);
214 irq_enable_int(0x1b);
216 DMAINTSTAT = 1;
217 DMAINTSTAT = 2;
218 DMAINTEN &= ~3;
219 DMAR10(0) |= 1;
220 DMAR10(1) |= 1;
223 void pcm_postinit(void)
225 audiohw_postinit();
228 void pcm_set_frequency(unsigned int frequency)
230 (void)frequency;
231 pcm_freq = HW_SAMPR_DEFAULT;
233 size_t pcm_get_bytes_waiting(void)
235 return p_size;
237 #endif /* CONFIG_CPU == */
239 /* dummy functions for those not actually supporting all this yet */
240 void pcm_apply_settings(void)
243 /** **/
245 void pcm_mute(bool mute)
247 #if defined(HAVE_WM8975) || defined(HAVE_WM8758) \
248 || defined(HAVE_WM8731) || defined(HAVE_WM8721)
249 audiohw_mute(mute);
250 #endif
251 if (mute)
252 sleep(HZ/16);
254 #if !defined(CPU_PP)
256 * This function goes directly into the DMA buffer to calculate the left and
257 * right peak values. To avoid missing peaks it tries to look forward two full
258 * peek periods (2/HZ sec, 100% overlap), although it's always possible that
259 * the entire period will not be visible. To reduce CPU load it only looks at
260 * every third sample, and this can be reduced even further if needed (even
261 * every tenth sample would still be pretty accurate).
264 /* Check for a peak every PEAK_STRIDE samples */
265 #define PEAK_STRIDE 3
266 /* Up to 1/50th of a second of audio for peak calculation */
267 /* This should use NATIVE_FREQUENCY, or eventually an adjustable freq. value */
268 #define PEAK_SAMPLES (44100/50)
269 void pcm_calculate_peaks(int *left, int *right)
271 short *addr;
272 short *end;
274 #if CONFIG_CPU == PNX0101
275 size_t samples = p_size / 4;
276 addr = p;
277 #endif /* CONFIG_CPU */.
279 if (samples > PEAK_SAMPLES)
280 samples = PEAK_SAMPLES - (PEAK_STRIDE - 1);
281 else
282 samples -= MIN(PEAK_STRIDE - 1, samples);
284 end = &addr[samples * 2];
287 if (left && right) {
288 int left_peak = 0, right_peak = 0;
290 while (addr < end) {
291 int value;
292 if ((value = addr [0]) > left_peak)
293 left_peak = value;
294 else if (-value > left_peak)
295 left_peak = -value;
297 if ((value = addr [PEAK_STRIDE | 1]) > right_peak)
298 right_peak = value;
299 else if (-value > right_peak)
300 right_peak = -value;
302 addr = &addr[PEAK_STRIDE * 2];
305 *left = left_peak;
306 *right = right_peak;
308 else if (left || right) {
309 int peak_value = 0, value;
311 if (right)
312 addr += (PEAK_STRIDE | 1);
314 while (addr < end) {
315 if ((value = addr [0]) > peak_value)
316 peak_value = value;
317 else if (-value > peak_value)
318 peak_value = -value;
320 addr += PEAK_STRIDE * 2;
323 if (left)
324 *left = peak_value;
325 else
326 *right = peak_value;
329 #endif /* !defined(CPU_PP) */
331 #endif /* !defined(CPU_COLDFIRE) && (CONFIG_CPU != S3C2440) */
333 /****************************************************************************
334 * Functions that do not require targeted implementation but only a targeted
335 * interface
338 /* Common code to pcm_play_data and pcm_play_pause
339 Returns true if DMA playback was started, else false. */
340 bool pcm_play_data_start(pcm_more_callback_type get_more,
341 unsigned char *start, size_t size)
343 if (!(start && size))
345 size = 0;
346 if (get_more)
347 get_more(&start, &size);
350 if (start && size)
352 pcm_play_dma_start(start, size);
353 return true;
356 return false;
359 void pcm_play_data(pcm_more_callback_type get_more,
360 unsigned char *start, size_t size)
362 pcm_callback_for_more = get_more;
364 if (pcm_play_data_start(get_more, start, size) && pcm_paused)
366 pcm_paused = false;
367 pcm_play_pause(false);
371 void pcm_play_pause(bool play)
373 bool needs_change = pcm_paused == play;
375 /* This needs to be done ahead of the rest to prevent infinite
376 recursion from pcm_play_data */
377 pcm_paused = !play;
379 if (pcm_playing && needs_change)
381 if (play)
383 if (pcm_get_bytes_waiting())
385 logf("unpause");
386 pcm_play_pause_unpause();
388 else
390 logf("unpause, no data waiting");
391 if (!pcm_play_data_start(pcm_callback_for_more, NULL, 0))
393 pcm_play_dma_stop();
394 logf("unpause attempted, no data");
398 else
400 logf("pause");
401 pcm_play_pause_pause();
403 } /* pcm_playing && needs_change */
406 void pcm_play_stop(void)
408 if (pcm_playing)
409 pcm_play_dma_stop();
412 bool pcm_is_playing(void)
414 return pcm_playing;
417 bool pcm_is_paused(void)
419 return pcm_paused;