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
49 * pcm_play_dma_get_peak_buffer
50 * Data Read/Written within TSP -
57 * ==Playback/Recording==
61 * pcm_dma_apply_settings
68 * pcm_rec_more_ready_callback
73 * pcm_rec_dma_get_peak_buffer
74 * Data Read/Written within TSP -
77 * States are set _after_ the target's pcm driver is called so that it may
78 * know from whence the state is changed. One exception is init.
82 /* the registered callback function to ask for more mp3 data */
83 static pcm_play_callback_type pcm_callback_for_more SHAREDBSS_ATTR
= NULL
;
84 void (* pcm_play_dma_started
)(void) SHAREDBSS_ATTR
= NULL
;
85 /* PCM playback state */
86 volatile bool pcm_playing SHAREDBSS_ATTR
= false;
87 /* PCM paused state. paused implies playing */
88 volatile bool pcm_paused SHAREDBSS_ATTR
= false;
89 /* samplerate of currently playing audio - undefined if stopped */
90 unsigned long pcm_curr_sampr SHAREDBSS_ATTR
= 0;
91 /* samplerate waiting to be set */
92 unsigned long pcm_sampr SHAREDBSS_ATTR
= HW_SAMPR_DEFAULT
;
93 /* samplerate frequency selection index */
94 int pcm_fsel SHAREDBSS_ATTR
= HW_FREQ_DEFAULT
;
96 /* peak data for the global peak values - i.e. what the final output is */
97 static struct pcm_peaks global_peaks
;
99 /* Called internally by functions to reset the state */
100 static void pcm_play_stopped(void)
102 pcm_callback_for_more
= NULL
;
103 pcm_play_dma_started
= NULL
;
109 * Perform peak calculation on a buffer of packed 16-bit samples.
111 * Used for recording and playback.
113 static void pcm_peak_peeker(const int32_t *addr
, int count
, uint16_t peaks
[2])
115 int peak_l
= 0, peak_r
= 0;
116 const int32_t * const end
= addr
+ count
;
120 int32_t value
= *addr
;
123 #ifdef ROCKBOX_BIG_ENDIAN
133 #ifdef ROCKBOX_BIG_ENDIAN
151 void pcm_do_peak_calculation(struct pcm_peaks
*peaks
, bool active
,
152 const void *addr
, int count
)
154 long tick
= current_tick
;
156 /* Peak no farther ahead than expected period to avoid overcalculation */
157 long period
= tick
- peaks
->tick
;
159 /* Keep reasonable limits on period */
162 else if (period
> HZ
/5)
165 peaks
->period
= (3*peaks
->period
+ period
) >> 2;
170 int framecount
= peaks
->period
*pcm_curr_sampr
/ HZ
;
171 count
= MIN(framecount
, count
);
174 pcm_peak_peeker((int32_t *)addr
, count
, peaks
->val
);
175 /* else keep previous peak values */
180 peaks
->val
[0] = peaks
->val
[1] = 0;
184 void pcm_calculate_peaks(int *left
, int *right
)
187 const void *addr
= pcm_play_dma_get_peak_buffer(&count
);
189 pcm_do_peak_calculation(&global_peaks
, pcm_playing
&& !pcm_paused
,
193 *left
= global_peaks
.val
[0];
196 *right
= global_peaks
.val
[1];
199 const void* pcm_get_peak_buffer(int * count
)
201 return pcm_play_dma_get_peak_buffer(count
);
204 bool pcm_is_playing(void)
209 bool pcm_is_paused(void)
214 /****************************************************************************
215 * Functions that do not require targeted implementation but only a targeted
219 /* This should only be called at startup before any audio playback or
220 recording is attempted */
227 pcm_set_frequency(HW_SAMPR_DEFAULT
);
229 logf(" pcm_play_dma_init");
233 /* Common code to pcm_play_data and pcm_play_pause */
234 static void pcm_play_data_start(unsigned char *start
, size_t size
)
236 start
= (unsigned char *)(((uintptr_t)start
+ 3) & ~3);
239 if (!(start
&& size
))
241 pcm_play_callback_type get_more
= pcm_callback_for_more
;
246 get_more(&start
, &size
);
248 start
= (unsigned char *)(((uintptr_t)start
+ 3) & ~3);
255 logf(" pcm_play_dma_start");
256 pcm_apply_settings();
257 pcm_play_dma_start(start
, size
);
264 logf(" pcm_play_dma_stop");
269 void pcm_play_data(pcm_play_callback_type get_more
,
270 unsigned char *start
, size_t size
)
272 logf("pcm_play_data");
276 pcm_callback_for_more
= get_more
;
278 logf(" pcm_play_data_start");
279 pcm_play_data_start(start
, size
);
284 void pcm_play_get_more_callback(void **start
, size_t *size
)
286 pcm_play_callback_type get_more
= pcm_callback_for_more
;
290 if (get_more
&& start
)
292 /* Call registered callback */
293 get_more((unsigned char **)start
, size
);
295 *start
= (void *)(((uintptr_t)*start
+ 3) & ~3);
302 /* Error, callback missing or no more DMA to do */
307 void pcm_play_pause(bool play
)
309 logf("pcm_play_pause: %s", play
? "play" : "pause");
313 if (play
== pcm_paused
&& pcm_playing
)
317 logf(" pcm_play_dma_pause");
318 pcm_play_dma_pause(true);
321 else if (pcm_get_bytes_waiting() > 0)
323 logf(" pcm_play_dma_pause");
324 pcm_apply_settings();
325 pcm_play_dma_pause(false);
330 logf(" pcm_play_dma_start: no data");
331 pcm_play_data_start(NULL
, 0);
342 void pcm_play_stop(void)
344 logf("pcm_play_stop");
350 logf(" pcm_play_dma_stop");
356 logf(" not playing");
364 /* set frequency next frequency used by the audio hardware -
365 * what pcm_apply_settings will set */
366 void pcm_set_frequency(unsigned int samplerate
)
368 logf("pcm_set_frequency");
372 #ifdef CONFIG_SAMPR_TYPES
373 #ifdef HAVE_RECORDING
374 unsigned int type
= samplerate
& SAMPR_TYPE_MASK
;
376 samplerate
&= ~SAMPR_TYPE_MASK
;
378 #ifdef HAVE_RECORDING
379 #if SAMPR_TYPE_REC != 0
380 /* For now, supported targets have direct conversion when configured with
381 * CONFIG_SAMPR_TYPES.
382 * Some hypothetical target with independent rates would need slightly
383 * different handling throughout this source. */
384 if (type
== SAMPR_TYPE_REC
)
385 samplerate
= pcm_sampr_type_rec_to_play(samplerate
);
387 #endif /* HAVE_RECORDING */
388 #endif /* CONFIG_SAMPR_TYPES */
390 index
= round_value_to_list32(samplerate
, hw_freq_sampr
,
393 if (samplerate
!= hw_freq_sampr
[index
])
394 index
= HW_FREQ_DEFAULT
; /* Invalid = default */
396 pcm_sampr
= hw_freq_sampr
[index
];
400 /* apply pcm settings to the hardware */
401 void pcm_apply_settings(void)
403 logf("pcm_apply_settings");
405 if (pcm_sampr
!= pcm_curr_sampr
)
407 logf(" pcm_dma_apply_settings");
408 pcm_dma_apply_settings();
409 pcm_curr_sampr
= pcm_sampr
;
413 /* register callback to buffer more data */
414 void pcm_play_set_dma_started_callback(void (* callback
)(void))
416 pcm_play_dma_started
= callback
;
419 #ifdef HAVE_RECORDING
420 /** Low level pcm recording apis **/
422 /* Next start for recording peaks */
423 static const void * volatile pcm_rec_peak_addr SHAREDBSS_ATTR
= NULL
;
424 /* the registered callback function for when more data is available */
425 static volatile pcm_rec_callback_type
426 pcm_callback_more_ready SHAREDBSS_ATTR
= NULL
;
427 /* DMA transfer in is currently active */
428 volatile bool pcm_recording SHAREDBSS_ATTR
= false;
430 /* Called internally by functions to reset the state */
431 static void pcm_recording_stopped(void)
433 pcm_recording
= false;
434 pcm_callback_more_ready
= NULL
;
438 * Return recording peaks - From the end of the last peak up to
439 * current write position.
441 void pcm_calculate_rec_peaks(int *left
, int *right
)
443 static uint16_t peaks
[2];
447 const void *peak_addr
= pcm_rec_peak_addr
;
448 const void *addr
= pcm_rec_dma_get_peak_buffer();
452 int count
= (int)(((intptr_t)addr
- (intptr_t)peak_addr
) >> 2);
456 pcm_peak_peeker((int32_t *)peak_addr
, count
, peaks
);
458 if (peak_addr
== pcm_rec_peak_addr
)
459 pcm_rec_peak_addr
= addr
;
462 /* else keep previous peak values */
466 peaks
[0] = peaks
[1] = 0;
474 } /* pcm_calculate_rec_peaks */
476 bool pcm_is_recording(void)
478 return pcm_recording
;
481 /****************************************************************************
482 * Functions that do not require targeted implementation but only a targeted
486 void pcm_init_recording(void)
488 logf("pcm_init_recording");
490 /* Stop the beasty before attempting recording */
493 /* Recording init is locked unlike general pcm init since this is not
494 * just a one-time event at startup and it should and must be safe by
498 logf(" pcm_rec_dma_init");
499 pcm_recording_stopped();
505 void pcm_close_recording(void)
507 logf("pcm_close_recording");
513 logf(" pcm_rec_dma_stop");
515 pcm_recording_stopped();
518 logf(" pcm_rec_dma_close");
524 void pcm_record_data(pcm_rec_callback_type more_ready
,
525 void *start
, size_t size
)
527 logf("pcm_record_data");
529 /* 32-bit aligned and sized data only */
530 start
= (void *)(((uintptr_t)start
+ 3) & ~3);
533 if (!(start
&& size
))
541 pcm_callback_more_ready
= more_ready
;
543 #ifdef HAVE_PCM_REC_DMA_ADDRESS
544 /* Need a physical DMA address translation, if not already physical. */
545 pcm_rec_peak_addr
= pcm_dma_addr(start
);
547 pcm_rec_peak_addr
= start
;
550 logf(" pcm_rec_dma_start");
551 pcm_apply_settings();
552 pcm_rec_dma_start(start
, size
);
553 pcm_recording
= true;
556 } /* pcm_record_data */
558 void pcm_stop_recording(void)
560 logf("pcm_stop_recording");
566 logf(" pcm_rec_dma_stop");
568 pcm_recording_stopped();
572 } /* pcm_stop_recording */
574 void pcm_rec_more_ready_callback(int status
, void **start
, size_t *size
)
576 pcm_rec_callback_type have_more
= pcm_callback_more_ready
;
580 if (have_more
&& start
)
582 have_more(status
, start
, size
);
583 *start
= (void *)(((uintptr_t)*start
+ 3) & ~3);
588 #ifdef HAVE_PCM_REC_DMA_ADDRESS
589 /* Need a physical DMA address translation, if not already
591 pcm_rec_peak_addr
= pcm_dma_addr(*start
);
593 pcm_rec_peak_addr
= *start
;
599 /* Error, callback missing or no more DMA to do */
601 pcm_recording_stopped();
604 #endif /* HAVE_RECORDING */