1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
26 #include "pcm-internal.h"
27 #include "pcm_mixer.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 */
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 /** 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
);
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
);
102 chan
->status
= CHANNEL_STOPPED
;
105 /* Main PCM callback - sends the current prepared frame to play */
106 static void mixer_pcm_callback(unsigned char **start
, size_t *size
)
108 *start
= (unsigned char *)downmix_buf
[downmix_index
];
112 /* Buffering callback - calls sub-callbacks and mixes the data for next
113 buffer to be sent from mixer_pcm_callback() */
114 static void MIXER_CALLBACK_ICODE
mixer_buffer_callback(void)
116 downmix_index
^= 1; /* Next buffer */
118 void *mixptr
= downmix_buf
[downmix_index
];
119 size_t mixsize
= MIX_FRAME_SIZE
;
120 struct mixer_channel
**chan_p
;
124 /* "Loop" back here if one round wasn't enough to fill a frame */
126 chan_p
= active_channels
;
130 /* Find the active channel with the least data remaining and call any
131 callbacks for channels that ran out - stopping whichever report
133 struct mixer_channel
*chan
= *chan_p
;
134 chan
->start
+= chan
->last_size
;
135 chan
->size
-= chan
->last_size
;
141 chan
->get_more(&chan
->start
, &chan
->size
);
142 ALIGN_AUDIOBUF(chan
->start
, chan
->size
);
145 if (!(chan
->start
&& chan
->size
))
147 /* Channel is stopping */
148 channel_stopped(chan
);
153 /* Channel will play for at least part of this frame */
155 /* Channel with least amount of data remaining determines the downmix
157 if (chan
->size
< mixsize
)
158 mixsize
= chan
->size
;
163 /* Add all still-active channels to the downmix */
164 chan_p
= active_channels
;
168 struct mixer_channel
*chan
= *chan_p
++;
170 if (LIKELY(!*chan_p
))
172 write_samples(mixptr
, (void *)chan
->start
,
173 chan
->amplitude
, mixsize
);
178 unsigned int amp0
, amp1
;
180 /* Mix first two channels with each other as the downmix */
182 amp0
= chan
->amplitude
;
183 chan
->last_size
= mixsize
;
187 amp1
= chan
->amplitude
;
191 mix_samples(mixptr
, src0
, amp0
, src1
, amp1
, mixsize
);
196 /* More channels to mix - mix each with existing downmix */
197 chan
->last_size
= mixsize
;
200 amp0
= MIX_AMP_UNITY
;
202 amp1
= chan
->amplitude
;
206 chan
->last_size
= mixsize
;
207 next_size
+= mixsize
;
209 if (next_size
< MIX_FRAME_SIZE
)
211 /* There is still space remaining in this frame */
213 mixsize
= MIX_FRAME_SIZE
- next_size
;
217 else if (idle_counter
++ < MAX_IDLE_FRAMES
)
219 /* Pad incomplete frames with silence */
220 if (idle_counter
<= 3)
221 memset(mixptr
, 0, MIX_FRAME_SIZE
- next_size
);
223 next_size
= MIX_FRAME_SIZE
;
225 /* else silence period ran out - go to sleep */
227 #if FRAME_BOUNDARY_MARKERS != 0
229 *downmix_buf
[downmix_index
] = downmix_index
? 0x7fff7fff : 0x80008000;
233 /* Start PCM driver if it's not currently playing */
234 static void mixer_start_pcm(void)
236 if (pcm_is_playing())
239 #if defined(HAVE_RECORDING)
240 if (pcm_is_recording())
244 /* Requires a shared global sample rate for all channels */
245 pcm_set_frequency(NATIVE_FREQUENCY
);
247 /* Prepare initial frames and set up the double buffer */
248 mixer_buffer_callback();
250 /* Save the previous call's output */
251 void *start
= downmix_buf
[downmix_index
];
253 mixer_buffer_callback();
255 pcm_play_set_dma_started_callback(mixer_buffer_callback
);
256 pcm_play_data(mixer_pcm_callback
, start
, MIX_FRAME_SIZE
);
259 /* Initialize the channel and start it if it has data */
260 static void mixer_channel_play_start(struct mixer_channel
*chan
,
261 pcm_play_callback_type get_more
,
262 unsigned char *start
, size_t size
)
264 pcm_play_unlock(); /* Allow playback while doing any callback */
266 ALIGN_AUDIOBUF(start
, size
);
268 if (!(start
&& size
))
270 /* Initial buffer not passed - call the callback now */
274 get_more(&start
, &size
);
275 ALIGN_AUDIOBUF(start
, size
);
281 /* We have data - start the channel */
282 chan
->status
= CHANNEL_PLAYING
;
286 chan
->get_more
= get_more
;
289 mixer_activate_channel(chan
);
294 /* Never had anything - stop it now */
296 channel_stopped(chan
);
301 /** Public interfaces **/
303 /* Start playback on a channel */
304 void mixer_channel_play_data(enum pcm_mixer_channel channel
,
305 pcm_play_callback_type get_more
,
306 unsigned char *start
, size_t size
)
308 struct mixer_channel
*chan
= &channels
[channel
];
311 mixer_deactivate_channel(chan
);
312 mixer_channel_play_start(chan
, get_more
, start
, size
);
316 /* Pause or resume a channel (when started) */
317 void mixer_channel_play_pause(enum pcm_mixer_channel channel
, bool play
)
319 struct mixer_channel
*chan
= &channels
[channel
];
323 if (play
== (chan
->status
== CHANNEL_PAUSED
) &&
324 chan
->status
!= CHANNEL_STOPPED
)
328 chan
->status
= CHANNEL_PLAYING
;
329 mixer_activate_channel(chan
);
334 mixer_deactivate_channel(chan
);
335 chan
->status
= CHANNEL_PAUSED
;
342 /* Stop playback on a channel */
343 void mixer_channel_stop(enum pcm_mixer_channel channel
)
345 struct mixer_channel
*chan
= &channels
[channel
];
348 channel_stopped(chan
);
352 /* Set channel's amplitude factor */
353 void mixer_channel_set_amplitude(enum pcm_mixer_channel channel
,
354 unsigned int amplitude
)
356 channels
[channel
].amplitude
= MIN(amplitude
, MIX_AMP_UNITY
);
359 /* Return channel's playback status */
360 enum channel_status
mixer_channel_status(enum pcm_mixer_channel channel
)
362 return channels
[channel
].status
;
365 /* Returns amount data remaining in channel before next callback */
366 size_t mixer_channel_get_bytes_waiting(enum pcm_mixer_channel channel
)
368 return channels
[channel
].size
;
371 /* Return pointer to channel's playing audio data and the size remaining */
372 void * mixer_channel_get_buffer(enum pcm_mixer_channel channel
, int *count
)
374 struct mixer_channel
*chan
= &channels
[channel
];
375 void * buf
= *(unsigned char * volatile *)&chan
->start
;
376 size_t size
= *(size_t volatile *)&chan
->size
;
377 void * buf2
= *(unsigned char * volatile *)&chan
->start
;
379 /* Still same buffer? */
385 /* else can't be sure buf and size are related */
391 /* Calculate peak values for channel */
392 void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel
,
393 int *left
, int *right
)
395 struct mixer_channel
*chan
= &channels
[channel
];
396 struct pcm_peaks
*peaks
= &channel_peaks
[channel
];
398 const void *addr
= mixer_channel_get_buffer(channel
, &count
);
400 pcm_do_peak_calculation(peaks
, chan
->status
== CHANNEL_PLAYING
,
404 *left
= peaks
->val
[0];
407 *right
= peaks
->val
[1];
410 /* Stop ALL channels and PCM and reset state */
411 void mixer_reset(void)
415 while (*active_channels
)
416 channel_stopped(*active_channels
);