skin_engine: rework the parser to be closer to the langauge grammar.
[maemo-rb.git] / firmware / pcm_mixer.c
blob00891f367d5885db9fea1f54f4020a34f5a77103
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 /* Certain SoC's have to do cleanup */
236 mixer_buffer_callback_exit();
238 return PCM_DMAST_OK;
241 /* Start PCM driver if it's not currently playing */
242 static void mixer_start_pcm(void)
244 if (pcm_is_playing())
245 return;
247 #if defined(HAVE_RECORDING)
248 if (pcm_is_recording())
249 return;
250 #endif
252 /* Requires a shared global sample rate for all channels */
253 pcm_set_frequency(NATIVE_FREQUENCY);
255 /* Prepare initial frames and set up the double buffer */
256 mixer_buffer_callback(PCM_DMAST_STARTED);
258 /* Save the previous call's output */
259 void *start = downmix_buf[downmix_index];
261 mixer_buffer_callback(PCM_DMAST_STARTED);
263 pcm_play_data(mixer_pcm_callback, mixer_buffer_callback,
264 start, MIX_FRAME_SIZE);
267 /** Public interfaces **/
269 /* Start playback on a channel */
270 void mixer_channel_play_data(enum pcm_mixer_channel channel,
271 pcm_play_callback_type get_more,
272 const void *start, size_t size)
274 struct mixer_channel *chan = &channels[channel];
276 ALIGN_AUDIOBUF(start, size);
278 if (!(start && size) && get_more)
280 /* Initial buffer not passed - call the callback now */
281 pcm_play_lock();
282 mixer_deactivate_channel(chan); /* Protect chan struct if active;
283 may also be same callback which
284 must not be reentered */
285 pcm_play_unlock(); /* Allow playback while doing callback */
287 size = 0;
288 get_more(&start, &size);
289 ALIGN_AUDIOBUF(start, size);
292 pcm_play_lock();
294 if (start && size)
296 /* We have data - start the channel */
297 chan->status = CHANNEL_PLAYING;
298 chan->start = start;
299 chan->size = size;
300 chan->last_size = 0;
301 chan->get_more = get_more;
303 mixer_activate_channel(chan);
304 mixer_start_pcm();
306 else
308 /* Never had anything - stop it now */
309 channel_stopped(chan);
312 pcm_play_unlock();
315 /* Pause or resume a channel (when started) */
316 void mixer_channel_play_pause(enum pcm_mixer_channel channel, bool play)
318 struct mixer_channel *chan = &channels[channel];
320 pcm_play_lock();
322 if (play == (chan->status == CHANNEL_PAUSED) &&
323 chan->status != CHANNEL_STOPPED)
325 if (play)
327 chan->status = CHANNEL_PLAYING;
328 mixer_activate_channel(chan);
329 mixer_start_pcm();
331 else
333 mixer_deactivate_channel(chan);
334 chan->status = CHANNEL_PAUSED;
338 pcm_play_unlock();
341 /* Stop playback on a channel */
342 void mixer_channel_stop(enum pcm_mixer_channel channel)
344 struct mixer_channel *chan = &channels[channel];
346 pcm_play_lock();
347 channel_stopped(chan);
348 pcm_play_unlock();
351 /* Set channel's amplitude factor */
352 void mixer_channel_set_amplitude(enum pcm_mixer_channel channel,
353 unsigned int amplitude)
355 channels[channel].amplitude = MIN(amplitude, MIX_AMP_UNITY);
358 /* Return channel's playback status */
359 enum channel_status mixer_channel_status(enum pcm_mixer_channel channel)
361 return channels[channel].status;
364 /* Returns amount data remaining in channel before next callback */
365 size_t mixer_channel_get_bytes_waiting(enum pcm_mixer_channel channel)
367 return channels[channel].size;
370 /* Return pointer to channel's playing audio data and the size remaining */
371 const void * mixer_channel_get_buffer(enum pcm_mixer_channel channel, int *count)
373 struct mixer_channel *chan = &channels[channel];
374 const void * buf = *(const void * volatile *)&chan->start;
375 size_t size = *(size_t volatile *)&chan->size;
376 const void * buf2 = *(const void * volatile *)&chan->start;
378 /* Still same buffer? */
379 if (buf == buf2)
381 *count = size >> 2;
382 return buf;
384 /* else can't be sure buf and size are related */
386 *count = 0;
387 return NULL;
390 /* Calculate peak values for channel */
391 void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel,
392 int *left, int *right)
394 struct mixer_channel *chan = &channels[channel];
395 struct pcm_peaks *peaks = &channel_peaks[channel];
396 int count;
397 const void *addr = mixer_channel_get_buffer(channel, &count);
399 pcm_do_peak_calculation(peaks, chan->status == CHANNEL_PLAYING,
400 addr, count);
402 if (left)
403 *left = peaks->val[0];
405 if (right)
406 *right = peaks->val[1];
409 /* Stop ALL channels and PCM and reset state */
410 void mixer_reset(void)
412 pcm_play_stop();
414 while (*active_channels)
415 channel_stopped(*active_channels);
417 idle_counter = 0;