rbutil: fix voice download for some targets. (again naming issues)
[kugel-rb.git] / apps / dsp.c
blob496e333bc582b7c100d26c5eeeba27b66613b8a4
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"
35 #include "tdspeed.h"
36 #include "buffer.h"
38 /* 16-bit samples are scaled based on these constants. The shift should be
39 * no more than 15.
41 #define WORD_SHIFT 12
42 #define WORD_FRACBITS 27
44 #define NATIVE_DEPTH 16
45 /* If the small buffer size changes, check the assembly code! */
46 #define SMALL_SAMPLE_BUF_COUNT 256
47 #define DEFAULT_GAIN 0x01000000
49 /* enums to index conversion properly with stereo mode and other settings */
50 enum
52 SAMPLE_INPUT_LE_NATIVE_I_STEREO = STEREO_INTERLEAVED,
53 SAMPLE_INPUT_LE_NATIVE_NI_STEREO = STEREO_NONINTERLEAVED,
54 SAMPLE_INPUT_LE_NATIVE_MONO = STEREO_MONO,
55 SAMPLE_INPUT_GT_NATIVE_I_STEREO = STEREO_INTERLEAVED + STEREO_NUM_MODES,
56 SAMPLE_INPUT_GT_NATIVE_NI_STEREO = STEREO_NONINTERLEAVED + STEREO_NUM_MODES,
57 SAMPLE_INPUT_GT_NATIVE_MONO = STEREO_MONO + STEREO_NUM_MODES,
58 SAMPLE_INPUT_GT_NATIVE_1ST_INDEX = STEREO_NUM_MODES
61 enum
63 SAMPLE_OUTPUT_MONO = 0,
64 SAMPLE_OUTPUT_STEREO,
65 SAMPLE_OUTPUT_DITHERED_MONO,
66 SAMPLE_OUTPUT_DITHERED_STEREO
69 /****************************************************************************
70 * NOTE: Any assembly routines that use these structures must be updated
71 * if current data members are moved or changed.
73 struct resample_data
75 uint32_t delta; /* 00h */
76 uint32_t phase; /* 04h */
77 int32_t last_sample[2]; /* 08h */
78 /* 10h */
81 /* This is for passing needed data to assembly dsp routines. If another
82 * dsp parameter needs to be passed, add to the end of the structure
83 * and remove from dsp_config.
84 * If another function type becomes assembly optimized and requires dsp
85 * config info, add a pointer paramter of type "struct dsp_data *".
86 * If removing something from other than the end, reserve the spot or
87 * else update every implementation for every target.
88 * Be sure to add the offset of the new member for easy viewing as well. :)
89 * It is the first member of dsp_config and all members can be accessesed
90 * through the main aggregate but this is intended to make a safe haven
91 * for these items whereas the c part can be rearranged at will. dsp_data
92 * could even moved within dsp_config without disurbing the order.
94 struct dsp_data
96 int output_scale; /* 00h */
97 int num_channels; /* 04h */
98 struct resample_data resample_data; /* 08h */
99 int32_t clip_min; /* 18h */
100 int32_t clip_max; /* 1ch */
101 int32_t gain; /* 20h - Note that this is in S8.23 format. */
102 /* 24h */
105 /* No asm...yet */
106 struct dither_data
108 long error[3]; /* 00h */
109 long random; /* 0ch */
110 /* 10h */
113 struct crossfeed_data
115 int32_t gain; /* 00h - Direct path gain */
116 int32_t coefs[3]; /* 04h - Coefficients for the shelving filter */
117 int32_t history[4]; /* 10h - Format is x[n - 1], y[n - 1] for both channels */
118 int32_t delay[13][2]; /* 20h */
119 int32_t *index; /* 88h - Current pointer into the delay line */
120 /* 8ch */
123 /* Current setup is one lowshelf filters three peaking filters and one
124 * highshelf filter. Varying the number of shelving filters make no sense,
125 * but adding peaking filters is possible.
127 struct eq_state
129 char enabled[5]; /* 00h - Flags for active filters */
130 struct eqfilter filters[5]; /* 08h - packing is 4? */
131 /* 10ch */
134 /* Include header with defines which functions are implemented in assembly
135 code for the target */
136 #include <dsp_asm.h>
138 /* Typedefs keep things much neater in this case */
139 typedef void (*sample_input_fn_type)(int count, const char *src[],
140 int32_t *dst[]);
141 typedef int (*resample_fn_type)(int count, struct dsp_data *data,
142 const int32_t *src[], int32_t *dst[]);
143 typedef void (*sample_output_fn_type)(int count, struct dsp_data *data,
144 const 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 bool tdspeed_enabled; /* User has enabled timestretch */
165 int tdspeed_percent; /* Speed % */
166 bool tdspeed_active; /* Timestretch is in use */
167 int frac_bits;
168 #ifdef HAVE_SW_TONE_CONTROLS
169 /* Filter struct for software bass/treble controls */
170 struct eqfilter tone_filter;
171 #endif
172 /* Functions that change depending upon settings - NULL if stage is
173 disabled */
174 sample_input_fn_type input_samples;
175 resample_fn_type resample;
176 sample_output_fn_type output_samples;
177 /* These will be NULL for the voice codec and is more economical that
178 way */
179 channels_process_dsp_fn_type apply_gain;
180 channels_process_fn_type apply_crossfeed;
181 channels_process_fn_type eq_process;
182 channels_process_fn_type channels_process;
185 /* General DSP config */
186 static struct dsp_config dsp_conf[2] IBSS_ATTR; /* 0=A, 1=V */
187 /* Dithering */
188 static struct dither_data dither_data[2] IBSS_ATTR; /* 0=left, 1=right */
189 static long dither_mask IBSS_ATTR;
190 static long dither_bias IBSS_ATTR;
191 /* Crossfeed */
192 struct crossfeed_data crossfeed_data IDATA_ATTR = /* A */
194 .index = (int32_t *)crossfeed_data.delay
197 /* Equalizer */
198 static struct eq_state eq_data; /* A */
200 /* Software tone controls */
201 #ifdef HAVE_SW_TONE_CONTROLS
202 static int prescale; /* A/V */
203 static int bass; /* A/V */
204 static int treble; /* A/V */
205 #endif
207 /* Settings applicable to audio codec only */
208 static int pitch_ratio = 1000;
209 static int channels_mode;
210 long dsp_sw_gain;
211 long dsp_sw_cross;
212 static bool dither_enabled;
213 static long eq_precut;
214 static long track_gain;
215 static bool new_gain;
216 static long album_gain;
217 static long track_peak;
218 static long album_peak;
219 static long replaygain;
220 static bool crossfeed_enabled;
222 #define AUDIO_DSP (dsp_conf[CODEC_IDX_AUDIO])
223 #define VOICE_DSP (dsp_conf[CODEC_IDX_VOICE])
225 /* The internal format is 32-bit samples, non-interleaved, stereo. This
226 * format is similar to the raw output from several codecs, so the amount
227 * of copying needed is minimized for that case.
230 #define RESAMPLE_RATIO 4 /* Enough for 11,025 Hz -> 44,100 Hz */
232 static int32_t small_sample_buf[SMALL_SAMPLE_BUF_COUNT] IBSS_ATTR;
233 static int32_t small_resample_buf[SMALL_SAMPLE_BUF_COUNT * RESAMPLE_RATIO] IBSS_ATTR;
235 static int32_t *big_sample_buf = NULL;
236 static int32_t *big_resample_buf = NULL;
237 static int big_sample_buf_count = -1; /* -1=unknown, 0=not available */
239 static int sample_buf_count;
240 static int32_t *sample_buf;
241 static int32_t *resample_buf;
243 #define SAMPLE_BUF_LEFT_CHANNEL 0
244 #define SAMPLE_BUF_RIGHT_CHANNEL (sample_buf_count/2)
245 #define RESAMPLE_BUF_LEFT_CHANNEL 0
246 #define RESAMPLE_BUF_RIGHT_CHANNEL (sample_buf_count/2 * RESAMPLE_RATIO)
249 /* Clip sample to signed 16 bit range */
250 static inline int32_t clip_sample_16(int32_t sample)
252 if ((int16_t)sample != sample)
253 sample = 0x7fff ^ (sample >> 31);
254 return sample;
257 int sound_get_pitch(void)
259 return pitch_ratio;
262 void sound_set_pitch(int permille)
264 pitch_ratio = permille;
265 dsp_configure(&AUDIO_DSP, DSP_SWITCH_FREQUENCY,
266 AUDIO_DSP.codec_frequency);
269 void tdspeed_setup(struct dsp_config *dspc)
271 dspc->tdspeed_active = false;
272 if (dspc == &AUDIO_DSP)
274 if (!dspc->tdspeed_enabled)
275 return;
276 if (dspc->tdspeed_percent == 0)
277 dspc->tdspeed_percent = 100;
278 if (!tdspeed_config(
279 dspc->codec_frequency == 0 ? NATIVE_FREQUENCY : dspc->codec_frequency,
280 dspc->stereo_mode != STEREO_MONO,
281 dspc->tdspeed_percent))
282 return;
283 if (dspc->tdspeed_percent == 100 || big_sample_buf_count <= 0)
284 return;
285 dspc->tdspeed_active = true;
289 void dsp_timestretch_enable(bool enable)
291 if (enable)
293 /* Set up timestretch buffers on first enable */
294 if (big_sample_buf_count < 0)
296 big_sample_buf_count = SMALL_SAMPLE_BUF_COUNT * RESAMPLE_RATIO;
297 big_sample_buf = small_resample_buf;
298 big_resample_buf = (int32_t *) buffer_alloc(big_sample_buf_count * RESAMPLE_RATIO * sizeof(int32_t));
301 else
303 /* If not enabled at startup, buffers will never be available */
304 if (big_sample_buf_count < 0)
305 big_sample_buf_count = 0;
307 AUDIO_DSP.tdspeed_enabled = enable;
308 tdspeed_setup(&AUDIO_DSP);
311 void dsp_set_timestretch(int percent)
313 AUDIO_DSP.tdspeed_percent = percent;
314 tdspeed_setup(&AUDIO_DSP);
317 int dsp_get_timestretch()
319 return AUDIO_DSP.tdspeed_percent;
322 bool dsp_timestretch_enabled()
324 return (AUDIO_DSP.tdspeed_enabled && big_sample_buf_count > 0);
327 /* Convert count samples to the internal format, if needed. Updates src
328 * to point past the samples "consumed" and dst is set to point to the
329 * samples to consume. Note that for mono, dst[0] equals dst[1], as there
330 * is no point in processing the same data twice.
333 /* convert count 16-bit mono to 32-bit mono */
334 static void sample_input_lte_native_mono(
335 int count, const char *src[], int32_t *dst[])
337 const int16_t *s = (int16_t *) src[0];
338 const int16_t * const send = s + count;
339 int32_t *d = dst[0] = dst[1] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
340 int scale = WORD_SHIFT;
344 *d++ = *s++ << scale;
346 while (s < send);
348 src[0] = (char *)s;
351 /* convert count 16-bit interleaved stereo to 32-bit noninterleaved */
352 static void sample_input_lte_native_i_stereo(
353 int count, const char *src[], int32_t *dst[])
355 const int32_t *s = (int32_t *) src[0];
356 const int32_t * const send = s + count;
357 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
358 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
359 int scale = WORD_SHIFT;
363 int32_t slr = *s++;
364 #ifdef ROCKBOX_LITTLE_ENDIAN
365 *dl++ = (slr >> 16) << scale;
366 *dr++ = (int32_t)(int16_t)slr << scale;
367 #else /* ROCKBOX_BIG_ENDIAN */
368 *dl++ = (int32_t)(int16_t)slr << scale;
369 *dr++ = (slr >> 16) << scale;
370 #endif
372 while (s < send);
374 src[0] = (char *)s;
377 /* convert count 16-bit noninterleaved stereo to 32-bit noninterleaved */
378 static void sample_input_lte_native_ni_stereo(
379 int count, const char *src[], int32_t *dst[])
381 const int16_t *sl = (int16_t *) src[0];
382 const int16_t *sr = (int16_t *) src[1];
383 const int16_t * const slend = sl + count;
384 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
385 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
386 int scale = WORD_SHIFT;
390 *dl++ = *sl++ << scale;
391 *dr++ = *sr++ << scale;
393 while (sl < slend);
395 src[0] = (char *)sl;
396 src[1] = (char *)sr;
399 /* convert count 32-bit mono to 32-bit mono */
400 static void sample_input_gt_native_mono(
401 int count, const char *src[], int32_t *dst[])
403 dst[0] = dst[1] = (int32_t *)src[0];
404 src[0] = (char *)(dst[0] + count);
407 /* convert count 32-bit interleaved stereo to 32-bit noninterleaved stereo */
408 static void sample_input_gt_native_i_stereo(
409 int count, const char *src[], int32_t *dst[])
411 const int32_t *s = (int32_t *)src[0];
412 const int32_t * const send = s + 2*count;
413 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
414 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
418 *dl++ = *s++;
419 *dr++ = *s++;
421 while (s < send);
423 src[0] = (char *)send;
426 /* convert 32 bit-noninterleaved stereo to 32-bit noninterleaved stereo */
427 static void sample_input_gt_native_ni_stereo(
428 int count, const char *src[], int32_t *dst[])
430 dst[0] = (int32_t *)src[0];
431 dst[1] = (int32_t *)src[1];
432 src[0] = (char *)(dst[0] + count);
433 src[1] = (char *)(dst[1] + count);
437 * sample_input_new_format()
439 * set the to-native sample conversion function based on dsp sample parameters
441 * !DSPPARAMSYNC
442 * needs syncing with changes to the following dsp parameters:
443 * * dsp->stereo_mode (A/V)
444 * * dsp->sample_depth (A/V)
446 static void sample_input_new_format(struct dsp_config *dsp)
448 static const sample_input_fn_type sample_input_functions[] =
450 [SAMPLE_INPUT_LE_NATIVE_MONO] = sample_input_lte_native_mono,
451 [SAMPLE_INPUT_LE_NATIVE_I_STEREO] = sample_input_lte_native_i_stereo,
452 [SAMPLE_INPUT_LE_NATIVE_NI_STEREO] = sample_input_lte_native_ni_stereo,
453 [SAMPLE_INPUT_GT_NATIVE_MONO] = sample_input_gt_native_mono,
454 [SAMPLE_INPUT_GT_NATIVE_I_STEREO] = sample_input_gt_native_i_stereo,
455 [SAMPLE_INPUT_GT_NATIVE_NI_STEREO] = sample_input_gt_native_ni_stereo,
458 int convert = dsp->stereo_mode;
460 if (dsp->sample_depth > NATIVE_DEPTH)
461 convert += SAMPLE_INPUT_GT_NATIVE_1ST_INDEX;
463 dsp->input_samples = sample_input_functions[convert];
467 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO
468 /* write mono internal format to output format */
469 static void sample_output_mono(int count, struct dsp_data *data,
470 const int32_t *src[], int16_t *dst)
472 const int32_t *s0 = src[0];
473 const int scale = data->output_scale;
474 const int dc_bias = 1 << (scale - 1);
478 int32_t lr = clip_sample_16((*s0++ + dc_bias) >> scale);
479 *dst++ = lr;
480 *dst++ = lr;
482 while (--count > 0);
484 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO */
486 /* write stereo internal format to output format */
487 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO
488 static void sample_output_stereo(int count, struct dsp_data *data,
489 const int32_t *src[], int16_t *dst)
491 const int32_t *s0 = src[0];
492 const int32_t *s1 = src[1];
493 const int scale = data->output_scale;
494 const int dc_bias = 1 << (scale - 1);
498 *dst++ = clip_sample_16((*s0++ + dc_bias) >> scale);
499 *dst++ = clip_sample_16((*s1++ + dc_bias) >> scale);
501 while (--count > 0);
503 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO */
506 * The "dither" code to convert the 24-bit samples produced by libmad was
507 * taken from the coolplayer project - coolplayer.sourceforge.net
509 * This function handles mono and stereo outputs.
511 static void sample_output_dithered(int count, struct dsp_data *data,
512 const int32_t *src[], int16_t *dst)
514 const int32_t mask = dither_mask;
515 const int32_t bias = dither_bias;
516 const int scale = data->output_scale;
517 const int32_t min = data->clip_min;
518 const int32_t max = data->clip_max;
519 const int32_t range = max - min;
520 int ch;
521 int16_t *d;
523 for (ch = 0; ch < data->num_channels; ch++)
525 struct dither_data * const dither = &dither_data[ch];
526 const int32_t *s = src[ch];
527 int i;
529 for (i = 0, d = &dst[ch]; i < count; i++, s++, d += 2)
531 int32_t output, sample;
532 int32_t random;
534 /* Noise shape and bias (for correct rounding later) */
535 sample = *s;
536 sample += dither->error[0] - dither->error[1] + dither->error[2];
537 dither->error[2] = dither->error[1];
538 dither->error[1] = dither->error[0]/2;
540 output = sample + bias;
542 /* Dither, highpass triangle PDF */
543 random = dither->random*0x0019660dL + 0x3c6ef35fL;
544 output += (random & mask) - (dither->random & mask);
545 dither->random = random;
547 /* Round sample to output range */
548 output &= ~mask;
550 /* Error feedback */
551 dither->error[0] = sample - output;
553 /* Clip */
554 if ((uint32_t)(output - min) > (uint32_t)range)
556 int32_t c = min;
557 if (output > min)
558 c += range;
559 output = c;
562 /* Quantize and store */
563 *d = output >> scale;
567 if (data->num_channels == 2)
568 return;
570 /* Have to duplicate left samples into the right channel since
571 pcm buffer and hardware is interleaved stereo */
572 d = &dst[0];
576 int16_t s = *d++;
577 *d++ = s;
579 while (--count > 0);
583 * sample_output_new_format()
585 * set the from-native to ouput sample conversion routine
587 * !DSPPARAMSYNC
588 * needs syncing with changes to the following dsp parameters:
589 * * dsp->stereo_mode (A/V)
590 * * dither_enabled (A)
592 static void sample_output_new_format(struct dsp_config *dsp)
594 static const sample_output_fn_type sample_output_functions[] =
596 sample_output_mono,
597 sample_output_stereo,
598 sample_output_dithered,
599 sample_output_dithered
602 int out = dsp->data.num_channels - 1;
604 if (dsp == &AUDIO_DSP && dither_enabled)
605 out += 2;
607 dsp->output_samples = sample_output_functions[out];
611 * Linear interpolation resampling that introduces a one sample delay because
612 * of our inability to look into the future at the end of a frame.
614 #ifndef DSP_HAVE_ASM_RESAMPLING
615 static int dsp_downsample(int count, struct dsp_data *data,
616 const int32_t *src[], int32_t *dst[])
618 int ch = data->num_channels - 1;
619 uint32_t delta = data->resample_data.delta;
620 uint32_t phase, pos;
621 int32_t *d;
623 /* Rolled channel loop actually showed slightly faster. */
626 /* Just initialize things and not worry too much about the relatively
627 * uncommon case of not being able to spit out a sample for the frame.
629 const int32_t *s = src[ch];
630 int32_t last = data->resample_data.last_sample[ch];
632 data->resample_data.last_sample[ch] = s[count - 1];
633 d = dst[ch];
634 phase = data->resample_data.phase;
635 pos = phase >> 16;
637 /* Do we need last sample of previous frame for interpolation? */
638 if (pos > 0)
639 last = s[pos - 1];
641 while (pos < (uint32_t)count)
643 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last);
644 phase += delta;
645 pos = phase >> 16;
646 last = s[pos - 1];
649 while (--ch >= 0);
651 /* Wrap phase accumulator back to start of next frame. */
652 data->resample_data.phase = phase - (count << 16);
653 return d - dst[0];
656 static int dsp_upsample(int count, struct dsp_data *data,
657 const int32_t *src[], int32_t *dst[])
659 int ch = data->num_channels - 1;
660 uint32_t delta = data->resample_data.delta;
661 uint32_t phase, pos;
662 int32_t *d;
664 /* Rolled channel loop actually showed slightly faster. */
667 /* Should always be able to output a sample for a ratio up to RESAMPLE_RATIO */
668 const int32_t *s = src[ch];
669 int32_t last = data->resample_data.last_sample[ch];
671 data->resample_data.last_sample[ch] = s[count - 1];
672 d = dst[ch];
673 phase = data->resample_data.phase;
674 pos = phase >> 16;
676 while (pos == 0)
678 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[0] - last);
679 phase += delta;
680 pos = phase >> 16;
683 while (pos < (uint32_t)count)
685 last = s[pos - 1];
686 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last);
687 phase += delta;
688 pos = phase >> 16;
691 while (--ch >= 0);
693 /* Wrap phase accumulator back to start of next frame. */
694 data->resample_data.phase = phase & 0xffff;
695 return d - dst[0];
697 #endif /* DSP_HAVE_ASM_RESAMPLING */
699 static void resampler_new_delta(struct dsp_config *dsp)
701 dsp->data.resample_data.delta = (unsigned long)
702 dsp->frequency * 65536LL / NATIVE_FREQUENCY;
704 if (dsp->frequency == NATIVE_FREQUENCY)
706 /* NOTE: If fully glitch-free transistions from no resampling to
707 resampling are desired, last_sample history should be maintained
708 even when not resampling. */
709 dsp->resample = NULL;
710 dsp->data.resample_data.phase = 0;
711 dsp->data.resample_data.last_sample[0] = 0;
712 dsp->data.resample_data.last_sample[1] = 0;
714 else if (dsp->frequency < NATIVE_FREQUENCY)
715 dsp->resample = dsp_upsample;
716 else
717 dsp->resample = dsp_downsample;
720 /* Resample count stereo samples. Updates the src array, if resampling is
721 * done, to refer to the resampled data. Returns number of stereo samples
722 * for further processing.
724 static inline int resample(struct dsp_config *dsp, int count, int32_t *src[])
726 int32_t *dst[2] =
728 &resample_buf[RESAMPLE_BUF_LEFT_CHANNEL],
729 &resample_buf[RESAMPLE_BUF_RIGHT_CHANNEL],
732 count = dsp->resample(count, &dsp->data, (const int32_t **)src, dst);
734 src[0] = dst[0];
735 src[1] = dst[dsp->data.num_channels - 1];
737 return count;
740 static void dither_init(struct dsp_config *dsp)
742 memset(dither_data, 0, sizeof (dither_data));
743 dither_bias = (1L << (dsp->frac_bits - NATIVE_DEPTH));
744 dither_mask = (1L << (dsp->frac_bits + 1 - NATIVE_DEPTH)) - 1;
747 void dsp_dither_enable(bool enable)
749 struct dsp_config *dsp = &AUDIO_DSP;
750 dither_enabled = enable;
751 sample_output_new_format(dsp);
754 /* Applies crossfeed to the stereo signal in src.
755 * Crossfeed is a process where listening over speakers is simulated. This
756 * is good for old hard panned stereo records, which might be quite fatiguing
757 * to listen to on headphones with no crossfeed.
759 #ifndef DSP_HAVE_ASM_CROSSFEED
760 static void apply_crossfeed(int count, int32_t *buf[])
762 int32_t *hist_l = &crossfeed_data.history[0];
763 int32_t *hist_r = &crossfeed_data.history[2];
764 int32_t *delay = &crossfeed_data.delay[0][0];
765 int32_t *coefs = &crossfeed_data.coefs[0];
766 int32_t gain = crossfeed_data.gain;
767 int32_t *di = crossfeed_data.index;
769 int32_t acc;
770 int32_t left, right;
771 int i;
773 for (i = 0; i < count; i++)
775 left = buf[0][i];
776 right = buf[1][i];
778 /* Filter delayed sample from left speaker */
779 acc = FRACMUL(*di, coefs[0]);
780 acc += FRACMUL(hist_l[0], coefs[1]);
781 acc += FRACMUL(hist_l[1], coefs[2]);
782 /* Save filter history for left speaker */
783 hist_l[1] = acc;
784 hist_l[0] = *di;
785 *di++ = left;
786 /* Filter delayed sample from right speaker */
787 acc = FRACMUL(*di, coefs[0]);
788 acc += FRACMUL(hist_r[0], coefs[1]);
789 acc += FRACMUL(hist_r[1], coefs[2]);
790 /* Save filter history for right speaker */
791 hist_r[1] = acc;
792 hist_r[0] = *di;
793 *di++ = right;
794 /* Now add the attenuated direct sound and write to outputs */
795 buf[0][i] = FRACMUL(left, gain) + hist_r[1];
796 buf[1][i] = FRACMUL(right, gain) + hist_l[1];
798 /* Wrap delay line index if bigger than delay line size */
799 if (di >= delay + 13*2)
800 di = delay;
802 /* Write back local copies of data we've modified */
803 crossfeed_data.index = di;
805 #endif /* DSP_HAVE_ASM_CROSSFEED */
808 * dsp_set_crossfeed(bool enable)
810 * !DSPPARAMSYNC
811 * needs syncing with changes to the following dsp parameters:
812 * * dsp->stereo_mode (A)
814 void dsp_set_crossfeed(bool enable)
816 crossfeed_enabled = enable;
817 AUDIO_DSP.apply_crossfeed = (enable && AUDIO_DSP.data.num_channels > 1)
818 ? apply_crossfeed : NULL;
821 void dsp_set_crossfeed_direct_gain(int gain)
823 crossfeed_data.gain = get_replaygain_int(gain * 10) << 7;
824 /* If gain is negative, the calculation overflowed and we need to clamp */
825 if (crossfeed_data.gain < 0)
826 crossfeed_data.gain = 0x7fffffff;
829 /* Both gains should be below 0 dB */
830 void dsp_set_crossfeed_cross_params(long lf_gain, long hf_gain, long cutoff)
832 int32_t *c = crossfeed_data.coefs;
833 long scaler = get_replaygain_int(lf_gain * 10) << 7;
835 cutoff = 0xffffffff/NATIVE_FREQUENCY*cutoff;
836 hf_gain -= lf_gain;
837 /* Divide cutoff by sqrt(10^(hf_gain/20)) to place cutoff at the -3 dB
838 * point instead of shelf midpoint. This is for compatibility with the old
839 * crossfeed shelf filter and should be removed if crossfeed settings are
840 * ever made incompatible for any other good reason.
842 cutoff = DIV64(cutoff, get_replaygain_int(hf_gain*5), 24);
843 filter_shelf_coefs(cutoff, hf_gain, false, c);
844 /* Scale coefs by LF gain and shift them to s0.31 format. We have no gains
845 * over 1 and can do this safely
847 c[0] = FRACMUL_SHL(c[0], scaler, 4);
848 c[1] = FRACMUL_SHL(c[1], scaler, 4);
849 c[2] <<= 4;
852 /* Apply a constant gain to the samples (e.g., for ReplayGain).
853 * Note that this must be called before the resampler.
855 #ifndef DSP_HAVE_ASM_APPLY_GAIN
856 static void dsp_apply_gain(int count, struct dsp_data *data, int32_t *buf[])
858 const int32_t gain = data->gain;
859 int ch;
861 for (ch = 0; ch < data->num_channels; ch++)
863 int32_t *d = buf[ch];
864 int i;
866 for (i = 0; i < count; i++)
867 d[i] = FRACMUL_SHL(d[i], gain, 8);
870 #endif /* DSP_HAVE_ASM_APPLY_GAIN */
872 /* Combine all gains to a global gain. */
873 static void set_gain(struct dsp_config *dsp)
875 dsp->data.gain = DEFAULT_GAIN;
877 /* Replay gain not relevant to voice */
878 if (dsp == &AUDIO_DSP && replaygain)
880 dsp->data.gain = replaygain;
883 if (dsp->eq_process && eq_precut)
885 dsp->data.gain =
886 (long) (((int64_t) dsp->data.gain * eq_precut) >> 24);
889 if (dsp->data.gain == DEFAULT_GAIN)
891 dsp->data.gain = 0;
893 else
895 dsp->data.gain >>= 1;
898 dsp->apply_gain = dsp->data.gain != 0 ? dsp_apply_gain : NULL;
902 * Update the amount to cut the audio before applying the equalizer.
904 * @param precut to apply in decibels (multiplied by 10)
906 void dsp_set_eq_precut(int precut)
908 eq_precut = get_replaygain_int(precut * -10);
909 set_gain(&AUDIO_DSP);
913 * Synchronize the equalizer filter coefficients with the global settings.
915 * @param band the equalizer band to synchronize
917 void dsp_set_eq_coefs(int band)
919 const int *setting;
920 long gain;
921 unsigned long cutoff, q;
923 /* Adjust setting pointer to the band we actually want to change */
924 setting = &global_settings.eq_band0_cutoff + (band * 3);
926 /* Convert user settings to format required by coef generator functions */
927 cutoff = 0xffffffff / NATIVE_FREQUENCY * (*setting++);
928 q = *setting++;
929 gain = *setting++;
931 if (q == 0)
932 q = 1;
934 /* NOTE: The coef functions assume the EMAC unit is in fractional mode,
935 which it should be, since we're executed from the main thread. */
937 /* Assume a band is disabled if the gain is zero */
938 if (gain == 0)
940 eq_data.enabled[band] = 0;
942 else
944 if (band == 0)
945 eq_ls_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
946 else if (band == 4)
947 eq_hs_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
948 else
949 eq_pk_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
951 eq_data.enabled[band] = 1;
955 /* Apply EQ filters to those bands that have got it switched on. */
956 static void eq_process(int count, int32_t *buf[])
958 static const int shifts[] =
960 EQ_SHELF_SHIFT, /* low shelf */
961 EQ_PEAK_SHIFT, /* peaking */
962 EQ_PEAK_SHIFT, /* peaking */
963 EQ_PEAK_SHIFT, /* peaking */
964 EQ_SHELF_SHIFT, /* high shelf */
966 unsigned int channels = AUDIO_DSP.data.num_channels;
967 int i;
969 /* filter configuration currently is 1 low shelf filter, 3 band peaking
970 filters and 1 high shelf filter, in that order. we need to know this
971 so we can choose the correct shift factor.
973 for (i = 0; i < 5; i++)
975 if (!eq_data.enabled[i])
976 continue;
977 eq_filter(buf, &eq_data.filters[i], count, channels, shifts[i]);
982 * Use to enable the equalizer.
984 * @param enable true to enable the equalizer
986 void dsp_set_eq(bool enable)
988 AUDIO_DSP.eq_process = enable ? eq_process : NULL;
989 set_gain(&AUDIO_DSP);
992 static void dsp_set_stereo_width(int value)
994 long width, straight, cross;
996 width = value * 0x7fffff / 100;
998 if (value <= 100)
1000 straight = (0x7fffff + width) / 2;
1001 cross = straight - width;
1003 else
1005 /* straight = (1 + width) / (2 * width) */
1006 straight = ((int64_t)(0x7fffff + width) << 22) / width;
1007 cross = straight - 0x7fffff;
1010 dsp_sw_gain = straight << 8;
1011 dsp_sw_cross = cross << 8;
1015 * Implements the different channel configurations and stereo width.
1018 /* SOUND_CHAN_STEREO mode is a noop so has no function - just outline one for
1019 * completeness. */
1020 #if 0
1021 static void channels_process_sound_chan_stereo(int count, int32_t *buf[])
1023 /* The channels are each just themselves */
1024 (void)count; (void)buf;
1026 #endif
1028 #ifndef DSP_HAVE_ASM_SOUND_CHAN_MONO
1029 static void channels_process_sound_chan_mono(int count, int32_t *buf[])
1031 int32_t *sl = buf[0], *sr = buf[1];
1035 int32_t lr = *sl/2 + *sr/2;
1036 *sl++ = lr;
1037 *sr++ = lr;
1039 while (--count > 0);
1041 #endif /* DSP_HAVE_ASM_SOUND_CHAN_MONO */
1043 #ifndef DSP_HAVE_ASM_SOUND_CHAN_CUSTOM
1044 static void channels_process_sound_chan_custom(int count, int32_t *buf[])
1046 const int32_t gain = dsp_sw_gain;
1047 const int32_t cross = dsp_sw_cross;
1048 int32_t *sl = buf[0], *sr = buf[1];
1052 int32_t l = *sl;
1053 int32_t r = *sr;
1054 *sl++ = FRACMUL(l, gain) + FRACMUL(r, cross);
1055 *sr++ = FRACMUL(r, gain) + FRACMUL(l, cross);
1057 while (--count > 0);
1059 #endif /* DSP_HAVE_ASM_SOUND_CHAN_CUSTOM */
1061 static void channels_process_sound_chan_mono_left(int count, int32_t *buf[])
1063 /* Just copy over the other channel */
1064 memcpy(buf[1], buf[0], count * sizeof (*buf));
1067 static void channels_process_sound_chan_mono_right(int count, int32_t *buf[])
1069 /* Just copy over the other channel */
1070 memcpy(buf[0], buf[1], count * sizeof (*buf));
1073 #ifndef DSP_HAVE_ASM_SOUND_CHAN_KARAOKE
1074 static void channels_process_sound_chan_karaoke(int count, int32_t *buf[])
1076 int32_t *sl = buf[0], *sr = buf[1];
1080 int32_t ch = *sl/2 - *sr/2;
1081 *sl++ = ch;
1082 *sr++ = -ch;
1084 while (--count > 0);
1086 #endif /* DSP_HAVE_ASM_SOUND_CHAN_KARAOKE */
1088 static void dsp_set_channel_config(int value)
1090 static const channels_process_fn_type channels_process_functions[] =
1092 /* SOUND_CHAN_STEREO = All-purpose index for no channel processing */
1093 [SOUND_CHAN_STEREO] = NULL,
1094 [SOUND_CHAN_MONO] = channels_process_sound_chan_mono,
1095 [SOUND_CHAN_CUSTOM] = channels_process_sound_chan_custom,
1096 [SOUND_CHAN_MONO_LEFT] = channels_process_sound_chan_mono_left,
1097 [SOUND_CHAN_MONO_RIGHT] = channels_process_sound_chan_mono_right,
1098 [SOUND_CHAN_KARAOKE] = channels_process_sound_chan_karaoke,
1101 if ((unsigned)value >= ARRAYLEN(channels_process_functions) ||
1102 AUDIO_DSP.stereo_mode == STEREO_MONO)
1104 value = SOUND_CHAN_STEREO;
1107 /* This doesn't apply to voice */
1108 channels_mode = value;
1109 AUDIO_DSP.channels_process = channels_process_functions[value];
1112 #if CONFIG_CODEC == SWCODEC
1114 #ifdef HAVE_SW_TONE_CONTROLS
1115 static void set_tone_controls(void)
1117 filter_bishelf_coefs(0xffffffff/NATIVE_FREQUENCY*200,
1118 0xffffffff/NATIVE_FREQUENCY*3500,
1119 bass, treble, -prescale,
1120 AUDIO_DSP.tone_filter.coefs);
1121 /* Sync the voice dsp coefficients */
1122 memcpy(&VOICE_DSP.tone_filter.coefs, AUDIO_DSP.tone_filter.coefs,
1123 sizeof (VOICE_DSP.tone_filter.coefs));
1125 #endif
1127 /* Hook back from firmware/ part of audio, which can't/shouldn't call apps/
1128 * code directly.
1130 int dsp_callback(int msg, intptr_t param)
1132 switch (msg)
1134 #ifdef HAVE_SW_TONE_CONTROLS
1135 case DSP_CALLBACK_SET_PRESCALE:
1136 prescale = param;
1137 set_tone_controls();
1138 break;
1139 /* prescaler is always set after calling any of these, so we wait with
1140 * calculating coefs until the above case is hit.
1142 case DSP_CALLBACK_SET_BASS:
1143 bass = param;
1144 break;
1145 case DSP_CALLBACK_SET_TREBLE:
1146 treble = param;
1147 break;
1148 #endif
1149 case DSP_CALLBACK_SET_CHANNEL_CONFIG:
1150 dsp_set_channel_config(param);
1151 break;
1152 case DSP_CALLBACK_SET_STEREO_WIDTH:
1153 dsp_set_stereo_width(param);
1154 break;
1155 default:
1156 break;
1158 return 0;
1160 #endif
1162 /* Process and convert src audio to dst based on the DSP configuration,
1163 * reading count number of audio samples. dst is assumed to be large
1164 * enough; use dsp_output_count() to get the required number. src is an
1165 * array of pointers; for mono and interleaved stereo, it contains one
1166 * pointer to the start of the audio data and the other is ignored; for
1167 * non-interleaved stereo, it contains two pointers, one for each audio
1168 * channel. Returns number of bytes written to dst.
1170 int dsp_process(struct dsp_config *dsp, char *dst, const char *src[], int count)
1172 int32_t *tmp[2];
1173 static long last_yield;
1174 long tick;
1175 int written = 0;
1177 #if defined(CPU_COLDFIRE)
1178 /* set emac unit for dsp processing, and save old macsr, we're running in
1179 codec thread context at this point, so can't clobber it */
1180 unsigned long old_macsr = coldfire_get_macsr();
1181 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
1182 #endif
1184 if (new_gain)
1185 dsp_set_replaygain(); /* Gain has changed */
1187 /* Perform at least one yield before starting */
1188 last_yield = current_tick;
1189 yield();
1191 /* Testing function pointers for NULL is preferred since the pointer
1192 will be preloaded to be used for the call if not. */
1193 while (count > 0)
1195 int samples = MIN(sample_buf_count/2, count);
1196 count -= samples;
1198 dsp->input_samples(samples, src, tmp);
1200 if (dsp->tdspeed_active)
1201 samples = tdspeed_doit(tmp, samples);
1203 int chunk_offset = 0;
1204 while (samples > 0)
1206 int32_t *t2[2];
1207 t2[0] = tmp[0]+chunk_offset;
1208 t2[1] = tmp[1]+chunk_offset;
1210 int chunk = MIN(sample_buf_count/2, samples);
1211 chunk_offset += chunk;
1212 samples -= chunk;
1214 if (dsp->apply_gain)
1215 dsp->apply_gain(chunk, &dsp->data, t2);
1217 if (dsp->resample && (chunk = resample(dsp, chunk, t2)) <= 0)
1218 break; /* I'm pretty sure we're downsampling here */
1220 if (dsp->apply_crossfeed)
1221 dsp->apply_crossfeed(chunk, t2);
1223 if (dsp->eq_process)
1224 dsp->eq_process(chunk, t2);
1226 #ifdef HAVE_SW_TONE_CONTROLS
1227 if ((bass | treble) != 0)
1228 eq_filter(t2, &dsp->tone_filter, chunk,
1229 dsp->data.num_channels, FILTER_BISHELF_SHIFT);
1230 #endif
1232 if (dsp->channels_process)
1233 dsp->channels_process(chunk, t2);
1235 dsp->output_samples(chunk, &dsp->data, (const int32_t **)t2, (int16_t *)dst);
1237 written += chunk;
1238 dst += chunk * sizeof (int16_t) * 2;
1240 /* yield at least once each tick */
1241 tick = current_tick;
1242 if (TIME_AFTER(tick, last_yield))
1244 last_yield = tick;
1245 yield();
1250 #if defined(CPU_COLDFIRE)
1251 /* set old macsr again */
1252 coldfire_set_macsr(old_macsr);
1253 #endif
1254 return written;
1257 /* Given count number of input samples, calculate the maximum number of
1258 * samples of output data that would be generated (the calculation is not
1259 * entirely exact and rounds upwards to be on the safe side; during
1260 * resampling, the number of samples generated depends on the current state
1261 * of the resampler).
1263 /* dsp_input_size MUST be called afterwards */
1264 int dsp_output_count(struct dsp_config *dsp, int count)
1266 if (dsp->tdspeed_active)
1267 count = tdspeed_est_output_size();
1268 if (dsp->resample)
1270 count = (int)(((unsigned long)count * NATIVE_FREQUENCY
1271 + (dsp->frequency - 1)) / dsp->frequency);
1274 /* Now we have the resampled sample count which must not exceed
1275 * RESAMPLE_BUF_RIGHT_CHANNEL to avoid resample buffer overflow. One
1276 * must call dsp_input_count() to get the correct input sample
1277 * count.
1279 if (count > RESAMPLE_BUF_RIGHT_CHANNEL)
1280 count = RESAMPLE_BUF_RIGHT_CHANNEL;
1282 return count;
1285 /* Given count output samples, calculate number of input samples
1286 * that would be consumed in order to fill the output buffer.
1288 int dsp_input_count(struct dsp_config *dsp, int count)
1290 /* count is now the number of resampled input samples. Convert to
1291 original input samples. */
1292 if (dsp->resample)
1294 /* Use the real resampling delta =
1295 * dsp->frequency * 65536 / NATIVE_FREQUENCY, and
1296 * round towards zero to avoid buffer overflows. */
1297 count = (int)(((unsigned long)count *
1298 dsp->data.resample_data.delta) >> 16);
1301 if (dsp->tdspeed_active)
1302 count = tdspeed_est_input_size(count);
1304 return count;
1307 static void dsp_set_gain_var(long *var, long value)
1309 *var = value;
1310 new_gain = true;
1313 static void dsp_update_functions(struct dsp_config *dsp)
1315 sample_input_new_format(dsp);
1316 sample_output_new_format(dsp);
1317 if (dsp == &AUDIO_DSP)
1318 dsp_set_crossfeed(crossfeed_enabled);
1321 intptr_t dsp_configure(struct dsp_config *dsp, int setting, intptr_t value)
1323 switch (setting)
1325 case DSP_MYDSP:
1326 switch (value)
1328 case CODEC_IDX_AUDIO:
1329 return (intptr_t)&AUDIO_DSP;
1330 case CODEC_IDX_VOICE:
1331 return (intptr_t)&VOICE_DSP;
1332 default:
1333 return (intptr_t)NULL;
1336 case DSP_SET_FREQUENCY:
1337 memset(&dsp->data.resample_data, 0, sizeof (dsp->data.resample_data));
1338 /* Fall through!!! */
1339 case DSP_SWITCH_FREQUENCY:
1340 dsp->codec_frequency = (value == 0) ? NATIVE_FREQUENCY : value;
1341 /* Account for playback speed adjustment when setting dsp->frequency
1342 if we're called from the main audio thread. Voice UI thread should
1343 not need this feature.
1345 if (dsp == &AUDIO_DSP)
1346 dsp->frequency = pitch_ratio * dsp->codec_frequency / 1000;
1347 else
1348 dsp->frequency = dsp->codec_frequency;
1350 resampler_new_delta(dsp);
1351 tdspeed_setup(dsp);
1352 break;
1354 case DSP_SET_SAMPLE_DEPTH:
1355 dsp->sample_depth = value;
1357 if (dsp->sample_depth <= NATIVE_DEPTH)
1359 dsp->frac_bits = WORD_FRACBITS;
1360 dsp->sample_bytes = sizeof (int16_t); /* samples are 16 bits */
1361 dsp->data.clip_max = ((1 << WORD_FRACBITS) - 1);
1362 dsp->data.clip_min = -((1 << WORD_FRACBITS));
1364 else
1366 dsp->frac_bits = value;
1367 dsp->sample_bytes = sizeof (int32_t); /* samples are 32 bits */
1368 dsp->data.clip_max = (1 << value) - 1;
1369 dsp->data.clip_min = -(1 << value);
1372 dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
1373 sample_input_new_format(dsp);
1374 dither_init(dsp);
1375 break;
1377 case DSP_SET_STEREO_MODE:
1378 dsp->stereo_mode = value;
1379 dsp->data.num_channels = value == STEREO_MONO ? 1 : 2;
1380 dsp_update_functions(dsp);
1381 tdspeed_setup(dsp);
1382 break;
1384 case DSP_RESET:
1385 dsp->stereo_mode = STEREO_NONINTERLEAVED;
1386 dsp->data.num_channels = 2;
1387 dsp->sample_depth = NATIVE_DEPTH;
1388 dsp->frac_bits = WORD_FRACBITS;
1389 dsp->sample_bytes = sizeof (int16_t);
1390 dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
1391 dsp->data.clip_max = ((1 << WORD_FRACBITS) - 1);
1392 dsp->data.clip_min = -((1 << WORD_FRACBITS));
1393 dsp->codec_frequency = dsp->frequency = NATIVE_FREQUENCY;
1395 if (dsp == &AUDIO_DSP)
1397 track_gain = 0;
1398 album_gain = 0;
1399 track_peak = 0;
1400 album_peak = 0;
1401 new_gain = true;
1404 dsp_update_functions(dsp);
1405 resampler_new_delta(dsp);
1406 tdspeed_setup(dsp);
1407 break;
1409 case DSP_FLUSH:
1410 memset(&dsp->data.resample_data, 0,
1411 sizeof (dsp->data.resample_data));
1412 resampler_new_delta(dsp);
1413 dither_init(dsp);
1414 tdspeed_setup(dsp);
1415 break;
1417 case DSP_SET_TRACK_GAIN:
1418 if (dsp == &AUDIO_DSP)
1419 dsp_set_gain_var(&track_gain, value);
1420 break;
1422 case DSP_SET_ALBUM_GAIN:
1423 if (dsp == &AUDIO_DSP)
1424 dsp_set_gain_var(&album_gain, value);
1425 break;
1427 case DSP_SET_TRACK_PEAK:
1428 if (dsp == &AUDIO_DSP)
1429 dsp_set_gain_var(&track_peak, value);
1430 break;
1432 case DSP_SET_ALBUM_PEAK:
1433 if (dsp == &AUDIO_DSP)
1434 dsp_set_gain_var(&album_peak, value);
1435 break;
1437 default:
1438 return 0;
1441 if (!dsp->tdspeed_active)
1443 sample_buf = small_sample_buf;
1444 resample_buf = small_resample_buf;
1445 sample_buf_count = SMALL_SAMPLE_BUF_COUNT;
1447 else
1449 sample_buf = big_sample_buf;
1450 sample_buf_count = big_sample_buf_count;
1451 resample_buf = big_resample_buf;
1454 return 1;
1457 void dsp_set_replaygain(void)
1459 long gain = 0;
1461 new_gain = false;
1463 if (global_settings.replaygain || global_settings.replaygain_noclip)
1465 bool track_mode = get_replaygain_mode(track_gain != 0,
1466 album_gain != 0) == REPLAYGAIN_TRACK;
1467 long peak = (track_mode || !album_peak) ? track_peak : album_peak;
1469 if (global_settings.replaygain)
1471 gain = (track_mode || !album_gain) ? track_gain : album_gain;
1473 if (global_settings.replaygain_preamp)
1475 long preamp = get_replaygain_int(
1476 global_settings.replaygain_preamp * 10);
1478 gain = (long) (((int64_t) gain * preamp) >> 24);
1482 if (gain == 0)
1484 /* So that noclip can work even with no gain information. */
1485 gain = DEFAULT_GAIN;
1488 if (global_settings.replaygain_noclip && (peak != 0)
1489 && ((((int64_t) gain * peak) >> 24) >= DEFAULT_GAIN))
1491 gain = (((int64_t) DEFAULT_GAIN << 24) / peak);
1494 if (gain == DEFAULT_GAIN)
1496 /* Nothing to do, disable processing. */
1497 gain = 0;
1501 /* Store in S8.23 format to simplify calculations. */
1502 replaygain = gain;
1503 set_gain(&AUDIO_DSP);