Bump version numbers for 3.13
[maemo-rb.git] / firmware / pcm_mixer.c
blob34852e97e924ee59ad77b49b59a579edce83e585
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "config.h"
22 #include "system.h"
23 #include "general.h"
24 #include "kernel.h"
25 #include "pcm.h"
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 */
40 struct mixer_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);
81 if (!*elem)
83 idle_counter = 0;
84 *elem = 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);
98 chan->size = 0;
99 chan->start = NULL;
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];
107 *size = next_size;
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)
122 return status;
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;
130 next_size = 0;
132 /* "Loop" back here if one round wasn't enough to fill a frame */
133 fill_frame:
134 chan_p = active_channels;
136 while (*chan_p)
138 /* Find the active channel with the least data remaining and call any
139 callbacks for channels that ran out - stopping whichever report
140 "no more" */
141 struct mixer_channel *chan = *chan_p;
142 chan->start += chan->last_size;
143 chan->size -= chan->last_size;
145 if (chan->size == 0)
147 if (chan->get_more)
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);
157 continue;
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
166 size */
167 if (chan->size < mixsize)
168 mixsize = chan->size;
170 chan_p++;
173 /* Add all still-active channels to the downmix */
174 chan_p = active_channels;
176 if (LIKELY(*chan_p))
178 struct mixer_channel *chan = *chan_p++;
180 if (LIKELY(!*chan_p))
182 write_samples(mixptr, chan->start, chan->amplitude, mixsize);
184 else
186 const void *src0, *src1;
187 unsigned int amp0, amp1;
189 /* Mix first two channels with each other as the downmix */
190 src0 = chan->start;
191 amp0 = chan->amplitude;
192 chan->last_size = mixsize;
194 chan = *chan_p++;
195 src1 = chan->start;
196 amp1 = chan->amplitude;
198 while (1)
200 mix_samples(mixptr, src0, amp0, src1, amp1, mixsize);
202 if (!*chan_p)
203 break;
205 /* More channels to mix - mix each with existing downmix */
206 chan->last_size = mixsize;
207 chan = *chan_p++;
208 src0 = mixptr;
209 amp0 = MIX_AMP_UNITY;
210 src1 = chan->start;
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 */
221 mixptr += mixsize;
222 mixsize = MIX_FRAME_SIZE - next_size;
223 goto fill_frame;
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
237 if (next_size)
238 *downmix_buf[downmix_index] = downmix_index ? 0x7fff7fff : 0x80008000;
239 #endif
241 /* Certain SoC's have to do cleanup */
242 mixer_buffer_callback_exit();
244 return PCM_DMAST_OK;
247 /* Start PCM driver if it's not currently playing */
248 static void mixer_start_pcm(void)
250 if (pcm_is_playing())
251 return;
253 #if defined(HAVE_RECORDING)
254 if (pcm_is_recording())
255 return;
256 #endif
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 */
287 pcm_play_lock();
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 */
293 size = 0;
294 get_more(&start, &size);
295 ALIGN_AUDIOBUF(start, size);
298 pcm_play_lock();
300 if (start && size)
302 /* We have data - start the channel */
303 chan->status = CHANNEL_PLAYING;
304 chan->start = start;
305 chan->size = size;
306 chan->last_size = 0;
307 chan->get_more = get_more;
309 mixer_activate_channel(chan);
310 chan_call_buffer_hook(chan);
311 mixer_start_pcm();
313 else
315 /* Never had anything - stop it now */
316 channel_stopped(chan);
319 pcm_play_unlock();
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];
327 pcm_play_lock();
329 if (play == (chan->status == CHANNEL_PAUSED) &&
330 chan->status != CHANNEL_STOPPED)
332 if (play)
334 chan->status = CHANNEL_PLAYING;
335 mixer_activate_channel(chan);
336 mixer_start_pcm();
338 else
340 mixer_deactivate_channel(chan);
341 chan->status = CHANNEL_PAUSED;
345 pcm_play_unlock();
348 /* Stop playback on a channel */
349 void mixer_channel_stop(enum pcm_mixer_channel channel)
351 struct mixer_channel *chan = &channels[channel];
353 pcm_play_lock();
354 channel_stopped(chan);
355 pcm_play_unlock();
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? */
386 if (buf == buf2)
388 *count = size >> 2;
389 return buf;
391 /* else can't be sure buf and size are related */
393 *count = 0;
394 return NULL;
397 /* Calculate peak values for channel */
398 void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel,
399 struct pcm_peaks *peaks)
401 int count;
402 const void *addr = mixer_channel_get_buffer(channel, &count);
404 pcm_do_peak_calculation(peaks,
405 channels[channel].status == CHANNEL_PLAYING,
406 addr, count);
409 /* Adjust channel pointer by a given offset to support movable buffers */
410 void mixer_adjust_channel_address(enum pcm_mixer_channel channel,
411 off_t offset)
413 pcm_play_lock();
414 /* Makes no difference if it's stopped */
415 channels[channel].start += offset;
416 pcm_play_unlock();
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];
426 pcm_play_lock();
427 chan->buffer_hook = fn;
428 pcm_play_unlock();
431 /* Stop ALL channels and PCM and reset state */
432 void mixer_reset(void)
434 pcm_play_stop();
436 while (*active_channels)
437 channel_stopped(*active_channels);
439 idle_counter = 0;