Fix b&w LCD targets.
[maemo-rb.git] / apps / dsp.c
blob4ea2a0c3c4826bd8a5c474f2fd95b98769be49d3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <inttypes.h>
20 #include <string.h>
21 #include <sound.h>
22 #include "dsp.h"
23 #include "eq.h"
24 #include "kernel.h"
25 #include "playback.h"
26 #include "system.h"
27 #include "settings.h"
28 #include "replaygain.h"
29 #include "debug.h"
31 #ifndef SIMULATOR
32 #include <dsp_asm.h>
33 #endif
35 /* The "dither" code to convert the 24-bit samples produced by libmad was
36 * taken from the coolplayer project - coolplayer.sourceforge.net
39 /* 16-bit samples are scaled based on these constants. The shift should be
40 * no more than 15.
42 #define WORD_SHIFT 12
43 #define WORD_FRACBITS 27
45 #define NATIVE_DEPTH 16
46 #define SAMPLE_BUF_SIZE 256
47 #define RESAMPLE_BUF_SIZE (256 * 4) /* Enough for 11,025 Hz -> 44,100 Hz*/
48 #define DEFAULT_GAIN 0x01000000
50 #if defined(CPU_COLDFIRE) && !defined(SIMULATOR)
52 /* Multiply two S.31 fractional integers and return the sign bit and the
53 * 31 most significant bits of the result.
55 #define FRACMUL(x, y) \
56 ({ \
57 long t; \
58 asm volatile ("mac.l %[a], %[b], %%acc0\n\t" \
59 "movclr.l %%acc0, %[t]\n\t" \
60 : [t] "=r" (t) : [a] "r" (x), [b] "r" (y)); \
61 t; \
64 /* Multiply one S.31-bit and one S8.23 fractional integer and return the
65 * sign bit and the 31 most significant bits of the result.
67 #define FRACMUL_8(x, y) \
68 ({ \
69 long t; \
70 long u; \
71 asm volatile ("mac.l %[a], %[b], %%acc0\n\t" \
72 "move.l %%accext01, %[u]\n\t" \
73 "movclr.l %%acc0, %[t]\n\t" \
74 : [t] "=r" (t), [u] "=r" (u) : [a] "r" (x), [b] "r" (y)); \
75 (t << 8) | (u & 0xff); \
78 /* Multiply one S.31-bit and one S8.23 fractional integer and return the
79 * sign bit and the 31 most significant bits of the result. Load next value
80 * to multiply with into x from s (and increase s); x must contain the
81 * initial value.
83 #define FRACMUL_8_LOOP_PART(x, s, d, y) \
84 { \
85 long u; \
86 asm volatile ("mac.l %[a], %[b], (%[c])+, %[a], %%acc0\n\t" \
87 "move.l %%accext01, %[u]\n\t" \
88 "movclr.l %%acc0, %[t]" \
89 : [a] "+r" (x), [c] "+a" (s), [t] "=r" (d), [u] "=r" (u) \
90 : [b] "r" (y)); \
91 d = (d << 8) | (u & 0xff); \
94 #define FRACMUL_8_LOOP(x, y, s, d) \
95 { \
96 long t; \
97 FRACMUL_8_LOOP_PART(x, s, t, y); \
98 asm volatile ("move.l %[t],(%[d])+" \
99 : [d] "+a" (d)\
100 : [t] "r" (t)); \
103 #define ACC(acc, x, y) \
104 (void)acc; \
105 asm volatile ("mac.l %[a], %[b], %%acc0" \
106 : : [a] "i,r" (x), [b] "i,r" (y));
107 #define GET_ACC(acc) \
108 ({ \
109 long t; \
110 (void)acc; \
111 asm volatile ("movclr.l %%acc0, %[t]" \
112 : [t] "=r" (t)); \
113 t; \
116 #define ACC_INIT(acc, x, y) ACC(acc, x, y)
118 #elif defined(CPU_ARM) && !defined(SIMULATOR)
120 /* Multiply two S.31 fractional integers and return the sign bit and the
121 * 31 most significant bits of the result.
123 #define FRACMUL(x, y) \
124 ({ \
125 long t; \
126 asm volatile ("smull r0, r1, %[a], %[b]\n\t" \
127 "mov %[t], r1, asl #1\n\t" \
128 "orr %[t], %[t], r0, lsr #31\n\t" \
129 : [t] "=r" (t) : [a] "r" (x), [b] "r" (y) : "r0", "r1"); \
130 t; \
133 #define ACC_INIT(acc, x, y) acc = FRACMUL(x, y)
134 #define ACC(acc, x, y) acc += FRACMUL(x, y)
135 #define GET_ACC(acc) acc
137 /* Multiply one S.31-bit and one S8.23 fractional integer and store the
138 * sign bit and the 31 most significant bits of the result to d (and
139 * increase d). Load next value to multiply with into x from s (and
140 * increase s); x must contain the initial value.
142 #define FRACMUL_8_LOOP(x, y, s, d) \
143 ({ \
144 asm volatile ("smull r0, r1, %[a], %[b]\n\t" \
145 "mov %[t], r1, asl #9\n\t" \
146 "orr %[t], %[t], r0, lsr #23\n\t" \
147 : [t] "=r" (*(d)++) : [a] "r" (x), [b] "r" (y) : "r0", "r1"); \
148 x = *(s)++; \
151 #else
153 #define ACC_INIT(acc, x, y) acc = FRACMUL(x, y)
154 #define ACC(acc, x, y) acc += FRACMUL(x, y)
155 #define GET_ACC(acc) acc
156 #define FRACMUL(x, y) (long) (((((long long) (x)) * ((long long) (y))) >> 31))
157 #define FRACMUL_8(x, y) (long) (((((long long) (x)) * ((long long) (y))) >> 23))
158 #define FRACMUL_8_LOOP(x, y, s, d) \
159 ({ \
160 long t = x; \
161 x = *(s)++; \
162 *(d)++ = (long) (((((long long) (t)) * ((long long) (y))) >> 23)); \
165 #endif
167 struct dsp_config
169 long codec_frequency; /* Sample rate of data coming from the codec */
170 long frequency; /* Effective sample rate after pitch shift (if any) */
171 long clip_min;
172 long clip_max;
173 long track_gain;
174 long album_gain;
175 long track_peak;
176 long album_peak;
177 long replaygain;
178 int sample_depth;
179 int sample_bytes;
180 int stereo_mode;
181 int frac_bits;
182 bool dither_enabled;
183 bool new_gain;
184 bool crossfeed_enabled;
185 bool eq_enabled;
186 long eq_precut;
187 long gain; /* Note that this is in S8.23 format. */
190 struct resample_data
192 long phase, delta;
193 int32_t last_sample[2];
196 struct dither_data
198 long error[3];
199 long random;
202 struct crossfeed_data
204 int32_t gain; /* Direct path gain */
205 int32_t coefs[3]; /* Coefficients for the shelving filter */
206 int32_t history[4]; /* Format is x[n - 1], y[n - 1] for both channels */
207 int32_t delay[13][2];
208 int index; /* Current index into the delay line */
211 /* Current setup is one lowshelf filters, three peaking filters and one
212 highshelf filter. Varying the number of shelving filters make no sense,
213 but adding peaking filters is possible. */
214 struct eq_state {
215 char enabled[5]; /* Flags for active filters */
216 struct eqfilter filters[5];
219 static struct dsp_config dsp_conf[2] IBSS_ATTR;
220 static struct dither_data dither_data[2] IBSS_ATTR;
221 static struct resample_data resample_data[2] IBSS_ATTR;
222 struct crossfeed_data crossfeed_data IBSS_ATTR;
223 static struct eq_state eq_data;
225 static int pitch_ratio = 1000;
226 static int channels_mode = 0;
227 static int32_t sw_gain, sw_cross;
229 extern int current_codec;
230 struct dsp_config *dsp;
232 /* The internal format is 32-bit samples, non-interleaved, stereo. This
233 * format is similar to the raw output from several codecs, so the amount
234 * of copying needed is minimized for that case.
237 static int32_t sample_buf[SAMPLE_BUF_SIZE] IBSS_ATTR;
238 static int32_t resample_buf[RESAMPLE_BUF_SIZE] IBSS_ATTR;
240 int sound_get_pitch(void)
242 return pitch_ratio;
245 void sound_set_pitch(int permille)
247 pitch_ratio = permille;
249 dsp_configure(DSP_SWITCH_FREQUENCY, (int *)dsp->codec_frequency);
252 /* Convert at most count samples to the internal format, if needed. Returns
253 * number of samples ready for further processing. Updates src to point
254 * past the samples "consumed" and dst is set to point to the samples to
255 * consume. Note that for mono, dst[0] equals dst[1], as there is no point
256 * in processing the same data twice.
258 static int convert_to_internal(const char* src[], int count, int32_t* dst[])
260 count = MIN(SAMPLE_BUF_SIZE / 2, count);
262 if ((dsp->sample_depth <= NATIVE_DEPTH)
263 || (dsp->stereo_mode == STEREO_INTERLEAVED))
265 dst[0] = &sample_buf[0];
266 dst[1] = (dsp->stereo_mode == STEREO_MONO)
267 ? dst[0] : &sample_buf[SAMPLE_BUF_SIZE / 2];
269 else
271 dst[0] = (int32_t*) src[0];
272 dst[1] = (int32_t*) ((dsp->stereo_mode == STEREO_MONO) ? src[0] : src[1]);
275 if (dsp->sample_depth <= NATIVE_DEPTH)
277 short* s0 = (short*) src[0];
278 int32_t* d0 = dst[0];
279 int32_t* d1 = dst[1];
280 int scale = WORD_SHIFT;
281 int i;
283 if (dsp->stereo_mode == STEREO_INTERLEAVED)
285 for (i = 0; i < count; i++)
287 *d0++ = *s0++ << scale;
288 *d1++ = *s0++ << scale;
291 else if (dsp->stereo_mode == STEREO_NONINTERLEAVED)
293 short* s1 = (short*) src[1];
295 for (i = 0; i < count; i++)
297 *d0++ = *s0++ << scale;
298 *d1++ = *s1++ << scale;
301 else
303 for (i = 0; i < count; i++)
305 *d0++ = *s0++ << scale;
309 else if (dsp->stereo_mode == STEREO_INTERLEAVED)
311 int32_t* s0 = (int32_t*) src[0];
312 int32_t* d0 = dst[0];
313 int32_t* d1 = dst[1];
314 int i;
316 for (i = 0; i < count; i++)
318 *d0++ = *s0++;
319 *d1++ = *s0++;
323 if (dsp->stereo_mode == STEREO_NONINTERLEAVED)
325 src[0] += count * dsp->sample_bytes;
326 src[1] += count * dsp->sample_bytes;
328 else if (dsp->stereo_mode == STEREO_INTERLEAVED)
330 src[0] += count * dsp->sample_bytes * 2;
332 else
334 src[0] += count * dsp->sample_bytes;
337 return count;
340 static void resampler_set_delta(int frequency)
342 resample_data[current_codec].delta = (unsigned long)
343 frequency * 65536LL / NATIVE_FREQUENCY;
346 /* Linear interpolation resampling that introduces a one sample delay because
347 * of our inability to look into the future at the end of a frame.
350 /* TODO: we really should have a separate set of resample functions for both
351 mono and stereo to avoid all this internal branching and looping. */
352 static long downsample(int32_t **dst, int32_t **src, int count,
353 struct resample_data *r)
355 long phase = r->phase;
356 long delta = r->delta;
357 int32_t last_sample;
358 int32_t *d[2] = { dst[0], dst[1] };
359 int pos = phase >> 16;
360 int i = 1, j;
361 int num_channels = dsp->stereo_mode == STEREO_MONO ? 1 : 2;
363 for (j = 0; j < num_channels; j++) {
364 last_sample = r->last_sample[j];
365 /* Do we need last sample of previous frame for interpolation? */
366 if (pos > 0)
368 last_sample = src[j][pos - 1];
370 *d[j]++ = last_sample + FRACMUL((phase & 0xffff) << 15,
371 src[j][pos] - last_sample);
373 phase += delta;
375 while ((pos = phase >> 16) < count)
377 for (j = 0; j < num_channels; j++)
378 *d[j]++ = src[j][pos - 1] + FRACMUL((phase & 0xffff) << 15,
379 src[j][pos] - src[j][pos - 1]);
380 phase += delta;
381 i++;
384 /* Wrap phase accumulator back to start of next frame. */
385 r->phase = phase - (count << 16);
386 r->delta = delta;
387 r->last_sample[0] = src[0][count - 1];
388 r->last_sample[1] = src[1][count - 1];
389 return i;
392 static long upsample(int32_t **dst, int32_t **src, int count, struct resample_data *r)
394 long phase = r->phase;
395 long delta = r->delta;
396 int32_t *d[2] = { dst[0], dst[1] };
397 int i = 0, j;
398 int pos;
399 int num_channels = dsp->stereo_mode == STEREO_MONO ? 1 : 2;
401 while ((pos = phase >> 16) == 0)
403 for (j = 0; j < num_channels; j++)
404 *d[j]++ = r->last_sample[j] + FRACMUL((phase & 0xffff) << 15,
405 src[j][pos] - r->last_sample[j]);
406 phase += delta;
407 i++;
410 while ((pos = phase >> 16) < count)
412 for (j = 0; j < num_channels; j++)
413 *d[j]++ = src[j][pos - 1] + FRACMUL((phase & 0xffff) << 15,
414 src[j][pos] - src[j][pos - 1]);
415 phase += delta;
416 i++;
419 /* Wrap phase accumulator back to start of next frame. */
420 r->phase = phase - (count << 16);
421 r->delta = delta;
422 r->last_sample[0] = src[0][count - 1];
423 r->last_sample[1] = src[1][count - 1];
424 return i;
427 /* Resample count stereo samples. Updates the src array, if resampling is
428 * done, to refer to the resampled data. Returns number of stereo samples
429 * for further processing.
431 static inline int resample(int32_t* src[], int count)
433 long new_count;
435 if (dsp->frequency != NATIVE_FREQUENCY)
437 int32_t* dst[2] = {&resample_buf[0], &resample_buf[RESAMPLE_BUF_SIZE / 2]};
439 if (dsp->frequency < NATIVE_FREQUENCY)
441 new_count = upsample(dst, src, count,
442 &resample_data[current_codec]);
444 else
446 new_count = downsample(dst, src, count,
447 &resample_data[current_codec]);
450 src[0] = dst[0];
451 if (dsp->stereo_mode != STEREO_MONO)
452 src[1] = dst[1];
453 else
454 src[1] = dst[0];
456 else
458 new_count = count;
461 return new_count;
464 static inline long clip_sample(int32_t sample, int32_t min, int32_t max)
466 if (sample > max)
468 sample = max;
470 else if (sample < min)
472 sample = min;
475 return sample;
478 /* The "dither" code to convert the 24-bit samples produced by libmad was
479 * taken from the coolplayer project - coolplayer.sourceforge.net
482 static long dither_sample(int32_t sample, int32_t bias, int32_t mask,
483 struct dither_data* dither)
485 int32_t output;
486 int32_t random;
487 int32_t min;
488 int32_t max;
490 /* Noise shape and bias */
492 sample += dither->error[0] - dither->error[1] + dither->error[2];
493 dither->error[2] = dither->error[1];
494 dither->error[1] = dither->error[0] / 2;
496 output = sample + bias;
498 /* Dither */
500 random = dither->random * 0x0019660dL + 0x3c6ef35fL;
501 sample += (random & mask) - (dither->random & mask);
502 dither->random = random;
504 /* Clip and quantize */
506 min = dsp->clip_min;
507 max = dsp->clip_max;
508 sample = clip_sample(sample, min, max);
509 output = clip_sample(output, min, max) & ~mask;
511 /* Error feedback */
513 dither->error[0] = sample - output;
515 return output;
518 void dsp_set_crossfeed(bool enable)
520 dsp->crossfeed_enabled = enable;
523 void dsp_set_crossfeed_direct_gain(int gain)
525 /* Work around bug in get_replaygain_int which returns 0 for 0 dB */
526 if (gain == 0)
527 crossfeed_data.gain = 0x7fffffff;
528 else
529 crossfeed_data.gain = get_replaygain_int(gain * -10) << 7;
532 void dsp_set_crossfeed_cross_params(long lf_gain, long hf_gain, long cutoff)
534 long g1 = get_replaygain_int(lf_gain * -10) << 3;
535 long g2 = get_replaygain_int(hf_gain * -10) << 3;
537 filter_bishelf_coefs(0xffffffff/NATIVE_FREQUENCY*cutoff, g1, g2,
538 crossfeed_data.coefs);
541 /* Applies crossfeed to the stereo signal in src.
542 * Crossfeed is a process where listening over speakers is simulated. This
543 * is good for old hard panned stereo records, which might be quite fatiguing
544 * to listen to on headphones with no crossfeed.
546 #ifndef DSP_HAVE_ASM_CROSSFEED
547 void apply_crossfeed(int32_t* src[], int count)
549 int32_t *hist_l = &crossfeed_data.history[0];
550 int32_t *hist_r = &crossfeed_data.history[2];
551 int32_t *delay = &crossfeed_data.delay[0][0];
552 int32_t *coefs = &crossfeed_data.coefs[0];
553 int32_t gain = crossfeed_data.gain;
554 int di = crossfeed_data.index;
556 int32_t acc;
557 int32_t left, right;
558 int i;
560 for (i = 0; i < count; i++) {
561 left = src[0][i];
562 right = src[1][i];
564 /* Filter delayed sample from left speaker */
565 ACC_INIT(acc, delay[di*2], coefs[0]);
566 ACC(acc, hist_l[0], coefs[1]);
567 ACC(acc, hist_l[1], coefs[2]);
568 /* Save filter history for left speaker */
569 hist_l[1] = GET_ACC(acc);
570 hist_l[0] = delay[di*2];
571 /* Filter delayed sample from right speaker */
572 ACC_INIT(acc, delay[di*2 + 1], coefs[0]);
573 ACC(acc, hist_r[0], coefs[1]);
574 ACC(acc, hist_r[1], coefs[2]);
575 /* Save filter history for right speaker */
576 hist_r[1] = GET_ACC(acc);
577 hist_r[0] = delay[di*2 + 1];
578 delay[di*2] = left;
579 delay[di*2 + 1] = right;
580 /* Now add the attenuated direct sound and write to outputs */
581 src[0][i] = FRACMUL(left, gain) + hist_r[1];
582 src[1][i] = FRACMUL(right, gain) + hist_l[1];
584 /* Wrap delay line index if bigger than delay line size */
585 if (++di > 12)
586 di = 0;
588 /* Write back local copies of data we've modified */
589 crossfeed_data.index = di;
591 #endif
593 /* Combine all gains to a global gain. */
594 static void set_gain(void)
596 dsp->gain = DEFAULT_GAIN;
598 if (dsp->replaygain)
600 dsp->gain = dsp->replaygain;
603 if (dsp->eq_enabled && dsp->eq_precut)
605 dsp->gain = (long) (((int64_t) dsp->gain * dsp->eq_precut) >> 24);
608 if (dsp->gain == DEFAULT_GAIN)
610 dsp->gain = 0;
612 else
614 dsp->gain >>= 1;
619 * Use to enable the equalizer.
621 * @param enable true to enable the equalizer
623 void dsp_set_eq(bool enable)
625 dsp->eq_enabled = enable;
629 * Update the amount to cut the audio before applying the equalizer.
631 * @param precut to apply in decibels (multiplied by 10)
633 void dsp_set_eq_precut(int precut)
635 dsp->eq_precut = get_replaygain_int(precut * -10);
636 set_gain();
640 * Synchronize the equalizer filter coefficients with the global settings.
642 * @param band the equalizer band to synchronize
644 void dsp_set_eq_coefs(int band)
646 const int *setting;
647 long gain;
648 unsigned long cutoff, q;
650 /* Adjust setting pointer to the band we actually want to change */
651 setting = &global_settings.eq_band0_cutoff + (band * 3);
653 /* Convert user settings to format required by coef generator functions */
654 cutoff = 0xffffffff / NATIVE_FREQUENCY * (*setting++);
655 q = ((*setting++) << 16) / 10; /* 16.16 */
656 gain = ((*setting++) << 16) / 10; /* s15.16 */
658 if (q == 0)
659 q = 1;
661 /* NOTE: The coef functions assume the EMAC unit is in fractional mode,
662 which it should be, since we're executed from the main thread. */
664 /* Assume a band is disabled if the gain is zero */
665 if (gain == 0) {
666 eq_data.enabled[band] = 0;
667 } else {
668 if (band == 0)
669 eq_ls_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
670 else if (band == 4)
671 eq_hs_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
672 else
673 eq_pk_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
675 eq_data.enabled[band] = 1;
679 /* Apply EQ filters to those bands that have got it switched on. */
680 static void eq_process(int32_t **x, unsigned num)
682 int i;
683 unsigned int channels = dsp->stereo_mode != STEREO_MONO ? 2 : 1;
684 unsigned shift;
686 /* filter configuration currently is 1 low shelf filter, 3 band peaking
687 filters and 1 high shelf filter, in that order. we need to know this
688 so we can choose the correct shift factor.
690 for (i = 0; i < 5; i++) {
691 if (eq_data.enabled[i]) {
692 if (i == 0 || i == 4) /* shelving filters */
693 shift = EQ_SHELF_SHIFT;
694 else
695 shift = EQ_PEAK_SHIFT;
696 eq_filter(x, &eq_data.filters[i], num, channels, shift);
701 /* Apply a constant gain to the samples (e.g., for ReplayGain). May update
702 * the src array if gain was applied.
703 * Note that this must be called before the resampler.
705 static void apply_gain(int32_t* _src[], int _count)
707 if (dsp->gain)
709 int32_t** src = _src;
710 int count = _count;
711 int32_t* s0 = src[0];
712 int32_t* s1 = src[1];
713 long gain = dsp->gain;
714 int32_t s;
715 int i;
716 int32_t *d;
718 if (s0 != s1)
720 d = &sample_buf[SAMPLE_BUF_SIZE / 2];
721 src[1] = d;
722 s = *s1++;
724 for (i = 0; i < count; i++)
725 FRACMUL_8_LOOP(s, gain, s1, d);
727 else
729 src[1] = &sample_buf[0];
732 d = &sample_buf[0];
733 src[0] = d;
734 s = *s0++;
736 for (i = 0; i < count; i++)
737 FRACMUL_8_LOOP(s, gain, s0, d);
741 void channels_set(int value)
743 channels_mode = value;
746 void stereo_width_set(int value)
748 long width, straight, cross;
750 width = value * 0x7fffff / 100;
751 if (value <= 100) {
752 straight = (0x7fffff + width) / 2;
753 cross = straight - width;
754 } else {
755 /* straight = (1 + width) / (2 * width) */
756 straight = ((int64_t)(0x7fffff + width) << 22) / width;
757 cross = straight - 0x7fffff;
759 sw_gain = straight << 8;
760 sw_cross = cross << 8;
763 /* Implements the different channel configurations and stereo width.
764 * We might want to combine this with the write_samples stage for efficiency,
765 * but for now we'll just let it stay as a stage of its own.
767 static void channels_process(int32_t **src, int num)
769 int i;
770 int32_t *sl = src[0], *sr = src[1];
772 if (channels_mode == SOUND_CHAN_STEREO)
773 return;
774 switch (channels_mode) {
775 case SOUND_CHAN_MONO:
776 for (i = 0; i < num; i++)
777 sl[i] = sr[i] = sl[i]/2 + sr[i]/2;
778 break;
779 case SOUND_CHAN_CUSTOM:
780 for (i = 0; i < num; i++) {
781 int32_t left_sample = sl[i];
783 sl[i] = FRACMUL(sl[i], sw_gain) + FRACMUL(sr[i], sw_cross);
784 sr[i] = FRACMUL(sr[i], sw_gain) + FRACMUL(left_sample, sw_cross);
786 break;
787 case SOUND_CHAN_MONO_LEFT:
788 for (i = 0; i < num; i++)
789 sr[i] = sl[i];
790 break;
791 case SOUND_CHAN_MONO_RIGHT:
792 for (i = 0; i < num; i++)
793 sl[i] = sr[i];
794 break;
795 case SOUND_CHAN_KARAOKE:
796 for (i = 0; i < num; i++) {
797 int32_t left_sample = sl[i]/2;
799 sl[i] = left_sample - sr[i]/2;
800 sr[i] = sr[i]/2 - left_sample;
802 break;
806 static void write_samples(short* dst, int32_t* src[], int count)
808 int32_t* s0 = src[0];
809 int32_t* s1 = src[1];
810 int scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
812 if (dsp->dither_enabled)
814 long bias = (1L << (dsp->frac_bits - NATIVE_DEPTH));
815 long mask = (1L << scale) - 1;
817 while (count-- > 0)
819 *dst++ = (short) (dither_sample(*s0++, bias, mask, &dither_data[0])
820 >> scale);
821 *dst++ = (short) (dither_sample(*s1++, bias, mask, &dither_data[1])
822 >> scale);
825 else
827 long min = dsp->clip_min;
828 long max = dsp->clip_max;
830 while (count-- > 0)
832 *dst++ = (short) (clip_sample(*s0++, min, max) >> scale);
833 *dst++ = (short) (clip_sample(*s1++, min, max) >> scale);
838 /* Process and convert src audio to dst based on the DSP configuration,
839 * reading size bytes of audio data. dst is assumed to be large enough; use
840 * dst_get_dest_size() to get the required size. src is an array of
841 * pointers; for mono and interleaved stereo, it contains one pointer to the
842 * start of the audio data; for non-interleaved stereo, it contains two
843 * pointers, one for each audio channel. Returns number of bytes written to
844 * dest.
846 long dsp_process(char* dst, const char* src[], long size)
848 int32_t* tmp[2];
849 long written = 0;
850 long factor;
851 int samples;
853 #if defined(CPU_COLDFIRE) && !defined(SIMULATOR)
854 /* set emac unit for dsp processing, and save old macsr, we're running in
855 codec thread context at this point, so can't clobber it */
856 unsigned long old_macsr = coldfire_get_macsr();
857 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
858 #endif
860 dsp = &dsp_conf[current_codec];
862 factor = (dsp->stereo_mode != STEREO_MONO) ? 2 : 1;
863 size /= dsp->sample_bytes * factor;
864 dsp_set_replaygain(false);
866 while (size > 0)
868 samples = convert_to_internal(src, size, tmp);
869 size -= samples;
870 apply_gain(tmp, samples);
871 samples = resample(tmp, samples);
872 if (dsp->crossfeed_enabled && dsp->stereo_mode != STEREO_MONO)
873 apply_crossfeed(tmp, samples);
874 if (dsp->eq_enabled)
875 eq_process(tmp, samples);
876 if (dsp->stereo_mode != STEREO_MONO)
877 channels_process(tmp, samples);
878 write_samples((short*) dst, tmp, samples);
879 written += samples;
880 dst += samples * sizeof(short) * 2;
881 yield();
883 #if defined(CPU_COLDFIRE) && !defined(SIMULATOR)
884 /* set old macsr again */
885 coldfire_set_macsr(old_macsr);
886 #endif
887 return written * sizeof(short) * 2;
890 /* Given size bytes of input data, calculate the maximum number of bytes of
891 * output data that would be generated (the calculation is not entirely
892 * exact and rounds upwards to be on the safe side; during resampling,
893 * the number of samples generated depends on the current state of the
894 * resampler).
896 /* dsp_input_size MUST be called afterwards */
897 long dsp_output_size(long size)
899 dsp = &dsp_conf[current_codec];
901 if (dsp->sample_depth > NATIVE_DEPTH)
903 size /= 2;
906 if (dsp->frequency != NATIVE_FREQUENCY)
908 size = (long) ((((unsigned long) size * NATIVE_FREQUENCY)
909 + (dsp->frequency - 1)) / dsp->frequency);
912 /* round to the next multiple of 2 (these are shorts) */
913 size = (size + 1) & ~1;
915 if (dsp->stereo_mode == STEREO_MONO)
917 size *= 2;
920 /* now we have the size in bytes for two resampled channels,
921 * and the size in (short) must not exceed RESAMPLE_BUF_SIZE to
922 * avoid resample buffer overflow. One must call dsp_input_size()
923 * to get the correct input buffer size. */
924 if (size > RESAMPLE_BUF_SIZE*2)
925 size = RESAMPLE_BUF_SIZE*2;
927 return size;
930 /* Given size bytes of output buffer, calculate number of bytes of input
931 * data that would be consumed in order to fill the output buffer.
933 long dsp_input_size(long size)
935 dsp = &dsp_conf[current_codec];
937 /* convert to number of output stereo samples. */
938 size /= 2;
940 /* Mono means we need half input samples to fill the output buffer */
941 if (dsp->stereo_mode == STEREO_MONO)
942 size /= 2;
944 /* size is now the number of resampled input samples. Convert to
945 original input samples. */
946 if (dsp->frequency != NATIVE_FREQUENCY)
948 /* Use the real resampling delta =
949 * (unsigned long) dsp->frequency * 65536 / NATIVE_FREQUENCY, and
950 * round towards zero to avoid buffer overflows. */
951 size = ((unsigned long)size *
952 resample_data[current_codec].delta) >> 16;
955 /* Convert back to bytes. */
956 if (dsp->sample_depth > NATIVE_DEPTH)
957 size *= 4;
958 else
959 size *= 2;
961 return size;
964 int dsp_stereo_mode(void)
966 dsp = &dsp_conf[current_codec];
968 return dsp->stereo_mode;
971 bool dsp_configure(int setting, void *value)
973 dsp = &dsp_conf[current_codec];
975 switch (setting)
977 case DSP_SET_FREQUENCY:
978 memset(&resample_data[current_codec], 0,
979 sizeof(struct resample_data));
980 /* Fall through!!! */
981 case DSP_SWITCH_FREQUENCY:
982 dsp->codec_frequency = ((long) value == 0) ? NATIVE_FREQUENCY : (long) value;
983 /* Account for playback speed adjustment when settingg dsp->frequency
984 if we're called from the main audio thread. Voice UI thread should
985 not need this feature.
987 if (current_codec == CODEC_IDX_AUDIO)
988 dsp->frequency = pitch_ratio * dsp->codec_frequency / 1000;
989 else
990 dsp->frequency = dsp->codec_frequency;
991 resampler_set_delta(dsp->frequency);
992 break;
994 case DSP_SET_CLIP_MIN:
995 dsp->clip_min = (long) value;
996 break;
998 case DSP_SET_CLIP_MAX:
999 dsp->clip_max = (long) value;
1000 break;
1002 case DSP_SET_SAMPLE_DEPTH:
1003 dsp->sample_depth = (long) value;
1005 if (dsp->sample_depth <= NATIVE_DEPTH)
1007 dsp->frac_bits = WORD_FRACBITS;
1008 dsp->sample_bytes = sizeof(short);
1009 dsp->clip_max = ((1 << WORD_FRACBITS) - 1);
1010 dsp->clip_min = -((1 << WORD_FRACBITS));
1012 else
1014 dsp->frac_bits = (long) value;
1015 dsp->sample_bytes = 4; /* samples are 32 bits */
1016 dsp->clip_max = (1 << (long)value) - 1;
1017 dsp->clip_min = -(1 << (long)value);
1020 break;
1022 case DSP_SET_STEREO_MODE:
1023 dsp->stereo_mode = (long) value;
1024 break;
1026 case DSP_RESET:
1027 dsp->dither_enabled = false;
1028 dsp->stereo_mode = STEREO_NONINTERLEAVED;
1029 dsp->clip_max = ((1 << WORD_FRACBITS) - 1);
1030 dsp->clip_min = -((1 << WORD_FRACBITS));
1031 dsp->track_gain = 0;
1032 dsp->album_gain = 0;
1033 dsp->track_peak = 0;
1034 dsp->album_peak = 0;
1035 dsp->codec_frequency = dsp->frequency = NATIVE_FREQUENCY;
1036 dsp->sample_depth = NATIVE_DEPTH;
1037 dsp->frac_bits = WORD_FRACBITS;
1038 dsp->new_gain = true;
1039 break;
1041 case DSP_DITHER:
1042 memset(dither_data, 0, sizeof(dither_data));
1043 dsp->dither_enabled = (bool) value;
1044 break;
1046 case DSP_SET_TRACK_GAIN:
1047 dsp->track_gain = (long) value;
1048 dsp->new_gain = true;
1049 break;
1051 case DSP_SET_ALBUM_GAIN:
1052 dsp->album_gain = (long) value;
1053 dsp->new_gain = true;
1054 break;
1056 case DSP_SET_TRACK_PEAK:
1057 dsp->track_peak = (long) value;
1058 dsp->new_gain = true;
1059 break;
1061 case DSP_SET_ALBUM_PEAK:
1062 dsp->album_peak = (long) value;
1063 dsp->new_gain = true;
1064 break;
1066 default:
1067 return 0;
1070 return 1;
1073 void dsp_set_replaygain(bool always)
1075 dsp = &dsp_conf[current_codec];
1077 if (always || dsp->new_gain)
1079 long gain = 0;
1081 dsp->new_gain = false;
1083 if (global_settings.replaygain || global_settings.replaygain_noclip)
1085 bool track_mode
1086 = ((global_settings.replaygain_type == REPLAYGAIN_TRACK)
1087 || ((global_settings.replaygain_type == REPLAYGAIN_SHUFFLE)
1088 && global_settings.playlist_shuffle));
1089 long peak = (track_mode || !dsp->album_peak)
1090 ? dsp->track_peak : dsp->album_peak;
1092 if (global_settings.replaygain)
1094 gain = (track_mode || !dsp->album_gain)
1095 ? dsp->track_gain : dsp->album_gain;
1097 if (global_settings.replaygain_preamp)
1099 long preamp = get_replaygain_int(
1100 global_settings.replaygain_preamp * 10);
1102 gain = (long) (((int64_t) gain * preamp) >> 24);
1106 if (gain == 0)
1108 /* So that noclip can work even with no gain information. */
1109 gain = DEFAULT_GAIN;
1112 if (global_settings.replaygain_noclip && (peak != 0)
1113 && ((((int64_t) gain * peak) >> 24) >= DEFAULT_GAIN))
1115 gain = (((int64_t) DEFAULT_GAIN << 24) / peak);
1118 if (gain == DEFAULT_GAIN)
1120 /* Nothing to do, disable processing. */
1121 gain = 0;
1126 /* Store in S8.23 format to simplify calculations. */
1127 dsp->replaygain = gain;
1128 set_gain();