Simplify touchscreen scrollbar handling code
[Rockbox.git] / apps / dsp.c
blob6520e528987ff910b8159cd2b0d357f83ab98fd3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Miika Pekkarinen
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 <stdbool.h>
23 #include <inttypes.h>
24 #include <string.h>
25 #include <sound.h>
26 #include "dsp.h"
27 #include "eq.h"
28 #include "kernel.h"
29 #include "playback.h"
30 #include "system.h"
31 #include "settings.h"
32 #include "replaygain.h"
33 #include "misc.h"
34 #include "debug.h"
36 /* 16-bit samples are scaled based on these constants. The shift should be
37 * no more than 15.
39 #define WORD_SHIFT 12
40 #define WORD_FRACBITS 27
42 #define NATIVE_DEPTH 16
43 /* If the buffer sizes change, check the assembly code! */
44 #define SAMPLE_BUF_COUNT 256
45 #define RESAMPLE_BUF_COUNT (256 * 4) /* Enough for 11,025 Hz -> 44,100 Hz*/
46 #define DEFAULT_GAIN 0x01000000
47 #define SAMPLE_BUF_LEFT_CHANNEL 0
48 #define SAMPLE_BUF_RIGHT_CHANNEL (SAMPLE_BUF_COUNT/2)
49 #define RESAMPLE_BUF_LEFT_CHANNEL 0
50 #define RESAMPLE_BUF_RIGHT_CHANNEL (RESAMPLE_BUF_COUNT/2)
52 /* enums to index conversion properly with stereo mode and other settings */
53 enum
55 SAMPLE_INPUT_LE_NATIVE_I_STEREO = STEREO_INTERLEAVED,
56 SAMPLE_INPUT_LE_NATIVE_NI_STEREO = STEREO_NONINTERLEAVED,
57 SAMPLE_INPUT_LE_NATIVE_MONO = STEREO_MONO,
58 SAMPLE_INPUT_GT_NATIVE_I_STEREO = STEREO_INTERLEAVED + STEREO_NUM_MODES,
59 SAMPLE_INPUT_GT_NATIVE_NI_STEREO = STEREO_NONINTERLEAVED + STEREO_NUM_MODES,
60 SAMPLE_INPUT_GT_NATIVE_MONO = STEREO_MONO + STEREO_NUM_MODES,
61 SAMPLE_INPUT_GT_NATIVE_1ST_INDEX = STEREO_NUM_MODES
64 enum
66 SAMPLE_OUTPUT_MONO = 0,
67 SAMPLE_OUTPUT_STEREO,
68 SAMPLE_OUTPUT_DITHERED_MONO,
69 SAMPLE_OUTPUT_DITHERED_STEREO
72 /****************************************************************************
73 * NOTE: Any assembly routines that use these structures must be updated
74 * if current data members are moved or changed.
76 struct resample_data
78 uint32_t delta; /* 00h */
79 uint32_t phase; /* 04h */
80 int32_t last_sample[2]; /* 08h */
81 /* 10h */
84 /* This is for passing needed data to assembly dsp routines. If another
85 * dsp parameter needs to be passed, add to the end of the structure
86 * and remove from dsp_config.
87 * If another function type becomes assembly optimized and requires dsp
88 * config info, add a pointer paramter of type "struct dsp_data *".
89 * If removing something from other than the end, reserve the spot or
90 * else update every implementation for every target.
91 * Be sure to add the offset of the new member for easy viewing as well. :)
92 * It is the first member of dsp_config and all members can be accessesed
93 * through the main aggregate but this is intended to make a safe haven
94 * for these items whereas the c part can be rearranged at will. dsp_data
95 * could even moved within dsp_config without disurbing the order.
97 struct dsp_data
99 int output_scale; /* 00h */
100 int num_channels; /* 04h */
101 struct resample_data resample_data; /* 08h */
102 int32_t clip_min; /* 18h */
103 int32_t clip_max; /* 1ch */
104 int32_t gain; /* 20h - Note that this is in S8.23 format. */
105 /* 24h */
108 /* No asm...yet */
109 struct dither_data
111 long error[3]; /* 00h */
112 long random; /* 0ch */
113 /* 10h */
116 struct crossfeed_data
118 int32_t gain; /* 00h - Direct path gain */
119 int32_t coefs[3]; /* 04h - Coefficients for the shelving filter */
120 int32_t history[4]; /* 10h - Format is x[n - 1], y[n - 1] for both channels */
121 int32_t delay[13][2]; /* 20h */
122 int32_t *index; /* 88h - Current pointer into the delay line */
123 /* 8ch */
126 /* Current setup is one lowshelf filters three peaking filters and one
127 * highshelf filter. Varying the number of shelving filters make no sense,
128 * but adding peaking filters is possible.
130 struct eq_state
132 char enabled[5]; /* 00h - Flags for active filters */
133 struct eqfilter filters[5]; /* 08h - packing is 4? */
134 /* 10ch */
137 /* Include header with defines which functions are implemented in assembly
138 code for the target */
139 #include <dsp_asm.h>
141 /* Typedefs keep things much neater in this case */
142 typedef void (*sample_input_fn_type)(int count, const char *src[],
143 int32_t *dst[]);
144 typedef int (*resample_fn_type)(int count, struct dsp_data *data,
145 int32_t *src[], int32_t *dst[]);
146 typedef void (*sample_output_fn_type)(int count, struct dsp_data *data,
147 int32_t *src[], int16_t *dst);
148 /* Single-DSP channel processing in place */
149 typedef void (*channels_process_fn_type)(int count, int32_t *buf[]);
150 /* DSP local channel processing in place */
151 typedef void (*channels_process_dsp_fn_type)(int count, struct dsp_data *data,
152 int32_t *buf[]);
156 ***************************************************************************/
158 struct dsp_config
160 struct dsp_data data; /* Config members for use in asm routines */
161 long codec_frequency; /* Sample rate of data coming from the codec */
162 long frequency; /* Effective sample rate after pitch shift (if any) */
163 int sample_depth;
164 int sample_bytes;
165 int stereo_mode;
166 int frac_bits;
167 #ifdef HAVE_SW_TONE_CONTROLS
168 /* Filter struct for software bass/treble controls */
169 struct eqfilter tone_filter;
170 #endif
171 /* Functions that change depending upon settings - NULL if stage is
172 disabled */
173 sample_input_fn_type input_samples;
174 resample_fn_type resample;
175 sample_output_fn_type output_samples;
176 /* These will be NULL for the voice codec and is more economical that
177 way */
178 channels_process_dsp_fn_type apply_gain;
179 channels_process_fn_type apply_crossfeed;
180 channels_process_fn_type eq_process;
181 channels_process_fn_type channels_process;
184 /* General DSP config */
185 static struct dsp_config dsp_conf[2] IBSS_ATTR; /* 0=A, 1=V */
186 /* Dithering */
187 static struct dither_data dither_data[2] IBSS_ATTR; /* 0=left, 1=right */
188 static long dither_mask IBSS_ATTR;
189 static long dither_bias IBSS_ATTR;
190 /* Crossfeed */
191 struct crossfeed_data crossfeed_data IDATA_ATTR = /* A */
193 .index = (int32_t *)crossfeed_data.delay
196 /* Equalizer */
197 static struct eq_state eq_data; /* A */
199 /* Software tone controls */
200 #ifdef HAVE_SW_TONE_CONTROLS
201 static int prescale; /* A/V */
202 static int bass; /* A/V */
203 static int treble; /* A/V */
204 #endif
206 /* Settings applicable to audio codec only */
207 static int pitch_ratio = 1000;
208 static int channels_mode;
209 long dsp_sw_gain;
210 long dsp_sw_cross;
211 static bool dither_enabled;
212 static long eq_precut;
213 static long track_gain;
214 static bool new_gain;
215 static long album_gain;
216 static long track_peak;
217 static long album_peak;
218 static long replaygain;
219 static bool crossfeed_enabled;
221 #define audio_dsp (dsp_conf[CODEC_IDX_AUDIO])
222 #define voice_dsp (dsp_conf[CODEC_IDX_VOICE])
224 /* The internal format is 32-bit samples, non-interleaved, stereo. This
225 * format is similar to the raw output from several codecs, so the amount
226 * of copying needed is minimized for that case.
229 int32_t sample_buf[SAMPLE_BUF_COUNT] IBSS_ATTR;
230 static int32_t resample_buf[RESAMPLE_BUF_COUNT] IBSS_ATTR;
232 #if 0
233 /* Clip sample to arbitrary limits where range > 0 and min + range = max */
234 static inline long clip_sample(int32_t sample, int32_t min, int32_t range)
236 if ((uint32_t)(sample - min) > (uint32_t)range)
238 int32_t c = min;
239 if (sample > min)
240 c += range;
241 sample = c;
243 return sample;
245 #endif
247 /* Clip sample to signed 16 bit range */
248 static inline int32_t clip_sample_16(int32_t sample)
250 if ((int16_t)sample != sample)
251 sample = 0x7fff ^ (sample >> 31);
252 return sample;
255 int sound_get_pitch(void)
257 return pitch_ratio;
260 void sound_set_pitch(int permille)
262 pitch_ratio = permille;
263 dsp_configure(&audio_dsp, DSP_SWITCH_FREQUENCY,
264 audio_dsp.codec_frequency);
267 /* Convert count samples to the internal format, if needed. Updates src
268 * to point past the samples "consumed" and dst is set to point to the
269 * samples to consume. Note that for mono, dst[0] equals dst[1], as there
270 * is no point in processing the same data twice.
273 /* convert count 16-bit mono to 32-bit mono */
274 static void sample_input_lte_native_mono(
275 int count, const char *src[], int32_t *dst[])
277 const int16_t *s = (int16_t *) src[0];
278 const int16_t * const send = s + count;
279 int32_t *d = dst[0] = dst[1] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
280 int scale = WORD_SHIFT;
284 *d++ = *s++ << scale;
286 while (s < send);
288 src[0] = (char *)s;
291 /* convert count 16-bit interleaved stereo to 32-bit noninterleaved */
292 static void sample_input_lte_native_i_stereo(
293 int count, const char *src[], int32_t *dst[])
295 const int32_t *s = (int32_t *) src[0];
296 const int32_t * const send = s + count;
297 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
298 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
299 int scale = WORD_SHIFT;
303 int32_t slr = *s++;
304 #ifdef ROCKBOX_LITTLE_ENDIAN
305 *dl++ = (slr >> 16) << scale;
306 *dr++ = (int32_t)(int16_t)slr << scale;
307 #else /* ROCKBOX_BIG_ENDIAN */
308 *dl++ = (int32_t)(int16_t)slr << scale;
309 *dr++ = (slr >> 16) << scale;
310 #endif
312 while (s < send);
314 src[0] = (char *)s;
317 /* convert count 16-bit noninterleaved stereo to 32-bit noninterleaved */
318 static void sample_input_lte_native_ni_stereo(
319 int count, const char *src[], int32_t *dst[])
321 const int16_t *sl = (int16_t *) src[0];
322 const int16_t *sr = (int16_t *) src[1];
323 const int16_t * const slend = sl + count;
324 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
325 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
326 int scale = WORD_SHIFT;
330 *dl++ = *sl++ << scale;
331 *dr++ = *sr++ << scale;
333 while (sl < slend);
335 src[0] = (char *)sl;
336 src[1] = (char *)sr;
339 /* convert count 32-bit mono to 32-bit mono */
340 static void sample_input_gt_native_mono(
341 int count, const char *src[], int32_t *dst[])
343 dst[0] = dst[1] = (int32_t *)src[0];
344 src[0] = (char *)(dst[0] + count);
347 /* convert count 32-bit interleaved stereo to 32-bit noninterleaved stereo */
348 static void sample_input_gt_native_i_stereo(
349 int count, const char *src[], int32_t *dst[])
351 const int32_t *s = (int32_t *)src[0];
352 const int32_t * const send = s + 2*count;
353 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
354 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
358 *dl++ = *s++;
359 *dr++ = *s++;
361 while (s < send);
363 src[0] = (char *)send;
366 /* convert 32 bit-noninterleaved stereo to 32-bit noninterleaved stereo */
367 static void sample_input_gt_native_ni_stereo(
368 int count, const char *src[], int32_t *dst[])
370 dst[0] = (int32_t *)src[0];
371 dst[1] = (int32_t *)src[1];
372 src[0] = (char *)(dst[0] + count);
373 src[1] = (char *)(dst[1] + count);
377 * sample_input_new_format()
379 * set the to-native sample conversion function based on dsp sample parameters
381 * !DSPPARAMSYNC
382 * needs syncing with changes to the following dsp parameters:
383 * * dsp->stereo_mode (A/V)
384 * * dsp->sample_depth (A/V)
386 static void sample_input_new_format(struct dsp_config *dsp)
388 static const sample_input_fn_type sample_input_functions[] =
390 [SAMPLE_INPUT_LE_NATIVE_MONO] = sample_input_lte_native_mono,
391 [SAMPLE_INPUT_LE_NATIVE_I_STEREO] = sample_input_lte_native_i_stereo,
392 [SAMPLE_INPUT_LE_NATIVE_NI_STEREO] = sample_input_lte_native_ni_stereo,
393 [SAMPLE_INPUT_GT_NATIVE_MONO] = sample_input_gt_native_mono,
394 [SAMPLE_INPUT_GT_NATIVE_I_STEREO] = sample_input_gt_native_i_stereo,
395 [SAMPLE_INPUT_GT_NATIVE_NI_STEREO] = sample_input_gt_native_ni_stereo,
398 int convert = dsp->stereo_mode;
400 if (dsp->sample_depth > NATIVE_DEPTH)
401 convert += SAMPLE_INPUT_GT_NATIVE_1ST_INDEX;
403 dsp->input_samples = sample_input_functions[convert];
406 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO
407 /* write mono internal format to output format */
408 static void sample_output_mono(int count, struct dsp_data *data,
409 int32_t *src[], int16_t *dst)
411 const int32_t *s0 = src[0];
412 const int scale = data->output_scale;
413 const int dc_bias = 1 << (scale - 1);
417 int32_t lr = clip_sample_16((*s0++ + dc_bias) >> scale);
418 *dst++ = lr;
419 *dst++ = lr;
421 while (--count > 0);
423 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO */
425 /* write stereo internal format to output format */
426 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO
427 static void sample_output_stereo(int count, struct dsp_data *data,
428 int32_t *src[], int16_t *dst)
430 const int32_t *s0 = src[0];
431 const int32_t *s1 = src[1];
432 const int scale = data->output_scale;
433 const int dc_bias = 1 << (scale - 1);
437 *dst++ = clip_sample_16((*s0++ + dc_bias) >> scale);
438 *dst++ = clip_sample_16((*s1++ + dc_bias) >> scale);
440 while (--count > 0);
442 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO */
445 * The "dither" code to convert the 24-bit samples produced by libmad was
446 * taken from the coolplayer project - coolplayer.sourceforge.net
448 * This function handles mono and stereo outputs.
450 static void sample_output_dithered(int count, struct dsp_data *data,
451 int32_t *src[], int16_t *dst)
453 const int32_t mask = dither_mask;
454 const int32_t bias = dither_bias;
455 const int scale = data->output_scale;
456 const int32_t min = data->clip_min;
457 const int32_t max = data->clip_max;
458 const int32_t range = max - min;
459 int ch;
460 int16_t *d;
462 for (ch = 0; ch < data->num_channels; ch++)
464 struct dither_data * const dither = &dither_data[ch];
465 int32_t *s = src[ch];
466 int i;
468 for (i = 0, d = &dst[ch]; i < count; i++, s++, d += 2)
470 int32_t output, sample;
471 int32_t random;
473 /* Noise shape and bias (for correct rounding later) */
474 sample = *s;
475 sample += dither->error[0] - dither->error[1] + dither->error[2];
476 dither->error[2] = dither->error[1];
477 dither->error[1] = dither->error[0]/2;
479 output = sample + bias;
481 /* Dither, highpass triangle PDF */
482 random = dither->random*0x0019660dL + 0x3c6ef35fL;
483 output += (random & mask) - (dither->random & mask);
484 dither->random = random;
486 /* Round sample to output range */
487 output &= ~mask;
489 /* Error feedback */
490 dither->error[0] = sample - output;
492 /* Clip */
493 if ((uint32_t)(output - min) > (uint32_t)range)
495 int32_t c = min;
496 if (output > min)
497 c += range;
498 output = c;
501 /* Quantize and store */
502 *d = output >> scale;
506 if (data->num_channels == 2)
507 return;
509 /* Have to duplicate left samples into the right channel since
510 pcm buffer and hardware is interleaved stereo */
511 d = &dst[0];
515 int16_t s = *d++;
516 *d++ = s;
518 while (--count > 0);
522 * sample_output_new_format()
524 * set the from-native to ouput sample conversion routine
526 * !DSPPARAMSYNC
527 * needs syncing with changes to the following dsp parameters:
528 * * dsp->stereo_mode (A/V)
529 * * dither_enabled (A)
531 static void sample_output_new_format(struct dsp_config *dsp)
533 static const sample_output_fn_type sample_output_functions[] =
535 sample_output_mono,
536 sample_output_stereo,
537 sample_output_dithered,
538 sample_output_dithered
541 int out = dsp->data.num_channels - 1;
543 if (dsp == &audio_dsp && dither_enabled)
544 out += 2;
546 dsp->output_samples = sample_output_functions[out];
550 * Linear interpolation resampling that introduces a one sample delay because
551 * of our inability to look into the future at the end of a frame.
553 #ifndef DSP_HAVE_ASM_RESAMPLING
554 static int dsp_downsample(int count, struct dsp_data *data,
555 int32_t *src[], int32_t *dst[])
557 int ch = data->num_channels - 1;
558 uint32_t delta = data->resample_data.delta;
559 uint32_t phase, pos;
560 int32_t *d;
562 /* Rolled channel loop actually showed slightly faster. */
565 /* Just initialize things and not worry too much about the relatively
566 * uncommon case of not being able to spit out a sample for the frame.
568 int32_t *s = src[ch];
569 int32_t last = data->resample_data.last_sample[ch];
571 data->resample_data.last_sample[ch] = s[count - 1];
572 d = dst[ch];
573 phase = data->resample_data.phase;
574 pos = phase >> 16;
576 /* Do we need last sample of previous frame for interpolation? */
577 if (pos > 0)
578 last = s[pos - 1];
580 while (pos < (uint32_t)count)
582 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last);
583 phase += delta;
584 pos = phase >> 16;
585 last = s[pos - 1];
588 while (--ch >= 0);
590 /* Wrap phase accumulator back to start of next frame. */
591 data->resample_data.phase = phase - (count << 16);
592 return d - dst[0];
595 static int dsp_upsample(int count, struct dsp_data *data,
596 int32_t *src[], int32_t *dst[])
598 int ch = data->num_channels - 1;
599 uint32_t delta = data->resample_data.delta;
600 uint32_t phase, pos;
601 int32_t *d;
603 /* Rolled channel loop actually showed slightly faster. */
606 /* Should always be able to output a sample for a ratio up to
607 RESAMPLE_BUF_COUNT / SAMPLE_BUF_COUNT. */
608 int32_t *s = src[ch];
609 int32_t last = data->resample_data.last_sample[ch];
611 data->resample_data.last_sample[ch] = s[count - 1];
612 d = dst[ch];
613 phase = data->resample_data.phase;
614 pos = phase >> 16;
616 while (pos == 0)
618 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[0] - last);
619 phase += delta;
620 pos = phase >> 16;
623 while (pos < (uint32_t)count)
625 last = s[pos - 1];
626 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last);
627 phase += delta;
628 pos = phase >> 16;
631 while (--ch >= 0);
633 /* Wrap phase accumulator back to start of next frame. */
634 data->resample_data.phase = phase & 0xffff;
635 return d - dst[0];
637 #endif /* DSP_HAVE_ASM_RESAMPLING */
639 static void resampler_new_delta(struct dsp_config *dsp)
641 dsp->data.resample_data.delta = (unsigned long)
642 dsp->frequency * 65536LL / NATIVE_FREQUENCY;
644 if (dsp->frequency == NATIVE_FREQUENCY)
646 /* NOTE: If fully glitch-free transistions from no resampling to
647 resampling are desired, last_sample history should be maintained
648 even when not resampling. */
649 dsp->resample = NULL;
650 dsp->data.resample_data.phase = 0;
651 dsp->data.resample_data.last_sample[0] = 0;
652 dsp->data.resample_data.last_sample[1] = 0;
654 else if (dsp->frequency < NATIVE_FREQUENCY)
655 dsp->resample = dsp_upsample;
656 else
657 dsp->resample = dsp_downsample;
660 /* Resample count stereo samples. Updates the src array, if resampling is
661 * done, to refer to the resampled data. Returns number of stereo samples
662 * for further processing.
664 static inline int resample(struct dsp_config *dsp, int count, int32_t *src[])
666 int32_t *dst[2] =
668 &resample_buf[RESAMPLE_BUF_LEFT_CHANNEL],
669 &resample_buf[RESAMPLE_BUF_RIGHT_CHANNEL],
672 count = dsp->resample(count, &dsp->data, src, dst);
674 src[0] = dst[0];
675 src[1] = dst[dsp->data.num_channels - 1];
677 return count;
680 static void dither_init(struct dsp_config *dsp)
682 memset(dither_data, 0, sizeof (dither_data));
683 dither_bias = (1L << (dsp->frac_bits - NATIVE_DEPTH));
684 dither_mask = (1L << (dsp->frac_bits + 1 - NATIVE_DEPTH)) - 1;
687 void dsp_dither_enable(bool enable)
689 struct dsp_config *dsp = &audio_dsp;
690 dither_enabled = enable;
691 sample_output_new_format(dsp);
694 /* Applies crossfeed to the stereo signal in src.
695 * Crossfeed is a process where listening over speakers is simulated. This
696 * is good for old hard panned stereo records, which might be quite fatiguing
697 * to listen to on headphones with no crossfeed.
699 #ifndef DSP_HAVE_ASM_CROSSFEED
700 static void apply_crossfeed(int count, int32_t *buf[])
702 int32_t *hist_l = &crossfeed_data.history[0];
703 int32_t *hist_r = &crossfeed_data.history[2];
704 int32_t *delay = &crossfeed_data.delay[0][0];
705 int32_t *coefs = &crossfeed_data.coefs[0];
706 int32_t gain = crossfeed_data.gain;
707 int32_t *di = crossfeed_data.index;
709 int32_t acc;
710 int32_t left, right;
711 int i;
713 for (i = 0; i < count; i++)
715 left = buf[0][i];
716 right = buf[1][i];
718 /* Filter delayed sample from left speaker */
719 acc = FRACMUL(*di, coefs[0]);
720 acc += FRACMUL(hist_l[0], coefs[1]);
721 acc += FRACMUL(hist_l[1], coefs[2]);
722 /* Save filter history for left speaker */
723 hist_l[1] = acc;
724 hist_l[0] = *di;
725 *di++ = left;
726 /* Filter delayed sample from right speaker */
727 acc = FRACMUL(*di, coefs[0]);
728 acc += FRACMUL(hist_r[0], coefs[1]);
729 acc += FRACMUL(hist_r[1], coefs[2]);
730 /* Save filter history for right speaker */
731 hist_r[1] = acc;
732 hist_r[0] = *di;
733 *di++ = right;
734 /* Now add the attenuated direct sound and write to outputs */
735 buf[0][i] = FRACMUL(left, gain) + hist_r[1];
736 buf[1][i] = FRACMUL(right, gain) + hist_l[1];
738 /* Wrap delay line index if bigger than delay line size */
739 if (di >= delay + 13*2)
740 di = delay;
742 /* Write back local copies of data we've modified */
743 crossfeed_data.index = di;
745 #endif /* DSP_HAVE_ASM_CROSSFEED */
748 * dsp_set_crossfeed(bool enable)
750 * !DSPPARAMSYNC
751 * needs syncing with changes to the following dsp parameters:
752 * * dsp->stereo_mode (A)
754 void dsp_set_crossfeed(bool enable)
756 crossfeed_enabled = enable;
757 audio_dsp.apply_crossfeed = (enable && audio_dsp.data.num_channels > 1)
758 ? apply_crossfeed : NULL;
761 void dsp_set_crossfeed_direct_gain(int gain)
763 crossfeed_data.gain = get_replaygain_int(gain * 10) << 7;
764 /* If gain is negative, the calculation overflowed and we need to clamp */
765 if (crossfeed_data.gain < 0)
766 crossfeed_data.gain = 0x7fffffff;
769 /* Both gains should be below 0 dB */
770 void dsp_set_crossfeed_cross_params(long lf_gain, long hf_gain, long cutoff)
772 int32_t *c = crossfeed_data.coefs;
773 long scaler = get_replaygain_int(lf_gain * 10) << 7;
775 cutoff = 0xffffffff/NATIVE_FREQUENCY*cutoff;
776 hf_gain -= lf_gain;
777 /* Divide cutoff by sqrt(10^(hf_gain/20)) to place cutoff at the -3 dB
778 * point instead of shelf midpoint. This is for compatibility with the old
779 * crossfeed shelf filter and should be removed if crossfeed settings are
780 * ever made incompatible for any other good reason.
782 cutoff = DIV64(cutoff, get_replaygain_int(hf_gain*5), 24);
783 filter_shelf_coefs(cutoff, hf_gain, false, c);
784 /* Scale coefs by LF gain and shift them to s0.31 format. We have no gains
785 * over 1 and can do this safely
787 c[0] = FRACMUL_SHL(c[0], scaler, 4);
788 c[1] = FRACMUL_SHL(c[1], scaler, 4);
789 c[2] <<= 4;
792 /* Apply a constant gain to the samples (e.g., for ReplayGain).
793 * Note that this must be called before the resampler.
795 #ifndef DSP_HAVE_ASM_APPLY_GAIN
796 static void dsp_apply_gain(int count, struct dsp_data *data, int32_t *buf[])
798 const int32_t gain = data->gain;
799 int ch;
801 for (ch = 0; ch < data->num_channels; ch++)
803 int32_t *d = buf[ch];
804 int i;
806 for (i = 0; i < count; i++)
807 d[i] = FRACMUL_SHL(d[i], gain, 8);
810 #endif /* DSP_HAVE_ASM_APPLY_GAIN */
812 /* Combine all gains to a global gain. */
813 static void set_gain(struct dsp_config *dsp)
815 dsp->data.gain = DEFAULT_GAIN;
817 /* Replay gain not relevant to voice */
818 if (dsp == &audio_dsp && replaygain)
820 dsp->data.gain = replaygain;
823 if (dsp->eq_process && eq_precut)
825 dsp->data.gain =
826 (long) (((int64_t) dsp->data.gain * eq_precut) >> 24);
829 if (dsp->data.gain == DEFAULT_GAIN)
831 dsp->data.gain = 0;
833 else
835 dsp->data.gain >>= 1;
838 dsp->apply_gain = dsp->data.gain != 0 ? dsp_apply_gain : NULL;
842 * Update the amount to cut the audio before applying the equalizer.
844 * @param precut to apply in decibels (multiplied by 10)
846 void dsp_set_eq_precut(int precut)
848 eq_precut = get_replaygain_int(precut * -10);
849 set_gain(&audio_dsp);
853 * Synchronize the equalizer filter coefficients with the global settings.
855 * @param band the equalizer band to synchronize
857 void dsp_set_eq_coefs(int band)
859 const int *setting;
860 long gain;
861 unsigned long cutoff, q;
863 /* Adjust setting pointer to the band we actually want to change */
864 setting = &global_settings.eq_band0_cutoff + (band * 3);
866 /* Convert user settings to format required by coef generator functions */
867 cutoff = 0xffffffff / NATIVE_FREQUENCY * (*setting++);
868 q = *setting++;
869 gain = *setting++;
871 if (q == 0)
872 q = 1;
874 /* NOTE: The coef functions assume the EMAC unit is in fractional mode,
875 which it should be, since we're executed from the main thread. */
877 /* Assume a band is disabled if the gain is zero */
878 if (gain == 0)
880 eq_data.enabled[band] = 0;
882 else
884 if (band == 0)
885 eq_ls_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
886 else if (band == 4)
887 eq_hs_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
888 else
889 eq_pk_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
891 eq_data.enabled[band] = 1;
895 /* Apply EQ filters to those bands that have got it switched on. */
896 static void eq_process(int count, int32_t *buf[])
898 static const int shifts[] =
900 EQ_SHELF_SHIFT, /* low shelf */
901 EQ_PEAK_SHIFT, /* peaking */
902 EQ_PEAK_SHIFT, /* peaking */
903 EQ_PEAK_SHIFT, /* peaking */
904 EQ_SHELF_SHIFT, /* high shelf */
906 unsigned int channels = audio_dsp.data.num_channels;
907 int i;
909 /* filter configuration currently is 1 low shelf filter, 3 band peaking
910 filters and 1 high shelf filter, in that order. we need to know this
911 so we can choose the correct shift factor.
913 for (i = 0; i < 5; i++)
915 if (!eq_data.enabled[i])
916 continue;
917 eq_filter(buf, &eq_data.filters[i], count, channels, shifts[i]);
922 * Use to enable the equalizer.
924 * @param enable true to enable the equalizer
926 void dsp_set_eq(bool enable)
928 audio_dsp.eq_process = enable ? eq_process : NULL;
929 set_gain(&audio_dsp);
932 static void dsp_set_stereo_width(int value)
934 long width, straight, cross;
936 width = value * 0x7fffff / 100;
938 if (value <= 100)
940 straight = (0x7fffff + width) / 2;
941 cross = straight - width;
943 else
945 /* straight = (1 + width) / (2 * width) */
946 straight = ((int64_t)(0x7fffff + width) << 22) / width;
947 cross = straight - 0x7fffff;
950 dsp_sw_gain = straight << 8;
951 dsp_sw_cross = cross << 8;
955 * Implements the different channel configurations and stereo width.
958 /* SOUND_CHAN_STEREO mode is a noop so has no function - just outline one for
959 * completeness. */
960 #if 0
961 static void channels_process_sound_chan_stereo(int count, int32_t *buf[])
963 /* The channels are each just themselves */
964 (void)count; (void)buf;
966 #endif
968 #ifndef DSP_HAVE_ASM_SOUND_CHAN_MONO
969 static void channels_process_sound_chan_mono(int count, int32_t *buf[])
971 int32_t *sl = buf[0], *sr = buf[1];
975 int32_t lr = *sl/2 + *sr/2;
976 *sl++ = lr;
977 *sr++ = lr;
979 while (--count > 0);
981 #endif /* DSP_HAVE_ASM_SOUND_CHAN_MONO */
983 #ifndef DSP_HAVE_ASM_SOUND_CHAN_CUSTOM
984 static void channels_process_sound_chan_custom(int count, int32_t *buf[])
986 const int32_t gain = dsp_sw_gain;
987 const int32_t cross = dsp_sw_cross;
988 int32_t *sl = buf[0], *sr = buf[1];
992 int32_t l = *sl;
993 int32_t r = *sr;
994 *sl++ = FRACMUL(l, gain) + FRACMUL(r, cross);
995 *sr++ = FRACMUL(r, gain) + FRACMUL(l, cross);
997 while (--count > 0);
999 #endif /* DSP_HAVE_ASM_SOUND_CHAN_CUSTOM */
1001 static void channels_process_sound_chan_mono_left(int count, int32_t *buf[])
1003 /* Just copy over the other channel */
1004 memcpy(buf[1], buf[0], count * sizeof (*buf));
1007 static void channels_process_sound_chan_mono_right(int count, int32_t *buf[])
1009 /* Just copy over the other channel */
1010 memcpy(buf[0], buf[1], count * sizeof (*buf));
1013 #ifndef DSP_HAVE_ASM_SOUND_CHAN_KARAOKE
1014 static void channels_process_sound_chan_karaoke(int count, int32_t *buf[])
1016 int32_t *sl = buf[0], *sr = buf[1];
1020 int32_t ch = *sl/2 - *sr/2;
1021 *sl++ = ch;
1022 *sr++ = -ch;
1024 while (--count > 0);
1026 #endif /* DSP_HAVE_ASM_SOUND_CHAN_KARAOKE */
1028 static void dsp_set_channel_config(int value)
1030 static const channels_process_fn_type channels_process_functions[] =
1032 /* SOUND_CHAN_STEREO = All-purpose index for no channel processing */
1033 [SOUND_CHAN_STEREO] = NULL,
1034 [SOUND_CHAN_MONO] = channels_process_sound_chan_mono,
1035 [SOUND_CHAN_CUSTOM] = channels_process_sound_chan_custom,
1036 [SOUND_CHAN_MONO_LEFT] = channels_process_sound_chan_mono_left,
1037 [SOUND_CHAN_MONO_RIGHT] = channels_process_sound_chan_mono_right,
1038 [SOUND_CHAN_KARAOKE] = channels_process_sound_chan_karaoke,
1041 if ((unsigned)value >= ARRAYLEN(channels_process_functions) ||
1042 audio_dsp.stereo_mode == STEREO_MONO)
1044 value = SOUND_CHAN_STEREO;
1047 /* This doesn't apply to voice */
1048 channels_mode = value;
1049 audio_dsp.channels_process = channels_process_functions[value];
1052 #if CONFIG_CODEC == SWCODEC
1054 #ifdef HAVE_SW_TONE_CONTROLS
1055 static void set_tone_controls(void)
1057 filter_bishelf_coefs(0xffffffff/NATIVE_FREQUENCY*200,
1058 0xffffffff/NATIVE_FREQUENCY*3500,
1059 bass, treble, -prescale,
1060 audio_dsp.tone_filter.coefs);
1061 /* Sync the voice dsp coefficients */
1062 memcpy(&voice_dsp.tone_filter.coefs, audio_dsp.tone_filter.coefs,
1063 sizeof (voice_dsp.tone_filter.coefs));
1065 #endif
1067 /* Hook back from firmware/ part of audio, which can't/shouldn't call apps/
1068 * code directly.
1070 int dsp_callback(int msg, intptr_t param)
1072 switch (msg) {
1073 #ifdef HAVE_SW_TONE_CONTROLS
1074 case DSP_CALLBACK_SET_PRESCALE:
1075 prescale = param;
1076 set_tone_controls();
1077 break;
1078 /* prescaler is always set after calling any of these, so we wait with
1079 * calculating coefs until the above case is hit.
1081 case DSP_CALLBACK_SET_BASS:
1082 bass = param;
1083 break;
1084 case DSP_CALLBACK_SET_TREBLE:
1085 treble = param;
1086 #endif
1087 case DSP_CALLBACK_SET_CHANNEL_CONFIG:
1088 dsp_set_channel_config(param);
1089 break;
1090 case DSP_CALLBACK_SET_STEREO_WIDTH:
1091 dsp_set_stereo_width(param);
1092 break;
1093 default:
1094 break;
1096 return 0;
1098 #endif
1100 /* Process and convert src audio to dst based on the DSP configuration,
1101 * reading count number of audio samples. dst is assumed to be large
1102 * enough; use dsp_output_count() to get the required number. src is an
1103 * array of pointers; for mono and interleaved stereo, it contains one
1104 * pointer to the start of the audio data and the other is ignored; for
1105 * non-interleaved stereo, it contains two pointers, one for each audio
1106 * channel. Returns number of bytes written to dst.
1108 int dsp_process(struct dsp_config *dsp, char *dst, const char *src[], int count)
1110 int32_t *tmp[2];
1111 static long last_yield;
1112 long tick;
1113 int written = 0;
1114 int samples;
1116 #if defined(CPU_COLDFIRE)
1117 /* set emac unit for dsp processing, and save old macsr, we're running in
1118 codec thread context at this point, so can't clobber it */
1119 unsigned long old_macsr = coldfire_get_macsr();
1120 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
1121 #endif
1123 if (new_gain)
1124 dsp_set_replaygain(); /* Gain has changed */
1126 /* Perform at least one yield before starting */
1127 last_yield = current_tick;
1128 yield();
1130 /* Testing function pointers for NULL is preferred since the pointer
1131 will be preloaded to be used for the call if not. */
1132 while (count > 0)
1134 samples = MIN(SAMPLE_BUF_COUNT/2, count);
1135 count -= samples;
1137 dsp->input_samples(samples, src, tmp);
1139 if (dsp->apply_gain)
1140 dsp->apply_gain(samples, &dsp->data, tmp);
1142 if (dsp->resample && (samples = resample(dsp, samples, tmp)) <= 0)
1143 break; /* I'm pretty sure we're downsampling here */
1145 if (dsp->apply_crossfeed)
1146 dsp->apply_crossfeed(samples, tmp);
1148 if (dsp->eq_process)
1149 dsp->eq_process(samples, tmp);
1151 #ifdef HAVE_SW_TONE_CONTROLS
1152 if ((bass | treble) != 0)
1153 eq_filter(tmp, &dsp->tone_filter, samples,
1154 dsp->data.num_channels, FILTER_BISHELF_SHIFT);
1155 #endif
1157 if (dsp->channels_process)
1158 dsp->channels_process(samples, tmp);
1160 dsp->output_samples(samples, &dsp->data, tmp, (int16_t *)dst);
1162 written += samples;
1163 dst += samples * sizeof (int16_t) * 2;
1165 /* yield at least once each tick */
1166 tick = current_tick;
1167 if (TIME_AFTER(tick, last_yield))
1169 last_yield = tick;
1170 yield();
1174 #if defined(CPU_COLDFIRE)
1175 /* set old macsr again */
1176 coldfire_set_macsr(old_macsr);
1177 #endif
1178 return written;
1181 /* Given count number of input samples, calculate the maximum number of
1182 * samples of output data that would be generated (the calculation is not
1183 * entirely exact and rounds upwards to be on the safe side; during
1184 * resampling, the number of samples generated depends on the current state
1185 * of the resampler).
1187 /* dsp_input_size MUST be called afterwards */
1188 int dsp_output_count(struct dsp_config *dsp, int count)
1190 if (dsp->resample)
1192 count = (int)(((unsigned long)count * NATIVE_FREQUENCY
1193 + (dsp->frequency - 1)) / dsp->frequency);
1196 /* Now we have the resampled sample count which must not exceed
1197 * RESAMPLE_BUF_COUNT/2 to avoid resample buffer overflow. One
1198 * must call dsp_input_count() to get the correct input sample
1199 * count.
1201 if (count > RESAMPLE_BUF_COUNT/2)
1202 count = RESAMPLE_BUF_COUNT/2;
1204 return count;
1207 /* Given count output samples, calculate number of input samples
1208 * that would be consumed in order to fill the output buffer.
1210 int dsp_input_count(struct dsp_config *dsp, int count)
1212 /* count is now the number of resampled input samples. Convert to
1213 original input samples. */
1214 if (dsp->resample)
1216 /* Use the real resampling delta =
1217 * dsp->frequency * 65536 / NATIVE_FREQUENCY, and
1218 * round towards zero to avoid buffer overflows. */
1219 count = (int)(((unsigned long)count *
1220 dsp->data.resample_data.delta) >> 16);
1223 return count;
1226 static void dsp_set_gain_var(long *var, long value)
1228 *var = value;
1229 new_gain = true;
1232 static void dsp_update_functions(struct dsp_config *dsp)
1234 sample_input_new_format(dsp);
1235 sample_output_new_format(dsp);
1236 if (dsp == &audio_dsp)
1237 dsp_set_crossfeed(crossfeed_enabled);
1240 intptr_t dsp_configure(struct dsp_config *dsp, int setting, intptr_t value)
1242 switch (setting)
1244 case DSP_MYDSP:
1245 switch (value)
1247 case CODEC_IDX_AUDIO:
1248 return (intptr_t)&audio_dsp;
1249 case CODEC_IDX_VOICE:
1250 return (intptr_t)&voice_dsp;
1251 default:
1252 return (intptr_t)NULL;
1255 case DSP_SET_FREQUENCY:
1256 memset(&dsp->data.resample_data, 0, sizeof (dsp->data.resample_data));
1257 /* Fall through!!! */
1258 case DSP_SWITCH_FREQUENCY:
1259 dsp->codec_frequency = (value == 0) ? NATIVE_FREQUENCY : value;
1260 /* Account for playback speed adjustment when setting dsp->frequency
1261 if we're called from the main audio thread. Voice UI thread should
1262 not need this feature.
1264 if (dsp == &audio_dsp)
1265 dsp->frequency = pitch_ratio * dsp->codec_frequency / 1000;
1266 else
1267 dsp->frequency = dsp->codec_frequency;
1269 resampler_new_delta(dsp);
1270 break;
1272 case DSP_SET_SAMPLE_DEPTH:
1273 dsp->sample_depth = value;
1275 if (dsp->sample_depth <= NATIVE_DEPTH)
1277 dsp->frac_bits = WORD_FRACBITS;
1278 dsp->sample_bytes = sizeof (int16_t); /* samples are 16 bits */
1279 dsp->data.clip_max = ((1 << WORD_FRACBITS) - 1);
1280 dsp->data.clip_min = -((1 << WORD_FRACBITS));
1282 else
1284 dsp->frac_bits = value;
1285 dsp->sample_bytes = sizeof (int32_t); /* samples are 32 bits */
1286 dsp->data.clip_max = (1 << value) - 1;
1287 dsp->data.clip_min = -(1 << value);
1290 dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
1291 sample_input_new_format(dsp);
1292 dither_init(dsp);
1293 break;
1295 case DSP_SET_STEREO_MODE:
1296 dsp->stereo_mode = value;
1297 dsp->data.num_channels = value == STEREO_MONO ? 1 : 2;
1298 dsp_update_functions(dsp);
1299 break;
1301 case DSP_RESET:
1302 dsp->stereo_mode = STEREO_NONINTERLEAVED;
1303 dsp->data.num_channels = 2;
1304 dsp->sample_depth = NATIVE_DEPTH;
1305 dsp->frac_bits = WORD_FRACBITS;
1306 dsp->sample_bytes = sizeof (int16_t);
1307 dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
1308 dsp->data.clip_max = ((1 << WORD_FRACBITS) - 1);
1309 dsp->data.clip_min = -((1 << WORD_FRACBITS));
1310 dsp->codec_frequency = dsp->frequency = NATIVE_FREQUENCY;
1312 if (dsp == &audio_dsp)
1314 track_gain = 0;
1315 album_gain = 0;
1316 track_peak = 0;
1317 album_peak = 0;
1318 new_gain = true;
1321 dsp_update_functions(dsp);
1322 resampler_new_delta(dsp);
1323 break;
1325 case DSP_FLUSH:
1326 memset(&dsp->data.resample_data, 0,
1327 sizeof (dsp->data.resample_data));
1328 resampler_new_delta(dsp);
1329 dither_init(dsp);
1330 break;
1332 case DSP_SET_TRACK_GAIN:
1333 if (dsp == &audio_dsp)
1334 dsp_set_gain_var(&track_gain, value);
1335 break;
1337 case DSP_SET_ALBUM_GAIN:
1338 if (dsp == &audio_dsp)
1339 dsp_set_gain_var(&album_gain, value);
1340 break;
1342 case DSP_SET_TRACK_PEAK:
1343 if (dsp == &audio_dsp)
1344 dsp_set_gain_var(&track_peak, value);
1345 break;
1347 case DSP_SET_ALBUM_PEAK:
1348 if (dsp == &audio_dsp)
1349 dsp_set_gain_var(&album_peak, value);
1350 break;
1352 default:
1353 return 0;
1356 return 1;
1359 void dsp_set_replaygain(void)
1361 long gain = 0;
1363 new_gain = false;
1365 if (global_settings.replaygain || global_settings.replaygain_noclip)
1367 bool track_mode = get_replaygain_mode(track_gain != 0,
1368 album_gain != 0) == REPLAYGAIN_TRACK;
1369 long peak = (track_mode || !album_peak) ? track_peak : album_peak;
1371 if (global_settings.replaygain)
1373 gain = (track_mode || !album_gain) ? track_gain : album_gain;
1375 if (global_settings.replaygain_preamp)
1377 long preamp = get_replaygain_int(
1378 global_settings.replaygain_preamp * 10);
1380 gain = (long) (((int64_t) gain * preamp) >> 24);
1384 if (gain == 0)
1386 /* So that noclip can work even with no gain information. */
1387 gain = DEFAULT_GAIN;
1390 if (global_settings.replaygain_noclip && (peak != 0)
1391 && ((((int64_t) gain * peak) >> 24) >= DEFAULT_GAIN))
1393 gain = (((int64_t) DEFAULT_GAIN << 24) / peak);
1396 if (gain == DEFAULT_GAIN)
1398 /* Nothing to do, disable processing. */
1399 gain = 0;
1403 /* Store in S8.23 format to simplify calculations. */
1404 replaygain = gain;
1405 set_gain(&audio_dsp);