1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * PCM output buffer definitions
12 * Copyright (c) 2007 Michael Sevakis
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "mpegplayer.h"
27 static struct pcm_frame_header
* ALIGNED_ATTR(4) pcm_buffer
;
28 /* End of buffer (not guard) */
29 static struct pcm_frame_header
* ALIGNED_ATTR(4) pcmbuf_end
;
31 static struct pcm_frame_header
* ALIGNED_ATTR(4) pcmbuf_head IBSS_ATTR
;
33 static struct pcm_frame_header
* ALIGNED_ATTR(4) pcmbuf_tail IBSS_ATTR
;
36 static uint64_t pcmbuf_read IBSS_ATTR
; /* Number of bytes read by DMA */
37 static uint64_t pcmbuf_written IBSS_ATTR
; /* Number of bytes written by source */
38 static ssize_t pcmbuf_threshold IBSS_ATTR
; /* Non-silence threshold */
41 static uint32_t clock_base IBSS_ATTR
; /* Our base clock */
42 static uint32_t clock_start IBSS_ATTR
; /* Clock at playback start */
43 static int32_t clock_adjust IBSS_ATTR
; /* Clock drift adjustment */
45 /* Small silence clip. ~5.80ms @ 44.1kHz */
46 static int16_t silence
[256*2] ALIGNED_ATTR(4) = { 0 };
48 /* Advance a PCM buffer pointer by size bytes circularly */
49 static inline void pcm_advance_buffer(struct pcm_frame_header
**p
,
52 *p
= SKIPBYTES(*p
, size
);
57 /* Inline internally but not externally */
58 inline ssize_t
pcm_output_used(void)
60 return (ssize_t
)(pcmbuf_written
- pcmbuf_read
);
63 inline ssize_t
pcm_output_free(void)
65 return (ssize_t
)(PCMOUT_BUFSIZE
- pcmbuf_written
+ pcmbuf_read
);
68 /* Audio DMA handler */
69 static void get_more(unsigned char **start
, size_t *size
)
71 ssize_t sz
= pcm_output_used();
73 if (sz
> pcmbuf_threshold
)
75 pcmbuf_threshold
= PCMOUT_LOW_WM
;
79 uint32_t time
= pcmbuf_head
->time
;
80 int32_t offset
= time
- (clock_base
+ clock_adjust
);
82 sz
= pcmbuf_head
->size
;
84 if (sz
< (ssize_t
)(sizeof(pcmbuf_head
) + 4) ||
87 /* Just show a warning about this - will never happen
88 * without a bug in the audio thread code or a clobbered
90 DEBUGF("get_more: invalid size (%ld)\n", sz
);
93 if (offset
< -100*CLOCK_RATE
/1000)
95 /* Frame more than 100ms late - drop it */
96 pcm_advance_buffer(&pcmbuf_head
, sz
);
98 if (pcmbuf_read
< pcmbuf_written
)
101 else if (offset
< 100*CLOCK_RATE
/1000)
103 /* Frame less than 100ms early - play it */
104 *start
= (unsigned char *)pcmbuf_head
->data
;
106 pcm_advance_buffer(&pcmbuf_head
, sz
);
109 sz
-= sizeof (struct pcm_frame_header
);
113 /* Audio is time master - keep clock synchronized */
114 clock_adjust
= time
- clock_base
;
116 /* Update base clock */
117 clock_base
+= sz
>> 2;
120 /* Frame will be dropped - play silence clip */
126 /* Ran out so revert to default watermark */
127 pcmbuf_threshold
= PCMOUT_PLAY_WM
;
130 /* Keep clock going at all times */
131 *start
= (unsigned char *)silence
;
132 *size
= sizeof (silence
);
134 clock_base
+= sizeof (silence
) / 4;
136 if (pcmbuf_read
> pcmbuf_written
)
137 pcmbuf_read
= pcmbuf_written
;
140 struct pcm_frame_header
* pcm_output_get_buffer(void)
145 void pcm_output_add_data(void)
147 size_t size
= pcmbuf_tail
->size
;
148 pcm_advance_buffer(&pcmbuf_tail
, size
);
149 pcmbuf_written
+= size
;
152 /* Flushes the buffer - clock keeps counting */
153 void pcm_output_flush(void)
157 pcmbuf_threshold
= PCMOUT_PLAY_WM
;
158 pcmbuf_read
= pcmbuf_written
= 0;
159 pcmbuf_head
= pcmbuf_tail
= pcm_buffer
;
161 rb
->pcm_play_unlock();
164 /* Seek the reference clock to the specified time - next audio data ready to
165 go to DMA should be on the buffer with the same time index or else the PCM
166 buffer should be empty */
167 void pcm_output_set_clock(uint32_t time
)
175 rb
->pcm_play_unlock();
178 uint32_t pcm_output_get_clock(void)
180 return clock_base
+ clock_adjust
181 - (rb
->pcm_get_bytes_waiting() >> 2);
184 uint32_t pcm_output_get_ticks(uint32_t *start
)
187 *start
= clock_start
;
189 return clock_base
- (rb
->pcm_get_bytes_waiting() >> 2);
192 /* Pauses/Starts pcm playback - and the clock */
193 void pcm_output_play_pause(bool play
)
197 if (rb
->pcm_is_playing())
199 rb
->pcm_play_pause(play
);
203 rb
->pcm_play_data(get_more
, NULL
, 0);
206 rb
->pcm_play_unlock();
209 /* Stops all playback and resets the clock */
210 void pcm_output_stop(void)
214 if (rb
->pcm_is_playing())
218 pcm_output_set_clock(0);
220 rb
->pcm_play_unlock();
223 /* Drains any data if the start threshold hasn't been reached */
224 void pcm_output_drain(void)
227 pcmbuf_threshold
= PCMOUT_LOW_WM
;
228 rb
->pcm_play_unlock();
231 bool pcm_output_init(void)
233 pcm_buffer
= mpeg_malloc(PCMOUT_ALLOC_SIZE
, MPEG_ALLOC_PCMOUT
);
234 if (pcm_buffer
== NULL
)
237 pcmbuf_threshold
= PCMOUT_PLAY_WM
;
238 pcmbuf_head
= pcm_buffer
;
239 pcmbuf_tail
= pcm_buffer
;
240 pcmbuf_end
= SKIPBYTES(pcm_buffer
, PCMOUT_BUFSIZE
);
244 rb
->pcm_set_frequency(SAMPR_44
);
246 #if INPUT_SRC_CAPS != 0
247 /* Select playback */
248 rb
->audio_set_input_source(AUDIO_SRC_PLAYBACK
, SRCF_PLAYBACK
);
249 rb
->audio_set_output_source(AUDIO_SRC_PLAYBACK
);
252 #if SILENCE_TEST_TONE
253 /* Make the silence clip a square wave */
254 const int16_t silence_amp
= 32767 / 16;
257 for (i
= 0; i
< ARRAYLEN(silence
); i
+= 2)
259 if (i
< ARRAYLEN(silence
)/2)
261 silence
[i
] = silence_amp
;
262 silence
[i
+1] = silence_amp
;
266 silence
[i
] = -silence_amp
;
267 silence
[i
+1] = -silence_amp
;
275 void pcm_output_exit(void)
277 rb
->pcm_set_frequency(HW_SAMPR_DEFAULT
);