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
47 * pcm_play_dma_get_peak_buffer
48 * Data Read/Written within TSP -
52 * pcm_callback_for_more (R)
56 * ==Playback/Recording==
58 * pcm_dma_apply_settings
70 * pcm_rec_dma_get_peak_buffer
71 * Data Read/Written within TSP -
72 * pcm_rec_peak_addr (R/W)
73 * pcm_callback_more_ready (R)
76 * States are set _after_ the target's pcm driver is called so that it may
77 * know from whence the state is changed. One exception is init.
81 /* the registered callback function to ask for more mp3 data */
82 volatile pcm_more_callback_type pcm_callback_for_more
83 SHAREDBSS_ATTR
= NULL
;
84 /* PCM playback state */
85 volatile bool pcm_playing SHAREDBSS_ATTR
= false;
86 /* PCM paused state. paused implies playing */
87 volatile bool pcm_paused SHAREDBSS_ATTR
= false;
88 /* samplerate of currently playing audio - undefined if stopped */
89 unsigned long pcm_curr_sampr SHAREDBSS_ATTR
= 0;
90 /* samplerate waiting to be set */
91 unsigned long pcm_sampr SHAREDBSS_ATTR
= HW_SAMPR_DEFAULT
;
92 /* samplerate frequency selection index */
93 int pcm_fsel SHAREDBSS_ATTR
= HW_FREQ_DEFAULT
;
96 * Do peak calculation using distance squared from axis and save a lot
97 * of jumps and negation. Don't bother with the calculations of left or
98 * right only as it's never really used and won't save much time.
100 * Used for recording and playback.
102 static void pcm_peak_peeker(const void *addr
, int count
, int peaks
[2])
104 int32_t peak_l
= 0, peak_r
= 0;
105 int32_t peaksq_l
= 0, peaksq_r
= 0;
109 int32_t value
= *(int32_t *)addr
;
111 #ifdef ROCKBOX_BIG_ENDIAN
118 peak_l
= ch
, peaksq_l
= chsq
;
120 #ifdef ROCKBOX_BIG_ENDIAN
127 peak_r
= ch
, peaksq_r
= chsq
;
134 peaks
[0] = abs(peak_l
);
135 peaks
[1] = abs(peak_r
);
138 void pcm_calculate_peaks(int *left
, int *right
)
140 static int peaks
[2] = { 0, 0 };
141 static unsigned long last_peak_tick
= 0;
142 static unsigned long frame_period
= 0;
144 long tick
= current_tick
;
148 /* Throttled peak ahead based on calling period */
149 long period
= tick
- last_peak_tick
;
151 /* Keep reasonable limits on period */
154 else if (period
> HZ
/5)
157 frame_period
= (3*frame_period
+ period
) >> 2;
159 last_peak_tick
= tick
;
161 addr
= pcm_play_dma_get_peak_buffer(&count
);
163 if (pcm_playing
&& !pcm_paused
)
167 framecount
= frame_period
*pcm_curr_sampr
/ HZ
;
168 count
= MIN(framecount
, count
);
171 pcm_peak_peeker(addr
, count
, peaks
);
172 /* else keep previous peak values */
176 peaks
[0] = peaks
[1] = 0;
186 /****************************************************************************
187 * Functions that do not require targeted implementation but only a targeted
191 /* This should only be called at startup before any audio playback or
192 recording is attempted */
197 pcm_play_dma_stopped_callback();
199 pcm_set_frequency(HW_SAMPR_DEFAULT
);
201 logf(" pcm_play_dma_init");
205 /* Common code to pcm_play_data and pcm_play_pause */
206 static void pcm_play_data_start(unsigned char *start
, size_t size
)
208 if (!(start
&& size
))
210 pcm_more_callback_type get_more
= pcm_callback_for_more
;
215 get_more(&start
, &size
);
221 logf(" pcm_play_dma_start");
222 pcm_apply_settings();
223 pcm_play_dma_start(start
, size
);
230 logf(" pcm_play_dma_stop");
232 pcm_play_dma_stopped_callback();
235 void pcm_play_data(pcm_more_callback_type get_more
,
236 unsigned char *start
, size_t size
)
238 logf("pcm_play_data");
242 pcm_callback_for_more
= get_more
;
244 logf(" pcm_play_data_start");
245 pcm_play_data_start(start
, size
);
250 void pcm_play_pause(bool play
)
252 logf("pcm_play_pause: %s", play
? "play" : "pause");
256 if (play
== pcm_paused
&& pcm_playing
)
260 logf(" pcm_play_dma_pause");
261 pcm_play_dma_pause(true);
264 else if (pcm_get_bytes_waiting() > 0)
266 logf(" pcm_play_dma_pause");
267 pcm_apply_settings();
268 pcm_play_dma_pause(false);
273 logf(" pcm_play_dma_start: no data");
274 pcm_play_data_start(NULL
, 0);
285 void pcm_play_stop(void)
287 logf("pcm_play_stop");
293 logf(" pcm_play_dma_stop");
295 pcm_play_dma_stopped_callback();
299 logf(" not playing");
305 void pcm_play_dma_stopped_callback(void)
307 pcm_callback_for_more
= NULL
;
314 /* set frequency next frequency used by the audio hardware -
315 * what pcm_apply_settings will set */
316 void pcm_set_frequency(unsigned int samplerate
)
318 logf("pcm_set_frequency");
320 int index
= round_value_to_list32(samplerate
, hw_freq_sampr
,
323 if (samplerate
!= hw_freq_sampr
[index
])
324 index
= HW_FREQ_DEFAULT
; /* Invalid = default */
326 pcm_sampr
= hw_freq_sampr
[index
];
330 /* apply pcm settings to the hardware */
331 void pcm_apply_settings(void)
333 logf("pcm_apply_settings");
335 if (pcm_sampr
!= pcm_curr_sampr
)
337 logf(" pcm_dma_apply_settings");
338 pcm_dma_apply_settings();
339 pcm_curr_sampr
= pcm_sampr
;
343 bool pcm_is_playing(void)
348 bool pcm_is_paused(void)
353 void pcm_mute(bool mute
)
363 #ifdef HAVE_RECORDING
364 /** Low level pcm recording apis **/
366 /* Next start for recording peaks */
367 const volatile void *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
)
382 const void *addr
= pcm_rec_dma_get_peak_buffer(&count
);
388 pcm_peak_peeker(addr
, count
, peaks
);
390 if (addr
== pcm_rec_peak_addr
)
391 pcm_rec_peak_addr
= (int32_t *)addr
+ count
;
393 /* else keep previous peak values */
397 peaks
[0] = peaks
[1] = 0;
405 } /* pcm_calculate_rec_peaks */
407 /****************************************************************************
408 * Functions that do not require targeted implementation but only a targeted
411 void pcm_init_recording(void)
413 logf("pcm_init_recording");
415 /* Recording init is locked unlike general pcm init since this is not
416 * just a one-time event at startup and it should and must be safe by
420 logf(" pcm_rec_dma_init");
421 pcm_rec_dma_stopped_callback();
427 void pcm_close_recording(void)
429 logf("pcm_close_recording");
435 logf(" pcm_rec_dma_stop");
437 pcm_rec_dma_stopped_callback();
440 logf(" pcm_rec_dma_close");
446 void pcm_record_data(pcm_more_callback_type2 more_ready
,
447 void *start
, size_t size
)
449 logf("pcm_record_data");
451 if (!(start
&& size
))
459 pcm_callback_more_ready
= more_ready
;
461 logf(" pcm_rec_dma_start");
462 pcm_apply_settings();
463 pcm_rec_dma_start(start
, size
);
464 pcm_recording
= true;
467 } /* pcm_record_data */
469 void pcm_stop_recording(void)
471 logf("pcm_stop_recording");
477 logf(" pcm_rec_dma_stop");
479 pcm_rec_dma_stopped_callback();
483 } /* pcm_stop_recording */
485 bool pcm_is_recording(void)
487 return pcm_recording
;
490 void pcm_rec_dma_stopped_callback(void)
492 pcm_recording
= false;
493 pcm_callback_more_ready
= NULL
;
496 #endif /* HAVE_RECORDING */