tuner-as3525v2.c: only build what's needed
[maemo-rb.git] / firmware / pcm_mixer.c
blob25c41c2586990b5af8cb58771e2bac939dc12f88
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 unsigned char *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 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
75 /* Include any implemented CPU-optimized mixdown routines */
76 #if defined(CPU_ARM)
77 #if ARM_ARCH >= 6
78 #include "pcm-mixer-armv6.c"
79 #elif ARM_ARCH >= 5
80 #include "pcm-mixer-armv5.c"
81 #else
82 #include "pcm-mixer-armv4.c"
83 #endif /* ARM_ARCH */
84 #elif defined (CPU_COLDFIRE)
85 #include "pcm-mixer-coldfire.c"
86 #endif /* CPU_* */
88 #endif /* CONFIG_PLATFORM */
90 /** Generic mixing routines **/
92 #ifndef MIXER_OPTIMIZED_MIX_SAMPLES
93 #include "dsp-util.h" /* for clip_sample_16 */
95 /* Mix channels' samples and apply gain factors */
96 static FORCE_INLINE void mix_samples(uint32_t *out,
97 int16_t *src0,
98 int32_t src0_amp,
99 int16_t *src1,
100 int32_t src1_amp,
101 size_t size)
103 if (src0_amp == MIX_AMP_UNITY && src1_amp == MIX_AMP_UNITY)
105 /* Both are unity amplitude */
108 int32_t l = *src0++ + *src1++;
109 int32_t h = *src0++ + *src1++;
110 *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
112 while ((size -= 4) > 0);
114 else if (src0_amp != MIX_AMP_UNITY && src1_amp != MIX_AMP_UNITY)
116 /* Neither are unity amplitude */
119 int32_t l = (*src0++ * src0_amp >> 16) + (*src1++ * src1_amp >> 16);
120 int32_t h = (*src0++ * src0_amp >> 16) + (*src1++ * src1_amp >> 16);
121 *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
123 while ((size -= 4) > 0);
125 else
127 /* One is unity amplitude */
128 if (src0_amp != MIX_AMP_UNITY)
130 /* Keep unity in src0, amp0 */
131 int16_t *src_tmp = src0;
132 src0 = src1;
133 src1 = src_tmp;
134 src1_amp = src0_amp;
135 src0_amp = MIX_AMP_UNITY;
140 int32_t l = *src0++ + (*src1++ * src1_amp >> 16);
141 int32_t h = *src0++ + (*src1++ * src1_amp >> 16);
142 *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
144 while ((size -= 4) > 0);
147 #endif /* MIXER_OPTIMIZED_MIX_SAMPLES */
149 #ifndef MIXER_OPTIMIZED_WRITE_SAMPLES
150 /* Write channel's samples and apply gain factor */
151 static FORCE_INLINE void write_samples(uint32_t *out,
152 int16_t *src,
153 int32_t amp,
154 size_t size)
156 if (LIKELY(amp == MIX_AMP_UNITY))
158 /* Channel is unity amplitude */
159 memcpy(out, src, size);
161 else
163 /* Channel needs amplitude cut */
166 int32_t l = *src++ * amp >> 16;
167 int32_t h = *src++ * amp & 0xffff0000;
168 *out++ = (uint16_t)l | h;
170 while ((size -= 4) > 0);
173 #endif /* MIXER_OPTIMIZED_WRITE_SAMPLES */
176 /** Private generic routines **/
178 /* Mark channel active to mix its data */
179 static void mixer_activate_channel(struct mixer_channel *chan)
181 void **elem = find_array_ptr((void **)active_channels, chan);
183 if (!*elem)
185 idle_counter = 0;
186 *elem = chan;
190 /* Stop channel from mixing */
191 static void mixer_deactivate_channel(struct mixer_channel *chan)
193 remove_array_ptr((void **)active_channels, chan);
196 /* Deactivate channel and change it to stopped state */
197 static void channel_stopped(struct mixer_channel *chan)
199 mixer_deactivate_channel(chan);
200 chan->size = 0;
201 chan->start = NULL;
202 chan->status = CHANNEL_STOPPED;
205 /* Main PCM callback - sends the current prepared frame to play */
206 static void mixer_pcm_callback(unsigned char **start, size_t *size)
208 *start = (unsigned char *)downmix_buf[downmix_index];
209 *size = next_size;
212 /* Buffering callback - calls sub-callbacks and mixes the data for next
213 buffer to be sent from mixer_pcm_callback() */
214 static void MIXER_CALLBACK_ICODE mixer_buffer_callback(void)
216 downmix_index ^= 1; /* Next buffer */
218 void *mixptr = downmix_buf[downmix_index];
219 size_t mixsize = MIX_FRAME_SIZE;
220 struct mixer_channel **chan_p;
222 next_size = 0;
224 /* "Loop" back here if one round wasn't enough to fill a frame */
225 fill_frame:
226 chan_p = active_channels;
228 while (*chan_p)
230 /* Find the active channel with the least data remaining and call any
231 callbacks for channels that ran out - stopping whichever report
232 "no more" */
233 struct mixer_channel *chan = *chan_p;
234 chan->start += chan->last_size;
235 chan->size -= chan->last_size;
237 if (chan->size == 0)
239 if (chan->get_more)
241 chan->get_more(&chan->start, &chan->size);
242 ALIGN_AUDIOBUF(chan->start, chan->size);
245 if (!(chan->start && chan->size))
247 /* Channel is stopping */
248 channel_stopped(chan);
249 continue;
253 /* Channel will play for at least part of this frame */
255 /* Channel with least amount of data remaining determines the downmix
256 size */
257 if (chan->size < mixsize)
258 mixsize = chan->size;
260 chan_p++;
263 /* Add all still-active channels to the downmix */
264 chan_p = active_channels;
266 if (LIKELY(*chan_p))
268 struct mixer_channel *chan = *chan_p++;
270 if (LIKELY(!*chan_p))
272 write_samples(mixptr, (void *)chan->start,
273 chan->amplitude, mixsize);
275 else
277 void *src0, *src1;
278 unsigned int amp0, amp1;
280 /* Mix first two channels with each other as the downmix */
281 src0 = chan->start;
282 amp0 = chan->amplitude;
283 chan->last_size = mixsize;
285 chan = *chan_p++;
286 src1 = chan->start;
287 amp1 = chan->amplitude;
289 while (1)
291 mix_samples(mixptr, src0, amp0, src1, amp1, mixsize);
293 if (!*chan_p)
294 break;
296 /* More channels to mix - mix each with existing downmix */
297 chan->last_size = mixsize;
298 chan = *chan_p++;
299 src0 = mixptr;
300 amp0 = MIX_AMP_UNITY;
301 src1 = chan->start;
302 amp1 = chan->amplitude;
306 chan->last_size = mixsize;
307 next_size += mixsize;
309 if (next_size < MIX_FRAME_SIZE)
311 /* There is still space remaining in this frame */
312 mixptr += mixsize;
313 mixsize = MIX_FRAME_SIZE - next_size;
314 goto fill_frame;
317 else if (idle_counter++ < MAX_IDLE_FRAMES)
319 /* Pad incomplete frames with silence */
320 if (idle_counter <= 3)
321 memset(mixptr, 0, MIX_FRAME_SIZE - next_size);
323 next_size = MIX_FRAME_SIZE;
325 /* else silence period ran out - go to sleep */
327 #if FRAME_BOUNDARY_MARKERS != 0
328 if (next_size)
329 *downmix_buf[downmix_index] = downmix_index ? 0x7fff7fff : 0x80008000;
330 #endif
333 /* Start PCM driver if it's not currently playing */
334 static void mixer_start_pcm(void)
336 if (pcm_is_playing())
337 return;
339 #if defined(HAVE_RECORDING)
340 if (pcm_is_recording())
341 return;
342 #endif
344 /* Requires a shared global sample rate for all channels */
345 pcm_set_frequency(NATIVE_FREQUENCY);
347 /* Prepare initial frames and set up the double buffer */
348 mixer_buffer_callback();
350 /* Save the previous call's output */
351 void *start = downmix_buf[downmix_index];
353 mixer_buffer_callback();
355 pcm_play_set_dma_started_callback(mixer_buffer_callback);
356 pcm_play_data(mixer_pcm_callback, start, MIX_FRAME_SIZE);
359 /* Initialize the channel and start it if it has data */
360 static void mixer_channel_play_start(struct mixer_channel *chan,
361 pcm_play_callback_type get_more,
362 unsigned char *start, size_t size)
364 pcm_play_unlock(); /* Allow playback while doing any callback */
366 ALIGN_AUDIOBUF(start, size);
368 if (!(start && size))
370 /* Initial buffer not passed - call the callback now */
371 size = 0;
372 if (get_more)
374 get_more(&start, &size);
375 ALIGN_AUDIOBUF(start, size);
379 if (start && size)
381 /* We have data - start the channel */
382 chan->status = CHANNEL_PLAYING;
383 chan->start = start;
384 chan->size = size;
385 chan->last_size = 0;
386 chan->get_more = get_more;
388 pcm_play_lock();
389 mixer_activate_channel(chan);
390 mixer_start_pcm();
392 else
394 /* Never had anything - stop it now */
395 pcm_play_lock();
396 channel_stopped(chan);
401 /** Public interfaces **/
403 /* Start playback on a channel */
404 void mixer_channel_play_data(enum pcm_mixer_channel channel,
405 pcm_play_callback_type get_more,
406 unsigned char *start, size_t size)
408 struct mixer_channel *chan = &channels[channel];
410 pcm_play_lock();
411 mixer_deactivate_channel(chan);
412 mixer_channel_play_start(chan, get_more, start, size);
413 pcm_play_unlock();
416 /* Pause or resume a channel (when started) */
417 void mixer_channel_play_pause(enum pcm_mixer_channel channel, bool play)
419 struct mixer_channel *chan = &channels[channel];
421 pcm_play_lock();
423 if (play == (chan->status == CHANNEL_PAUSED) &&
424 chan->status != CHANNEL_STOPPED)
426 if (play)
428 chan->status = CHANNEL_PLAYING;
429 mixer_activate_channel(chan);
430 mixer_start_pcm();
432 else
434 mixer_deactivate_channel(chan);
435 chan->status = CHANNEL_PAUSED;
439 pcm_play_unlock();
442 /* Stop playback on a channel */
443 void mixer_channel_stop(enum pcm_mixer_channel channel)
445 struct mixer_channel *chan = &channels[channel];
447 pcm_play_lock();
448 channel_stopped(chan);
449 pcm_play_unlock();
452 /* Set channel's amplitude factor */
453 void mixer_channel_set_amplitude(enum pcm_mixer_channel channel,
454 unsigned int amplitude)
456 channels[channel].amplitude = MIN(amplitude, MIX_AMP_UNITY);
459 /* Return channel's playback status */
460 enum channel_status mixer_channel_status(enum pcm_mixer_channel channel)
462 return channels[channel].status;
465 /* Returns amount data remaining in channel before next callback */
466 size_t mixer_channel_get_bytes_waiting(enum pcm_mixer_channel channel)
468 return channels[channel].size;
471 /* Return pointer to channel's playing audio data and the size remaining */
472 void * mixer_channel_get_buffer(enum pcm_mixer_channel channel, int *count)
474 struct mixer_channel *chan = &channels[channel];
475 void * buf = *(unsigned char * volatile *)&chan->start;
476 size_t size = *(size_t volatile *)&chan->size;
477 void * buf2 = *(unsigned char * volatile *)&chan->start;
479 /* Still same buffer? */
480 if (buf == buf2)
482 *count = size >> 2;
483 return buf;
485 /* else can't be sure buf and size are related */
487 *count = 0;
488 return NULL;
491 /* Calculate peak values for channel */
492 void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel,
493 int *left, int *right)
495 struct mixer_channel *chan = &channels[channel];
496 struct pcm_peaks *peaks = &channel_peaks[channel];
497 int count;
498 const void *addr = mixer_channel_get_buffer(channel, &count);
500 pcm_do_peak_calculation(peaks, chan->status == CHANNEL_PLAYING,
501 addr, count);
503 if (left)
504 *left = peaks->val[0];
506 if (right)
507 *right = peaks->val[1];
510 /* Stop ALL channels and PCM and reset state */
511 void mixer_reset(void)
513 pcm_play_stop();
515 while (*active_channels)
516 channel_stopped(*active_channels);
518 idle_counter = 0;