1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
30 #include "replaygain.h"
34 /* 16-bit samples are scaled based on these constants. The shift should be
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 */
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
64 SAMPLE_OUTPUT_MONO
= 0,
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.
76 uint32_t delta
; /* 00h */
77 uint32_t phase
; /* 04h */
78 int32_t last_sample
[2]; /* 08h */
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.
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. */
109 long error
[3]; /* 00h */
110 long random
; /* 0ch */
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 */
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.
130 char enabled
[5]; /* 00h - Flags for active filters */
131 struct eqfilter filters
[5]; /* 08h - packing is 4? */
135 /* Include header with defines which functions are implemented in assembly
136 code for the target */
139 /* Typedefs keep things much neater in this case */
140 typedef void (*sample_input_fn_type
)(int count
, const char *src
[],
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
,
154 ***************************************************************************/
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) */
165 /* Functions that change depending upon settings - NULL if stage is
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
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 */
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
;
184 struct crossfeed_data crossfeed_data IDATA_ATTR
= /* A */
186 .index
= (int32_t *)crossfeed_data
.delay
190 static struct eq_state eq_data
; /* A/V */
191 #ifdef HAVE_SW_TONE_CONTROLS
195 /* Filter struct for software bass/treble controls */
196 static struct eqfilter tone_filter
;
199 /* Settings applicable to audio codec only */
200 static int pitch_ratio
= 1000;
201 static int channels_mode
;
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
;
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 int32_t c
= sample
- min
;
240 if ((uint32_t)c
> (uint32_t)range
)
251 /* Clip sample to signed 16 bit range */
252 static inline int32_t clip_sample_16(int32_t sample
)
254 if ((int16_t)sample
!= sample
)
255 sample
= 0x7fff ^ (sample
>> 31);
259 int sound_get_pitch(void)
264 void sound_set_pitch(int permille
)
266 pitch_ratio
= permille
;
268 dsp_configure(DSP_SWITCH_FREQUENCY
, dsp
->codec_frequency
);
271 /* Convert count samples to the internal format, if needed. Updates src
272 * to point past the samples "consumed" and dst is set to point to the
273 * samples to consume. Note that for mono, dst[0] equals dst[1], as there
274 * is no point in processing the same data twice.
277 /* convert count 16-bit mono to 32-bit mono */
278 static void sample_input_lte_native_mono(
279 int count
, const char *src
[], int32_t *dst
[])
281 const int16_t *s
= (int16_t *) src
[0];
282 const int16_t * const send
= s
+ count
;
283 int32_t *d
= dst
[0] = dst
[1] = &sample_buf
[SAMPLE_BUF_LEFT_CHANNEL
];
284 int scale
= WORD_SHIFT
;
288 *d
++ = *s
++ << scale
;
295 /* convert count 16-bit interleaved stereo to 32-bit noninterleaved */
296 static void sample_input_lte_native_i_stereo(
297 int count
, const char *src
[], int32_t *dst
[])
299 const int32_t *s
= (int32_t *) src
[0];
300 const int32_t * const send
= s
+ count
;
301 int32_t *dl
= dst
[0] = &sample_buf
[SAMPLE_BUF_LEFT_CHANNEL
];
302 int32_t *dr
= dst
[1] = &sample_buf
[SAMPLE_BUF_RIGHT_CHANNEL
];
303 int scale
= WORD_SHIFT
;
308 #ifdef ROCKBOX_LITTLE_ENDIAN
309 *dl
++ = (slr
>> 16) << scale
;
310 *dr
++ = (int32_t)(int16_t)slr
<< scale
;
311 #else /* ROCKBOX_BIG_ENDIAN */
312 *dl
++ = (int32_t)(int16_t)slr
<< scale
;
313 *dr
++ = (slr
>> 16) << scale
;
321 /* convert count 16-bit noninterleaved stereo to 32-bit noninterleaved */
322 static void sample_input_lte_native_ni_stereo(
323 int count
, const char *src
[], int32_t *dst
[])
325 const int16_t *sl
= (int16_t *) src
[0];
326 const int16_t *sr
= (int16_t *) src
[1];
327 const int16_t * const slend
= sl
+ count
;
328 int32_t *dl
= dst
[0] = &sample_buf
[SAMPLE_BUF_LEFT_CHANNEL
];
329 int32_t *dr
= dst
[1] = &sample_buf
[SAMPLE_BUF_RIGHT_CHANNEL
];
330 int scale
= WORD_SHIFT
;
334 *dl
++ = *sl
++ << scale
;
335 *dr
++ = *sr
++ << scale
;
343 /* convert count 32-bit mono to 32-bit mono */
344 static void sample_input_gt_native_mono(
345 int count
, const char *src
[], int32_t *dst
[])
347 dst
[0] = dst
[1] = (int32_t *)src
[0];
348 src
[0] = (char *)(dst
[0] + count
);
351 /* convert count 32-bit interleaved stereo to 32-bit noninterleaved stereo */
352 static void sample_input_gt_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
+ 2*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
];
367 src
[0] = (char *)send
;
370 /* convert 32 bit-noninterleaved stereo to 32-bit noninterleaved stereo */
371 static void sample_input_gt_native_ni_stereo(
372 int count
, const char *src
[], int32_t *dst
[])
374 dst
[0] = (int32_t *)src
[0];
375 dst
[1] = (int32_t *)src
[1];
376 src
[0] = (char *)(dst
[0] + count
);
377 src
[1] = (char *)(dst
[1] + count
);
381 * sample_input_new_format()
383 * set the to-native sample conversion function based on dsp sample parameters
386 * needs syncing with changes to the following dsp parameters:
387 * * dsp->stereo_mode (A/V)
388 * * dsp->sample_depth (A/V)
390 static void sample_input_new_format(void)
392 static const sample_input_fn_type sample_input_functions
[] =
394 [SAMPLE_INPUT_LE_NATIVE_MONO
] = sample_input_lte_native_mono
,
395 [SAMPLE_INPUT_LE_NATIVE_I_STEREO
] = sample_input_lte_native_i_stereo
,
396 [SAMPLE_INPUT_LE_NATIVE_NI_STEREO
] = sample_input_lte_native_ni_stereo
,
397 [SAMPLE_INPUT_GT_NATIVE_MONO
] = sample_input_gt_native_mono
,
398 [SAMPLE_INPUT_GT_NATIVE_I_STEREO
] = sample_input_gt_native_i_stereo
,
399 [SAMPLE_INPUT_GT_NATIVE_NI_STEREO
] = sample_input_gt_native_ni_stereo
,
402 int convert
= dsp
->stereo_mode
;
404 if (dsp
->sample_depth
> NATIVE_DEPTH
)
405 convert
+= SAMPLE_INPUT_GT_NATIVE_1ST_INDEX
;
407 dsp
->input_samples
= sample_input_functions
[convert
];
410 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO
411 /* write mono internal format to output format */
412 static void sample_output_mono(int count
, struct dsp_data
*data
,
413 int32_t *src
[], int16_t *dst
)
415 const int32_t *s0
= src
[0];
416 const int scale
= data
->output_scale
;
417 const int dc_bias
= 1 << (scale
- 1);
421 int32_t lr
= clip_sample_16((*s0
++ + dc_bias
) >> scale
);
427 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO */
429 /* write stereo internal format to output format */
430 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO
431 static void sample_output_stereo(int count
, struct dsp_data
*data
,
432 int32_t *src
[], int16_t *dst
)
434 const int32_t *s0
= src
[0];
435 const int32_t *s1
= src
[1];
436 const int scale
= data
->output_scale
;
437 const int dc_bias
= 1 << (scale
- 1);
441 *dst
++ = clip_sample_16((*s0
++ + dc_bias
) >> scale
);
442 *dst
++ = clip_sample_16((*s1
++ + dc_bias
) >> scale
);
446 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO */
449 * The "dither" code to convert the 24-bit samples produced by libmad was
450 * taken from the coolplayer project - coolplayer.sourceforge.net
452 * This function handles mono and stereo outputs.
454 static void sample_output_dithered(int count
, struct dsp_data
*data
,
455 int32_t *src
[], int16_t *dst
)
457 const int32_t mask
= dither_mask
;
458 const int32_t bias
= dither_bias
;
459 const int scale
= data
->output_scale
;
460 const int32_t min
= data
->clip_min
;
461 const int32_t max
= data
->clip_max
;
462 const int32_t range
= max
- min
;
466 for (ch
= 0; ch
< dsp
->data
.num_channels
; ch
++)
468 struct dither_data
* const dither
= &dither_data
[ch
];
469 int32_t *s
= src
[ch
];
472 for (i
= 0, d
= &dst
[ch
]; i
< count
; i
++, s
++, d
+= 2)
474 int32_t output
, sample
;
477 /* Noise shape and bias */
479 sample
+= dither
->error
[0] - dither
->error
[1] + dither
->error
[2];
480 dither
->error
[2] = dither
->error
[1];
481 dither
->error
[1] = dither
->error
[0]/2;
483 output
= sample
+ bias
;
486 random
= dither
->random
*0x0019660dL
+ 0x3c6ef35fL
;
487 output
+= (random
& mask
) - (dither
->random
& mask
);
488 dither
->random
= random
;
491 int32_t c
= output
- min
;
492 if ((uint32_t)c
> (uint32_t)range
)
501 else if (sample
< min
)
510 dither
->error
[0] = sample
- output
;
513 *d
= output
>> scale
;
517 if (dsp
->data
.num_channels
== 2)
520 /* Have to duplicate left samples into the right channel since
521 pcm buffer and hardware is interleaved stereo */
533 * sample_output_new_format()
535 * set the from-native to ouput sample conversion routine
538 * needs syncing with changes to the following dsp parameters:
539 * * dsp->stereo_mode (A/V)
540 * * dither_enabled (A)
542 static void sample_output_new_format(void)
544 static const sample_output_fn_type sample_output_functions
[] =
547 sample_output_stereo
,
548 sample_output_dithered
,
549 sample_output_dithered
552 int out
= dsp
->data
.num_channels
- 1;
554 if (dsp
== audio_dsp
&& dither_enabled
)
557 dsp
->output_samples
= sample_output_functions
[out
];
561 * Linear interpolation resampling that introduces a one sample delay because
562 * of our inability to look into the future at the end of a frame.
564 #ifndef DSP_HAVE_ASM_RESAMPLING
565 static int dsp_downsample(int count
, struct dsp_data
*data
,
566 int32_t *src
[], int32_t *dst
[])
568 int ch
= data
->num_channels
- 1;
569 uint32_t delta
= data
->resample_data
.delta
;
573 /* Rolled channel loop actually showed slightly faster. */
576 /* Just initialize things and not worry too much about the relatively
577 * uncommon case of not being able to spit out a sample for the frame.
579 int32_t *s
= src
[ch
];
580 int32_t last
= data
->resample_data
.last_sample
[ch
];
582 data
->resample_data
.last_sample
[ch
] = s
[count
- 1];
584 phase
= data
->resample_data
.phase
;
587 /* Do we need last sample of previous frame for interpolation? */
591 while (pos
< (uint32_t)count
)
593 *d
++ = last
+ FRACMUL((phase
& 0xffff) << 15, s
[pos
] - last
);
601 /* Wrap phase accumulator back to start of next frame. */
602 data
->resample_data
.phase
= phase
- (count
<< 16);
606 static int dsp_upsample(int count
, struct dsp_data
*data
,
607 int32_t *src
[], int32_t *dst
[])
609 int ch
= data
->num_channels
- 1;
610 uint32_t delta
= data
->resample_data
.delta
;
614 /* Rolled channel loop actually showed slightly faster. */
617 /* Should always be able to output a sample for a ratio up to
618 RESAMPLE_BUF_COUNT / SAMPLE_BUF_COUNT. */
619 int32_t *s
= src
[ch
];
620 int32_t last
= data
->resample_data
.last_sample
[ch
];
622 data
->resample_data
.last_sample
[ch
] = s
[count
- 1];
624 phase
= data
->resample_data
.phase
;
629 *d
++ = last
+ FRACMUL((phase
& 0xffff) << 15, s
[0] - last
);
634 while (pos
< (uint32_t)count
)
637 *d
++ = last
+ FRACMUL((phase
& 0xffff) << 15, s
[pos
] - last
);
644 /* Wrap phase accumulator back to start of next frame. */
645 data
->resample_data
.phase
= phase
& 0xffff;
648 #endif /* DSP_HAVE_ASM_RESAMPLING */
650 static void resampler_new_delta(void)
652 dsp
->data
.resample_data
.delta
= (unsigned long)
653 dsp
->frequency
* 65536LL / NATIVE_FREQUENCY
;
655 if (dsp
->frequency
== NATIVE_FREQUENCY
)
657 /* NOTE: If fully glitch-free transistions from no resampling to
658 resampling are desired, last_sample history should be maintained
659 even when not resampling. */
660 dsp
->resample
= NULL
;
661 dsp
->data
.resample_data
.phase
= 0;
662 dsp
->data
.resample_data
.last_sample
[0] = 0;
663 dsp
->data
.resample_data
.last_sample
[1] = 0;
665 else if (dsp
->frequency
< NATIVE_FREQUENCY
)
666 dsp
->resample
= dsp_upsample
;
668 dsp
->resample
= dsp_downsample
;
671 /* Resample count stereo samples. Updates the src array, if resampling is
672 * done, to refer to the resampled data. Returns number of stereo samples
673 * for further processing.
675 static inline int resample(int count
, int32_t *src
[])
679 &resample_buf
[RESAMPLE_BUF_LEFT_CHANNEL
],
680 &resample_buf
[RESAMPLE_BUF_RIGHT_CHANNEL
],
683 count
= dsp
->resample(count
, &dsp
->data
, src
, dst
);
686 src
[1] = dst
[dsp
->data
.num_channels
- 1];
691 static void dither_init(void)
693 /* Voice codec should not reset the audio codec's dither data */
694 if (dsp
!= audio_dsp
)
697 memset(dither_data
, 0, sizeof (dither_data
));
698 dither_bias
= (1L << (dsp
->frac_bits
- NATIVE_DEPTH
));
699 dither_mask
= (1L << (dsp
->frac_bits
+ 1 - NATIVE_DEPTH
)) - 1;
702 void dsp_dither_enable(bool enable
)
704 /* Be sure audio dsp is current to set correct function */
705 struct dsp_config
*old_dsp
= switch_dsp(audio_dsp
);
706 dither_enabled
= enable
;
707 sample_output_new_format();
711 /* Applies crossfeed to the stereo signal in src.
712 * Crossfeed is a process where listening over speakers is simulated. This
713 * is good for old hard panned stereo records, which might be quite fatiguing
714 * to listen to on headphones with no crossfeed.
716 #ifndef DSP_HAVE_ASM_CROSSFEED
717 static void apply_crossfeed(int count
, int32_t *buf
[])
719 int32_t *hist_l
= &crossfeed_data
.history
[0];
720 int32_t *hist_r
= &crossfeed_data
.history
[2];
721 int32_t *delay
= &crossfeed_data
.delay
[0][0];
722 int32_t *coefs
= &crossfeed_data
.coefs
[0];
723 int32_t gain
= crossfeed_data
.gain
;
724 int32_t *di
= crossfeed_data
.index
;
730 for (i
= 0; i
< count
; i
++)
735 /* Filter delayed sample from left speaker */
736 ACC_INIT(acc
, *di
, coefs
[0]);
737 ACC(acc
, hist_l
[0], coefs
[1]);
738 ACC(acc
, hist_l
[1], coefs
[2]);
739 /* Save filter history for left speaker */
740 hist_l
[1] = GET_ACC(acc
);
743 /* Filter delayed sample from right speaker */
744 ACC_INIT(acc
, *di
, coefs
[0]);
745 ACC(acc
, hist_r
[0], coefs
[1]);
746 ACC(acc
, hist_r
[1], coefs
[2]);
747 /* Save filter history for right speaker */
748 hist_r
[1] = GET_ACC(acc
);
751 /* Now add the attenuated direct sound and write to outputs */
752 buf
[0][i
] = FRACMUL(left
, gain
) + hist_r
[1];
753 buf
[1][i
] = FRACMUL(right
, gain
) + hist_l
[1];
755 /* Wrap delay line index if bigger than delay line size */
756 if (di
>= delay
+ 13*2)
759 /* Write back local copies of data we've modified */
760 crossfeed_data
.index
= di
;
762 #endif /* DSP_HAVE_ASM_CROSSFEED */
765 * dsp_set_crossfeed(bool enable)
768 * needs syncing with changes to the following dsp parameters:
769 * * dsp->stereo_mode (A)
771 void dsp_set_crossfeed(bool enable
)
773 crossfeed_enabled
= enable
;
774 audio_dsp
->apply_crossfeed
=
775 (enable
&& audio_dsp
->data
.num_channels
> 1)
776 ? apply_crossfeed
: NULL
;
779 void dsp_set_crossfeed_direct_gain(int gain
)
781 crossfeed_data
.gain
= get_replaygain_int(gain
* -10) << 7;
782 /* If gain is negative, the calculation overflowed and we need to clamp */
783 if (crossfeed_data
.gain
< 0)
784 crossfeed_data
.gain
= 0x7fffffff;
787 /* Both gains should be below 0 dB (when inverted) */
788 void dsp_set_crossfeed_cross_params(long lf_gain
, long hf_gain
, long cutoff
)
790 int32_t *c
= crossfeed_data
.coefs
;
791 long scaler
= get_replaygain_int(lf_gain
* -10) << 7;
793 cutoff
= 0xffffffff/NATIVE_FREQUENCY
*cutoff
;
795 /* Divide cutoff by sqrt(10^(-hf_gain/20)) to place cutoff at the -3 dB
796 * point instead of shelf midpoint. This is for compatibility with the old
797 * crossfeed shelf filter and should be removed if crossfeed settings are
798 * ever made incompatible for any other good reason.
800 cutoff
= DIV64(cutoff
, get_replaygain_int(-hf_gain
*5), 24);
801 filter_shelf_coefs(cutoff
, -hf_gain
, false, c
);
802 /* Scale coefs by LF gain and shift them to s0.31 format. We have no gains
803 * over 1 and can do this safely
805 c
[0] = FRACMUL_SHL(c
[0], scaler
, 4);
806 c
[1] = FRACMUL_SHL(c
[1], scaler
, 4);
810 /* Apply a constant gain to the samples (e.g., for ReplayGain).
811 * Note that this must be called before the resampler.
813 #ifndef DSP_HAVE_ASM_APPLY_GAIN
814 static void dsp_apply_gain(int count
, struct dsp_data
*data
, int32_t *buf
[])
816 const int32_t gain
= data
->gain
;
817 int ch
= data
->num_channels
- 1;
821 int32_t *s
= buf
[ch
];
822 int32_t *d
= buf
[ch
];
828 FRACMUL_8_LOOP(samp
, gain
, s
, d
);
834 #endif /* DSP_HAVE_ASM_APPLY_GAIN */
836 /* Combine all gains to a global gain. */
837 static void set_gain(struct dsp_config
*dsp
)
839 dsp
->data
.gain
= DEFAULT_GAIN
;
841 /* Replay gain not relevant to voice */
842 if (dsp
== audio_dsp
&& replaygain
)
844 dsp
->data
.gain
= replaygain
;
847 if (eq_enabled
&& eq_precut
)
850 (long) (((int64_t) dsp
->data
.gain
* eq_precut
) >> 24);
853 if (dsp
->data
.gain
== DEFAULT_GAIN
)
859 dsp
->data
.gain
>>= 1;
862 dsp
->apply_gain
= dsp
->data
.gain
!= 0 ? dsp_apply_gain
: NULL
;
866 * Use to enable the equalizer.
868 * @param enable true to enable the equalizer
870 void dsp_set_eq(bool enable
)
876 * Update the amount to cut the audio before applying the equalizer.
878 * @param precut to apply in decibels (multiplied by 10)
880 void dsp_set_eq_precut(int precut
)
882 eq_precut
= get_replaygain_int(precut
* -10);
884 set_gain(voice_dsp
); /* For EQ precut */
888 * Synchronize the equalizer filter coefficients with the global settings.
890 * @param band the equalizer band to synchronize
892 void dsp_set_eq_coefs(int band
)
896 unsigned long cutoff
, q
;
898 /* Adjust setting pointer to the band we actually want to change */
899 setting
= &global_settings
.eq_band0_cutoff
+ (band
* 3);
901 /* Convert user settings to format required by coef generator functions */
902 cutoff
= 0xffffffff / NATIVE_FREQUENCY
* (*setting
++);
909 /* NOTE: The coef functions assume the EMAC unit is in fractional mode,
910 which it should be, since we're executed from the main thread. */
912 /* Assume a band is disabled if the gain is zero */
915 eq_data
.enabled
[band
] = 0;
920 eq_ls_coefs(cutoff
, q
, gain
, eq_data
.filters
[band
].coefs
);
922 eq_hs_coefs(cutoff
, q
, gain
, eq_data
.filters
[band
].coefs
);
924 eq_pk_coefs(cutoff
, q
, gain
, eq_data
.filters
[band
].coefs
);
926 eq_data
.enabled
[band
] = 1;
930 /* Apply EQ filters to those bands that have got it switched on. */
931 static void eq_process(int count
, int32_t *buf
[])
933 static const int shifts
[] =
935 EQ_SHELF_SHIFT
, /* low shelf */
936 EQ_PEAK_SHIFT
, /* peaking */
937 EQ_PEAK_SHIFT
, /* peaking */
938 EQ_PEAK_SHIFT
, /* peaking */
939 EQ_SHELF_SHIFT
, /* high shelf */
941 unsigned int channels
= dsp
->data
.num_channels
;
944 /* filter configuration currently is 1 low shelf filter, 3 band peaking
945 filters and 1 high shelf filter, in that order. we need to know this
946 so we can choose the correct shift factor.
948 for (i
= 0; i
< 5; i
++)
950 if (!eq_data
.enabled
[i
])
952 eq_filter(buf
, &eq_data
.filters
[i
], count
, channels
, shifts
[i
]);
956 void dsp_set_stereo_width(int value
)
958 long width
, straight
, cross
;
960 width
= value
* 0x7fffff / 100;
964 straight
= (0x7fffff + width
) / 2;
965 cross
= straight
- width
;
969 /* straight = (1 + width) / (2 * width) */
970 straight
= ((int64_t)(0x7fffff + width
) << 22) / width
;
971 cross
= straight
- 0x7fffff;
974 dsp_sw_gain
= straight
<< 8;
975 dsp_sw_cross
= cross
<< 8;
978 #if CONFIG_CODEC == SWCODEC
980 #ifdef HAVE_SW_TONE_CONTROLS
981 static void set_tone_controls(void)
983 filter_bishelf_coefs(0xffffffff/NATIVE_FREQUENCY
*200,
984 0xffffffff/NATIVE_FREQUENCY
*3500,
985 bass
, treble
, -prescale
, tone_filter
.coefs
);
989 int dsp_callback(int msg
, intptr_t param
)
992 #ifdef HAVE_SW_TONE_CONTROLS
993 case DSP_CALLBACK_SET_PRESCALE
:
997 /* prescaler is always set after calling any of these, so we wait with
998 * calculating coefs until the above case is hit.
1000 case DSP_CALLBACK_SET_BASS
:
1003 case DSP_CALLBACK_SET_TREBLE
:
1006 case DSP_CALLBACK_SET_CHANNEL_CONFIG
:
1007 dsp_set_channel_config(param
);
1009 case DSP_CALLBACK_SET_STEREO_WIDTH
:
1010 dsp_set_stereo_width(param
);
1020 * Implements the different channel configurations and stereo width.
1023 /* SOUND_CHAN_STEREO mode is a noop so has no function - just outline one for
1026 static void channels_process_sound_chan_stereo(int count
, int32_t *buf
[])
1028 /* The channels are each just themselves */
1029 (void)count
; (void)buf
;
1033 #ifndef DSP_HAVE_ASM_SOUND_CHAN_MONO
1034 static void channels_process_sound_chan_mono(int count
, int32_t *buf
[])
1036 int32_t *sl
= buf
[0], *sr
= buf
[1];
1040 int32_t lr
= *sl
/2 + *sr
/2;
1044 while (--count
> 0);
1046 #endif /* DSP_HAVE_ASM_SOUND_CHAN_MONO */
1048 #ifndef DSP_HAVE_ASM_SOUND_CHAN_CUSTOM
1049 static void channels_process_sound_chan_custom(int count
, int32_t *buf
[])
1051 const int32_t gain
= dsp_sw_gain
;
1052 const int32_t cross
= dsp_sw_cross
;
1053 int32_t *sl
= buf
[0], *sr
= buf
[1];
1059 *sl
++ = FRACMUL(l
, gain
) + FRACMUL(r
, cross
);
1060 *sr
++ = FRACMUL(r
, gain
) + FRACMUL(l
, cross
);
1062 while (--count
> 0);
1064 #endif /* DSP_HAVE_ASM_SOUND_CHAN_CUSTOM */
1066 static void channels_process_sound_chan_mono_left(int count
, int32_t *buf
[])
1068 /* Just copy over the other channel */
1069 memcpy(buf
[1], buf
[0], count
* sizeof (*buf
));
1072 static void channels_process_sound_chan_mono_right(int count
, int32_t *buf
[])
1074 /* Just copy over the other channel */
1075 memcpy(buf
[0], buf
[1], count
* sizeof (*buf
));
1078 #ifndef DSP_HAVE_ASM_SOUND_CHAN_KARAOKE
1079 static void channels_process_sound_chan_karaoke(int count
, int32_t *buf
[])
1081 int32_t *sl
= buf
[0], *sr
= buf
[1];
1085 int32_t ch
= *sl
/2 - *sr
/2;
1089 while (--count
> 0);
1091 #endif /* DSP_HAVE_ASM_SOUND_CHAN_KARAOKE */
1093 void dsp_set_channel_config(int value
)
1095 static const channels_process_fn_type channels_process_functions
[] =
1097 /* SOUND_CHAN_STEREO = All-purpose index for no channel processing */
1098 [SOUND_CHAN_STEREO
] = NULL
,
1099 [SOUND_CHAN_MONO
] = channels_process_sound_chan_mono
,
1100 [SOUND_CHAN_CUSTOM
] = channels_process_sound_chan_custom
,
1101 [SOUND_CHAN_MONO_LEFT
] = channels_process_sound_chan_mono_left
,
1102 [SOUND_CHAN_MONO_RIGHT
] = channels_process_sound_chan_mono_right
,
1103 [SOUND_CHAN_KARAOKE
] = channels_process_sound_chan_karaoke
,
1106 if ((unsigned)value
>= ARRAYLEN(channels_process_functions
) ||
1107 audio_dsp
->stereo_mode
== STEREO_MONO
)
1108 value
= SOUND_CHAN_STEREO
;
1110 /* This doesn't apply to voice */
1111 channels_mode
= value
;
1112 audio_dsp
->channels_process
= channels_process_functions
[value
];
1115 /* Process and convert src audio to dst based on the DSP configuration,
1116 * reading count number of audio samples. dst is assumed to be large
1117 * enough; use dsp_output_count() to get the required number. src is an
1118 * array of pointers; for mono and interleaved stereo, it contains one
1119 * pointer to the start of the audio data and the other is ignored; for
1120 * non-interleaved stereo, it contains two pointers, one for each audio
1121 * channel. Returns number of bytes written to dst.
1123 int dsp_process(char *dst
, const char *src
[], int count
)
1129 #if defined(CPU_COLDFIRE)
1130 /* set emac unit for dsp processing, and save old macsr, we're running in
1131 codec thread context at this point, so can't clobber it */
1132 unsigned long old_macsr
= coldfire_get_macsr();
1133 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
1137 dsp_set_replaygain(); /* Gain has changed */
1139 /* Testing function pointers for NULL is preferred since the pointer
1140 will be preloaded to be used for the call if not. */
1143 samples
= MIN(SAMPLE_BUF_COUNT
/2, count
);
1146 dsp
->input_samples(samples
, src
, tmp
);
1148 if (dsp
->apply_gain
)
1149 dsp
->apply_gain(samples
, &dsp
->data
, tmp
);
1151 if (dsp
->resample
&& (samples
= resample(samples
, tmp
)) <= 0)
1152 break; /* I'm pretty sure we're downsampling here */
1154 if (dsp
->apply_crossfeed
)
1155 dsp
->apply_crossfeed(samples
, tmp
);
1157 /* TODO: EQ and tone controls need separate structs for audio and voice
1158 * DSP processing thanks to filter history. isn't really audible now, but
1159 * might be the day we start handling voice more delicately. Planned
1160 * changes may well run all relevent channels through the same EQ so
1164 eq_process(samples
, tmp
);
1166 #ifdef HAVE_SW_TONE_CONTROLS
1167 if ((bass
| treble
) != 0)
1168 eq_filter(tmp
, &tone_filter
, samples
, dsp
->data
.num_channels
,
1169 FILTER_BISHELF_SHIFT
);
1172 if (dsp
->channels_process
)
1173 dsp
->channels_process(samples
, tmp
);
1175 dsp
->output_samples(samples
, &dsp
->data
, tmp
, (int16_t *)dst
);
1178 dst
+= samples
* sizeof (int16_t) * 2;
1182 #if defined(CPU_COLDFIRE)
1183 /* set old macsr again */
1184 coldfire_set_macsr(old_macsr
);
1189 /* Given count number of input samples, calculate the maximum number of
1190 * samples of output data that would be generated (the calculation is not
1191 * entirely exact and rounds upwards to be on the safe side; during
1192 * resampling, the number of samples generated depends on the current state
1193 * of the resampler).
1195 /* dsp_input_size MUST be called afterwards */
1196 int dsp_output_count(int count
)
1200 count
= (int)(((unsigned long)count
* NATIVE_FREQUENCY
1201 + (dsp
->frequency
- 1)) / dsp
->frequency
);
1204 /* Now we have the resampled sample count which must not exceed
1205 * RESAMPLE_BUF_COUNT/2 to avoid resample buffer overflow. One
1206 * must call dsp_input_count() to get the correct input sample
1209 if (count
> RESAMPLE_BUF_COUNT
/2)
1210 count
= RESAMPLE_BUF_COUNT
/2;
1215 /* Given count output samples, calculate number of input samples
1216 * that would be consumed in order to fill the output buffer.
1218 int dsp_input_count(int count
)
1220 /* count is now the number of resampled input samples. Convert to
1221 original input samples. */
1224 /* Use the real resampling delta =
1225 * dsp->frequency * 65536 / NATIVE_FREQUENCY, and
1226 * round towards zero to avoid buffer overflows. */
1227 count
= (int)(((unsigned long)count
*
1228 dsp
->data
.resample_data
.delta
) >> 16);
1234 int dsp_stereo_mode(void)
1236 return dsp
->stereo_mode
;
1239 static void dsp_set_gain_var(long *var
, long value
)
1241 /* Voice shouldn't mess with these */
1242 if (dsp
== audio_dsp
)
1249 static void dsp_update_functions(void)
1251 sample_input_new_format();
1252 sample_output_new_format();
1253 if (dsp
== audio_dsp
)
1254 dsp_set_crossfeed(crossfeed_enabled
);
1257 bool dsp_configure(int setting
, intptr_t value
)
1261 case DSP_SWITCH_CODEC
:
1262 if ((uintptr_t)value
<= 1)
1263 switch_dsp(&dsp_conf
[value
]);
1266 case DSP_SET_FREQUENCY
:
1267 memset(&dsp
->data
.resample_data
, 0,
1268 sizeof (dsp
->data
.resample_data
));
1269 /* Fall through!!! */
1270 case DSP_SWITCH_FREQUENCY
:
1271 dsp
->codec_frequency
= (value
== 0) ? NATIVE_FREQUENCY
: value
;
1272 /* Account for playback speed adjustment when setting dsp->frequency
1273 if we're called from the main audio thread. Voice UI thread should
1274 not need this feature.
1276 if (dsp
== audio_dsp
)
1277 dsp
->frequency
= pitch_ratio
* dsp
->codec_frequency
/ 1000;
1279 dsp
->frequency
= dsp
->codec_frequency
;
1281 resampler_new_delta();
1284 case DSP_SET_SAMPLE_DEPTH
:
1285 dsp
->sample_depth
= value
;
1287 if (dsp
->sample_depth
<= NATIVE_DEPTH
)
1289 dsp
->frac_bits
= WORD_FRACBITS
;
1290 dsp
->sample_bytes
= sizeof (int16_t); /* samples are 16 bits */
1291 dsp
->data
.clip_max
= ((1 << WORD_FRACBITS
) - 1);
1292 dsp
->data
.clip_min
= -((1 << WORD_FRACBITS
));
1296 dsp
->frac_bits
= value
;
1297 dsp
->sample_bytes
= sizeof (int32_t); /* samples are 32 bits */
1298 dsp
->data
.clip_max
= (1 << value
) - 1;
1299 dsp
->data
.clip_min
= -(1 << value
);
1302 dsp
->data
.output_scale
= dsp
->frac_bits
+ 1 - NATIVE_DEPTH
;
1303 sample_input_new_format();
1307 case DSP_SET_STEREO_MODE
:
1308 dsp
->stereo_mode
= value
;
1309 dsp
->data
.num_channels
= value
== STEREO_MONO
? 1 : 2;
1310 dsp_update_functions();
1314 dsp
->stereo_mode
= STEREO_NONINTERLEAVED
;
1315 dsp
->data
.num_channels
= 2;
1316 dsp
->sample_depth
= NATIVE_DEPTH
;
1317 dsp
->frac_bits
= WORD_FRACBITS
;
1318 dsp
->sample_bytes
= sizeof (int16_t);
1319 dsp
->data
.output_scale
= dsp
->frac_bits
+ 1 - NATIVE_DEPTH
;
1320 dsp
->data
.clip_max
= ((1 << WORD_FRACBITS
) - 1);
1321 dsp
->data
.clip_min
= -((1 << WORD_FRACBITS
));
1322 dsp
->codec_frequency
= dsp
->frequency
= NATIVE_FREQUENCY
;
1324 if (dsp
== audio_dsp
)
1333 dsp_update_functions();
1334 resampler_new_delta();
1338 memset(&dsp
->data
.resample_data
, 0,
1339 sizeof (dsp
->data
.resample_data
));
1340 resampler_new_delta();
1344 case DSP_SET_TRACK_GAIN
:
1345 dsp_set_gain_var(&track_gain
, value
);
1348 case DSP_SET_ALBUM_GAIN
:
1349 dsp_set_gain_var(&album_gain
, value
);
1352 case DSP_SET_TRACK_PEAK
:
1353 dsp_set_gain_var(&track_peak
, value
);
1356 case DSP_SET_ALBUM_PEAK
:
1357 dsp_set_gain_var(&album_peak
, value
);
1367 void dsp_set_replaygain(void)
1373 if (global_settings
.replaygain
|| global_settings
.replaygain_noclip
)
1375 bool track_mode
= get_replaygain_mode(track_gain
!= 0,
1376 album_gain
!= 0) == REPLAYGAIN_TRACK
;
1377 long peak
= (track_mode
|| !album_peak
) ? track_peak
: album_peak
;
1379 if (global_settings
.replaygain
)
1381 gain
= (track_mode
|| !album_gain
) ? track_gain
: album_gain
;
1383 if (global_settings
.replaygain_preamp
)
1385 long preamp
= get_replaygain_int(
1386 global_settings
.replaygain_preamp
* 10);
1388 gain
= (long) (((int64_t) gain
* preamp
) >> 24);
1394 /* So that noclip can work even with no gain information. */
1395 gain
= DEFAULT_GAIN
;
1398 if (global_settings
.replaygain_noclip
&& (peak
!= 0)
1399 && ((((int64_t) gain
* peak
) >> 24) >= DEFAULT_GAIN
))
1401 gain
= (((int64_t) DEFAULT_GAIN
<< 24) / peak
);
1404 if (gain
== DEFAULT_GAIN
)
1406 /* Nothing to do, disable processing. */
1411 /* Store in S8.23 format to simplify calculations. */
1413 set_gain(audio_dsp
);