1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2011 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 ****************************************************************************/
26 #include "pcm-internal.h"
27 #include "pcm_mixer.h"
28 #include "dsp_core.h" /* For NATIVE_FREQUENCY */
30 /* Channels use standard-style PCM callback interface but a latency of one
31 frame by double-buffering is introduced in order to facilitate mixing and
32 keep the hardware fed. There must be sufficient time to perform operations
33 before the last samples are sent to the codec and so things are done in
34 parallel (as much as possible) with sending-out data. */
36 /* Define this to nonzero to add a marker pulse at each frame start */
37 #define FRAME_BOUNDARY_MARKERS 0
39 /* Descriptor for each channel */
42 const void *start
; /* Buffer pointer */
43 size_t size
; /* Bytes remaining */
44 size_t last_size
; /* Size of consumed data in prev. cycle */
45 pcm_play_callback_type get_more
; /* Registered callback */
46 enum channel_status status
; /* Playback status */
47 uint32_t amplitude
; /* Amp. factor: 0x0000 = mute, 0x10000 = unity */
48 chan_buffer_hook_fn_type buffer_hook
; /* Callback for new buffer */
51 /* Forget about boost here for the moment */
52 #define MIX_FRAME_SIZE (MIX_FRAME_SAMPLES*4)
54 /* Because of the double-buffering, playback is always from here, otherwise a
55 mechanism for the channel callbacks not to free buffers too early would be
56 needed (if we _really_ want it and it's worth it, we _can_ do that ;-) ) */
57 static uint32_t downmix_buf
[2][MIX_FRAME_SAMPLES
] DOWNMIX_BUF_IBSS MEM_ALIGN_ATTR
;
58 static int downmix_index
= 0; /* Which downmix_buf? */
59 static size_t next_size
= 0; /* Size of buffer to play next time */
61 /* Descriptors for all available channels */
62 static struct mixer_channel channels
[PCM_MIXER_NUM_CHANNELS
] IBSS_ATTR
;
64 /* Packed pointer array of all playing (active) channels in "channels" array */
65 static struct mixer_channel
* active_channels
[PCM_MIXER_NUM_CHANNELS
+1] IBSS_ATTR
;
67 /* Number of silence frames to play after all data has played */
68 #define MAX_IDLE_FRAMES (NATIVE_FREQUENCY*3 / MIX_FRAME_SAMPLES)
69 static unsigned int idle_counter
= 0;
71 /** Mixing routines, CPU optmized **/
72 #include "asm/pcm-mixer.c"
74 /** Private generic routines **/
76 /* Mark channel active to mix its data */
77 static void mixer_activate_channel(struct mixer_channel
*chan
)
79 void **elem
= find_array_ptr((void **)active_channels
, chan
);
88 /* Stop channel from mixing */
89 static void mixer_deactivate_channel(struct mixer_channel
*chan
)
91 remove_array_ptr((void **)active_channels
, chan
);
94 /* Deactivate channel and change it to stopped state */
95 static void channel_stopped(struct mixer_channel
*chan
)
97 mixer_deactivate_channel(chan
);
100 chan
->status
= CHANNEL_STOPPED
;
103 /* Main PCM callback - sends the current prepared frame to play */
104 static void mixer_pcm_callback(const void **addr
, size_t *size
)
106 *addr
= downmix_buf
[downmix_index
];
110 static inline void chan_call_buffer_hook(struct mixer_channel
*chan
)
112 if (UNLIKELY(chan
->buffer_hook
))
113 chan
->buffer_hook(chan
->start
, chan
->size
);
116 /* Buffering callback - calls sub-callbacks and mixes the data for next
117 buffer to be sent from mixer_pcm_callback() */
118 static enum pcm_dma_status MIXER_CALLBACK_ICODE
119 mixer_buffer_callback(enum pcm_dma_status status
)
121 if (status
!= PCM_DMAST_STARTED
)
124 downmix_index
^= 1; /* Next buffer */
126 void *mixptr
= downmix_buf
[downmix_index
];
127 size_t mixsize
= MIX_FRAME_SIZE
;
128 struct mixer_channel
**chan_p
;
132 /* "Loop" back here if one round wasn't enough to fill a frame */
134 chan_p
= active_channels
;
138 /* Find the active channel with the least data remaining and call any
139 callbacks for channels that ran out - stopping whichever report
141 struct mixer_channel
*chan
= *chan_p
;
142 chan
->start
+= chan
->last_size
;
143 chan
->size
-= chan
->last_size
;
149 chan
->get_more(&chan
->start
, &chan
->size
);
150 ALIGN_AUDIOBUF(chan
->start
, chan
->size
);
153 if (!(chan
->start
&& chan
->size
))
155 /* Channel is stopping */
156 channel_stopped(chan
);
160 chan_call_buffer_hook(chan
);
163 /* Channel will play for at least part of this frame */
165 /* Channel with least amount of data remaining determines the downmix
167 if (chan
->size
< mixsize
)
168 mixsize
= chan
->size
;
173 /* Add all still-active channels to the downmix */
174 chan_p
= active_channels
;
178 struct mixer_channel
*chan
= *chan_p
++;
180 if (LIKELY(!*chan_p
))
182 write_samples(mixptr
, chan
->start
, chan
->amplitude
, mixsize
);
186 const void *src0
, *src1
;
187 unsigned int amp0
, amp1
;
189 /* Mix first two channels with each other as the downmix */
191 amp0
= chan
->amplitude
;
192 chan
->last_size
= mixsize
;
196 amp1
= chan
->amplitude
;
200 mix_samples(mixptr
, src0
, amp0
, src1
, amp1
, mixsize
);
205 /* More channels to mix - mix each with existing downmix */
206 chan
->last_size
= mixsize
;
209 amp0
= MIX_AMP_UNITY
;
211 amp1
= chan
->amplitude
;
215 chan
->last_size
= mixsize
;
216 next_size
+= mixsize
;
218 if (next_size
< MIX_FRAME_SIZE
)
220 /* There is still space remaining in this frame */
222 mixsize
= MIX_FRAME_SIZE
- next_size
;
226 else if (idle_counter
++ < MAX_IDLE_FRAMES
)
228 /* Pad incomplete frames with silence */
229 if (idle_counter
<= 3)
230 memset(mixptr
, 0, MIX_FRAME_SIZE
- next_size
);
232 next_size
= MIX_FRAME_SIZE
;
234 /* else silence period ran out - go to sleep */
236 #if FRAME_BOUNDARY_MARKERS != 0
238 *downmix_buf
[downmix_index
] = downmix_index
? 0x7fff7fff : 0x80008000;
241 /* Certain SoC's have to do cleanup */
242 mixer_buffer_callback_exit();
247 /* Start PCM driver if it's not currently playing */
248 static void mixer_start_pcm(void)
250 if (pcm_is_playing())
253 #if defined(HAVE_RECORDING)
254 if (pcm_is_recording())
258 /* Requires a shared global sample rate for all channels */
259 pcm_set_frequency(NATIVE_FREQUENCY
);
261 /* Prepare initial frames and set up the double buffer */
262 mixer_buffer_callback(PCM_DMAST_STARTED
);
264 /* Save the previous call's output */
265 void *start
= downmix_buf
[downmix_index
];
267 mixer_buffer_callback(PCM_DMAST_STARTED
);
269 pcm_play_data(mixer_pcm_callback
, mixer_buffer_callback
,
270 start
, MIX_FRAME_SIZE
);
273 /** Public interfaces **/
275 /* Start playback on a channel */
276 void mixer_channel_play_data(enum pcm_mixer_channel channel
,
277 pcm_play_callback_type get_more
,
278 const void *start
, size_t size
)
280 struct mixer_channel
*chan
= &channels
[channel
];
282 ALIGN_AUDIOBUF(start
, size
);
284 if (!(start
&& size
) && get_more
)
286 /* Initial buffer not passed - call the callback now */
288 mixer_deactivate_channel(chan
); /* Protect chan struct if active;
289 may also be same callback which
290 must not be reentered */
291 pcm_play_unlock(); /* Allow playback while doing callback */
294 get_more(&start
, &size
);
295 ALIGN_AUDIOBUF(start
, size
);
302 /* We have data - start the channel */
303 chan
->status
= CHANNEL_PLAYING
;
307 chan
->get_more
= get_more
;
309 mixer_activate_channel(chan
);
310 chan_call_buffer_hook(chan
);
315 /* Never had anything - stop it now */
316 channel_stopped(chan
);
322 /* Pause or resume a channel (when started) */
323 void mixer_channel_play_pause(enum pcm_mixer_channel channel
, bool play
)
325 struct mixer_channel
*chan
= &channels
[channel
];
329 if (play
== (chan
->status
== CHANNEL_PAUSED
) &&
330 chan
->status
!= CHANNEL_STOPPED
)
334 chan
->status
= CHANNEL_PLAYING
;
335 mixer_activate_channel(chan
);
340 mixer_deactivate_channel(chan
);
341 chan
->status
= CHANNEL_PAUSED
;
348 /* Stop playback on a channel */
349 void mixer_channel_stop(enum pcm_mixer_channel channel
)
351 struct mixer_channel
*chan
= &channels
[channel
];
354 channel_stopped(chan
);
358 /* Set channel's amplitude factor */
359 void mixer_channel_set_amplitude(enum pcm_mixer_channel channel
,
360 unsigned int amplitude
)
362 channels
[channel
].amplitude
= MIN(amplitude
, MIX_AMP_UNITY
);
365 /* Return channel's playback status */
366 enum channel_status
mixer_channel_status(enum pcm_mixer_channel channel
)
368 return channels
[channel
].status
;
371 /* Returns amount data remaining in channel before next callback */
372 size_t mixer_channel_get_bytes_waiting(enum pcm_mixer_channel channel
)
374 return channels
[channel
].size
;
377 /* Return pointer to channel's playing audio data and the size remaining */
378 const void * mixer_channel_get_buffer(enum pcm_mixer_channel channel
, int *count
)
380 struct mixer_channel
*chan
= &channels
[channel
];
381 const void * buf
= *(const void * volatile *)&chan
->start
;
382 size_t size
= *(size_t volatile *)&chan
->size
;
383 const void * buf2
= *(const void * volatile *)&chan
->start
;
385 /* Still same buffer? */
391 /* else can't be sure buf and size are related */
397 /* Calculate peak values for channel */
398 void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel
,
399 struct pcm_peaks
*peaks
)
402 const void *addr
= mixer_channel_get_buffer(channel
, &count
);
404 pcm_do_peak_calculation(peaks
,
405 channels
[channel
].status
== CHANNEL_PLAYING
,
409 /* Adjust channel pointer by a given offset to support movable buffers */
410 void mixer_adjust_channel_address(enum pcm_mixer_channel channel
,
414 /* Makes no difference if it's stopped */
415 channels
[channel
].start
+= offset
;
419 /* Set a hook that is called upon getting a new source buffer for a channel
420 NOTE: Called for each buffer, not each mixer chunk */
421 void mixer_channel_set_buffer_hook(enum pcm_mixer_channel channel
,
422 chan_buffer_hook_fn_type fn
)
424 struct mixer_channel
*chan
= &channels
[channel
];
427 chan
->buffer_hook
= fn
;
431 /* Stop ALL channels and PCM and reset state */
432 void mixer_reset(void)
436 while (*active_channels
)
437 channel_stopped(*active_channels
);