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 ****************************************************************************/
30 * Aspects implemented in the target-specific portion:
35 * pcm_get_bytes_waiting
44 * pcm_play_dma_get_peak_buffer
45 * Data Read/Written within TSP -
49 * pcm_callback_for_more (R)
53 * ==Playback/Recording==
55 * pcm_dma_apply_settings
66 * pcm_rec_dma_get_peak_buffer
67 * Data Read/Written within TSP -
68 * pcm_rec_peak_addr (R)
69 * pcm_callback_more_ready (R)
72 * States are set _after_ the target's pcm driver is called so that it may
73 * know from whence the state is changed. One exception is init.
77 /* the registered callback function to ask for more mp3 data */
78 volatile pcm_more_callback_type pcm_callback_for_more
79 SHAREDBSS_ATTR
= NULL
;
80 /* PCM playback state */
81 volatile bool pcm_playing SHAREDBSS_ATTR
= false;
82 /* PCM paused state. paused implies playing */
83 volatile bool pcm_paused SHAREDBSS_ATTR
= false;
84 /* samplerate of currently playing audio - undefined if stopped */
85 unsigned long pcm_curr_sampr SHAREDBSS_ATTR
= 0;
86 /* samplerate waiting to be set */
87 unsigned long pcm_sampr SHAREDBSS_ATTR
= HW_SAMPR_DEFAULT
;
88 /* samplerate frequency selection index */
89 int pcm_fsel SHAREDBSS_ATTR
= HW_FREQ_DEFAULT
;
92 * Do peak calculation using distance squared from axis and save a lot
93 * of jumps and negation. Don't bother with the calculations of left or
94 * right only as it's never really used and won't save much time.
96 * Used for recording and playback.
98 static void pcm_peak_peeker(const void *addr
, int count
, int peaks
[2])
100 int32_t peak_l
= 0, peak_r
= 0;
101 int32_t peaksq_l
= 0, peaksq_r
= 0;
105 int32_t value
= *(int32_t *)addr
;
107 #ifdef ROCKBOX_BIG_ENDIAN
114 peak_l
= ch
, peaksq_l
= chsq
;
116 #ifdef ROCKBOX_BIG_ENDIAN
123 peak_r
= ch
, peaksq_r
= chsq
;
130 peaks
[0] = abs(peak_l
);
131 peaks
[1] = abs(peak_r
);
134 void pcm_calculate_peaks(int *left
, int *right
)
136 static int peaks
[2] = { 0, 0 };
137 static unsigned long last_peak_tick
= 0;
138 static unsigned long frame_period
= 0;
140 long tick
= current_tick
;
144 /* Throttled peak ahead based on calling period */
145 long period
= tick
- last_peak_tick
;
147 /* Keep reasonable limits on period */
150 else if (period
> HZ
/5)
153 frame_period
= (3*frame_period
+ period
) >> 2;
155 last_peak_tick
= tick
;
157 addr
= pcm_play_dma_get_peak_buffer(&count
);
159 if (pcm_playing
&& !pcm_paused
)
163 framecount
= frame_period
*pcm_curr_sampr
/ HZ
;
164 count
= MIN(framecount
, count
);
167 pcm_peak_peeker(addr
, count
, peaks
);
168 /* else keep previous peak values */
172 peaks
[0] = peaks
[1] = 0;
182 /****************************************************************************
183 * Functions that do not require targeted implementation but only a targeted
187 /* This should only be called at startup before any audio playback or
188 recording is attempted */
193 pcm_play_dma_stopped_callback();
195 pcm_set_frequency(HW_SAMPR_DEFAULT
);
197 logf(" pcm_play_dma_init");
201 /* Common code to pcm_play_data and pcm_play_pause */
202 static void pcm_play_data_start(unsigned char *start
, size_t size
)
204 if (!(start
&& size
))
206 pcm_more_callback_type get_more
= pcm_callback_for_more
;
211 get_more(&start
, &size
);
217 logf(" pcm_play_dma_start");
218 pcm_apply_settings();
219 pcm_play_dma_start(start
, size
);
226 logf(" pcm_play_dma_stop");
228 pcm_play_dma_stopped_callback();
231 void pcm_play_data(pcm_more_callback_type get_more
,
232 unsigned char *start
, size_t size
)
234 logf("pcm_play_data");
238 pcm_callback_for_more
= get_more
;
240 logf(" pcm_play_data_start");
241 pcm_play_data_start(start
, size
);
246 void pcm_play_pause(bool play
)
248 logf("pcm_play_pause: %s", play
? "play" : "pause");
252 if (play
== pcm_paused
&& pcm_playing
)
256 logf(" pcm_play_dma_pause");
257 pcm_play_dma_pause(true);
260 else if (pcm_get_bytes_waiting() > 0)
262 logf(" pcm_play_dma_pause");
263 pcm_apply_settings();
264 pcm_play_dma_pause(false);
269 logf(" pcm_play_dma_start: no data");
270 pcm_play_data_start(NULL
, 0);
281 void pcm_play_stop(void)
283 logf("pcm_play_stop");
289 logf(" pcm_play_dma_stop");
291 pcm_play_dma_stopped_callback();
295 logf(" not playing");
301 void pcm_play_dma_stopped_callback(void)
303 pcm_callback_for_more
= NULL
;
310 /* set frequency next frequency used by the audio hardware -
311 * what pcm_apply_settings will set */
312 void pcm_set_frequency(unsigned int samplerate
)
314 logf("pcm_set_frequency");
316 int index
= round_value_to_list32(samplerate
, hw_freq_sampr
,
319 if (samplerate
!= hw_freq_sampr
[index
])
320 index
= HW_FREQ_DEFAULT
; /* Invalid = default */
322 pcm_sampr
= hw_freq_sampr
[index
];
326 /* apply pcm settings to the hardware */
327 void pcm_apply_settings(void)
329 logf("pcm_apply_settings");
331 if (pcm_sampr
!= pcm_curr_sampr
)
333 logf(" pcm_dma_apply_settings");
334 pcm_dma_apply_settings();
335 pcm_curr_sampr
= pcm_sampr
;
339 bool pcm_is_playing(void)
344 bool pcm_is_paused(void)
349 void pcm_mute(bool mute
)
359 #ifdef HAVE_RECORDING
360 /** Low level pcm recording apis **/
362 /* Next start for recording peaks */
363 const volatile void *pcm_rec_peak_addr SHAREDBSS_ATTR
= NULL
;
364 /* the registered callback function for when more data is available */
365 volatile pcm_more_callback_type2
366 pcm_callback_more_ready SHAREDBSS_ATTR
= NULL
;
367 /* DMA transfer in is currently active */
368 volatile bool pcm_recording SHAREDBSS_ATTR
= false;
371 * Return recording peaks - From the end of the last peak up to
372 * current write position.
374 void pcm_calculate_rec_peaks(int *left
, int *right
)
378 const void *addr
= pcm_rec_dma_get_peak_buffer(&count
);
384 pcm_peak_peeker(addr
, count
, peaks
);
386 if (addr
== pcm_rec_peak_addr
)
387 pcm_rec_peak_addr
= (int32_t *)addr
+ count
;
389 /* else keep previous peak values */
393 peaks
[0] = peaks
[1] = 0;
401 } /* pcm_calculate_rec_peaks */
403 /****************************************************************************
404 * Functions that do not require targeted implementation but only a targeted
407 void pcm_init_recording(void)
409 logf("pcm_init_recording");
411 /* Recording init is locked unlike general pcm init since this is not
412 * just a one-time event at startup and it should and must be safe by
416 logf(" pcm_rec_dma_init");
417 pcm_rec_dma_stopped_callback();
423 void pcm_close_recording(void)
425 logf("pcm_close_recording");
431 logf(" pcm_rec_dma_stop");
433 pcm_rec_dma_stopped_callback();
436 logf(" pcm_rec_dma_close");
442 void pcm_record_data(pcm_more_callback_type2 more_ready
,
443 void *start
, size_t size
)
445 logf("pcm_record_data");
447 if (!(start
&& size
))
455 pcm_callback_more_ready
= more_ready
;
457 logf(" pcm_rec_dma_start");
458 pcm_apply_settings();
459 pcm_rec_dma_start(start
, size
);
460 pcm_recording
= true;
463 } /* pcm_record_data */
465 void pcm_stop_recording(void)
467 logf("pcm_stop_recording");
473 logf(" pcm_rec_dma_stop");
475 pcm_rec_dma_stopped_callback();
479 } /* pcm_stop_recording */
481 bool pcm_is_recording(void)
483 return pcm_recording
;
486 void pcm_rec_dma_stopped_callback(void)
488 pcm_recording
= false;
489 pcm_callback_more_ready
= NULL
;
492 #endif /* HAVE_RECORDING */