test_X plugins PLA integration
[maemo-rb.git] / firmware / pcm_mixer.c
blob9077c6f27146a8d24317830479495050481d9ff3
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.h"
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 */
50 /* Forget about boost here for the moment */
51 #define MIX_FRAME_SIZE (MIX_FRAME_SAMPLES*4)
53 /* Because of the double-buffering, playback is always from here, otherwise a
54 mechanism for the channel callbacks not to free buffers too early would be
55 needed (if we _really_ want it and it's worth it, we _can_ do that ;-) ) */
56 static uint32_t downmix_buf[2][MIX_FRAME_SAMPLES] DOWNMIX_BUF_IBSS MEM_ALIGN_ATTR;
57 static int downmix_index = 0; /* Which downmix_buf? */
58 static size_t next_size = 0; /* Size of buffer to play next time */
60 /* Descriptors for all available channels */
61 static struct mixer_channel channels[PCM_MIXER_NUM_CHANNELS] IBSS_ATTR;
63 /* History for channel peaks */
64 static struct pcm_peaks channel_peaks[PCM_MIXER_NUM_CHANNELS];
66 /* Packed pointer array of all playing (active) channels in "channels" array */
67 static struct mixer_channel * active_channels[PCM_MIXER_NUM_CHANNELS+1] IBSS_ATTR;
69 /* Number of silence frames to play after all data has played */
70 #define MAX_IDLE_FRAMES (NATIVE_FREQUENCY*3 / MIX_FRAME_SAMPLES)
71 static unsigned int idle_counter = 0;
73 /** Mixing routines, CPU optmized **/
74 #include "asm/pcm-mixer.c"
76 /** Private generic routines **/
78 /* Mark channel active to mix its data */
79 static void mixer_activate_channel(struct mixer_channel *chan)
81 void **elem = find_array_ptr((void **)active_channels, chan);
83 if (!*elem)
85 idle_counter = 0;
86 *elem = chan;
90 /* Stop channel from mixing */
91 static void mixer_deactivate_channel(struct mixer_channel *chan)
93 remove_array_ptr((void **)active_channels, chan);
96 /* Deactivate channel and change it to stopped state */
97 static void channel_stopped(struct mixer_channel *chan)
99 mixer_deactivate_channel(chan);
100 chan->size = 0;
101 chan->start = NULL;
102 chan->status = CHANNEL_STOPPED;
105 /* Main PCM callback - sends the current prepared frame to play */
106 static void mixer_pcm_callback(const void **addr, size_t *size)
108 *addr = downmix_buf[downmix_index];
109 *size = next_size;
112 /* Buffering callback - calls sub-callbacks and mixes the data for next
113 buffer to be sent from mixer_pcm_callback() */
114 static enum pcm_dma_status MIXER_CALLBACK_ICODE
115 mixer_buffer_callback(enum pcm_dma_status status)
117 if (status != PCM_DMAST_STARTED)
118 return status;
120 downmix_index ^= 1; /* Next buffer */
122 void *mixptr = downmix_buf[downmix_index];
123 size_t mixsize = MIX_FRAME_SIZE;
124 struct mixer_channel **chan_p;
126 next_size = 0;
128 /* "Loop" back here if one round wasn't enough to fill a frame */
129 fill_frame:
130 chan_p = active_channels;
132 while (*chan_p)
134 /* Find the active channel with the least data remaining and call any
135 callbacks for channels that ran out - stopping whichever report
136 "no more" */
137 struct mixer_channel *chan = *chan_p;
138 chan->start += chan->last_size;
139 chan->size -= chan->last_size;
141 if (chan->size == 0)
143 if (chan->get_more)
145 chan->get_more(&chan->start, &chan->size);
146 ALIGN_AUDIOBUF(chan->start, chan->size);
149 if (!(chan->start && chan->size))
151 /* Channel is stopping */
152 channel_stopped(chan);
153 continue;
157 /* Channel will play for at least part of this frame */
159 /* Channel with least amount of data remaining determines the downmix
160 size */
161 if (chan->size < mixsize)
162 mixsize = chan->size;
164 chan_p++;
167 /* Add all still-active channels to the downmix */
168 chan_p = active_channels;
170 if (LIKELY(*chan_p))
172 struct mixer_channel *chan = *chan_p++;
174 if (LIKELY(!*chan_p))
176 write_samples(mixptr, chan->start, chan->amplitude, mixsize);
178 else
180 const void *src0, *src1;
181 unsigned int amp0, amp1;
183 /* Mix first two channels with each other as the downmix */
184 src0 = chan->start;
185 amp0 = chan->amplitude;
186 chan->last_size = mixsize;
188 chan = *chan_p++;
189 src1 = chan->start;
190 amp1 = chan->amplitude;
192 while (1)
194 mix_samples(mixptr, src0, amp0, src1, amp1, mixsize);
196 if (!*chan_p)
197 break;
199 /* More channels to mix - mix each with existing downmix */
200 chan->last_size = mixsize;
201 chan = *chan_p++;
202 src0 = mixptr;
203 amp0 = MIX_AMP_UNITY;
204 src1 = chan->start;
205 amp1 = chan->amplitude;
209 chan->last_size = mixsize;
210 next_size += mixsize;
212 if (next_size < MIX_FRAME_SIZE)
214 /* There is still space remaining in this frame */
215 mixptr += mixsize;
216 mixsize = MIX_FRAME_SIZE - next_size;
217 goto fill_frame;
220 else if (idle_counter++ < MAX_IDLE_FRAMES)
222 /* Pad incomplete frames with silence */
223 if (idle_counter <= 3)
224 memset(mixptr, 0, MIX_FRAME_SIZE - next_size);
226 next_size = MIX_FRAME_SIZE;
228 /* else silence period ran out - go to sleep */
230 #if FRAME_BOUNDARY_MARKERS != 0
231 if (next_size)
232 *downmix_buf[downmix_index] = downmix_index ? 0x7fff7fff : 0x80008000;
233 #endif
235 return PCM_DMAST_OK;
238 /* Start PCM driver if it's not currently playing */
239 static void mixer_start_pcm(void)
241 if (pcm_is_playing())
242 return;
244 #if defined(HAVE_RECORDING)
245 if (pcm_is_recording())
246 return;
247 #endif
249 /* Requires a shared global sample rate for all channels */
250 pcm_set_frequency(NATIVE_FREQUENCY);
252 /* Prepare initial frames and set up the double buffer */
253 mixer_buffer_callback(PCM_DMAST_STARTED);
255 /* Save the previous call's output */
256 void *start = downmix_buf[downmix_index];
258 mixer_buffer_callback(PCM_DMAST_STARTED);
260 pcm_play_data(mixer_pcm_callback, mixer_buffer_callback,
261 start, MIX_FRAME_SIZE);
264 /** Public interfaces **/
266 /* Start playback on a channel */
267 void mixer_channel_play_data(enum pcm_mixer_channel channel,
268 pcm_play_callback_type get_more,
269 const void *start, size_t size)
271 struct mixer_channel *chan = &channels[channel];
273 ALIGN_AUDIOBUF(start, size);
275 if (!(start && size) && get_more)
277 /* Initial buffer not passed - call the callback now */
278 pcm_play_lock();
279 mixer_deactivate_channel(chan); /* Protect chan struct if active;
280 may also be same callback which
281 must not be reentered */
282 pcm_play_unlock(); /* Allow playback while doing callback */
284 size = 0;
285 get_more(&start, &size);
286 ALIGN_AUDIOBUF(start, size);
289 pcm_play_lock();
291 if (start && size)
293 /* We have data - start the channel */
294 chan->status = CHANNEL_PLAYING;
295 chan->start = start;
296 chan->size = size;
297 chan->last_size = 0;
298 chan->get_more = get_more;
300 mixer_activate_channel(chan);
301 mixer_start_pcm();
303 else
305 /* Never had anything - stop it now */
306 channel_stopped(chan);
309 pcm_play_unlock();
312 /* Pause or resume a channel (when started) */
313 void mixer_channel_play_pause(enum pcm_mixer_channel channel, bool play)
315 struct mixer_channel *chan = &channels[channel];
317 pcm_play_lock();
319 if (play == (chan->status == CHANNEL_PAUSED) &&
320 chan->status != CHANNEL_STOPPED)
322 if (play)
324 chan->status = CHANNEL_PLAYING;
325 mixer_activate_channel(chan);
326 mixer_start_pcm();
328 else
330 mixer_deactivate_channel(chan);
331 chan->status = CHANNEL_PAUSED;
335 pcm_play_unlock();
338 /* Stop playback on a channel */
339 void mixer_channel_stop(enum pcm_mixer_channel channel)
341 struct mixer_channel *chan = &channels[channel];
343 pcm_play_lock();
344 channel_stopped(chan);
345 pcm_play_unlock();
348 /* Set channel's amplitude factor */
349 void mixer_channel_set_amplitude(enum pcm_mixer_channel channel,
350 unsigned int amplitude)
352 channels[channel].amplitude = MIN(amplitude, MIX_AMP_UNITY);
355 /* Return channel's playback status */
356 enum channel_status mixer_channel_status(enum pcm_mixer_channel channel)
358 return channels[channel].status;
361 /* Returns amount data remaining in channel before next callback */
362 size_t mixer_channel_get_bytes_waiting(enum pcm_mixer_channel channel)
364 return channels[channel].size;
367 /* Return pointer to channel's playing audio data and the size remaining */
368 const void * mixer_channel_get_buffer(enum pcm_mixer_channel channel, int *count)
370 struct mixer_channel *chan = &channels[channel];
371 const void * buf = *(const void * volatile *)&chan->start;
372 size_t size = *(size_t volatile *)&chan->size;
373 const void * buf2 = *(const void * volatile *)&chan->start;
375 /* Still same buffer? */
376 if (buf == buf2)
378 *count = size >> 2;
379 return buf;
381 /* else can't be sure buf and size are related */
383 *count = 0;
384 return NULL;
387 /* Calculate peak values for channel */
388 void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel,
389 int *left, int *right)
391 struct mixer_channel *chan = &channels[channel];
392 struct pcm_peaks *peaks = &channel_peaks[channel];
393 int count;
394 const void *addr = mixer_channel_get_buffer(channel, &count);
396 pcm_do_peak_calculation(peaks, chan->status == CHANNEL_PLAYING,
397 addr, count);
399 if (left)
400 *left = peaks->val[0];
402 if (right)
403 *right = peaks->val[1];
406 /* Stop ALL channels and PCM and reset state */
407 void mixer_reset(void)
409 pcm_play_stop();
411 while (*active_channels)
412 channel_stopped(*active_channels);
414 idle_counter = 0;