Fix warning
[kugel-rb.git] / firmware / pcm_mixer.c
blobc84762938ec0a55c3dc33ae6d85fc755202f714a
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 /* Cheapo buffer align macro to align to the 16-16 PCM size */
74 #define ALIGN_CHANNEL(start, size) \
75 ({ start = (void *)(((uintptr_t)start + 3) & ~3); \
76 size &= ~3; })
78 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
80 /* Include any implemented CPU-optimized mixdown routines */
81 #if defined(CPU_ARM)
82 #if ARM_ARCH >= 6
83 #include "pcm-mixer-armv6.c"
84 #elif ARM_ARCH >= 5
85 #include "pcm-mixer-armv5.c"
86 #else
87 #include "pcm-mixer-armv4.c"
88 #endif /* ARM_ARCH */
89 #elif defined (CPU_COLDFIRE)
90 #include "pcm-mixer-coldfire.c"
91 #endif /* CPU_* */
93 #endif /* CONFIG_PLATFORM */
95 /** Generic mixing routines **/
97 #ifndef MIXER_OPTIMIZED_MIX_SAMPLES
98 /* Clip sample to signed 16 bit range */
99 static FORCE_INLINE int32_t clip_sample_16(int32_t sample)
101 if ((int16_t)sample != sample)
102 sample = 0x7fff ^ (sample >> 31);
103 return sample;
106 /* Mix channels' samples and apply gain factors */
107 static FORCE_INLINE void mix_samples(uint32_t *out,
108 int16_t *src0,
109 int32_t src0_amp,
110 int16_t *src1,
111 int32_t src1_amp,
112 size_t size)
114 if (src0_amp == MIX_AMP_UNITY && src1_amp == MIX_AMP_UNITY)
116 /* Both are unity amplitude */
119 int32_t l = *src0++ + *src1++;
120 int32_t h = *src0++ + *src1++;
121 *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
123 while ((size -= 4) > 0);
125 else if (src0_amp != MIX_AMP_UNITY && src1_amp != MIX_AMP_UNITY)
127 /* Neither are unity amplitude */
130 int32_t l = (*src0++ * src0_amp >> 16) + (*src1++ * src1_amp >> 16);
131 int32_t h = (*src0++ * src0_amp >> 16) + (*src1++ * src1_amp >> 16);
132 *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
134 while ((size -= 4) > 0);
136 else
138 /* One is unity amplitude */
139 if (src0_amp != MIX_AMP_UNITY)
141 /* Keep unity in src0, amp0 */
142 int16_t *src_tmp = src0;
143 src0 = src1;
144 src1 = src_tmp;
145 src1_amp = src0_amp;
146 src0_amp = MIX_AMP_UNITY;
151 int32_t l = *src0++ + (*src1++ * src1_amp >> 16);
152 int32_t h = *src0++ + (*src1++ * src1_amp >> 16);
153 *out++ = (uint16_t)clip_sample_16(l) | (clip_sample_16(h) << 16);
155 while ((size -= 4) > 0);
158 #endif /* MIXER_OPTIMIZED_MIX_SAMPLES */
160 #ifndef MIXER_OPTIMIZED_WRITE_SAMPLES
161 /* Write channel's samples and apply gain factor */
162 static FORCE_INLINE void write_samples(uint32_t *out,
163 int16_t *src,
164 int32_t amp,
165 size_t size)
167 if (LIKELY(amp == MIX_AMP_UNITY))
169 /* Channel is unity amplitude */
170 memcpy(out, src, size);
172 else
174 /* Channel needs amplitude cut */
177 int32_t l = *src++ * amp >> 16;
178 int32_t h = *src++ * amp & 0xffff0000;
179 *out++ = (uint16_t)l | h;
181 while ((size -= 4) > 0);
184 #endif /* MIXER_OPTIMIZED_WRITE_SAMPLES */
187 /** Private generic routines **/
189 /* Mark channel active to mix its data */
190 static void mixer_activate_channel(struct mixer_channel *chan)
192 void **elem = find_array_ptr((void **)active_channels, chan);
194 if (!*elem)
196 idle_counter = 0;
197 *elem = chan;
201 /* Stop channel from mixing */
202 static void mixer_deactivate_channel(struct mixer_channel *chan)
204 remove_array_ptr((void **)active_channels, chan);
207 /* Deactivate channel and change it to stopped state */
208 static void channel_stopped(struct mixer_channel *chan)
210 mixer_deactivate_channel(chan);
211 chan->size = 0;
212 chan->start = NULL;
213 chan->status = CHANNEL_STOPPED;
216 /* Main PCM callback - sends the current prepared frame to play */
217 static void mixer_pcm_callback(unsigned char **start, size_t *size)
219 *start = (unsigned char *)downmix_buf[downmix_index];
220 *size = next_size;
223 /* Buffering callback - calls sub-callbacks and mixes the data for next
224 buffer to be sent from mixer_pcm_callback() */
225 static void MIXER_CALLBACK_ICODE mixer_buffer_callback(void)
227 downmix_index ^= 1; /* Next buffer */
229 void *mixptr = downmix_buf[downmix_index];
230 size_t mixsize = MIX_FRAME_SIZE;
231 struct mixer_channel **chan_p;
233 next_size = 0;
235 /* "Loop" back here if one round wasn't enough to fill a frame */
236 fill_frame:
237 chan_p = active_channels;
239 while (*chan_p)
241 /* Find the active channel with the least data remaining and call any
242 callbacks for channels that ran out - stopping whichever report
243 "no more" */
244 struct mixer_channel *chan = *chan_p;
245 chan->start += chan->last_size;
246 chan->size -= chan->last_size;
248 if (chan->size == 0)
250 if (chan->get_more)
252 chan->get_more(&chan->start, &chan->size);
253 ALIGN_CHANNEL(chan->start, chan->size);
256 if (!(chan->start && chan->size))
258 /* Channel is stopping */
259 channel_stopped(chan);
260 continue;
264 /* Channel will play for at least part of this frame */
266 /* Channel with least amount of data remaining determines the downmix
267 size */
268 if (chan->size < mixsize)
269 mixsize = chan->size;
271 chan_p++;
274 /* Add all still-active channels to the downmix */
275 chan_p = active_channels;
277 if (LIKELY(*chan_p))
279 struct mixer_channel *chan = *chan_p++;
281 if (LIKELY(!*chan_p))
283 write_samples(mixptr, (void *)chan->start,
284 chan->amplitude, mixsize);
286 else
288 void *src0, *src1;
289 unsigned int amp0, amp1;
291 /* Mix first two channels with each other as the downmix */
292 src0 = chan->start;
293 amp0 = chan->amplitude;
294 chan->last_size = mixsize;
296 chan = *chan_p++;
297 src1 = chan->start;
298 amp1 = chan->amplitude;
300 while (1)
302 mix_samples(mixptr, src0, amp0, src1, amp1, mixsize);
304 if (!*chan_p)
305 break;
307 /* More channels to mix - mix each with existing downmix */
308 chan->last_size = mixsize;
309 chan = *chan_p++;
310 src0 = mixptr;
311 amp0 = MIX_AMP_UNITY;
312 src1 = chan->start;
313 amp1 = chan->amplitude;
317 chan->last_size = mixsize;
318 next_size += mixsize;
320 if (next_size < MIX_FRAME_SIZE)
322 /* There is still space remaining in this frame */
323 mixptr += mixsize;
324 mixsize = MIX_FRAME_SIZE - next_size;
325 goto fill_frame;
328 else if (idle_counter++ < MAX_IDLE_FRAMES)
330 /* Pad incomplete frames with silence */
331 if (idle_counter <= 3)
332 memset(mixptr, 0, MIX_FRAME_SIZE - next_size);
334 next_size = MIX_FRAME_SIZE;
336 /* else silence period ran out - go to sleep */
338 #if FRAME_BOUNDARY_MARKERS != 0
339 if (next_size)
340 *downmix_buf[downmix_index] = downmix_index ? 0x7fff7fff : 0x80008000;
341 #endif
344 /* Start PCM driver if it's not currently playing */
345 static void mixer_start_pcm(void)
347 if (pcm_is_playing())
348 return;
350 #if defined(HAVE_RECORDING)
351 if (pcm_is_recording())
352 return;
353 #endif
355 /* Requires a shared global sample rate for all channels */
356 pcm_set_frequency(NATIVE_FREQUENCY);
358 /* Prepare initial frames and set up the double buffer */
359 mixer_buffer_callback();
361 /* Save the previous call's output */
362 void *start = downmix_buf[downmix_index];
364 mixer_buffer_callback();
366 pcm_play_set_dma_started_callback(mixer_buffer_callback);
367 pcm_play_data(mixer_pcm_callback, start, MIX_FRAME_SIZE);
370 /* Initialize the channel and start it if it has data */
371 static void mixer_channel_play_start(struct mixer_channel *chan,
372 pcm_play_callback_type get_more,
373 unsigned char *start, size_t size)
375 pcm_play_unlock(); /* Allow playback while doing any callback */
377 ALIGN_CHANNEL(start, size);
379 if (!(start && size))
381 /* Initial buffer not passed - call the callback now */
382 size = 0;
383 if (get_more)
385 get_more(&start, &size);
386 ALIGN_CHANNEL(start, size);
390 if (start && size)
392 /* We have data - start the channel */
393 chan->status = CHANNEL_PLAYING;
394 chan->start = start;
395 chan->size = size;
396 chan->last_size = 0;
397 chan->get_more = get_more;
399 pcm_play_lock();
400 mixer_activate_channel(chan);
401 mixer_start_pcm();
403 else
405 /* Never had anything - stop it now */
406 pcm_play_lock();
407 channel_stopped(chan);
412 /** Public interfaces **/
414 /* Start playback on a channel */
415 void mixer_channel_play_data(enum pcm_mixer_channel channel,
416 pcm_play_callback_type get_more,
417 unsigned char *start, size_t size)
419 struct mixer_channel *chan = &channels[channel];
421 pcm_play_lock();
422 mixer_deactivate_channel(chan);
423 mixer_channel_play_start(chan, get_more, start, size);
424 pcm_play_unlock();
427 /* Pause or resume a channel (when started) */
428 void mixer_channel_play_pause(enum pcm_mixer_channel channel, bool play)
430 struct mixer_channel *chan = &channels[channel];
432 pcm_play_lock();
434 if (play == (chan->status == CHANNEL_PAUSED) &&
435 chan->status != CHANNEL_STOPPED)
437 if (play)
439 chan->status = CHANNEL_PLAYING;
440 mixer_activate_channel(chan);
441 mixer_start_pcm();
443 else
445 mixer_deactivate_channel(chan);
446 chan->status = CHANNEL_PAUSED;
450 pcm_play_unlock();
453 /* Stop playback on a channel */
454 void mixer_channel_stop(enum pcm_mixer_channel channel)
456 struct mixer_channel *chan = &channels[channel];
458 pcm_play_lock();
459 channel_stopped(chan);
460 pcm_play_unlock();
463 /* Set channel's amplitude factor */
464 void mixer_channel_set_amplitude(enum pcm_mixer_channel channel,
465 unsigned int amplitude)
467 channels[channel].amplitude = MIN(amplitude, MIX_AMP_UNITY);
470 /* Return channel's playback status */
471 enum channel_status mixer_channel_status(enum pcm_mixer_channel channel)
473 return channels[channel].status;
476 /* Returns amount data remaining in channel before next callback */
477 size_t mixer_channel_get_bytes_waiting(enum pcm_mixer_channel channel)
479 return channels[channel].size;
482 /* Return pointer to channel's playing audio data and the size remaining */
483 void * mixer_channel_get_buffer(enum pcm_mixer_channel channel, int *count)
485 struct mixer_channel *chan = &channels[channel];
486 void * buf = *(unsigned char * volatile *)&chan->start;
487 size_t size = *(size_t volatile *)&chan->size;
488 void * buf2 = *(unsigned char * volatile *)&chan->start;
490 /* Still same buffer? */
491 if (buf == buf2)
493 *count = size >> 2;
494 return buf;
496 /* else can't be sure buf and size are related */
498 *count = 0;
499 return NULL;
502 /* Calculate peak values for channel */
503 void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel,
504 int *left, int *right)
506 struct mixer_channel *chan = &channels[channel];
507 struct pcm_peaks *peaks = &channel_peaks[channel];
508 int count;
509 const void *addr = mixer_channel_get_buffer(channel, &count);
511 pcm_do_peak_calculation(peaks, chan->status == CHANNEL_PLAYING,
512 addr, count);
514 if (left)
515 *left = peaks->val[0];
517 if (right)
518 *right = peaks->val[1];
521 /* Stop ALL channels and PCM and reset state */
522 void mixer_reset(void)
524 pcm_play_stop();
526 while (*active_channels)
527 channel_stopped(*active_channels);
529 idle_counter = 0;