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*/
33 * Aspects implemented in the target-specific portion:
38 * pcm_get_bytes_waiting
46 * pcm_play_dma_get_peak_buffer
47 * Data Read/Written within TSP -
51 * pcm_callback_for_more (R)
55 * ==Playback/Recording==
57 * pcm_dma_apply_settings
68 * pcm_rec_dma_record_more
70 * pcm_rec_dma_get_peak_buffer
71 * Data Read/Written within TSP -
72 * pcm_callback_more_ready (R)
75 * States are set _after_ the target's pcm driver is called so that it may
76 * know from whence the state is changed. One exception is init.
80 /* the registered callback function to ask for more mp3 data */
81 volatile pcm_more_callback_type pcm_callback_for_more
82 SHAREDBSS_ATTR
= NULL
;
83 /* PCM playback state */
84 volatile bool pcm_playing SHAREDBSS_ATTR
= false;
85 /* PCM paused state. paused implies playing */
86 volatile bool pcm_paused SHAREDBSS_ATTR
= false;
87 /* samplerate of currently playing audio - undefined if stopped */
88 unsigned long pcm_curr_sampr SHAREDBSS_ATTR
= 0;
89 /* samplerate waiting to be set */
90 unsigned long pcm_sampr SHAREDBSS_ATTR
= HW_SAMPR_DEFAULT
;
91 /* samplerate frequency selection index */
92 int pcm_fsel SHAREDBSS_ATTR
= HW_FREQ_DEFAULT
;
95 * Perform peak calculation on a buffer of packed 16-bit samples.
97 * Used for recording and playback.
99 static void pcm_peak_peeker(const int32_t *addr
, int count
, int peaks
[2])
101 int peak_l
= 0, peak_r
= 0;
102 const int32_t * const end
= addr
+ count
;
106 int32_t value
= *addr
;
109 #ifdef ROCKBOX_BIG_ENDIAN
119 #ifdef ROCKBOX_BIG_ENDIAN
137 void pcm_calculate_peaks(int *left
, int *right
)
139 static int peaks
[2] = { 0, 0 };
140 static unsigned long last_peak_tick
= 0;
141 static unsigned long frame_period
= 0;
143 long tick
= current_tick
;
147 /* Throttled peak ahead based on calling period */
148 long period
= tick
- last_peak_tick
;
150 /* Keep reasonable limits on period */
153 else if (period
> HZ
/5)
156 frame_period
= (3*frame_period
+ period
) >> 2;
158 last_peak_tick
= tick
;
160 addr
= pcm_play_dma_get_peak_buffer(&count
);
162 if (pcm_playing
&& !pcm_paused
)
166 framecount
= frame_period
*pcm_curr_sampr
/ HZ
;
167 count
= MIN(framecount
, count
);
170 pcm_peak_peeker((int32_t *)addr
, count
, peaks
);
171 /* else keep previous peak values */
175 peaks
[0] = peaks
[1] = 0;
185 const void* pcm_get_peak_buffer(int * count
)
187 return pcm_play_dma_get_peak_buffer(count
);
190 /****************************************************************************
191 * Functions that do not require targeted implementation but only a targeted
195 /* This should only be called at startup before any audio playback or
196 recording is attempted */
201 pcm_play_dma_stopped_callback();
203 pcm_set_frequency(HW_SAMPR_DEFAULT
);
205 logf(" pcm_play_dma_init");
209 /* Common code to pcm_play_data and pcm_play_pause */
210 static void pcm_play_data_start(unsigned char *start
, size_t size
)
212 start
= (unsigned char *)(((uintptr_t)start
+ 3) & ~3);
215 if (!(start
&& size
))
217 pcm_more_callback_type get_more
= pcm_callback_for_more
;
222 get_more(&start
, &size
);
224 start
= (unsigned char *)(((uintptr_t)start
+ 3) & ~3);
231 logf(" pcm_play_dma_start");
232 pcm_apply_settings();
233 pcm_play_dma_start(start
, size
);
240 logf(" pcm_play_dma_stop");
242 pcm_play_dma_stopped_callback();
245 void pcm_play_data(pcm_more_callback_type get_more
,
246 unsigned char *start
, size_t size
)
248 logf("pcm_play_data");
252 pcm_callback_for_more
= get_more
;
254 logf(" pcm_play_data_start");
255 pcm_play_data_start(start
, size
);
260 void pcm_play_pause(bool play
)
262 logf("pcm_play_pause: %s", play
? "play" : "pause");
266 if (play
== pcm_paused
&& pcm_playing
)
270 logf(" pcm_play_dma_pause");
271 pcm_play_dma_pause(true);
274 else if (pcm_get_bytes_waiting() > 0)
276 logf(" pcm_play_dma_pause");
277 pcm_apply_settings();
278 pcm_play_dma_pause(false);
283 logf(" pcm_play_dma_start: no data");
284 pcm_play_data_start(NULL
, 0);
295 void pcm_play_stop(void)
297 logf("pcm_play_stop");
303 logf(" pcm_play_dma_stop");
305 pcm_play_dma_stopped_callback();
309 logf(" not playing");
315 void pcm_play_dma_stopped_callback(void)
317 pcm_callback_for_more
= NULL
;
324 /* set frequency next frequency used by the audio hardware -
325 * what pcm_apply_settings will set */
326 void pcm_set_frequency(unsigned int samplerate
)
328 logf("pcm_set_frequency");
330 int index
= round_value_to_list32(samplerate
, hw_freq_sampr
,
333 if (samplerate
!= hw_freq_sampr
[index
])
334 index
= HW_FREQ_DEFAULT
; /* Invalid = default */
336 pcm_sampr
= hw_freq_sampr
[index
];
340 /* apply pcm settings to the hardware */
341 void pcm_apply_settings(void)
343 logf("pcm_apply_settings");
345 if (pcm_sampr
!= pcm_curr_sampr
)
347 logf(" pcm_dma_apply_settings");
348 pcm_dma_apply_settings();
349 pcm_curr_sampr
= pcm_sampr
;
353 bool pcm_is_playing(void)
358 bool pcm_is_paused(void)
363 #ifdef HAVE_RECORDING
364 /** Low level pcm recording apis **/
366 /* Next start for recording peaks */
367 static const void * volatile 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
)
384 const void *peak_addr
= pcm_rec_peak_addr
;
385 const void *addr
= pcm_rec_dma_get_peak_buffer();
389 int count
= (int)(((intptr_t)addr
- (intptr_t)peak_addr
) >> 2);
393 pcm_peak_peeker((int32_t *)peak_addr
, count
, peaks
);
395 if (peak_addr
== pcm_rec_peak_addr
)
396 pcm_rec_peak_addr
= addr
;
399 /* else keep previous peak values */
403 peaks
[0] = peaks
[1] = 0;
411 } /* pcm_calculate_rec_peaks */
413 /****************************************************************************
414 * Functions that do not require targeted implementation but only a targeted
417 void pcm_init_recording(void)
419 logf("pcm_init_recording");
421 /* Recording init is locked unlike general pcm init since this is not
422 * just a one-time event at startup and it should and must be safe by
426 logf(" pcm_rec_dma_init");
427 pcm_rec_dma_stopped_callback();
433 void pcm_close_recording(void)
435 logf("pcm_close_recording");
441 logf(" pcm_rec_dma_stop");
443 pcm_rec_dma_stopped_callback();
446 logf(" pcm_rec_dma_close");
452 void pcm_record_data(pcm_more_callback_type2 more_ready
,
453 void *start
, size_t size
)
455 logf("pcm_record_data");
457 /* 32-bit aligned and sized data only */
458 start
= (void *)(((uintptr_t)start
+ 3) & ~3);
461 if (!(start
&& size
))
469 pcm_callback_more_ready
= more_ready
;
471 #ifdef HAVE_PCM_REC_DMA_ADDRESS
472 /* Need a physical DMA address translation, if not already physical. */
473 pcm_rec_peak_addr
= pcm_dma_addr(start
);
475 pcm_rec_peak_addr
= start
;
478 logf(" pcm_rec_dma_start");
479 pcm_apply_settings();
480 pcm_rec_dma_start(start
, size
);
481 pcm_recording
= true;
484 } /* pcm_record_data */
486 void pcm_stop_recording(void)
488 logf("pcm_stop_recording");
494 logf(" pcm_rec_dma_stop");
496 pcm_rec_dma_stopped_callback();
500 } /* pcm_stop_recording */
502 void pcm_record_more(void *start
, size_t size
)
504 start
= (void *)(((uintptr_t)start
+ 3) & ~3);
510 pcm_rec_dma_stopped_callback();
514 #ifdef HAVE_PCM_REC_DMA_ADDRESS
515 /* Need a physical DMA address translation, if not already physical. */
516 pcm_rec_peak_addr
= pcm_dma_addr(start
);
518 pcm_rec_peak_addr
= start
;
521 pcm_rec_dma_record_more(start
, size
);
524 bool pcm_is_recording(void)
526 return pcm_recording
;
529 void pcm_rec_dma_stopped_callback(void)
531 pcm_recording
= false;
532 pcm_callback_more_ready
= NULL
;
535 #endif /* HAVE_RECORDING */