1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
25 /* Define LOGF_ENABLE to enable logf output in this file */
26 /*#define LOGF_ENABLE*/
31 #include "pcm-internal.h"
32 #include "pcm_mixer.h"
35 * Aspects implemented in the target-specific portion:
40 * pcm_get_bytes_waiting
44 * pcm_play_get_more_callback
46 * pcm_play_dma_postinit
50 * pcm_play_dma_get_peak_buffer
51 * Data Read/Written within TSP -
58 * ==Playback/Recording==
62 * pcm_dma_apply_settings
69 * pcm_rec_more_ready_callback
74 * pcm_rec_dma_get_peak_buffer
75 * Data Read/Written within TSP -
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
;
112 static void pcm_wait_for_init(void)
114 while (!pcm_is_ready
)
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
;
133 #ifdef ROCKBOX_BIG_ENDIAN
143 #ifdef ROCKBOX_BIG_ENDIAN
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 */
172 else if (period
> HZ
/5)
175 peaks
->period
= (3*peaks
->period
+ period
) >> 2;
180 int framecount
= peaks
->period
*pcm_curr_sampr
/ HZ
;
181 count
= MIN(framecount
, count
);
184 pcm_peak_peeker((int32_t *)addr
, count
, peaks
->val
);
185 /* else keep previous peak values */
190 peaks
->val
[0] = peaks
->val
[1] = 0;
194 void pcm_calculate_peaks(int *left
, int *right
)
197 const void *addr
= pcm_play_dma_get_peak_buffer(&count
);
199 pcm_do_peak_calculation(&global_peaks
, pcm_playing
&& !pcm_paused
,
203 *left
= global_peaks
.val
[0];
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)
219 bool pcm_is_paused(void)
224 /****************************************************************************
225 * Functions that do not require targeted implementation but only a targeted
229 /* This should only be called at startup before any audio playback or
230 recording is attempted */
237 pcm_set_frequency(HW_SAMPR_DEFAULT
);
239 logf(" 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();
255 bool pcm_is_initialized(void)
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
;
272 get_more(&start
, &size
);
273 ALIGN_AUDIOBUF(start
, size
);
279 logf(" pcm_play_dma_start");
280 pcm_apply_settings();
281 pcm_play_dma_start(start
, size
);
288 logf(" pcm_play_dma_stop");
293 void pcm_play_data(pcm_play_callback_type get_more
,
294 unsigned char *start
, size_t size
)
296 logf("pcm_play_data");
300 pcm_callback_for_more
= get_more
;
302 logf(" pcm_play_data_start");
303 pcm_play_data_start(start
, size
);
308 void pcm_play_get_more_callback(void **start
, size_t *size
)
310 pcm_play_callback_type get_more
= pcm_callback_for_more
;
314 if (get_more
&& start
)
316 /* Call registered callback */
317 get_more((unsigned char **)start
, size
);
319 ALIGN_AUDIOBUF(*start
, *size
);
325 /* Error, callback missing or no more DMA to do */
330 void pcm_play_pause(bool play
)
332 logf("pcm_play_pause: %s", play
? "play" : "pause");
336 if (play
== pcm_paused
&& pcm_playing
)
340 logf(" pcm_play_dma_pause");
341 pcm_play_dma_pause(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);
353 logf(" pcm_play_dma_start: no data");
354 pcm_play_data_start(NULL
, 0);
365 void pcm_play_stop(void)
367 logf("pcm_play_stop");
373 logf(" pcm_play_dma_stop");
379 logf(" not playing");
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");
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
,
409 if (samplerate
!= hw_freq_sampr
[index
])
410 index
= HW_FREQ_DEFAULT
; /* Invalid = default */
412 pcm_sampr
= hw_freq_sampr
[index
];
416 /* apply pcm settings to the hardware */
417 void pcm_apply_settings(void)
419 logf("pcm_apply_settings");
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];
465 const void *peak_addr
= pcm_rec_peak_addr
;
466 const void *addr
= pcm_rec_dma_get_peak_buffer();
470 int count
= (int)(((intptr_t)addr
- (intptr_t)peak_addr
) >> 2);
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 */
484 peaks
[0] = peaks
[1] = 0;
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
504 void pcm_init_recording(void)
506 logf("pcm_init_recording");
510 /* Stop the beasty before attempting recording */
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
518 logf(" pcm_rec_dma_init");
519 pcm_recording_stopped();
525 void pcm_close_recording(void)
527 logf("pcm_close_recording");
533 logf(" pcm_rec_dma_stop");
535 pcm_recording_stopped();
538 logf(" pcm_rec_dma_close");
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
))
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
);
565 pcm_rec_peak_addr
= start
;
568 logf(" pcm_rec_dma_start");
569 pcm_apply_settings();
570 pcm_rec_dma_start(start
, size
);
571 pcm_recording
= true;
574 } /* pcm_record_data */
576 void pcm_stop_recording(void)
578 logf("pcm_stop_recording");
584 logf(" pcm_rec_dma_stop");
586 pcm_recording_stopped();
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
;
598 if (have_more
&& start
)
600 have_more(status
, start
, size
);
601 ALIGN_AUDIOBUF(*start
, *size
);
605 #ifdef HAVE_PCM_REC_DMA_ADDRESS
606 /* Need a physical DMA address translation, if not already
608 pcm_rec_peak_addr
= pcm_dma_addr(*start
);
610 pcm_rec_peak_addr
= *start
;
616 /* Error, callback missing or no more DMA to do */
618 pcm_recording_stopped();
621 #endif /* HAVE_RECORDING */