FS#10512: Bookmarking does not behave correctly (fixes r22192)
[kugel-rb/myfork.git] / apps / dsp.c
blobf9b94e8228b77aaefecf4a1d29c69db5565b9d7e
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 "tdspeed.h"
35 #include "buffer.h"
36 #include "fixedpoint.h"
37 #include "fracmul.h"
39 /* 16-bit samples are scaled based on these constants. The shift should be
40 * no more than 15.
42 #define WORD_SHIFT 12
43 #define WORD_FRACBITS 27
45 #define NATIVE_DEPTH 16
46 /* If the small buffer size changes, check the assembly code! */
47 #define SMALL_SAMPLE_BUF_COUNT 256
48 #define DEFAULT_GAIN 0x01000000
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 const int32_t *src[], int32_t *dst[]);
144 typedef void (*sample_output_fn_type)(int count, struct dsp_data *data,
145 const int32_t *src[], int16_t *dst);
147 /* Single-DSP channel processing in place */
148 typedef void (*channels_process_fn_type)(int count, int32_t *buf[]);
149 /* DSP local channel processing in place */
150 typedef void (*channels_process_dsp_fn_type)(int count, struct dsp_data *data,
151 int32_t *buf[]);
155 ***************************************************************************/
157 struct dsp_config
159 struct dsp_data data; /* Config members for use in asm routines */
160 long codec_frequency; /* Sample rate of data coming from the codec */
161 long frequency; /* Effective sample rate after pitch shift (if any) */
162 int sample_depth;
163 int sample_bytes;
164 int stereo_mode;
165 int32_t tdspeed_percent; /* Speed% * PITCH_SPEED_PRECISION */
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 int32_t pitch_ratio = PITCH_SPEED_100;
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 int32_t sound_get_pitch(void)
259 return pitch_ratio;
262 void sound_set_pitch(int32_t percent)
264 pitch_ratio = percent;
265 dsp_configure(&AUDIO_DSP, DSP_SWITCH_FREQUENCY,
266 AUDIO_DSP.codec_frequency);
269 static void tdspeed_setup(struct dsp_config *dspc)
271 /* Assume timestretch will not be used */
272 dspc->tdspeed_active = false;
273 sample_buf = small_sample_buf;
274 resample_buf = small_resample_buf;
275 sample_buf_count = SMALL_SAMPLE_BUF_COUNT;
277 if(!dsp_timestretch_available())
278 return; /* Timestretch not enabled or buffer not allocated */
279 if (dspc->tdspeed_percent == 0)
280 dspc->tdspeed_percent = PITCH_SPEED_100;
281 if (!tdspeed_config(
282 dspc->codec_frequency == 0 ? NATIVE_FREQUENCY : dspc->codec_frequency,
283 dspc->stereo_mode != STEREO_MONO,
284 dspc->tdspeed_percent))
285 return; /* Timestretch not possible or needed with these parameters */
287 /* Timestretch is to be used */
288 dspc->tdspeed_active = true;
289 sample_buf = big_sample_buf;
290 sample_buf_count = big_sample_buf_count;
291 resample_buf = big_resample_buf;
294 void dsp_timestretch_enable(bool enabled)
296 /* Hook to set up timestretch buffer on first call to settings_apply() */
297 if (big_sample_buf_count < 0) /* Only do something on first call */
299 if (enabled)
301 /* Set up timestretch buffers */
302 big_sample_buf_count = SMALL_SAMPLE_BUF_COUNT * RESAMPLE_RATIO;
303 big_sample_buf = small_resample_buf;
304 big_resample_buf = (int32_t *) buffer_alloc(big_sample_buf_count * RESAMPLE_RATIO * sizeof(int32_t));
306 else
308 /* Not enabled at startup, "big" buffers will never be available */
309 big_sample_buf_count = 0;
311 tdspeed_setup(&AUDIO_DSP);
315 void dsp_set_timestretch(int32_t percent)
317 AUDIO_DSP.tdspeed_percent = percent;
318 tdspeed_setup(&AUDIO_DSP);
321 int32_t dsp_get_timestretch()
323 return AUDIO_DSP.tdspeed_percent;
326 bool dsp_timestretch_available()
328 return (global_settings.timestretch_enabled && big_sample_buf_count > 0);
331 /* Convert count samples to the internal format, if needed. Updates src
332 * to point past the samples "consumed" and dst is set to point to the
333 * samples to consume. Note that for mono, dst[0] equals dst[1], as there
334 * is no point in processing the same data twice.
337 /* convert count 16-bit mono to 32-bit mono */
338 static void sample_input_lte_native_mono(
339 int count, const char *src[], int32_t *dst[])
341 const int16_t *s = (int16_t *) src[0];
342 const int16_t * const send = s + count;
343 int32_t *d = dst[0] = dst[1] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
344 int scale = WORD_SHIFT;
348 *d++ = *s++ << scale;
350 while (s < send);
352 src[0] = (char *)s;
355 /* convert count 16-bit interleaved stereo to 32-bit noninterleaved */
356 static void sample_input_lte_native_i_stereo(
357 int count, const char *src[], int32_t *dst[])
359 const int32_t *s = (int32_t *) src[0];
360 const int32_t * const send = s + count;
361 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
362 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
363 int scale = WORD_SHIFT;
367 int32_t slr = *s++;
368 #ifdef ROCKBOX_LITTLE_ENDIAN
369 *dl++ = (slr >> 16) << scale;
370 *dr++ = (int32_t)(int16_t)slr << scale;
371 #else /* ROCKBOX_BIG_ENDIAN */
372 *dl++ = (int32_t)(int16_t)slr << scale;
373 *dr++ = (slr >> 16) << scale;
374 #endif
376 while (s < send);
378 src[0] = (char *)s;
381 /* convert count 16-bit noninterleaved stereo to 32-bit noninterleaved */
382 static void sample_input_lte_native_ni_stereo(
383 int count, const char *src[], int32_t *dst[])
385 const int16_t *sl = (int16_t *) src[0];
386 const int16_t *sr = (int16_t *) src[1];
387 const int16_t * const slend = sl + count;
388 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
389 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
390 int scale = WORD_SHIFT;
394 *dl++ = *sl++ << scale;
395 *dr++ = *sr++ << scale;
397 while (sl < slend);
399 src[0] = (char *)sl;
400 src[1] = (char *)sr;
403 /* convert count 32-bit mono to 32-bit mono */
404 static void sample_input_gt_native_mono(
405 int count, const char *src[], int32_t *dst[])
407 dst[0] = dst[1] = (int32_t *)src[0];
408 src[0] = (char *)(dst[0] + count);
411 /* convert count 32-bit interleaved stereo to 32-bit noninterleaved stereo */
412 static void sample_input_gt_native_i_stereo(
413 int count, const char *src[], int32_t *dst[])
415 const int32_t *s = (int32_t *)src[0];
416 const int32_t * const send = s + 2*count;
417 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
418 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
422 *dl++ = *s++;
423 *dr++ = *s++;
425 while (s < send);
427 src[0] = (char *)send;
430 /* convert 32 bit-noninterleaved stereo to 32-bit noninterleaved stereo */
431 static void sample_input_gt_native_ni_stereo(
432 int count, const char *src[], int32_t *dst[])
434 dst[0] = (int32_t *)src[0];
435 dst[1] = (int32_t *)src[1];
436 src[0] = (char *)(dst[0] + count);
437 src[1] = (char *)(dst[1] + count);
441 * sample_input_new_format()
443 * set the to-native sample conversion function based on dsp sample parameters
445 * !DSPPARAMSYNC
446 * needs syncing with changes to the following dsp parameters:
447 * * dsp->stereo_mode (A/V)
448 * * dsp->sample_depth (A/V)
450 static void sample_input_new_format(struct dsp_config *dsp)
452 static const sample_input_fn_type sample_input_functions[] =
454 [SAMPLE_INPUT_LE_NATIVE_MONO] = sample_input_lte_native_mono,
455 [SAMPLE_INPUT_LE_NATIVE_I_STEREO] = sample_input_lte_native_i_stereo,
456 [SAMPLE_INPUT_LE_NATIVE_NI_STEREO] = sample_input_lte_native_ni_stereo,
457 [SAMPLE_INPUT_GT_NATIVE_MONO] = sample_input_gt_native_mono,
458 [SAMPLE_INPUT_GT_NATIVE_I_STEREO] = sample_input_gt_native_i_stereo,
459 [SAMPLE_INPUT_GT_NATIVE_NI_STEREO] = sample_input_gt_native_ni_stereo,
462 int convert = dsp->stereo_mode;
464 if (dsp->sample_depth > NATIVE_DEPTH)
465 convert += SAMPLE_INPUT_GT_NATIVE_1ST_INDEX;
467 dsp->input_samples = sample_input_functions[convert];
471 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO
472 /* write mono internal format to output format */
473 static void sample_output_mono(int count, struct dsp_data *data,
474 const int32_t *src[], int16_t *dst)
476 const int32_t *s0 = src[0];
477 const int scale = data->output_scale;
478 const int dc_bias = 1 << (scale - 1);
482 int32_t lr = clip_sample_16((*s0++ + dc_bias) >> scale);
483 *dst++ = lr;
484 *dst++ = lr;
486 while (--count > 0);
488 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO */
490 /* write stereo internal format to output format */
491 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO
492 static void sample_output_stereo(int count, struct dsp_data *data,
493 const int32_t *src[], int16_t *dst)
495 const int32_t *s0 = src[0];
496 const int32_t *s1 = src[1];
497 const int scale = data->output_scale;
498 const int dc_bias = 1 << (scale - 1);
502 *dst++ = clip_sample_16((*s0++ + dc_bias) >> scale);
503 *dst++ = clip_sample_16((*s1++ + dc_bias) >> scale);
505 while (--count > 0);
507 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO */
510 * The "dither" code to convert the 24-bit samples produced by libmad was
511 * taken from the coolplayer project - coolplayer.sourceforge.net
513 * This function handles mono and stereo outputs.
515 static void sample_output_dithered(int count, struct dsp_data *data,
516 const int32_t *src[], int16_t *dst)
518 const int32_t mask = dither_mask;
519 const int32_t bias = dither_bias;
520 const int scale = data->output_scale;
521 const int32_t min = data->clip_min;
522 const int32_t max = data->clip_max;
523 const int32_t range = max - min;
524 int ch;
525 int16_t *d;
527 for (ch = 0; ch < data->num_channels; ch++)
529 struct dither_data * const dither = &dither_data[ch];
530 const int32_t *s = src[ch];
531 int i;
533 for (i = 0, d = &dst[ch]; i < count; i++, s++, d += 2)
535 int32_t output, sample;
536 int32_t random;
538 /* Noise shape and bias (for correct rounding later) */
539 sample = *s;
540 sample += dither->error[0] - dither->error[1] + dither->error[2];
541 dither->error[2] = dither->error[1];
542 dither->error[1] = dither->error[0]/2;
544 output = sample + bias;
546 /* Dither, highpass triangle PDF */
547 random = dither->random*0x0019660dL + 0x3c6ef35fL;
548 output += (random & mask) - (dither->random & mask);
549 dither->random = random;
551 /* Round sample to output range */
552 output &= ~mask;
554 /* Error feedback */
555 dither->error[0] = sample - output;
557 /* Clip */
558 if ((uint32_t)(output - min) > (uint32_t)range)
560 int32_t c = min;
561 if (output > min)
562 c += range;
563 output = c;
566 /* Quantize and store */
567 *d = output >> scale;
571 if (data->num_channels == 2)
572 return;
574 /* Have to duplicate left samples into the right channel since
575 pcm buffer and hardware is interleaved stereo */
576 d = &dst[0];
580 int16_t s = *d++;
581 *d++ = s;
583 while (--count > 0);
587 * sample_output_new_format()
589 * set the from-native to ouput sample conversion routine
591 * !DSPPARAMSYNC
592 * needs syncing with changes to the following dsp parameters:
593 * * dsp->stereo_mode (A/V)
594 * * dither_enabled (A)
596 static void sample_output_new_format(struct dsp_config *dsp)
598 static const sample_output_fn_type sample_output_functions[] =
600 sample_output_mono,
601 sample_output_stereo,
602 sample_output_dithered,
603 sample_output_dithered
606 int out = dsp->data.num_channels - 1;
608 if (dsp == &AUDIO_DSP && dither_enabled)
609 out += 2;
611 dsp->output_samples = sample_output_functions[out];
615 * Linear interpolation resampling that introduces a one sample delay because
616 * of our inability to look into the future at the end of a frame.
618 #ifndef DSP_HAVE_ASM_RESAMPLING
619 static int dsp_downsample(int count, struct dsp_data *data,
620 const int32_t *src[], int32_t *dst[])
622 int ch = data->num_channels - 1;
623 uint32_t delta = data->resample_data.delta;
624 uint32_t phase, pos;
625 int32_t *d;
627 /* Rolled channel loop actually showed slightly faster. */
630 /* Just initialize things and not worry too much about the relatively
631 * uncommon case of not being able to spit out a sample for the frame.
633 const int32_t *s = src[ch];
634 int32_t last = data->resample_data.last_sample[ch];
636 data->resample_data.last_sample[ch] = s[count - 1];
637 d = dst[ch];
638 phase = data->resample_data.phase;
639 pos = phase >> 16;
641 /* Do we need last sample of previous frame for interpolation? */
642 if (pos > 0)
643 last = s[pos - 1];
645 while (pos < (uint32_t)count)
647 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last);
648 phase += delta;
649 pos = phase >> 16;
650 last = s[pos - 1];
653 while (--ch >= 0);
655 /* Wrap phase accumulator back to start of next frame. */
656 data->resample_data.phase = phase - (count << 16);
657 return d - dst[0];
660 static int dsp_upsample(int count, struct dsp_data *data,
661 const int32_t *src[], int32_t *dst[])
663 int ch = data->num_channels - 1;
664 uint32_t delta = data->resample_data.delta;
665 uint32_t phase, pos;
666 int32_t *d;
668 /* Rolled channel loop actually showed slightly faster. */
671 /* Should always be able to output a sample for a ratio up to RESAMPLE_RATIO */
672 const int32_t *s = src[ch];
673 int32_t last = data->resample_data.last_sample[ch];
675 data->resample_data.last_sample[ch] = s[count - 1];
676 d = dst[ch];
677 phase = data->resample_data.phase;
678 pos = phase >> 16;
680 while (pos == 0)
682 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[0] - last);
683 phase += delta;
684 pos = phase >> 16;
687 while (pos < (uint32_t)count)
689 last = s[pos - 1];
690 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last);
691 phase += delta;
692 pos = phase >> 16;
695 while (--ch >= 0);
697 /* Wrap phase accumulator back to start of next frame. */
698 data->resample_data.phase = phase & 0xffff;
699 return d - dst[0];
701 #endif /* DSP_HAVE_ASM_RESAMPLING */
703 static void resampler_new_delta(struct dsp_config *dsp)
705 dsp->data.resample_data.delta = (unsigned long)
706 dsp->frequency * 65536LL / NATIVE_FREQUENCY;
708 if (dsp->frequency == NATIVE_FREQUENCY)
710 /* NOTE: If fully glitch-free transistions from no resampling to
711 resampling are desired, last_sample history should be maintained
712 even when not resampling. */
713 dsp->resample = NULL;
714 dsp->data.resample_data.phase = 0;
715 dsp->data.resample_data.last_sample[0] = 0;
716 dsp->data.resample_data.last_sample[1] = 0;
718 else if (dsp->frequency < NATIVE_FREQUENCY)
719 dsp->resample = dsp_upsample;
720 else
721 dsp->resample = dsp_downsample;
724 /* Resample count stereo samples. Updates the src array, if resampling is
725 * done, to refer to the resampled data. Returns number of stereo samples
726 * for further processing.
728 static inline int resample(struct dsp_config *dsp, int count, int32_t *src[])
730 int32_t *dst[2] =
732 &resample_buf[RESAMPLE_BUF_LEFT_CHANNEL],
733 &resample_buf[RESAMPLE_BUF_RIGHT_CHANNEL],
736 count = dsp->resample(count, &dsp->data, (const int32_t **)src, dst);
738 src[0] = dst[0];
739 src[1] = dst[dsp->data.num_channels - 1];
741 return count;
744 static void dither_init(struct dsp_config *dsp)
746 memset(dither_data, 0, sizeof (dither_data));
747 dither_bias = (1L << (dsp->frac_bits - NATIVE_DEPTH));
748 dither_mask = (1L << (dsp->frac_bits + 1 - NATIVE_DEPTH)) - 1;
751 void dsp_dither_enable(bool enable)
753 struct dsp_config *dsp = &AUDIO_DSP;
754 dither_enabled = enable;
755 sample_output_new_format(dsp);
758 /* Applies crossfeed to the stereo signal in src.
759 * Crossfeed is a process where listening over speakers is simulated. This
760 * is good for old hard panned stereo records, which might be quite fatiguing
761 * to listen to on headphones with no crossfeed.
763 #ifndef DSP_HAVE_ASM_CROSSFEED
764 static void apply_crossfeed(int count, int32_t *buf[])
766 int32_t *hist_l = &crossfeed_data.history[0];
767 int32_t *hist_r = &crossfeed_data.history[2];
768 int32_t *delay = &crossfeed_data.delay[0][0];
769 int32_t *coefs = &crossfeed_data.coefs[0];
770 int32_t gain = crossfeed_data.gain;
771 int32_t *di = crossfeed_data.index;
773 int32_t acc;
774 int32_t left, right;
775 int i;
777 for (i = 0; i < count; i++)
779 left = buf[0][i];
780 right = buf[1][i];
782 /* Filter delayed sample from left speaker */
783 acc = FRACMUL(*di, coefs[0]);
784 acc += FRACMUL(hist_l[0], coefs[1]);
785 acc += FRACMUL(hist_l[1], coefs[2]);
786 /* Save filter history for left speaker */
787 hist_l[1] = acc;
788 hist_l[0] = *di;
789 *di++ = left;
790 /* Filter delayed sample from right speaker */
791 acc = FRACMUL(*di, coefs[0]);
792 acc += FRACMUL(hist_r[0], coefs[1]);
793 acc += FRACMUL(hist_r[1], coefs[2]);
794 /* Save filter history for right speaker */
795 hist_r[1] = acc;
796 hist_r[0] = *di;
797 *di++ = right;
798 /* Now add the attenuated direct sound and write to outputs */
799 buf[0][i] = FRACMUL(left, gain) + hist_r[1];
800 buf[1][i] = FRACMUL(right, gain) + hist_l[1];
802 /* Wrap delay line index if bigger than delay line size */
803 if (di >= delay + 13*2)
804 di = delay;
806 /* Write back local copies of data we've modified */
807 crossfeed_data.index = di;
809 #endif /* DSP_HAVE_ASM_CROSSFEED */
812 * dsp_set_crossfeed(bool enable)
814 * !DSPPARAMSYNC
815 * needs syncing with changes to the following dsp parameters:
816 * * dsp->stereo_mode (A)
818 void dsp_set_crossfeed(bool enable)
820 crossfeed_enabled = enable;
821 AUDIO_DSP.apply_crossfeed = (enable && AUDIO_DSP.data.num_channels > 1)
822 ? apply_crossfeed : NULL;
825 void dsp_set_crossfeed_direct_gain(int gain)
827 crossfeed_data.gain = get_replaygain_int(gain * 10) << 7;
828 /* If gain is negative, the calculation overflowed and we need to clamp */
829 if (crossfeed_data.gain < 0)
830 crossfeed_data.gain = 0x7fffffff;
833 /* Both gains should be below 0 dB */
834 void dsp_set_crossfeed_cross_params(long lf_gain, long hf_gain, long cutoff)
836 int32_t *c = crossfeed_data.coefs;
837 long scaler = get_replaygain_int(lf_gain * 10) << 7;
839 cutoff = 0xffffffff/NATIVE_FREQUENCY*cutoff;
840 hf_gain -= lf_gain;
841 /* Divide cutoff by sqrt(10^(hf_gain/20)) to place cutoff at the -3 dB
842 * point instead of shelf midpoint. This is for compatibility with the old
843 * crossfeed shelf filter and should be removed if crossfeed settings are
844 * ever made incompatible for any other good reason.
846 cutoff = fp_div(cutoff, get_replaygain_int(hf_gain*5), 24);
847 filter_shelf_coefs(cutoff, hf_gain, false, c);
848 /* Scale coefs by LF gain and shift them to s0.31 format. We have no gains
849 * over 1 and can do this safely
851 c[0] = FRACMUL_SHL(c[0], scaler, 4);
852 c[1] = FRACMUL_SHL(c[1], scaler, 4);
853 c[2] <<= 4;
856 /* Apply a constant gain to the samples (e.g., for ReplayGain).
857 * Note that this must be called before the resampler.
859 #ifndef DSP_HAVE_ASM_APPLY_GAIN
860 static void dsp_apply_gain(int count, struct dsp_data *data, int32_t *buf[])
862 const int32_t gain = data->gain;
863 int ch;
865 for (ch = 0; ch < data->num_channels; ch++)
867 int32_t *d = buf[ch];
868 int i;
870 for (i = 0; i < count; i++)
871 d[i] = FRACMUL_SHL(d[i], gain, 8);
874 #endif /* DSP_HAVE_ASM_APPLY_GAIN */
876 /* Combine all gains to a global gain. */
877 static void set_gain(struct dsp_config *dsp)
879 dsp->data.gain = DEFAULT_GAIN;
881 /* Replay gain not relevant to voice */
882 if (dsp == &AUDIO_DSP && replaygain)
884 dsp->data.gain = replaygain;
887 if (dsp->eq_process && eq_precut)
889 dsp->data.gain =
890 (long) (((int64_t) dsp->data.gain * eq_precut) >> 24);
893 #ifdef HAVE_SW_VOLUME_CONTROL
894 if (global_settings.volume < SW_VOLUME_MAX ||
895 global_settings.volume > SW_VOLUME_MIN)
897 int vol_gain = get_replaygain_int(global_settings.volume * 100);
898 dsp->data.gain = (long) (((int64_t) dsp->data.gain * vol_gain) >> 24);
900 #endif
902 if (dsp->data.gain == DEFAULT_GAIN)
904 dsp->data.gain = 0;
906 else
908 dsp->data.gain >>= 1;
911 dsp->apply_gain = dsp->data.gain != 0 ? dsp_apply_gain : NULL;
915 * Update the amount to cut the audio before applying the equalizer.
917 * @param precut to apply in decibels (multiplied by 10)
919 void dsp_set_eq_precut(int precut)
921 eq_precut = get_replaygain_int(precut * -10);
922 set_gain(&AUDIO_DSP);
926 * Synchronize the equalizer filter coefficients with the global settings.
928 * @param band the equalizer band to synchronize
930 void dsp_set_eq_coefs(int band)
932 const int *setting;
933 long gain;
934 unsigned long cutoff, q;
936 /* Adjust setting pointer to the band we actually want to change */
937 setting = &global_settings.eq_band0_cutoff + (band * 3);
939 /* Convert user settings to format required by coef generator functions */
940 cutoff = 0xffffffff / NATIVE_FREQUENCY * (*setting++);
941 q = *setting++;
942 gain = *setting++;
944 if (q == 0)
945 q = 1;
947 /* NOTE: The coef functions assume the EMAC unit is in fractional mode,
948 which it should be, since we're executed from the main thread. */
950 /* Assume a band is disabled if the gain is zero */
951 if (gain == 0)
953 eq_data.enabled[band] = 0;
955 else
957 if (band == 0)
958 eq_ls_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
959 else if (band == 4)
960 eq_hs_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
961 else
962 eq_pk_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
964 eq_data.enabled[band] = 1;
968 /* Apply EQ filters to those bands that have got it switched on. */
969 static void eq_process(int count, int32_t *buf[])
971 static const int shifts[] =
973 EQ_SHELF_SHIFT, /* low shelf */
974 EQ_PEAK_SHIFT, /* peaking */
975 EQ_PEAK_SHIFT, /* peaking */
976 EQ_PEAK_SHIFT, /* peaking */
977 EQ_SHELF_SHIFT, /* high shelf */
979 unsigned int channels = AUDIO_DSP.data.num_channels;
980 int i;
982 /* filter configuration currently is 1 low shelf filter, 3 band peaking
983 filters and 1 high shelf filter, in that order. we need to know this
984 so we can choose the correct shift factor.
986 for (i = 0; i < 5; i++)
988 if (!eq_data.enabled[i])
989 continue;
990 eq_filter(buf, &eq_data.filters[i], count, channels, shifts[i]);
995 * Use to enable the equalizer.
997 * @param enable true to enable the equalizer
999 void dsp_set_eq(bool enable)
1001 AUDIO_DSP.eq_process = enable ? eq_process : NULL;
1002 set_gain(&AUDIO_DSP);
1005 static void dsp_set_stereo_width(int value)
1007 long width, straight, cross;
1009 width = value * 0x7fffff / 100;
1011 if (value <= 100)
1013 straight = (0x7fffff + width) / 2;
1014 cross = straight - width;
1016 else
1018 /* straight = (1 + width) / (2 * width) */
1019 straight = ((int64_t)(0x7fffff + width) << 22) / width;
1020 cross = straight - 0x7fffff;
1023 dsp_sw_gain = straight << 8;
1024 dsp_sw_cross = cross << 8;
1028 * Implements the different channel configurations and stereo width.
1031 /* SOUND_CHAN_STEREO mode is a noop so has no function - just outline one for
1032 * completeness. */
1033 #if 0
1034 static void channels_process_sound_chan_stereo(int count, int32_t *buf[])
1036 /* The channels are each just themselves */
1037 (void)count; (void)buf;
1039 #endif
1041 #ifndef DSP_HAVE_ASM_SOUND_CHAN_MONO
1042 static void channels_process_sound_chan_mono(int count, int32_t *buf[])
1044 int32_t *sl = buf[0], *sr = buf[1];
1048 int32_t lr = *sl/2 + *sr/2;
1049 *sl++ = lr;
1050 *sr++ = lr;
1052 while (--count > 0);
1054 #endif /* DSP_HAVE_ASM_SOUND_CHAN_MONO */
1056 #ifndef DSP_HAVE_ASM_SOUND_CHAN_CUSTOM
1057 static void channels_process_sound_chan_custom(int count, int32_t *buf[])
1059 const int32_t gain = dsp_sw_gain;
1060 const int32_t cross = dsp_sw_cross;
1061 int32_t *sl = buf[0], *sr = buf[1];
1065 int32_t l = *sl;
1066 int32_t r = *sr;
1067 *sl++ = FRACMUL(l, gain) + FRACMUL(r, cross);
1068 *sr++ = FRACMUL(r, gain) + FRACMUL(l, cross);
1070 while (--count > 0);
1072 #endif /* DSP_HAVE_ASM_SOUND_CHAN_CUSTOM */
1074 static void channels_process_sound_chan_mono_left(int count, int32_t *buf[])
1076 /* Just copy over the other channel */
1077 memcpy(buf[1], buf[0], count * sizeof (*buf));
1080 static void channels_process_sound_chan_mono_right(int count, int32_t *buf[])
1082 /* Just copy over the other channel */
1083 memcpy(buf[0], buf[1], count * sizeof (*buf));
1086 #ifndef DSP_HAVE_ASM_SOUND_CHAN_KARAOKE
1087 static void channels_process_sound_chan_karaoke(int count, int32_t *buf[])
1089 int32_t *sl = buf[0], *sr = buf[1];
1093 int32_t ch = *sl/2 - *sr/2;
1094 *sl++ = ch;
1095 *sr++ = -ch;
1097 while (--count > 0);
1099 #endif /* DSP_HAVE_ASM_SOUND_CHAN_KARAOKE */
1101 static void dsp_set_channel_config(int value)
1103 static const channels_process_fn_type channels_process_functions[] =
1105 /* SOUND_CHAN_STEREO = All-purpose index for no channel processing */
1106 [SOUND_CHAN_STEREO] = NULL,
1107 [SOUND_CHAN_MONO] = channels_process_sound_chan_mono,
1108 [SOUND_CHAN_CUSTOM] = channels_process_sound_chan_custom,
1109 [SOUND_CHAN_MONO_LEFT] = channels_process_sound_chan_mono_left,
1110 [SOUND_CHAN_MONO_RIGHT] = channels_process_sound_chan_mono_right,
1111 [SOUND_CHAN_KARAOKE] = channels_process_sound_chan_karaoke,
1114 if ((unsigned)value >= ARRAYLEN(channels_process_functions) ||
1115 AUDIO_DSP.stereo_mode == STEREO_MONO)
1117 value = SOUND_CHAN_STEREO;
1120 /* This doesn't apply to voice */
1121 channels_mode = value;
1122 AUDIO_DSP.channels_process = channels_process_functions[value];
1125 #if CONFIG_CODEC == SWCODEC
1127 #ifdef HAVE_SW_TONE_CONTROLS
1128 static void set_tone_controls(void)
1130 filter_bishelf_coefs(0xffffffff/NATIVE_FREQUENCY*200,
1131 0xffffffff/NATIVE_FREQUENCY*3500,
1132 bass, treble, -prescale,
1133 AUDIO_DSP.tone_filter.coefs);
1134 /* Sync the voice dsp coefficients */
1135 memcpy(&VOICE_DSP.tone_filter.coefs, AUDIO_DSP.tone_filter.coefs,
1136 sizeof (VOICE_DSP.tone_filter.coefs));
1138 #endif
1140 /* Hook back from firmware/ part of audio, which can't/shouldn't call apps/
1141 * code directly.
1143 int dsp_callback(int msg, intptr_t param)
1145 switch (msg)
1147 #ifdef HAVE_SW_TONE_CONTROLS
1148 case DSP_CALLBACK_SET_PRESCALE:
1149 prescale = param;
1150 set_tone_controls();
1151 break;
1152 /* prescaler is always set after calling any of these, so we wait with
1153 * calculating coefs until the above case is hit.
1155 case DSP_CALLBACK_SET_BASS:
1156 bass = param;
1157 break;
1158 case DSP_CALLBACK_SET_TREBLE:
1159 treble = param;
1160 break;
1161 #ifdef HAVE_SW_VOLUME_CONTROL
1162 case DSP_CALLBACK_SET_SW_VOLUME:
1163 set_gain(&AUDIO_DSP);
1164 break;
1165 #endif
1166 #endif
1167 case DSP_CALLBACK_SET_CHANNEL_CONFIG:
1168 dsp_set_channel_config(param);
1169 break;
1170 case DSP_CALLBACK_SET_STEREO_WIDTH:
1171 dsp_set_stereo_width(param);
1172 break;
1173 default:
1174 break;
1176 return 0;
1178 #endif
1180 /* Process and convert src audio to dst based on the DSP configuration,
1181 * reading count number of audio samples. dst is assumed to be large
1182 * enough; use dsp_output_count() to get the required number. src is an
1183 * array of pointers; for mono and interleaved stereo, it contains one
1184 * pointer to the start of the audio data and the other is ignored; for
1185 * non-interleaved stereo, it contains two pointers, one for each audio
1186 * channel. Returns number of bytes written to dst.
1188 int dsp_process(struct dsp_config *dsp, char *dst, const char *src[], int count)
1190 int32_t *tmp[2];
1191 static long last_yield;
1192 long tick;
1193 int written = 0;
1195 #if defined(CPU_COLDFIRE)
1196 /* set emac unit for dsp processing, and save old macsr, we're running in
1197 codec thread context at this point, so can't clobber it */
1198 unsigned long old_macsr = coldfire_get_macsr();
1199 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
1200 #endif
1202 if (new_gain)
1203 dsp_set_replaygain(); /* Gain has changed */
1205 /* Perform at least one yield before starting */
1206 last_yield = current_tick;
1207 yield();
1209 /* Testing function pointers for NULL is preferred since the pointer
1210 will be preloaded to be used for the call if not. */
1211 while (count > 0)
1213 int samples = MIN(sample_buf_count/2, count);
1214 count -= samples;
1216 dsp->input_samples(samples, src, tmp);
1218 if (dsp->tdspeed_active)
1219 samples = tdspeed_doit(tmp, samples);
1221 int chunk_offset = 0;
1222 while (samples > 0)
1224 int32_t *t2[2];
1225 t2[0] = tmp[0]+chunk_offset;
1226 t2[1] = tmp[1]+chunk_offset;
1228 int chunk = MIN(sample_buf_count/2, samples);
1229 chunk_offset += chunk;
1230 samples -= chunk;
1232 if (dsp->apply_gain)
1233 dsp->apply_gain(chunk, &dsp->data, t2);
1235 if (dsp->resample && (chunk = resample(dsp, chunk, t2)) <= 0)
1236 break; /* I'm pretty sure we're downsampling here */
1238 if (dsp->apply_crossfeed)
1239 dsp->apply_crossfeed(chunk, t2);
1241 if (dsp->eq_process)
1242 dsp->eq_process(chunk, t2);
1244 #ifdef HAVE_SW_TONE_CONTROLS
1245 if ((bass | treble) != 0)
1246 eq_filter(t2, &dsp->tone_filter, chunk,
1247 dsp->data.num_channels, FILTER_BISHELF_SHIFT);
1248 #endif
1250 if (dsp->channels_process)
1251 dsp->channels_process(chunk, t2);
1253 dsp->output_samples(chunk, &dsp->data, (const int32_t **)t2, (int16_t *)dst);
1255 written += chunk;
1256 dst += chunk * sizeof (int16_t) * 2;
1258 /* yield at least once each tick */
1259 tick = current_tick;
1260 if (TIME_AFTER(tick, last_yield))
1262 last_yield = tick;
1263 yield();
1268 #if defined(CPU_COLDFIRE)
1269 /* set old macsr again */
1270 coldfire_set_macsr(old_macsr);
1271 #endif
1272 return written;
1275 /* Given count number of input samples, calculate the maximum number of
1276 * samples of output data that would be generated (the calculation is not
1277 * entirely exact and rounds upwards to be on the safe side; during
1278 * resampling, the number of samples generated depends on the current state
1279 * of the resampler).
1281 /* dsp_input_size MUST be called afterwards */
1282 int dsp_output_count(struct dsp_config *dsp, int count)
1284 if (dsp->tdspeed_active)
1285 count = tdspeed_est_output_size();
1286 if (dsp->resample)
1288 count = (int)(((unsigned long)count * NATIVE_FREQUENCY
1289 + (dsp->frequency - 1)) / dsp->frequency);
1292 /* Now we have the resampled sample count which must not exceed
1293 * RESAMPLE_BUF_RIGHT_CHANNEL to avoid resample buffer overflow. One
1294 * must call dsp_input_count() to get the correct input sample
1295 * count.
1297 if (count > RESAMPLE_BUF_RIGHT_CHANNEL)
1298 count = RESAMPLE_BUF_RIGHT_CHANNEL;
1300 return count;
1303 /* Given count output samples, calculate number of input samples
1304 * that would be consumed in order to fill the output buffer.
1306 int dsp_input_count(struct dsp_config *dsp, int count)
1308 /* count is now the number of resampled input samples. Convert to
1309 original input samples. */
1310 if (dsp->resample)
1312 /* Use the real resampling delta =
1313 * dsp->frequency * 65536 / NATIVE_FREQUENCY, and
1314 * round towards zero to avoid buffer overflows. */
1315 count = (int)(((unsigned long)count *
1316 dsp->data.resample_data.delta) >> 16);
1319 if (dsp->tdspeed_active)
1320 count = tdspeed_est_input_size(count);
1322 return count;
1325 static void dsp_set_gain_var(long *var, long value)
1327 *var = value;
1328 new_gain = true;
1331 static void dsp_update_functions(struct dsp_config *dsp)
1333 sample_input_new_format(dsp);
1334 sample_output_new_format(dsp);
1335 if (dsp == &AUDIO_DSP)
1336 dsp_set_crossfeed(crossfeed_enabled);
1339 intptr_t dsp_configure(struct dsp_config *dsp, int setting, intptr_t value)
1341 switch (setting)
1343 case DSP_MYDSP:
1344 switch (value)
1346 case CODEC_IDX_AUDIO:
1347 return (intptr_t)&AUDIO_DSP;
1348 case CODEC_IDX_VOICE:
1349 return (intptr_t)&VOICE_DSP;
1350 default:
1351 return (intptr_t)NULL;
1354 case DSP_SET_FREQUENCY:
1355 memset(&dsp->data.resample_data, 0, sizeof (dsp->data.resample_data));
1356 /* Fall through!!! */
1357 case DSP_SWITCH_FREQUENCY:
1358 dsp->codec_frequency = (value == 0) ? NATIVE_FREQUENCY : value;
1359 /* Account for playback speed adjustment when setting dsp->frequency
1360 if we're called from the main audio thread. Voice UI thread should
1361 not need this feature.
1363 if (dsp == &AUDIO_DSP)
1364 dsp->frequency = pitch_ratio * dsp->codec_frequency / PITCH_SPEED_100;
1365 else
1366 dsp->frequency = dsp->codec_frequency;
1368 resampler_new_delta(dsp);
1369 tdspeed_setup(dsp);
1370 break;
1372 case DSP_SET_SAMPLE_DEPTH:
1373 dsp->sample_depth = value;
1375 if (dsp->sample_depth <= NATIVE_DEPTH)
1377 dsp->frac_bits = WORD_FRACBITS;
1378 dsp->sample_bytes = sizeof (int16_t); /* samples are 16 bits */
1379 dsp->data.clip_max = ((1 << WORD_FRACBITS) - 1);
1380 dsp->data.clip_min = -((1 << WORD_FRACBITS));
1382 else
1384 dsp->frac_bits = value;
1385 dsp->sample_bytes = sizeof (int32_t); /* samples are 32 bits */
1386 dsp->data.clip_max = (1 << value) - 1;
1387 dsp->data.clip_min = -(1 << value);
1390 dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
1391 sample_input_new_format(dsp);
1392 dither_init(dsp);
1393 break;
1395 case DSP_SET_STEREO_MODE:
1396 dsp->stereo_mode = value;
1397 dsp->data.num_channels = value == STEREO_MONO ? 1 : 2;
1398 dsp_update_functions(dsp);
1399 tdspeed_setup(dsp);
1400 break;
1402 case DSP_RESET:
1403 dsp->stereo_mode = STEREO_NONINTERLEAVED;
1404 dsp->data.num_channels = 2;
1405 dsp->sample_depth = NATIVE_DEPTH;
1406 dsp->frac_bits = WORD_FRACBITS;
1407 dsp->sample_bytes = sizeof (int16_t);
1408 dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
1409 dsp->data.clip_max = ((1 << WORD_FRACBITS) - 1);
1410 dsp->data.clip_min = -((1 << WORD_FRACBITS));
1411 dsp->codec_frequency = dsp->frequency = NATIVE_FREQUENCY;
1413 if (dsp == &AUDIO_DSP)
1415 track_gain = 0;
1416 album_gain = 0;
1417 track_peak = 0;
1418 album_peak = 0;
1419 new_gain = true;
1422 dsp_update_functions(dsp);
1423 resampler_new_delta(dsp);
1424 tdspeed_setup(dsp);
1425 break;
1427 case DSP_FLUSH:
1428 memset(&dsp->data.resample_data, 0,
1429 sizeof (dsp->data.resample_data));
1430 resampler_new_delta(dsp);
1431 dither_init(dsp);
1432 tdspeed_setup(dsp);
1433 break;
1435 case DSP_SET_TRACK_GAIN:
1436 if (dsp == &AUDIO_DSP)
1437 dsp_set_gain_var(&track_gain, value);
1438 break;
1440 case DSP_SET_ALBUM_GAIN:
1441 if (dsp == &AUDIO_DSP)
1442 dsp_set_gain_var(&album_gain, value);
1443 break;
1445 case DSP_SET_TRACK_PEAK:
1446 if (dsp == &AUDIO_DSP)
1447 dsp_set_gain_var(&track_peak, value);
1448 break;
1450 case DSP_SET_ALBUM_PEAK:
1451 if (dsp == &AUDIO_DSP)
1452 dsp_set_gain_var(&album_peak, value);
1453 break;
1455 default:
1456 return 0;
1459 return 1;
1462 void dsp_set_replaygain(void)
1464 long gain = 0;
1466 new_gain = false;
1468 if ((global_settings.replaygain_type != REPLAYGAIN_OFF) ||
1469 global_settings.replaygain_noclip)
1471 bool track_mode = get_replaygain_mode(track_gain != 0,
1472 album_gain != 0) == REPLAYGAIN_TRACK;
1473 long peak = (track_mode || !album_peak) ? track_peak : album_peak;
1475 if (global_settings.replaygain_type != REPLAYGAIN_OFF)
1477 gain = (track_mode || !album_gain) ? track_gain : album_gain;
1479 if (global_settings.replaygain_preamp)
1481 long preamp = get_replaygain_int(
1482 global_settings.replaygain_preamp * 10);
1484 gain = (long) (((int64_t) gain * preamp) >> 24);
1488 if (gain == 0)
1490 /* So that noclip can work even with no gain information. */
1491 gain = DEFAULT_GAIN;
1494 if (global_settings.replaygain_noclip && (peak != 0)
1495 && ((((int64_t) gain * peak) >> 24) >= DEFAULT_GAIN))
1497 gain = (((int64_t) DEFAULT_GAIN << 24) / peak);
1500 if (gain == DEFAULT_GAIN)
1502 /* Nothing to do, disable processing. */
1503 gain = 0;
1507 /* Store in S8.23 format to simplify calculations. */
1508 replaygain = gain;
1509 set_gain(&AUDIO_DSP);