Don't try to save settings if none available.
[maemo-rb.git] / firmware / pcm.c
blobd1a897dcabe397ca06137e632e39697335a8370b
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"
31 #include "pcm-internal.h"
32 #include "pcm_mixer.h"
34 /**
35 * Aspects implemented in the target-specific portion:
37 * ==Playback==
38 * Public -
39 * pcm_postinit
40 * pcm_get_bytes_waiting
41 * pcm_play_lock
42 * pcm_play_unlock
43 * Semi-private -
44 * pcm_play_get_more_callback
45 * pcm_play_dma_init
46 * pcm_play_dma_postinit
47 * pcm_play_dma_start
48 * pcm_play_dma_stop
49 * pcm_play_dma_pause
50 * pcm_play_dma_get_peak_buffer
51 * Data Read/Written within TSP -
52 * pcm_sampr (R)
53 * pcm_fsel (R)
54 * pcm_curr_sampr (R)
55 * pcm_playing (R)
56 * pcm_paused (R)
58 * ==Playback/Recording==
59 * Public -
60 * pcm_dma_addr
61 * Semi-private -
62 * pcm_dma_apply_settings
64 * ==Recording==
65 * Public -
66 * pcm_rec_lock
67 * pcm_rec_unlock
68 * Semi-private -
69 * pcm_rec_more_ready_callback
70 * pcm_rec_dma_init
71 * pcm_rec_dma_close
72 * pcm_rec_dma_start
73 * pcm_rec_dma_stop
74 * pcm_rec_dma_get_peak_buffer
75 * Data Read/Written within TSP -
76 * pcm_recording (R)
78 * States are set _after_ the target's pcm driver is called so that it may
79 * know from whence the state is changed. One exception is init.
83 /* 'true' when all stages of pcm initialization have completed */
84 static bool pcm_is_ready = false;
86 /* the registered callback function to ask for more mp3 data */
87 static pcm_play_callback_type pcm_callback_for_more SHAREDBSS_ATTR = NULL;
88 void (* pcm_play_dma_started)(void) SHAREDBSS_ATTR = NULL;
89 /* PCM playback state */
90 volatile bool pcm_playing SHAREDBSS_ATTR = false;
91 /* PCM paused state. paused implies playing */
92 volatile bool pcm_paused SHAREDBSS_ATTR = false;
93 /* samplerate of currently playing audio - undefined if stopped */
94 unsigned long pcm_curr_sampr SHAREDBSS_ATTR = 0;
95 /* samplerate waiting to be set */
96 unsigned long pcm_sampr SHAREDBSS_ATTR = HW_SAMPR_DEFAULT;
97 /* samplerate frequency selection index */
98 int pcm_fsel SHAREDBSS_ATTR = HW_FREQ_DEFAULT;
100 /* peak data for the global peak values - i.e. what the final output is */
101 static struct pcm_peaks global_peaks;
103 /* Called internally by functions to reset the state */
104 static void pcm_play_stopped(void)
106 pcm_callback_for_more = NULL;
107 pcm_play_dma_started = NULL;
108 pcm_paused = false;
109 pcm_playing = false;
112 static void pcm_wait_for_init(void)
114 while (!pcm_is_ready)
115 sleep(0);
119 * Perform peak calculation on a buffer of packed 16-bit samples.
121 * Used for recording and playback.
123 static void pcm_peak_peeker(const int32_t *addr, int count, uint16_t peaks[2])
125 int peak_l = 0, peak_r = 0;
126 const int32_t * const end = addr + count;
130 int32_t value = *addr;
131 int ch;
133 #ifdef ROCKBOX_BIG_ENDIAN
134 ch = value >> 16;
135 #else
136 ch = (int16_t)value;
137 #endif
138 if (ch < 0)
139 ch = -ch;
140 if (ch > peak_l)
141 peak_l = ch;
143 #ifdef ROCKBOX_BIG_ENDIAN
144 ch = (int16_t)value;
145 #else
146 ch = value >> 16;
147 #endif
148 if (ch < 0)
149 ch = -ch;
150 if (ch > peak_r)
151 peak_r = ch;
153 addr += 4;
155 while (addr < end);
157 peaks[0] = peak_l;
158 peaks[1] = peak_r;
161 void pcm_do_peak_calculation(struct pcm_peaks *peaks, bool active,
162 const void *addr, int count)
164 long tick = current_tick;
166 /* Peak no farther ahead than expected period to avoid overcalculation */
167 long period = tick - peaks->tick;
169 /* Keep reasonable limits on period */
170 if (period < 1)
171 period = 1;
172 else if (period > HZ/5)
173 period = HZ/5;
175 peaks->period = (3*peaks->period + period) >> 2;
176 peaks->tick = tick;
178 if (active)
180 int framecount = peaks->period*pcm_curr_sampr / HZ;
181 count = MIN(framecount, count);
183 if (count > 0)
184 pcm_peak_peeker((int32_t *)addr, count, peaks->val);
185 /* else keep previous peak values */
187 else
189 /* peaks are zero */
190 peaks->val[0] = peaks->val[1] = 0;
194 void pcm_calculate_peaks(int *left, int *right)
196 int count;
197 const void *addr = pcm_play_dma_get_peak_buffer(&count);
199 pcm_do_peak_calculation(&global_peaks, pcm_playing && !pcm_paused,
200 addr, count);
202 if (left)
203 *left = global_peaks.val[0];
205 if (right)
206 *right = global_peaks.val[1];
209 const void* pcm_get_peak_buffer(int * count)
211 return pcm_play_dma_get_peak_buffer(count);
214 bool pcm_is_playing(void)
216 return pcm_playing;
219 bool pcm_is_paused(void)
221 return pcm_paused;
224 /****************************************************************************
225 * Functions that do not require targeted implementation but only a targeted
226 * interface
229 /* This should only be called at startup before any audio playback or
230 recording is attempted */
231 void pcm_init(void)
233 logf("pcm_init");
235 pcm_play_stopped();
237 pcm_set_frequency(HW_SAMPR_DEFAULT);
239 logf(" pcm_play_dma_init");
240 pcm_play_dma_init();
243 /* Finish delayed init */
244 void pcm_postinit(void)
246 logf("pcm_postinit");
248 logf(" pcm_play_dma_postinit");
250 pcm_play_dma_postinit();
252 pcm_is_ready = true;
255 bool pcm_is_initialized(void)
257 return pcm_is_ready;
260 /* Common code to pcm_play_data and pcm_play_pause */
261 static void pcm_play_data_start(unsigned char *start, size_t size)
263 ALIGN_AUDIOBUF(start, size);
265 if (!(start && size))
267 pcm_play_callback_type get_more = pcm_callback_for_more;
268 size = 0;
269 if (get_more)
271 logf(" get_more");
272 get_more(&start, &size);
273 ALIGN_AUDIOBUF(start, size);
277 if (start && size)
279 logf(" pcm_play_dma_start");
280 pcm_apply_settings();
281 pcm_play_dma_start(start, size);
282 pcm_playing = true;
283 pcm_paused = false;
284 return;
287 /* Force a stop */
288 logf(" pcm_play_dma_stop");
289 pcm_play_dma_stop();
290 pcm_play_stopped();
293 void pcm_play_data(pcm_play_callback_type get_more,
294 unsigned char *start, size_t size)
296 logf("pcm_play_data");
298 pcm_play_lock();
300 pcm_callback_for_more = get_more;
302 logf(" pcm_play_data_start");
303 pcm_play_data_start(start, size);
305 pcm_play_unlock();
308 void pcm_play_get_more_callback(void **start, size_t *size)
310 pcm_play_callback_type get_more = pcm_callback_for_more;
312 *size = 0;
314 if (get_more && start)
316 /* Call registered callback */
317 get_more((unsigned char **)start, size);
319 ALIGN_AUDIOBUF(*start, *size);
321 if (*start && *size)
322 return;
325 /* Error, callback missing or no more DMA to do */
326 pcm_play_dma_stop();
327 pcm_play_stopped();
330 void pcm_play_pause(bool play)
332 logf("pcm_play_pause: %s", play ? "play" : "pause");
334 pcm_play_lock();
336 if (play == pcm_paused && pcm_playing)
338 if (!play)
340 logf(" pcm_play_dma_pause");
341 pcm_play_dma_pause(true);
342 pcm_paused = true;
344 else if (pcm_get_bytes_waiting() > 0)
346 logf(" pcm_play_dma_pause");
347 pcm_apply_settings();
348 pcm_play_dma_pause(false);
349 pcm_paused = false;
351 else
353 logf(" pcm_play_dma_start: no data");
354 pcm_play_data_start(NULL, 0);
357 else
359 logf(" no change");
362 pcm_play_unlock();
365 void pcm_play_stop(void)
367 logf("pcm_play_stop");
369 pcm_play_lock();
371 if (pcm_playing)
373 logf(" pcm_play_dma_stop");
374 pcm_play_dma_stop();
375 pcm_play_stopped();
377 else
379 logf(" not playing");
382 pcm_play_unlock();
385 /**/
387 /* set frequency next frequency used by the audio hardware -
388 * what pcm_apply_settings will set */
389 void pcm_set_frequency(unsigned int samplerate)
391 logf("pcm_set_frequency");
393 int index;
395 #ifdef CONFIG_SAMPR_TYPES
396 unsigned int type = samplerate & SAMPR_TYPE_MASK;
397 samplerate &= ~SAMPR_TYPE_MASK;
399 /* For now, supported targets have direct conversion when configured with
400 * CONFIG_SAMPR_TYPES.
401 * Some hypothetical target with independent rates would need slightly
402 * different handling throughout this source. */
403 samplerate = pcm_sampr_to_hw_sampr(samplerate, type);
404 #endif /* CONFIG_SAMPR_TYPES */
406 index = round_value_to_list32(samplerate, hw_freq_sampr,
407 HW_NUM_FREQ, false);
409 if (samplerate != hw_freq_sampr[index])
410 index = HW_FREQ_DEFAULT; /* Invalid = default */
412 pcm_sampr = hw_freq_sampr[index];
413 pcm_fsel = index;
416 /* apply pcm settings to the hardware */
417 void pcm_apply_settings(void)
419 logf("pcm_apply_settings");
421 pcm_wait_for_init();
423 if (pcm_sampr != pcm_curr_sampr)
425 logf(" pcm_dma_apply_settings");
426 pcm_dma_apply_settings();
427 pcm_curr_sampr = pcm_sampr;
431 /* register callback to buffer more data */
432 void pcm_play_set_dma_started_callback(void (* callback)(void))
434 pcm_play_dma_started = callback;
437 #ifdef HAVE_RECORDING
438 /** Low level pcm recording apis **/
440 /* Next start for recording peaks */
441 static const void * volatile pcm_rec_peak_addr SHAREDBSS_ATTR = NULL;
442 /* the registered callback function for when more data is available */
443 static volatile pcm_rec_callback_type
444 pcm_callback_more_ready SHAREDBSS_ATTR = NULL;
445 /* DMA transfer in is currently active */
446 volatile bool pcm_recording SHAREDBSS_ATTR = false;
448 /* Called internally by functions to reset the state */
449 static void pcm_recording_stopped(void)
451 pcm_recording = false;
452 pcm_callback_more_ready = NULL;
456 * Return recording peaks - From the end of the last peak up to
457 * current write position.
459 void pcm_calculate_rec_peaks(int *left, int *right)
461 static uint16_t peaks[2];
463 if (pcm_recording)
465 const void *peak_addr = pcm_rec_peak_addr;
466 const void *addr = pcm_rec_dma_get_peak_buffer();
468 if (addr != NULL)
470 int count = (int)(((intptr_t)addr - (intptr_t)peak_addr) >> 2);
472 if (count > 0)
474 pcm_peak_peeker((int32_t *)peak_addr, count, peaks);
476 if (peak_addr == pcm_rec_peak_addr)
477 pcm_rec_peak_addr = addr;
480 /* else keep previous peak values */
482 else
484 peaks[0] = peaks[1] = 0;
487 if (left)
488 *left = peaks[0];
490 if (right)
491 *right = peaks[1];
492 } /* pcm_calculate_rec_peaks */
494 bool pcm_is_recording(void)
496 return pcm_recording;
499 /****************************************************************************
500 * Functions that do not require targeted implementation but only a targeted
501 * interface
504 void pcm_init_recording(void)
506 logf("pcm_init_recording");
508 pcm_wait_for_init();
510 /* Stop the beasty before attempting recording */
511 mixer_reset();
513 /* Recording init is locked unlike general pcm init since this is not
514 * just a one-time event at startup and it should and must be safe by
515 * now. */
516 pcm_rec_lock();
518 logf(" pcm_rec_dma_init");
519 pcm_recording_stopped();
520 pcm_rec_dma_init();
522 pcm_rec_unlock();
525 void pcm_close_recording(void)
527 logf("pcm_close_recording");
529 pcm_rec_lock();
531 if (pcm_recording)
533 logf(" pcm_rec_dma_stop");
534 pcm_rec_dma_stop();
535 pcm_recording_stopped();
538 logf(" pcm_rec_dma_close");
539 pcm_rec_dma_close();
541 pcm_rec_unlock();
544 void pcm_record_data(pcm_rec_callback_type more_ready,
545 void *start, size_t size)
547 logf("pcm_record_data");
549 ALIGN_AUDIOBUF(start, size);
551 if (!(start && size))
553 logf(" no buffer");
554 return;
557 pcm_rec_lock();
559 pcm_callback_more_ready = more_ready;
561 #ifdef HAVE_PCM_REC_DMA_ADDRESS
562 /* Need a physical DMA address translation, if not already physical. */
563 pcm_rec_peak_addr = pcm_dma_addr(start);
564 #else
565 pcm_rec_peak_addr = start;
566 #endif
568 logf(" pcm_rec_dma_start");
569 pcm_apply_settings();
570 pcm_rec_dma_start(start, size);
571 pcm_recording = true;
573 pcm_rec_unlock();
574 } /* pcm_record_data */
576 void pcm_stop_recording(void)
578 logf("pcm_stop_recording");
580 pcm_rec_lock();
582 if (pcm_recording)
584 logf(" pcm_rec_dma_stop");
585 pcm_rec_dma_stop();
586 pcm_recording_stopped();
589 pcm_rec_unlock();
590 } /* pcm_stop_recording */
592 void pcm_rec_more_ready_callback(int status, void **start, size_t *size)
594 pcm_rec_callback_type have_more = pcm_callback_more_ready;
596 *size = 0;
598 if (have_more && start)
600 have_more(status, start, size);
601 ALIGN_AUDIOBUF(*start, *size);
603 if (*start && *size)
605 #ifdef HAVE_PCM_REC_DMA_ADDRESS
606 /* Need a physical DMA address translation, if not already
607 * physical. */
608 pcm_rec_peak_addr = pcm_dma_addr(*start);
609 #else
610 pcm_rec_peak_addr = *start;
611 #endif
612 return;
616 /* Error, callback missing or no more DMA to do */
617 pcm_rec_dma_stop();
618 pcm_recording_stopped();
621 #endif /* HAVE_RECORDING */