Updated italian translation.
[kugel-rb.git] / apps / dsp.c
blob4cade936b95d7d5ae41e0d2ffbaf0c7e82021bdc
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 "config.h"
20 #include <stdbool.h>
21 #include <inttypes.h>
22 #include <string.h>
23 #include <sound.h>
24 #include "dsp.h"
25 #include "eq.h"
26 #include "kernel.h"
27 #include "playback.h"
28 #include "system.h"
29 #include "settings.h"
30 #include "replaygain.h"
31 #include "misc.h"
32 #include "debug.h"
34 /* 16-bit samples are scaled based on these constants. The shift should be
35 * no more than 15.
37 #define WORD_SHIFT 12
38 #define WORD_FRACBITS 27
40 #define NATIVE_DEPTH 16
41 /* If the buffer sizes change, check the assembly code! */
42 #define SAMPLE_BUF_COUNT 256
43 #define RESAMPLE_BUF_COUNT (256 * 4) /* Enough for 11,025 Hz -> 44,100 Hz*/
44 #define DEFAULT_GAIN 0x01000000
45 #define SAMPLE_BUF_LEFT_CHANNEL 0
46 #define SAMPLE_BUF_RIGHT_CHANNEL (SAMPLE_BUF_COUNT/2)
47 #define RESAMPLE_BUF_LEFT_CHANNEL 0
48 #define RESAMPLE_BUF_RIGHT_CHANNEL (RESAMPLE_BUF_COUNT/2)
50 /* enums to index conversion properly with stereo mode and other settings */
51 enum
53 SAMPLE_INPUT_LE_NATIVE_I_STEREO = STEREO_INTERLEAVED,
54 SAMPLE_INPUT_LE_NATIVE_NI_STEREO = STEREO_NONINTERLEAVED,
55 SAMPLE_INPUT_LE_NATIVE_MONO = STEREO_MONO,
56 SAMPLE_INPUT_GT_NATIVE_I_STEREO = STEREO_INTERLEAVED + STEREO_NUM_MODES,
57 SAMPLE_INPUT_GT_NATIVE_NI_STEREO = STEREO_NONINTERLEAVED + STEREO_NUM_MODES,
58 SAMPLE_INPUT_GT_NATIVE_MONO = STEREO_MONO + STEREO_NUM_MODES,
59 SAMPLE_INPUT_GT_NATIVE_1ST_INDEX = STEREO_NUM_MODES
62 enum
64 SAMPLE_OUTPUT_MONO = 0,
65 SAMPLE_OUTPUT_STEREO,
66 SAMPLE_OUTPUT_DITHERED_MONO,
67 SAMPLE_OUTPUT_DITHERED_STEREO
70 /****************************************************************************
71 * NOTE: Any assembly routines that use these structures must be updated
72 * if current data members are moved or changed.
74 struct resample_data
76 uint32_t delta; /* 00h */
77 uint32_t phase; /* 04h */
78 int32_t last_sample[2]; /* 08h */
79 /* 10h */
82 /* This is for passing needed data to assembly dsp routines. If another
83 * dsp parameter needs to be passed, add to the end of the structure
84 * and remove from dsp_config.
85 * If another function type becomes assembly optimized and requires dsp
86 * config info, add a pointer paramter of type "struct dsp_data *".
87 * If removing something from other than the end, reserve the spot or
88 * else update every implementation for every target.
89 * Be sure to add the offset of the new member for easy viewing as well. :)
90 * It is the first member of dsp_config and all members can be accessesed
91 * through the main aggregate but this is intended to make a safe haven
92 * for these items whereas the c part can be rearranged at will. dsp_data
93 * could even moved within dsp_config without disurbing the order.
95 struct dsp_data
97 int output_scale; /* 00h */
98 int num_channels; /* 04h */
99 struct resample_data resample_data; /* 08h */
100 int32_t clip_min; /* 18h */
101 int32_t clip_max; /* 1ch */
102 int32_t gain; /* 20h - Note that this is in S8.23 format. */
103 /* 24h */
106 /* No asm...yet */
107 struct dither_data
109 long error[3]; /* 00h */
110 long random; /* 0ch */
111 /* 10h */
114 struct crossfeed_data
116 int32_t gain; /* 00h - Direct path gain */
117 int32_t coefs[3]; /* 04h - Coefficients for the shelving filter */
118 int32_t history[4]; /* 10h - Format is x[n - 1], y[n - 1] for both channels */
119 int32_t delay[13][2]; /* 20h */
120 int32_t *index; /* 88h - Current pointer into the delay line */
121 /* 8ch */
124 /* Current setup is one lowshelf filters three peaking filters and one
125 * highshelf filter. Varying the number of shelving filters make no sense,
126 * but adding peaking filters is possible.
128 struct eq_state
130 char enabled[5]; /* 00h - Flags for active filters */
131 struct eqfilter filters[5]; /* 08h - packing is 4? */
132 /* 10ch */
135 /* Include header with defines which functions are implemented in assembly
136 code for the target */
137 #include <dsp_asm.h>
139 /* Typedefs keep things much neater in this case */
140 typedef void (*sample_input_fn_type)(int count, const char *src[],
141 int32_t *dst[]);
142 typedef int (*resample_fn_type)(int count, struct dsp_data *data,
143 int32_t *src[], int32_t *dst[]);
144 typedef void (*sample_output_fn_type)(int count, struct dsp_data *data,
145 int32_t *src[], int16_t *dst);
146 /* Single-DSP channel processing in place */
147 typedef void (*channels_process_fn_type)(int count, int32_t *buf[]);
148 /* DSP local channel processing in place */
149 typedef void (*channels_process_dsp_fn_type)(int count, struct dsp_data *data,
150 int32_t *buf[]);
154 ***************************************************************************/
156 struct dsp_config
158 struct dsp_data data; /* Config members for use in asm routines */
159 long codec_frequency; /* Sample rate of data coming from the codec */
160 long frequency; /* Effective sample rate after pitch shift (if any) */
161 int sample_depth;
162 int sample_bytes;
163 int stereo_mode;
164 int frac_bits;
165 /* Functions that change depending upon settings - NULL if stage is
166 disabled */
167 sample_input_fn_type input_samples;
168 resample_fn_type resample;
169 sample_output_fn_type output_samples;
170 /* These will be NULL for the voice codec and is more economical that
171 way */
172 channels_process_dsp_fn_type apply_gain;
173 channels_process_fn_type apply_crossfeed;
174 channels_process_fn_type channels_process;
177 /* General DSP config */
178 static struct dsp_config dsp_conf[2] IBSS_ATTR; /* 0=A, 1=V */
179 /* Dithering */
180 static struct dither_data dither_data[2] IBSS_ATTR; /* 0=left, 1=right */
181 static long dither_mask IBSS_ATTR;
182 static long dither_bias IBSS_ATTR;
183 /* Crossfeed */
184 struct crossfeed_data crossfeed_data IDATA_ATTR = /* A */
186 .index = (int32_t *)crossfeed_data.delay
189 /* Equalizer */
190 static struct eq_state eq_data; /* A/V */
191 #ifdef HAVE_SW_TONE_CONTROLS
192 static int prescale;
193 static int bass;
194 static int treble;
195 /* Filter struct for software bass/treble controls */
196 static struct eqfilter tone_filter;
197 #endif
199 /* Settings applicable to audio codec only */
200 static int pitch_ratio = 1000;
201 static int channels_mode;
202 long dsp_sw_gain;
203 long dsp_sw_cross;
204 static bool dither_enabled;
205 static bool eq_enabled IBSS_ATTR;
206 static long eq_precut;
207 static long track_gain;
208 static bool new_gain;
209 static long album_gain;
210 static long track_peak;
211 static long album_peak;
212 static long replaygain;
213 static bool crossfeed_enabled;
215 #define audio_dsp (&dsp_conf[CODEC_IDX_AUDIO])
216 #define voice_dsp (&dsp_conf[CODEC_IDX_VOICE])
217 static struct dsp_config *dsp IDATA_ATTR = audio_dsp;
219 /* The internal format is 32-bit samples, non-interleaved, stereo. This
220 * format is similar to the raw output from several codecs, so the amount
221 * of copying needed is minimized for that case.
224 int32_t sample_buf[SAMPLE_BUF_COUNT] IBSS_ATTR;
225 static int32_t resample_buf[RESAMPLE_BUF_COUNT] IBSS_ATTR;
227 /* set a new dsp and return old one */
228 static inline struct dsp_config * switch_dsp(struct dsp_config *_dsp)
230 struct dsp_config * old_dsp = dsp;
231 dsp = _dsp;
232 return old_dsp;
235 #if 0
236 /* Clip sample to arbitrary limits where range > 0 and min + range = max */
237 static inline long clip_sample(int32_t sample, int32_t min, int32_t range)
239 if ((uint32_t)(sample - min) > (uint32_t)range)
241 int32_t c = min;
242 if (sample > min)
243 c += range;
244 sample = c;
246 return sample;
248 #endif
250 /* Clip sample to signed 16 bit range */
251 static inline int32_t clip_sample_16(int32_t sample)
253 if ((int16_t)sample != sample)
254 sample = 0x7fff ^ (sample >> 31);
255 return sample;
258 int sound_get_pitch(void)
260 return pitch_ratio;
263 void sound_set_pitch(int permille)
265 pitch_ratio = permille;
267 dsp_configure(DSP_SWITCH_FREQUENCY, dsp->codec_frequency);
270 /* Convert count samples to the internal format, if needed. Updates src
271 * to point past the samples "consumed" and dst is set to point to the
272 * samples to consume. Note that for mono, dst[0] equals dst[1], as there
273 * is no point in processing the same data twice.
276 /* convert count 16-bit mono to 32-bit mono */
277 static void sample_input_lte_native_mono(
278 int count, const char *src[], int32_t *dst[])
280 const int16_t *s = (int16_t *) src[0];
281 const int16_t * const send = s + count;
282 int32_t *d = dst[0] = dst[1] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
283 int scale = WORD_SHIFT;
287 *d++ = *s++ << scale;
289 while (s < send);
291 src[0] = (char *)s;
294 /* convert count 16-bit interleaved stereo to 32-bit noninterleaved */
295 static void sample_input_lte_native_i_stereo(
296 int count, const char *src[], int32_t *dst[])
298 const int32_t *s = (int32_t *) src[0];
299 const int32_t * const send = s + count;
300 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
301 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
302 int scale = WORD_SHIFT;
306 int32_t slr = *s++;
307 #ifdef ROCKBOX_LITTLE_ENDIAN
308 *dl++ = (slr >> 16) << scale;
309 *dr++ = (int32_t)(int16_t)slr << scale;
310 #else /* ROCKBOX_BIG_ENDIAN */
311 *dl++ = (int32_t)(int16_t)slr << scale;
312 *dr++ = (slr >> 16) << scale;
313 #endif
315 while (s < send);
317 src[0] = (char *)s;
320 /* convert count 16-bit noninterleaved stereo to 32-bit noninterleaved */
321 static void sample_input_lte_native_ni_stereo(
322 int count, const char *src[], int32_t *dst[])
324 const int16_t *sl = (int16_t *) src[0];
325 const int16_t *sr = (int16_t *) src[1];
326 const int16_t * const slend = sl + count;
327 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
328 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
329 int scale = WORD_SHIFT;
333 *dl++ = *sl++ << scale;
334 *dr++ = *sr++ << scale;
336 while (sl < slend);
338 src[0] = (char *)sl;
339 src[1] = (char *)sr;
342 /* convert count 32-bit mono to 32-bit mono */
343 static void sample_input_gt_native_mono(
344 int count, const char *src[], int32_t *dst[])
346 dst[0] = dst[1] = (int32_t *)src[0];
347 src[0] = (char *)(dst[0] + count);
350 /* convert count 32-bit interleaved stereo to 32-bit noninterleaved stereo */
351 static void sample_input_gt_native_i_stereo(
352 int count, const char *src[], int32_t *dst[])
354 const int32_t *s = (int32_t *)src[0];
355 const int32_t * const send = s + 2*count;
356 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
357 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
361 *dl++ = *s++;
362 *dr++ = *s++;
364 while (s < send);
366 src[0] = (char *)send;
369 /* convert 32 bit-noninterleaved stereo to 32-bit noninterleaved stereo */
370 static void sample_input_gt_native_ni_stereo(
371 int count, const char *src[], int32_t *dst[])
373 dst[0] = (int32_t *)src[0];
374 dst[1] = (int32_t *)src[1];
375 src[0] = (char *)(dst[0] + count);
376 src[1] = (char *)(dst[1] + count);
380 * sample_input_new_format()
382 * set the to-native sample conversion function based on dsp sample parameters
384 * !DSPPARAMSYNC
385 * needs syncing with changes to the following dsp parameters:
386 * * dsp->stereo_mode (A/V)
387 * * dsp->sample_depth (A/V)
389 static void sample_input_new_format(void)
391 static const sample_input_fn_type sample_input_functions[] =
393 [SAMPLE_INPUT_LE_NATIVE_MONO] = sample_input_lte_native_mono,
394 [SAMPLE_INPUT_LE_NATIVE_I_STEREO] = sample_input_lte_native_i_stereo,
395 [SAMPLE_INPUT_LE_NATIVE_NI_STEREO] = sample_input_lte_native_ni_stereo,
396 [SAMPLE_INPUT_GT_NATIVE_MONO] = sample_input_gt_native_mono,
397 [SAMPLE_INPUT_GT_NATIVE_I_STEREO] = sample_input_gt_native_i_stereo,
398 [SAMPLE_INPUT_GT_NATIVE_NI_STEREO] = sample_input_gt_native_ni_stereo,
401 int convert = dsp->stereo_mode;
403 if (dsp->sample_depth > NATIVE_DEPTH)
404 convert += SAMPLE_INPUT_GT_NATIVE_1ST_INDEX;
406 dsp->input_samples = sample_input_functions[convert];
409 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO
410 /* write mono internal format to output format */
411 static void sample_output_mono(int count, struct dsp_data *data,
412 int32_t *src[], int16_t *dst)
414 const int32_t *s0 = src[0];
415 const int scale = data->output_scale;
416 const int dc_bias = 1 << (scale - 1);
420 int32_t lr = clip_sample_16((*s0++ + dc_bias) >> scale);
421 *dst++ = lr;
422 *dst++ = lr;
424 while (--count > 0);
426 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO */
428 /* write stereo internal format to output format */
429 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO
430 static void sample_output_stereo(int count, struct dsp_data *data,
431 int32_t *src[], int16_t *dst)
433 const int32_t *s0 = src[0];
434 const int32_t *s1 = src[1];
435 const int scale = data->output_scale;
436 const int dc_bias = 1 << (scale - 1);
440 *dst++ = clip_sample_16((*s0++ + dc_bias) >> scale);
441 *dst++ = clip_sample_16((*s1++ + dc_bias) >> scale);
443 while (--count > 0);
445 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO */
448 * The "dither" code to convert the 24-bit samples produced by libmad was
449 * taken from the coolplayer project - coolplayer.sourceforge.net
451 * This function handles mono and stereo outputs.
453 static void sample_output_dithered(int count, struct dsp_data *data,
454 int32_t *src[], int16_t *dst)
456 const int32_t mask = dither_mask;
457 const int32_t bias = dither_bias;
458 const int scale = data->output_scale;
459 const int32_t min = data->clip_min;
460 const int32_t max = data->clip_max;
461 const int32_t range = max - min;
462 int ch;
463 int16_t *d;
465 for (ch = 0; ch < dsp->data.num_channels; ch++)
467 struct dither_data * const dither = &dither_data[ch];
468 int32_t *s = src[ch];
469 int i;
471 for (i = 0, d = &dst[ch]; i < count; i++, s++, d += 2)
473 int32_t output, sample;
474 int32_t random;
476 /* Noise shape and bias */
477 sample = *s;
478 sample += dither->error[0] - dither->error[1] + dither->error[2];
479 dither->error[2] = dither->error[1];
480 dither->error[1] = dither->error[0]/2;
482 output = sample + bias;
484 /* Dither */
485 random = dither->random*0x0019660dL + 0x3c6ef35fL;
486 output += (random & mask) - (dither->random & mask);
487 dither->random = random;
489 /* Clip */
490 if ((uint32_t)(output - min) > (uint32_t)range)
492 int32_t c = min;
493 if (output > min)
494 c += range;
495 output = c;
498 output &= ~mask;
500 /* Error feedback */
501 dither->error[0] = sample - output;
503 /* Quantize */
504 *d = output >> scale;
508 if (dsp->data.num_channels == 2)
509 return;
511 /* Have to duplicate left samples into the right channel since
512 pcm buffer and hardware is interleaved stereo */
513 d = &dst[0];
517 int16_t s = *d++;
518 *d++ = s;
520 while (--count > 0);
524 * sample_output_new_format()
526 * set the from-native to ouput sample conversion routine
528 * !DSPPARAMSYNC
529 * needs syncing with changes to the following dsp parameters:
530 * * dsp->stereo_mode (A/V)
531 * * dither_enabled (A)
533 static void sample_output_new_format(void)
535 static const sample_output_fn_type sample_output_functions[] =
537 sample_output_mono,
538 sample_output_stereo,
539 sample_output_dithered,
540 sample_output_dithered
543 int out = dsp->data.num_channels - 1;
545 if (dsp == audio_dsp && dither_enabled)
546 out += 2;
548 dsp->output_samples = sample_output_functions[out];
552 * Linear interpolation resampling that introduces a one sample delay because
553 * of our inability to look into the future at the end of a frame.
555 #ifndef DSP_HAVE_ASM_RESAMPLING
556 static int dsp_downsample(int count, struct dsp_data *data,
557 int32_t *src[], int32_t *dst[])
559 int ch = data->num_channels - 1;
560 uint32_t delta = data->resample_data.delta;
561 uint32_t phase, pos;
562 int32_t *d;
564 /* Rolled channel loop actually showed slightly faster. */
567 /* Just initialize things and not worry too much about the relatively
568 * uncommon case of not being able to spit out a sample for the frame.
570 int32_t *s = src[ch];
571 int32_t last = data->resample_data.last_sample[ch];
573 data->resample_data.last_sample[ch] = s[count - 1];
574 d = dst[ch];
575 phase = data->resample_data.phase;
576 pos = phase >> 16;
578 /* Do we need last sample of previous frame for interpolation? */
579 if (pos > 0)
580 last = s[pos - 1];
582 while (pos < (uint32_t)count)
584 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last);
585 phase += delta;
586 pos = phase >> 16;
587 last = s[pos - 1];
590 while (--ch >= 0);
592 /* Wrap phase accumulator back to start of next frame. */
593 data->resample_data.phase = phase - (count << 16);
594 return d - dst[0];
597 static int dsp_upsample(int count, struct dsp_data *data,
598 int32_t *src[], int32_t *dst[])
600 int ch = data->num_channels - 1;
601 uint32_t delta = data->resample_data.delta;
602 uint32_t phase, pos;
603 int32_t *d;
605 /* Rolled channel loop actually showed slightly faster. */
608 /* Should always be able to output a sample for a ratio up to
609 RESAMPLE_BUF_COUNT / SAMPLE_BUF_COUNT. */
610 int32_t *s = src[ch];
611 int32_t last = data->resample_data.last_sample[ch];
613 data->resample_data.last_sample[ch] = s[count - 1];
614 d = dst[ch];
615 phase = data->resample_data.phase;
616 pos = phase >> 16;
618 while (pos == 0)
620 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[0] - last);
621 phase += delta;
622 pos = phase >> 16;
625 while (pos < (uint32_t)count)
627 last = s[pos - 1];
628 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last);
629 phase += delta;
630 pos = phase >> 16;
633 while (--ch >= 0);
635 /* Wrap phase accumulator back to start of next frame. */
636 data->resample_data.phase = phase & 0xffff;
637 return d - dst[0];
639 #endif /* DSP_HAVE_ASM_RESAMPLING */
641 static void resampler_new_delta(void)
643 dsp->data.resample_data.delta = (unsigned long)
644 dsp->frequency * 65536LL / NATIVE_FREQUENCY;
646 if (dsp->frequency == NATIVE_FREQUENCY)
648 /* NOTE: If fully glitch-free transistions from no resampling to
649 resampling are desired, last_sample history should be maintained
650 even when not resampling. */
651 dsp->resample = NULL;
652 dsp->data.resample_data.phase = 0;
653 dsp->data.resample_data.last_sample[0] = 0;
654 dsp->data.resample_data.last_sample[1] = 0;
656 else if (dsp->frequency < NATIVE_FREQUENCY)
657 dsp->resample = dsp_upsample;
658 else
659 dsp->resample = dsp_downsample;
662 /* Resample count stereo samples. Updates the src array, if resampling is
663 * done, to refer to the resampled data. Returns number of stereo samples
664 * for further processing.
666 static inline int resample(int count, int32_t *src[])
668 int32_t *dst[2] =
670 &resample_buf[RESAMPLE_BUF_LEFT_CHANNEL],
671 &resample_buf[RESAMPLE_BUF_RIGHT_CHANNEL],
674 count = dsp->resample(count, &dsp->data, src, dst);
676 src[0] = dst[0];
677 src[1] = dst[dsp->data.num_channels - 1];
679 return count;
682 static void dither_init(void)
684 /* Voice codec should not reset the audio codec's dither data */
685 if (dsp != audio_dsp)
686 return;
688 memset(dither_data, 0, sizeof (dither_data));
689 dither_bias = (1L << (dsp->frac_bits - NATIVE_DEPTH));
690 dither_mask = (1L << (dsp->frac_bits + 1 - NATIVE_DEPTH)) - 1;
693 void dsp_dither_enable(bool enable)
695 /* Be sure audio dsp is current to set correct function */
696 struct dsp_config *old_dsp = switch_dsp(audio_dsp);
697 dither_enabled = enable;
698 sample_output_new_format();
699 switch_dsp(old_dsp);
702 /* Applies crossfeed to the stereo signal in src.
703 * Crossfeed is a process where listening over speakers is simulated. This
704 * is good for old hard panned stereo records, which might be quite fatiguing
705 * to listen to on headphones with no crossfeed.
707 #ifndef DSP_HAVE_ASM_CROSSFEED
708 static void apply_crossfeed(int count, int32_t *buf[])
710 int32_t *hist_l = &crossfeed_data.history[0];
711 int32_t *hist_r = &crossfeed_data.history[2];
712 int32_t *delay = &crossfeed_data.delay[0][0];
713 int32_t *coefs = &crossfeed_data.coefs[0];
714 int32_t gain = crossfeed_data.gain;
715 int32_t *di = crossfeed_data.index;
717 int32_t acc;
718 int32_t left, right;
719 int i;
721 for (i = 0; i < count; i++)
723 left = buf[0][i];
724 right = buf[1][i];
726 /* Filter delayed sample from left speaker */
727 ACC_INIT(acc, *di, coefs[0]);
728 ACC(acc, hist_l[0], coefs[1]);
729 ACC(acc, hist_l[1], coefs[2]);
730 /* Save filter history for left speaker */
731 hist_l[1] = GET_ACC(acc);
732 hist_l[0] = *di;
733 *di++ = left;
734 /* Filter delayed sample from right speaker */
735 ACC_INIT(acc, *di, coefs[0]);
736 ACC(acc, hist_r[0], coefs[1]);
737 ACC(acc, hist_r[1], coefs[2]);
738 /* Save filter history for right speaker */
739 hist_r[1] = GET_ACC(acc);
740 hist_r[0] = *di;
741 *di++ = right;
742 /* Now add the attenuated direct sound and write to outputs */
743 buf[0][i] = FRACMUL(left, gain) + hist_r[1];
744 buf[1][i] = FRACMUL(right, gain) + hist_l[1];
746 /* Wrap delay line index if bigger than delay line size */
747 if (di >= delay + 13*2)
748 di = delay;
750 /* Write back local copies of data we've modified */
751 crossfeed_data.index = di;
753 #endif /* DSP_HAVE_ASM_CROSSFEED */
756 * dsp_set_crossfeed(bool enable)
758 * !DSPPARAMSYNC
759 * needs syncing with changes to the following dsp parameters:
760 * * dsp->stereo_mode (A)
762 void dsp_set_crossfeed(bool enable)
764 crossfeed_enabled = enable;
765 audio_dsp->apply_crossfeed =
766 (enable && audio_dsp->data.num_channels > 1)
767 ? apply_crossfeed : NULL;
770 void dsp_set_crossfeed_direct_gain(int gain)
772 crossfeed_data.gain = get_replaygain_int(gain * -10) << 7;
773 /* If gain is negative, the calculation overflowed and we need to clamp */
774 if (crossfeed_data.gain < 0)
775 crossfeed_data.gain = 0x7fffffff;
778 /* Both gains should be below 0 dB (when inverted) */
779 void dsp_set_crossfeed_cross_params(long lf_gain, long hf_gain, long cutoff)
781 int32_t *c = crossfeed_data.coefs;
782 long scaler = get_replaygain_int(lf_gain * -10) << 7;
784 cutoff = 0xffffffff/NATIVE_FREQUENCY*cutoff;
785 hf_gain -= lf_gain;
786 /* Divide cutoff by sqrt(10^(-hf_gain/20)) to place cutoff at the -3 dB
787 * point instead of shelf midpoint. This is for compatibility with the old
788 * crossfeed shelf filter and should be removed if crossfeed settings are
789 * ever made incompatible for any other good reason.
791 cutoff = DIV64(cutoff, get_replaygain_int(-hf_gain*5), 24);
792 filter_shelf_coefs(cutoff, -hf_gain, false, c);
793 /* Scale coefs by LF gain and shift them to s0.31 format. We have no gains
794 * over 1 and can do this safely
796 c[0] = FRACMUL_SHL(c[0], scaler, 4);
797 c[1] = FRACMUL_SHL(c[1], scaler, 4);
798 c[2] <<= 4;
801 /* Apply a constant gain to the samples (e.g., for ReplayGain).
802 * Note that this must be called before the resampler.
804 #ifndef DSP_HAVE_ASM_APPLY_GAIN
805 static void dsp_apply_gain(int count, struct dsp_data *data, int32_t *buf[])
807 const int32_t gain = data->gain;
808 int ch = data->num_channels - 1;
812 int32_t *s = buf[ch];
813 int32_t *d = buf[ch];
814 int32_t samp = *s++;
815 int i = 0;
819 FRACMUL_8_LOOP(samp, gain, s, d);
821 while (++i < count);
823 while (--ch >= 0);
825 #endif /* DSP_HAVE_ASM_APPLY_GAIN */
827 /* Combine all gains to a global gain. */
828 static void set_gain(struct dsp_config *dsp)
830 dsp->data.gain = DEFAULT_GAIN;
832 /* Replay gain not relevant to voice */
833 if (dsp == audio_dsp && replaygain)
835 dsp->data.gain = replaygain;
838 if (eq_enabled && eq_precut)
840 dsp->data.gain =
841 (long) (((int64_t) dsp->data.gain * eq_precut) >> 24);
844 if (dsp->data.gain == DEFAULT_GAIN)
846 dsp->data.gain = 0;
848 else
850 dsp->data.gain >>= 1;
853 dsp->apply_gain = dsp->data.gain != 0 ? dsp_apply_gain : NULL;
857 * Use to enable the equalizer.
859 * @param enable true to enable the equalizer
861 void dsp_set_eq(bool enable)
863 eq_enabled = enable;
867 * Update the amount to cut the audio before applying the equalizer.
869 * @param precut to apply in decibels (multiplied by 10)
871 void dsp_set_eq_precut(int precut)
873 eq_precut = get_replaygain_int(precut * -10);
874 set_gain(audio_dsp);
875 set_gain(voice_dsp); /* For EQ precut */
879 * Synchronize the equalizer filter coefficients with the global settings.
881 * @param band the equalizer band to synchronize
883 void dsp_set_eq_coefs(int band)
885 const int *setting;
886 long gain;
887 unsigned long cutoff, q;
889 /* Adjust setting pointer to the band we actually want to change */
890 setting = &global_settings.eq_band0_cutoff + (band * 3);
892 /* Convert user settings to format required by coef generator functions */
893 cutoff = 0xffffffff / NATIVE_FREQUENCY * (*setting++);
894 q = *setting++;
895 gain = *setting++;
897 if (q == 0)
898 q = 1;
900 /* NOTE: The coef functions assume the EMAC unit is in fractional mode,
901 which it should be, since we're executed from the main thread. */
903 /* Assume a band is disabled if the gain is zero */
904 if (gain == 0)
906 eq_data.enabled[band] = 0;
908 else
910 if (band == 0)
911 eq_ls_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
912 else if (band == 4)
913 eq_hs_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
914 else
915 eq_pk_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
917 eq_data.enabled[band] = 1;
921 /* Apply EQ filters to those bands that have got it switched on. */
922 static void eq_process(int count, int32_t *buf[])
924 static const int shifts[] =
926 EQ_SHELF_SHIFT, /* low shelf */
927 EQ_PEAK_SHIFT, /* peaking */
928 EQ_PEAK_SHIFT, /* peaking */
929 EQ_PEAK_SHIFT, /* peaking */
930 EQ_SHELF_SHIFT, /* high shelf */
932 unsigned int channels = dsp->data.num_channels;
933 int i;
935 /* filter configuration currently is 1 low shelf filter, 3 band peaking
936 filters and 1 high shelf filter, in that order. we need to know this
937 so we can choose the correct shift factor.
939 for (i = 0; i < 5; i++)
941 if (!eq_data.enabled[i])
942 continue;
943 eq_filter(buf, &eq_data.filters[i], count, channels, shifts[i]);
947 void dsp_set_stereo_width(int value)
949 long width, straight, cross;
951 width = value * 0x7fffff / 100;
953 if (value <= 100)
955 straight = (0x7fffff + width) / 2;
956 cross = straight - width;
958 else
960 /* straight = (1 + width) / (2 * width) */
961 straight = ((int64_t)(0x7fffff + width) << 22) / width;
962 cross = straight - 0x7fffff;
965 dsp_sw_gain = straight << 8;
966 dsp_sw_cross = cross << 8;
969 #if CONFIG_CODEC == SWCODEC
971 #ifdef HAVE_SW_TONE_CONTROLS
972 static void set_tone_controls(void)
974 filter_bishelf_coefs(0xffffffff/NATIVE_FREQUENCY*200,
975 0xffffffff/NATIVE_FREQUENCY*3500,
976 bass, treble, -prescale, tone_filter.coefs);
978 #endif
980 /* Hook back from firmware/ part of audio, which can't/shouldn't call apps/
981 * code directly.
983 int dsp_callback(int msg, intptr_t param)
985 switch (msg) {
986 #ifdef HAVE_SW_TONE_CONTROLS
987 case DSP_CALLBACK_SET_PRESCALE:
988 prescale = param;
989 set_tone_controls();
990 break;
991 /* prescaler is always set after calling any of these, so we wait with
992 * calculating coefs until the above case is hit.
994 case DSP_CALLBACK_SET_BASS:
995 bass = param;
996 break;
997 case DSP_CALLBACK_SET_TREBLE:
998 treble = param;
999 #endif
1000 case DSP_CALLBACK_SET_CHANNEL_CONFIG:
1001 dsp_set_channel_config(param);
1002 break;
1003 case DSP_CALLBACK_SET_STEREO_WIDTH:
1004 dsp_set_stereo_width(param);
1005 break;
1006 default:
1007 break;
1009 return 0;
1011 #endif
1014 * Implements the different channel configurations and stereo width.
1017 /* SOUND_CHAN_STEREO mode is a noop so has no function - just outline one for
1018 * completeness. */
1019 #if 0
1020 static void channels_process_sound_chan_stereo(int count, int32_t *buf[])
1022 /* The channels are each just themselves */
1023 (void)count; (void)buf;
1025 #endif
1027 #ifndef DSP_HAVE_ASM_SOUND_CHAN_MONO
1028 static void channels_process_sound_chan_mono(int count, int32_t *buf[])
1030 int32_t *sl = buf[0], *sr = buf[1];
1034 int32_t lr = *sl/2 + *sr/2;
1035 *sl++ = lr;
1036 *sr++ = lr;
1038 while (--count > 0);
1040 #endif /* DSP_HAVE_ASM_SOUND_CHAN_MONO */
1042 #ifndef DSP_HAVE_ASM_SOUND_CHAN_CUSTOM
1043 static void channels_process_sound_chan_custom(int count, int32_t *buf[])
1045 const int32_t gain = dsp_sw_gain;
1046 const int32_t cross = dsp_sw_cross;
1047 int32_t *sl = buf[0], *sr = buf[1];
1051 int32_t l = *sl;
1052 int32_t r = *sr;
1053 *sl++ = FRACMUL(l, gain) + FRACMUL(r, cross);
1054 *sr++ = FRACMUL(r, gain) + FRACMUL(l, cross);
1056 while (--count > 0);
1058 #endif /* DSP_HAVE_ASM_SOUND_CHAN_CUSTOM */
1060 static void channels_process_sound_chan_mono_left(int count, int32_t *buf[])
1062 /* Just copy over the other channel */
1063 memcpy(buf[1], buf[0], count * sizeof (*buf));
1066 static void channels_process_sound_chan_mono_right(int count, int32_t *buf[])
1068 /* Just copy over the other channel */
1069 memcpy(buf[0], buf[1], count * sizeof (*buf));
1072 #ifndef DSP_HAVE_ASM_SOUND_CHAN_KARAOKE
1073 static void channels_process_sound_chan_karaoke(int count, int32_t *buf[])
1075 int32_t *sl = buf[0], *sr = buf[1];
1079 int32_t ch = *sl/2 - *sr/2;
1080 *sl++ = ch;
1081 *sr++ = -ch;
1083 while (--count > 0);
1085 #endif /* DSP_HAVE_ASM_SOUND_CHAN_KARAOKE */
1087 void dsp_set_channel_config(int value)
1089 static const channels_process_fn_type channels_process_functions[] =
1091 /* SOUND_CHAN_STEREO = All-purpose index for no channel processing */
1092 [SOUND_CHAN_STEREO] = NULL,
1093 [SOUND_CHAN_MONO] = channels_process_sound_chan_mono,
1094 [SOUND_CHAN_CUSTOM] = channels_process_sound_chan_custom,
1095 [SOUND_CHAN_MONO_LEFT] = channels_process_sound_chan_mono_left,
1096 [SOUND_CHAN_MONO_RIGHT] = channels_process_sound_chan_mono_right,
1097 [SOUND_CHAN_KARAOKE] = channels_process_sound_chan_karaoke,
1100 if ((unsigned)value >= ARRAYLEN(channels_process_functions) ||
1101 audio_dsp->stereo_mode == STEREO_MONO)
1102 value = SOUND_CHAN_STEREO;
1104 /* This doesn't apply to voice */
1105 channels_mode = value;
1106 audio_dsp->channels_process = channels_process_functions[value];
1109 /* Process and convert src audio to dst based on the DSP configuration,
1110 * reading count number of audio samples. dst is assumed to be large
1111 * enough; use dsp_output_count() to get the required number. src is an
1112 * array of pointers; for mono and interleaved stereo, it contains one
1113 * pointer to the start of the audio data and the other is ignored; for
1114 * non-interleaved stereo, it contains two pointers, one for each audio
1115 * channel. Returns number of bytes written to dst.
1117 int dsp_process(char *dst, const char *src[], int count)
1119 int32_t *tmp[2];
1120 int written = 0;
1121 int samples;
1123 #if defined(CPU_COLDFIRE)
1124 /* set emac unit for dsp processing, and save old macsr, we're running in
1125 codec thread context at this point, so can't clobber it */
1126 unsigned long old_macsr = coldfire_get_macsr();
1127 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
1128 #endif
1130 if (new_gain)
1131 dsp_set_replaygain(); /* Gain has changed */
1133 /* Testing function pointers for NULL is preferred since the pointer
1134 will be preloaded to be used for the call if not. */
1135 while (count > 0)
1137 samples = MIN(SAMPLE_BUF_COUNT/2, count);
1138 count -= samples;
1140 dsp->input_samples(samples, src, tmp);
1142 if (dsp->apply_gain)
1143 dsp->apply_gain(samples, &dsp->data, tmp);
1145 if (dsp->resample && (samples = resample(samples, tmp)) <= 0)
1146 break; /* I'm pretty sure we're downsampling here */
1148 if (dsp->apply_crossfeed)
1149 dsp->apply_crossfeed(samples, tmp);
1151 /* TODO: EQ and tone controls need separate structs for audio and voice
1152 * DSP processing thanks to filter history. isn't really audible now, but
1153 * might be the day we start handling voice more delicately. Planned
1154 * changes may well run all relevent channels through the same EQ so
1155 * perhaps not.
1157 if (eq_enabled)
1158 eq_process(samples, tmp);
1160 #ifdef HAVE_SW_TONE_CONTROLS
1161 if ((bass | treble) != 0)
1162 eq_filter(tmp, &tone_filter, samples, dsp->data.num_channels,
1163 FILTER_BISHELF_SHIFT);
1164 #endif
1166 if (dsp->channels_process)
1167 dsp->channels_process(samples, tmp);
1169 dsp->output_samples(samples, &dsp->data, tmp, (int16_t *)dst);
1171 written += samples;
1172 dst += samples * sizeof (int16_t) * 2;
1173 yield();
1176 #if defined(CPU_COLDFIRE)
1177 /* set old macsr again */
1178 coldfire_set_macsr(old_macsr);
1179 #endif
1180 return written;
1183 /* Given count number of input samples, calculate the maximum number of
1184 * samples of output data that would be generated (the calculation is not
1185 * entirely exact and rounds upwards to be on the safe side; during
1186 * resampling, the number of samples generated depends on the current state
1187 * of the resampler).
1189 /* dsp_input_size MUST be called afterwards */
1190 int dsp_output_count(int count)
1192 if (dsp->resample)
1194 count = (int)(((unsigned long)count * NATIVE_FREQUENCY
1195 + (dsp->frequency - 1)) / dsp->frequency);
1198 /* Now we have the resampled sample count which must not exceed
1199 * RESAMPLE_BUF_COUNT/2 to avoid resample buffer overflow. One
1200 * must call dsp_input_count() to get the correct input sample
1201 * count.
1203 if (count > RESAMPLE_BUF_COUNT/2)
1204 count = RESAMPLE_BUF_COUNT/2;
1206 return count;
1209 /* Given count output samples, calculate number of input samples
1210 * that would be consumed in order to fill the output buffer.
1212 int dsp_input_count(int count)
1214 /* count is now the number of resampled input samples. Convert to
1215 original input samples. */
1216 if (dsp->resample)
1218 /* Use the real resampling delta =
1219 * dsp->frequency * 65536 / NATIVE_FREQUENCY, and
1220 * round towards zero to avoid buffer overflows. */
1221 count = (int)(((unsigned long)count *
1222 dsp->data.resample_data.delta) >> 16);
1225 return count;
1228 int dsp_stereo_mode(void)
1230 return dsp->stereo_mode;
1233 static void dsp_set_gain_var(long *var, long value)
1235 /* Voice shouldn't mess with these */
1236 if (dsp == audio_dsp)
1238 *var = value;
1239 new_gain = true;
1243 static void dsp_update_functions(void)
1245 sample_input_new_format();
1246 sample_output_new_format();
1247 if (dsp == audio_dsp)
1248 dsp_set_crossfeed(crossfeed_enabled);
1251 bool dsp_configure(int setting, intptr_t value)
1253 switch (setting)
1255 case DSP_SWITCH_CODEC:
1256 if ((uintptr_t)value <= 1)
1257 switch_dsp(&dsp_conf[value]);
1258 break;
1260 case DSP_SET_FREQUENCY:
1261 memset(&dsp->data.resample_data, 0,
1262 sizeof (dsp->data.resample_data));
1263 /* Fall through!!! */
1264 case DSP_SWITCH_FREQUENCY:
1265 dsp->codec_frequency = (value == 0) ? NATIVE_FREQUENCY : value;
1266 /* Account for playback speed adjustment when setting dsp->frequency
1267 if we're called from the main audio thread. Voice UI thread should
1268 not need this feature.
1270 if (dsp == audio_dsp)
1271 dsp->frequency = pitch_ratio * dsp->codec_frequency / 1000;
1272 else
1273 dsp->frequency = dsp->codec_frequency;
1275 resampler_new_delta();
1276 break;
1278 case DSP_SET_SAMPLE_DEPTH:
1279 dsp->sample_depth = value;
1281 if (dsp->sample_depth <= NATIVE_DEPTH)
1283 dsp->frac_bits = WORD_FRACBITS;
1284 dsp->sample_bytes = sizeof (int16_t); /* samples are 16 bits */
1285 dsp->data.clip_max = ((1 << WORD_FRACBITS) - 1);
1286 dsp->data.clip_min = -((1 << WORD_FRACBITS));
1288 else
1290 dsp->frac_bits = value;
1291 dsp->sample_bytes = sizeof (int32_t); /* samples are 32 bits */
1292 dsp->data.clip_max = (1 << value) - 1;
1293 dsp->data.clip_min = -(1 << value);
1296 dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
1297 sample_input_new_format();
1298 dither_init();
1299 break;
1301 case DSP_SET_STEREO_MODE:
1302 dsp->stereo_mode = value;
1303 dsp->data.num_channels = value == STEREO_MONO ? 1 : 2;
1304 dsp_update_functions();
1305 break;
1307 case DSP_RESET:
1308 dsp->stereo_mode = STEREO_NONINTERLEAVED;
1309 dsp->data.num_channels = 2;
1310 dsp->sample_depth = NATIVE_DEPTH;
1311 dsp->frac_bits = WORD_FRACBITS;
1312 dsp->sample_bytes = sizeof (int16_t);
1313 dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
1314 dsp->data.clip_max = ((1 << WORD_FRACBITS) - 1);
1315 dsp->data.clip_min = -((1 << WORD_FRACBITS));
1316 dsp->codec_frequency = dsp->frequency = NATIVE_FREQUENCY;
1318 if (dsp == audio_dsp)
1320 track_gain = 0;
1321 album_gain = 0;
1322 track_peak = 0;
1323 album_peak = 0;
1324 new_gain = true;
1327 dsp_update_functions();
1328 resampler_new_delta();
1329 break;
1331 case DSP_FLUSH:
1332 memset(&dsp->data.resample_data, 0,
1333 sizeof (dsp->data.resample_data));
1334 resampler_new_delta();
1335 dither_init();
1336 break;
1338 case DSP_SET_TRACK_GAIN:
1339 dsp_set_gain_var(&track_gain, value);
1340 break;
1342 case DSP_SET_ALBUM_GAIN:
1343 dsp_set_gain_var(&album_gain, value);
1344 break;
1346 case DSP_SET_TRACK_PEAK:
1347 dsp_set_gain_var(&track_peak, value);
1348 break;
1350 case DSP_SET_ALBUM_PEAK:
1351 dsp_set_gain_var(&album_peak, value);
1352 break;
1354 default:
1355 return 0;
1358 return 1;
1361 void dsp_set_replaygain(void)
1363 long gain = 0;
1365 new_gain = false;
1367 if (global_settings.replaygain || global_settings.replaygain_noclip)
1369 bool track_mode = get_replaygain_mode(track_gain != 0,
1370 album_gain != 0) == REPLAYGAIN_TRACK;
1371 long peak = (track_mode || !album_peak) ? track_peak : album_peak;
1373 if (global_settings.replaygain)
1375 gain = (track_mode || !album_gain) ? track_gain : album_gain;
1377 if (global_settings.replaygain_preamp)
1379 long preamp = get_replaygain_int(
1380 global_settings.replaygain_preamp * 10);
1382 gain = (long) (((int64_t) gain * preamp) >> 24);
1386 if (gain == 0)
1388 /* So that noclip can work even with no gain information. */
1389 gain = DEFAULT_GAIN;
1392 if (global_settings.replaygain_noclip && (peak != 0)
1393 && ((((int64_t) gain * peak) >> 24) >= DEFAULT_GAIN))
1395 gain = (((int64_t) DEFAULT_GAIN << 24) / peak);
1398 if (gain == DEFAULT_GAIN)
1400 /* Nothing to do, disable processing. */
1401 gain = 0;
1405 /* Store in S8.23 format to simplify calculations. */
1406 replaygain = gain;
1407 set_gain(audio_dsp);