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"
28 #include "dsp_core.h" /* For NATIVE_FREQUENCY */
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 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 /* Packed pointer array of all playing (active) channels in "channels" array */
64 static struct mixer_channel
* active_channels
[PCM_MIXER_NUM_CHANNELS
+1] IBSS_ATTR
;
66 /* Number of silence frames to play after all data has played */
67 #define MAX_IDLE_FRAMES (NATIVE_FREQUENCY*3 / MIX_FRAME_SAMPLES)
68 static unsigned int idle_counter
= 0;
70 /** Mixing routines, CPU optmized **/
71 #include "asm/pcm-mixer.c"
73 /** Private generic routines **/
75 /* Mark channel active to mix its data */
76 static void mixer_activate_channel(struct mixer_channel
*chan
)
78 void **elem
= find_array_ptr((void **)active_channels
, chan
);
87 /* Stop channel from mixing */
88 static void mixer_deactivate_channel(struct mixer_channel
*chan
)
90 remove_array_ptr((void **)active_channels
, chan
);
93 /* Deactivate channel and change it to stopped state */
94 static void channel_stopped(struct mixer_channel
*chan
)
96 mixer_deactivate_channel(chan
);
99 chan
->status
= CHANNEL_STOPPED
;
102 /* Main PCM callback - sends the current prepared frame to play */
103 static void mixer_pcm_callback(const void **addr
, size_t *size
)
105 *addr
= downmix_buf
[downmix_index
];
109 /* Buffering callback - calls sub-callbacks and mixes the data for next
110 buffer to be sent from mixer_pcm_callback() */
111 static enum pcm_dma_status MIXER_CALLBACK_ICODE
112 mixer_buffer_callback(enum pcm_dma_status status
)
114 if (status
!= PCM_DMAST_STARTED
)
117 downmix_index
^= 1; /* Next buffer */
119 void *mixptr
= downmix_buf
[downmix_index
];
120 size_t mixsize
= MIX_FRAME_SIZE
;
121 struct mixer_channel
**chan_p
;
125 /* "Loop" back here if one round wasn't enough to fill a frame */
127 chan_p
= active_channels
;
131 /* Find the active channel with the least data remaining and call any
132 callbacks for channels that ran out - stopping whichever report
134 struct mixer_channel
*chan
= *chan_p
;
135 chan
->start
+= chan
->last_size
;
136 chan
->size
-= chan
->last_size
;
142 chan
->get_more(&chan
->start
, &chan
->size
);
143 ALIGN_AUDIOBUF(chan
->start
, chan
->size
);
146 if (!(chan
->start
&& chan
->size
))
148 /* Channel is stopping */
149 channel_stopped(chan
);
154 /* Channel will play for at least part of this frame */
156 /* Channel with least amount of data remaining determines the downmix
158 if (chan
->size
< mixsize
)
159 mixsize
= chan
->size
;
164 /* Add all still-active channels to the downmix */
165 chan_p
= active_channels
;
169 struct mixer_channel
*chan
= *chan_p
++;
171 if (LIKELY(!*chan_p
))
173 write_samples(mixptr
, chan
->start
, chan
->amplitude
, mixsize
);
177 const void *src0
, *src1
;
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;
232 /* Certain SoC's have to do cleanup */
233 mixer_buffer_callback_exit();
238 /* Start PCM driver if it's not currently playing */
239 static void mixer_start_pcm(void)
241 if (pcm_is_playing())
244 #if defined(HAVE_RECORDING)
245 if (pcm_is_recording())
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 */
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 */
285 get_more(&start
, &size
);
286 ALIGN_AUDIOBUF(start
, size
);
293 /* We have data - start the channel */
294 chan
->status
= CHANNEL_PLAYING
;
298 chan
->get_more
= get_more
;
300 mixer_activate_channel(chan
);
305 /* Never had anything - stop it now */
306 channel_stopped(chan
);
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
];
319 if (play
== (chan
->status
== CHANNEL_PAUSED
) &&
320 chan
->status
!= CHANNEL_STOPPED
)
324 chan
->status
= CHANNEL_PLAYING
;
325 mixer_activate_channel(chan
);
330 mixer_deactivate_channel(chan
);
331 chan
->status
= CHANNEL_PAUSED
;
338 /* Stop playback on a channel */
339 void mixer_channel_stop(enum pcm_mixer_channel channel
)
341 struct mixer_channel
*chan
= &channels
[channel
];
344 channel_stopped(chan
);
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? */
381 /* else can't be sure buf and size are related */
387 /* Calculate peak values for channel */
388 void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel
,
389 struct pcm_peaks
*peaks
)
392 const void *addr
= mixer_channel_get_buffer(channel
, &count
);
394 pcm_do_peak_calculation(peaks
,
395 channels
[channel
].status
== CHANNEL_PLAYING
,
399 /* Adjust channel pointer by a given offset to support movable buffers */
400 void mixer_adjust_channel_address(enum pcm_mixer_channel channel
,
404 /* Makes no difference if it's stopped */
405 channels
[channel
].start
+= offset
;
409 /* Stop ALL channels and PCM and reset state */
410 void mixer_reset(void)
414 while (*active_channels
)
415 channel_stopped(*active_channels
);