new skin token: %cx - 24 hour time format enabled in the setting.. e.g %?cx<24 hour...
[kugel-rb.git] / firmware / pcm.c
blobcddb2eaecdf7af71a70cbcd7998862db5850b0ae
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by 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 <stdlib.h>
22 #include "system.h"
23 #include "kernel.h"
25 /* Define LOGF_ENABLE to enable logf output in this file */
26 /*#define LOGF_ENABLE*/
27 #include "logf.h"
28 #include "audio.h"
29 #include "sound.h"
30 #include "general.h"
32 /**
33 * Aspects implemented in the target-specific portion:
35 * ==Playback==
36 * Public -
37 * pcm_postinit
38 * pcm_get_bytes_waiting
39 * pcm_play_lock
40 * pcm_play_unlock
41 * Semi-private -
42 * pcm_play_dma_init
43 * pcm_play_dma_init
44 * pcm_play_dma_start
45 * pcm_play_dma_stop
46 * pcm_play_dma_pause
47 * pcm_play_dma_get_peak_buffer
48 * Data Read/Written within TSP -
49 * pcm_sampr (R)
50 * pcm_fsel (R)
51 * pcm_curr_sampr (R)
52 * pcm_callback_for_more (R)
53 * pcm_playing (R)
54 * pcm_paused (R)
56 * ==Playback/Recording==
57 * Semi-private -
58 * pcm_dma_apply_settings
59 * pcm_dma_addr
61 * ==Recording==
62 * Public -
63 * pcm_rec_lock
64 * pcm_rec_unlock
65 * Semi-private -
66 * pcm_rec_dma_init
67 * pcm_rec_dma_close
68 * pcm_rec_dma_start
69 * pcm_rec_dma_stop
70 * pcm_rec_dma_get_peak_buffer
71 * Data Read/Written within TSP -
72 * pcm_rec_peak_addr (R/W)
73 * pcm_callback_more_ready (R)
74 * pcm_recording (R)
76 * States are set _after_ the target's pcm driver is called so that it may
77 * know from whence the state is changed. One exception is init.
81 /* the registered callback function to ask for more mp3 data */
82 volatile pcm_more_callback_type pcm_callback_for_more
83 SHAREDBSS_ATTR = NULL;
84 /* PCM playback state */
85 volatile bool pcm_playing SHAREDBSS_ATTR = false;
86 /* PCM paused state. paused implies playing */
87 volatile bool pcm_paused SHAREDBSS_ATTR = false;
88 /* samplerate of currently playing audio - undefined if stopped */
89 unsigned long pcm_curr_sampr SHAREDBSS_ATTR = 0;
90 /* samplerate waiting to be set */
91 unsigned long pcm_sampr SHAREDBSS_ATTR = HW_SAMPR_DEFAULT;
92 /* samplerate frequency selection index */
93 int pcm_fsel SHAREDBSS_ATTR = HW_FREQ_DEFAULT;
95 /**
96 * Do peak calculation using distance squared from axis and save a lot
97 * of jumps and negation. Don't bother with the calculations of left or
98 * right only as it's never really used and won't save much time.
100 * Used for recording and playback.
102 static void pcm_peak_peeker(const void *addr, int count, int peaks[2])
104 int32_t peak_l = 0, peak_r = 0;
105 int32_t peaksq_l = 0, peaksq_r = 0;
109 int32_t value = *(int32_t *)addr;
110 int32_t ch, chsq;
111 #ifdef ROCKBOX_BIG_ENDIAN
112 ch = value >> 16;
113 #else
114 ch = (int16_t)value;
115 #endif
116 chsq = ch*ch;
117 if (chsq > peaksq_l)
118 peak_l = ch, peaksq_l = chsq;
120 #ifdef ROCKBOX_BIG_ENDIAN
121 ch = (int16_t)value;
122 #else
123 ch = value >> 16;
124 #endif
125 chsq = ch*ch;
126 if (chsq > peaksq_r)
127 peak_r = ch, peaksq_r = chsq;
129 addr += 16;
130 count -= 4;
132 while (count > 0);
134 peaks[0] = abs(peak_l);
135 peaks[1] = abs(peak_r);
138 void pcm_calculate_peaks(int *left, int *right)
140 static int peaks[2] = { 0, 0 };
141 static unsigned long last_peak_tick = 0;
142 static unsigned long frame_period = 0;
144 long tick = current_tick;
145 int count;
146 const void *addr;
148 /* Throttled peak ahead based on calling period */
149 long period = tick - last_peak_tick;
151 /* Keep reasonable limits on period */
152 if (period < 1)
153 period = 1;
154 else if (period > HZ/5)
155 period = HZ/5;
157 frame_period = (3*frame_period + period) >> 2;
159 last_peak_tick = tick;
161 addr = pcm_play_dma_get_peak_buffer(&count);
163 if (pcm_playing && !pcm_paused)
165 int framecount;
167 framecount = frame_period*pcm_curr_sampr / HZ;
168 count = MIN(framecount, count);
170 if (count > 0)
171 pcm_peak_peeker(addr, count, peaks);
172 /* else keep previous peak values */
174 else
176 peaks[0] = peaks[1] = 0;
179 if (left)
180 *left = peaks[0];
182 if (right)
183 *right = peaks[1];
186 /****************************************************************************
187 * Functions that do not require targeted implementation but only a targeted
188 * interface
191 /* This should only be called at startup before any audio playback or
192 recording is attempted */
193 void pcm_init(void)
195 logf("pcm_init");
197 pcm_play_dma_stopped_callback();
199 pcm_set_frequency(HW_SAMPR_DEFAULT);
201 logf(" pcm_play_dma_init");
202 pcm_play_dma_init();
205 /* Common code to pcm_play_data and pcm_play_pause */
206 static void pcm_play_data_start(unsigned char *start, size_t size)
208 if (!(start && size))
210 pcm_more_callback_type get_more = pcm_callback_for_more;
211 size = 0;
212 if (get_more)
214 logf(" get_more");
215 get_more(&start, &size);
219 if (start && size)
221 logf(" pcm_play_dma_start");
222 pcm_apply_settings();
223 pcm_play_dma_start(start, size);
224 pcm_playing = true;
225 pcm_paused = false;
226 return;
229 /* Force a stop */
230 logf(" pcm_play_dma_stop");
231 pcm_play_dma_stop();
232 pcm_play_dma_stopped_callback();
235 void pcm_play_data(pcm_more_callback_type get_more,
236 unsigned char *start, size_t size)
238 logf("pcm_play_data");
240 pcm_play_lock();
242 pcm_callback_for_more = get_more;
244 logf(" pcm_play_data_start");
245 pcm_play_data_start(start, size);
247 pcm_play_unlock();
250 void pcm_play_pause(bool play)
252 logf("pcm_play_pause: %s", play ? "play" : "pause");
254 pcm_play_lock();
256 if (play == pcm_paused && pcm_playing)
258 if (!play)
260 logf(" pcm_play_dma_pause");
261 pcm_play_dma_pause(true);
262 pcm_paused = true;
264 else if (pcm_get_bytes_waiting() > 0)
266 logf(" pcm_play_dma_pause");
267 pcm_apply_settings();
268 pcm_play_dma_pause(false);
269 pcm_paused = false;
271 else
273 logf(" pcm_play_dma_start: no data");
274 pcm_play_data_start(NULL, 0);
277 else
279 logf(" no change");
282 pcm_play_unlock();
285 void pcm_play_stop(void)
287 logf("pcm_play_stop");
289 pcm_play_lock();
291 if (pcm_playing)
293 logf(" pcm_play_dma_stop");
294 pcm_play_dma_stop();
295 pcm_play_dma_stopped_callback();
297 else
299 logf(" not playing");
302 pcm_play_unlock();
305 void pcm_play_dma_stopped_callback(void)
307 pcm_callback_for_more = NULL;
308 pcm_paused = false;
309 pcm_playing = false;
312 /**/
314 /* set frequency next frequency used by the audio hardware -
315 * what pcm_apply_settings will set */
316 void pcm_set_frequency(unsigned int samplerate)
318 logf("pcm_set_frequency");
320 int index = round_value_to_list32(samplerate, hw_freq_sampr,
321 HW_NUM_FREQ, false);
323 if (samplerate != hw_freq_sampr[index])
324 index = HW_FREQ_DEFAULT; /* Invalid = default */
326 pcm_sampr = hw_freq_sampr[index];
327 pcm_fsel = index;
330 /* apply pcm settings to the hardware */
331 void pcm_apply_settings(void)
333 logf("pcm_apply_settings");
335 if (pcm_sampr != pcm_curr_sampr)
337 logf(" pcm_dma_apply_settings");
338 pcm_dma_apply_settings();
339 pcm_curr_sampr = pcm_sampr;
343 bool pcm_is_playing(void)
345 return pcm_playing;
348 bool pcm_is_paused(void)
350 return pcm_paused;
353 void pcm_mute(bool mute)
355 #ifndef SIMULATOR
356 audiohw_mute(mute);
357 #endif
359 if (mute)
360 sleep(HZ/16);
363 #ifdef HAVE_RECORDING
364 /** Low level pcm recording apis **/
366 /* Next start for recording peaks */
367 const volatile void *pcm_rec_peak_addr SHAREDBSS_ATTR = NULL;
368 /* the registered callback function for when more data is available */
369 volatile pcm_more_callback_type2
370 pcm_callback_more_ready SHAREDBSS_ATTR = NULL;
371 /* DMA transfer in is currently active */
372 volatile bool pcm_recording SHAREDBSS_ATTR = false;
375 * Return recording peaks - From the end of the last peak up to
376 * current write position.
378 void pcm_calculate_rec_peaks(int *left, int *right)
380 static int peaks[2];
381 int count;
382 const void *addr = pcm_rec_dma_get_peak_buffer(&count);
384 if (pcm_recording)
386 if (count > 0)
388 pcm_peak_peeker(addr, count, peaks);
390 if (addr == pcm_rec_peak_addr)
391 pcm_rec_peak_addr = (int32_t *)addr + count;
393 /* else keep previous peak values */
395 else
397 peaks[0] = peaks[1] = 0;
400 if (left)
401 *left = peaks[0];
403 if (right)
404 *right = peaks[1];
405 } /* pcm_calculate_rec_peaks */
407 /****************************************************************************
408 * Functions that do not require targeted implementation but only a targeted
409 * interface
411 void pcm_init_recording(void)
413 logf("pcm_init_recording");
415 /* Recording init is locked unlike general pcm init since this is not
416 * just a one-time event at startup and it should and must be safe by
417 * now. */
418 pcm_rec_lock();
420 logf(" pcm_rec_dma_init");
421 pcm_rec_dma_stopped_callback();
422 pcm_rec_dma_init();
424 pcm_rec_unlock();
427 void pcm_close_recording(void)
429 logf("pcm_close_recording");
431 pcm_rec_lock();
433 if (pcm_recording)
435 logf(" pcm_rec_dma_stop");
436 pcm_rec_dma_stop();
437 pcm_rec_dma_stopped_callback();
440 logf(" pcm_rec_dma_close");
441 pcm_rec_dma_close();
443 pcm_rec_unlock();
446 void pcm_record_data(pcm_more_callback_type2 more_ready,
447 void *start, size_t size)
449 logf("pcm_record_data");
451 if (!(start && size))
453 logf(" no buffer");
454 return;
457 pcm_rec_lock();
459 pcm_callback_more_ready = more_ready;
461 logf(" pcm_rec_dma_start");
462 pcm_apply_settings();
463 pcm_rec_dma_start(start, size);
464 pcm_recording = true;
466 pcm_rec_unlock();
467 } /* pcm_record_data */
469 void pcm_stop_recording(void)
471 logf("pcm_stop_recording");
473 pcm_rec_lock();
475 if (pcm_recording)
477 logf(" pcm_rec_dma_stop");
478 pcm_rec_dma_stop();
479 pcm_rec_dma_stopped_callback();
482 pcm_rec_unlock();
483 } /* pcm_stop_recording */
485 bool pcm_is_recording(void)
487 return pcm_recording;
490 void pcm_rec_dma_stopped_callback(void)
492 pcm_recording = false;
493 pcm_callback_more_ready = NULL;
496 #endif /* HAVE_RECORDING */