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 const void* pcm_get_peak_buffer(int * count
)
188 return pcm_play_dma_get_peak_buffer(count
);
191 /****************************************************************************
192 * Functions that do not require targeted implementation but only a targeted
196 /* This should only be called at startup before any audio playback or
197 recording is attempted */
202 pcm_play_dma_stopped_callback();
204 pcm_set_frequency(HW_SAMPR_DEFAULT
);
206 logf(" pcm_play_dma_init");
210 /* Common code to pcm_play_data and pcm_play_pause */
211 static void pcm_play_data_start(unsigned char *start
, size_t size
)
213 if (!(start
&& size
))
215 pcm_more_callback_type get_more
= pcm_callback_for_more
;
220 get_more(&start
, &size
);
226 logf(" pcm_play_dma_start");
227 pcm_apply_settings();
228 pcm_play_dma_start(start
, size
);
235 logf(" pcm_play_dma_stop");
237 pcm_play_dma_stopped_callback();
240 void pcm_play_data(pcm_more_callback_type get_more
,
241 unsigned char *start
, size_t size
)
243 logf("pcm_play_data");
247 pcm_callback_for_more
= get_more
;
249 logf(" pcm_play_data_start");
250 pcm_play_data_start(start
, size
);
255 void pcm_play_pause(bool play
)
257 logf("pcm_play_pause: %s", play
? "play" : "pause");
261 if (play
== pcm_paused
&& pcm_playing
)
265 logf(" pcm_play_dma_pause");
266 pcm_play_dma_pause(true);
269 else if (pcm_get_bytes_waiting() > 0)
271 logf(" pcm_play_dma_pause");
272 pcm_apply_settings();
273 pcm_play_dma_pause(false);
278 logf(" pcm_play_dma_start: no data");
279 pcm_play_data_start(NULL
, 0);
290 void pcm_play_stop(void)
292 logf("pcm_play_stop");
298 logf(" pcm_play_dma_stop");
300 pcm_play_dma_stopped_callback();
304 logf(" not playing");
310 void pcm_play_dma_stopped_callback(void)
312 pcm_callback_for_more
= NULL
;
319 /* set frequency next frequency used by the audio hardware -
320 * what pcm_apply_settings will set */
321 void pcm_set_frequency(unsigned int samplerate
)
323 logf("pcm_set_frequency");
325 int index
= round_value_to_list32(samplerate
, hw_freq_sampr
,
328 if (samplerate
!= hw_freq_sampr
[index
])
329 index
= HW_FREQ_DEFAULT
; /* Invalid = default */
331 pcm_sampr
= hw_freq_sampr
[index
];
335 /* apply pcm settings to the hardware */
336 void pcm_apply_settings(void)
338 logf("pcm_apply_settings");
340 if (pcm_sampr
!= pcm_curr_sampr
)
342 logf(" pcm_dma_apply_settings");
343 pcm_dma_apply_settings();
344 pcm_curr_sampr
= pcm_sampr
;
348 bool pcm_is_playing(void)
353 bool pcm_is_paused(void)
358 void pcm_mute(bool mute
)
368 #ifdef HAVE_RECORDING
369 /** Low level pcm recording apis **/
371 /* Next start for recording peaks */
372 const volatile void *pcm_rec_peak_addr SHAREDBSS_ATTR
= NULL
;
373 /* the registered callback function for when more data is available */
374 volatile pcm_more_callback_type2
375 pcm_callback_more_ready SHAREDBSS_ATTR
= NULL
;
376 /* DMA transfer in is currently active */
377 volatile bool pcm_recording SHAREDBSS_ATTR
= false;
380 * Return recording peaks - From the end of the last peak up to
381 * current write position.
383 void pcm_calculate_rec_peaks(int *left
, int *right
)
387 const void *addr
= pcm_rec_dma_get_peak_buffer(&count
);
393 pcm_peak_peeker(addr
, count
, peaks
);
395 if (addr
== pcm_rec_peak_addr
)
396 pcm_rec_peak_addr
= (int32_t *)addr
+ count
;
398 /* else keep previous peak values */
402 peaks
[0] = peaks
[1] = 0;
410 } /* pcm_calculate_rec_peaks */
412 /****************************************************************************
413 * Functions that do not require targeted implementation but only a targeted
416 void pcm_init_recording(void)
418 logf("pcm_init_recording");
420 /* Recording init is locked unlike general pcm init since this is not
421 * just a one-time event at startup and it should and must be safe by
425 logf(" pcm_rec_dma_init");
426 pcm_rec_dma_stopped_callback();
432 void pcm_close_recording(void)
434 logf("pcm_close_recording");
440 logf(" pcm_rec_dma_stop");
442 pcm_rec_dma_stopped_callback();
445 logf(" pcm_rec_dma_close");
451 void pcm_record_data(pcm_more_callback_type2 more_ready
,
452 void *start
, size_t size
)
454 logf("pcm_record_data");
456 if (!(start
&& size
))
464 pcm_callback_more_ready
= more_ready
;
466 logf(" pcm_rec_dma_start");
467 pcm_apply_settings();
468 pcm_rec_dma_start(start
, size
);
469 pcm_recording
= true;
472 } /* pcm_record_data */
474 void pcm_stop_recording(void)
476 logf("pcm_stop_recording");
482 logf(" pcm_rec_dma_stop");
484 pcm_rec_dma_stopped_callback();
488 } /* pcm_stop_recording */
490 bool pcm_is_recording(void)
492 return pcm_recording
;
495 void pcm_rec_dma_stopped_callback(void)
497 pcm_recording
= false;
498 pcm_callback_more_ready
= NULL
;
501 #endif /* HAVE_RECORDING */