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 ****************************************************************************/
28 #include "replaygain.h"
35 /* The "dither" code to convert the 24-bit samples produced by libmad was
36 * taken from the coolplayer project - coolplayer.sourceforge.net
39 /* 16-bit samples are scaled based on these constants. The shift should be
43 #define WORD_FRACBITS 27
45 #define NATIVE_DEPTH 16
46 #define SAMPLE_BUF_SIZE 256
47 #define RESAMPLE_BUF_SIZE (256 * 4) /* Enough for 11,025 Hz -> 44,100 Hz*/
48 #define DEFAULT_GAIN 0x01000000
50 #if defined(CPU_COLDFIRE) && !defined(SIMULATOR)
52 /* Multiply two S.31 fractional integers and return the sign bit and the
53 * 31 most significant bits of the result.
55 #define FRACMUL(x, y) \
58 asm volatile ("mac.l %[a], %[b], %%acc0\n\t" \
59 "movclr.l %%acc0, %[t]\n\t" \
60 : [t] "=r" (t) : [a] "r" (x), [b] "r" (y)); \
64 /* Multiply one S.31-bit and one S8.23 fractional integer and return the
65 * sign bit and the 31 most significant bits of the result.
67 #define FRACMUL_8(x, y) \
71 asm volatile ("mac.l %[a], %[b], %%acc0\n\t" \
72 "move.l %%accext01, %[u]\n\t" \
73 "movclr.l %%acc0, %[t]\n\t" \
74 : [t] "=r" (t), [u] "=r" (u) : [a] "r" (x), [b] "r" (y)); \
75 (t << 8) | (u & 0xff); \
78 /* Multiply one S.31-bit and one S8.23 fractional integer and return the
79 * sign bit and the 31 most significant bits of the result. Load next value
80 * to multiply with into x from s (and increase s); x must contain the
83 #define FRACMUL_8_LOOP_PART(x, s, d, y) \
86 asm volatile ("mac.l %[a], %[b], (%[c])+, %[a], %%acc0\n\t" \
87 "move.l %%accext01, %[u]\n\t" \
88 "movclr.l %%acc0, %[t]" \
89 : [a] "+r" (x), [c] "+a" (s), [t] "=r" (d), [u] "=r" (u) \
91 d = (d << 8) | (u & 0xff); \
94 #define FRACMUL_8_LOOP(x, y, s, d) \
97 FRACMUL_8_LOOP_PART(x, s, t, y); \
98 asm volatile ("move.l %[t],(%[d])+" \
103 #define ACC(acc, x, y) \
105 asm volatile ("mac.l %[a], %[b], %%acc0" \
106 : : [a] "i,r" (x), [b] "i,r" (y));
107 #define GET_ACC(acc) \
111 asm volatile ("movclr.l %%acc0, %[t]" \
116 #define ACC_INIT(acc, x, y) ACC(acc, x, y)
118 #elif defined(CPU_ARM) && !defined(SIMULATOR)
120 /* Multiply two S.31 fractional integers and return the sign bit and the
121 * 31 most significant bits of the result.
123 #define FRACMUL(x, y) \
126 asm volatile ("smull r0, r1, %[a], %[b]\n\t" \
127 "mov %[t], r1, asl #1\n\t" \
128 "orr %[t], %[t], r0, lsr #31\n\t" \
129 : [t] "=r" (t) : [a] "r" (x), [b] "r" (y) : "r0", "r1"); \
133 #define ACC_INIT(acc, x, y) acc = FRACMUL(x, y)
134 #define ACC(acc, x, y) acc += FRACMUL(x, y)
135 #define GET_ACC(acc) acc
137 /* Multiply one S.31-bit and one S8.23 fractional integer and store the
138 * sign bit and the 31 most significant bits of the result to d (and
139 * increase d). Load next value to multiply with into x from s (and
140 * increase s); x must contain the initial value.
142 #define FRACMUL_8_LOOP(x, y, s, d) \
144 asm volatile ("smull r0, r1, %[a], %[b]\n\t" \
145 "mov %[t], r1, asl #9\n\t" \
146 "orr %[t], %[t], r0, lsr #23\n\t" \
147 : [t] "=r" (*(d)++) : [a] "r" (x), [b] "r" (y) : "r0", "r1"); \
153 #define ACC_INIT(acc, x, y) acc = FRACMUL(x, y)
154 #define ACC(acc, x, y) acc += FRACMUL(x, y)
155 #define GET_ACC(acc) acc
156 #define FRACMUL(x, y) (long) (((((long long) (x)) * ((long long) (y))) >> 31))
157 #define FRACMUL_8(x, y) (long) (((((long long) (x)) * ((long long) (y))) >> 23))
158 #define FRACMUL_8_LOOP(x, y, s, d) \
162 *(d)++ = (long) (((((long long) (t)) * ((long long) (y))) >> 23)); \
169 long codec_frequency
; /* Sample rate of data coming from the codec */
170 long frequency
; /* Effective sample rate after pitch shift (if any) */
184 bool crossfeed_enabled
;
187 long gain
; /* Note that this is in S8.23 format. */
193 int32_t last_sample
[2];
202 struct crossfeed_data
204 int32_t gain
; /* Direct path gain */
205 int32_t coefs
[3]; /* Coefficients for the shelving filter */
206 int32_t history
[4]; /* Format is x[n - 1], y[n - 1] for both channels */
207 int32_t delay
[13][2];
208 int index
; /* Current index into the delay line */
211 /* Current setup is one lowshelf filters, three peaking filters and one
212 highshelf filter. Varying the number of shelving filters make no sense,
213 but adding peaking filters is possible. */
215 char enabled
[5]; /* Flags for active filters */
216 struct eqfilter filters
[5];
219 static struct dsp_config dsp_conf
[2] IBSS_ATTR
;
220 static struct dither_data dither_data
[2] IBSS_ATTR
;
221 static struct resample_data resample_data
[2] IBSS_ATTR
;
222 struct crossfeed_data crossfeed_data IBSS_ATTR
;
223 static struct eq_state eq_data
;
225 static int pitch_ratio
= 1000;
226 static int channels_mode
= 0;
227 static int32_t sw_gain
, sw_cross
;
229 extern int current_codec
;
230 struct dsp_config
*dsp
;
232 /* The internal format is 32-bit samples, non-interleaved, stereo. This
233 * format is similar to the raw output from several codecs, so the amount
234 * of copying needed is minimized for that case.
237 static int32_t sample_buf
[SAMPLE_BUF_SIZE
] IBSS_ATTR
;
238 static int32_t resample_buf
[RESAMPLE_BUF_SIZE
] IBSS_ATTR
;
240 int sound_get_pitch(void)
245 void sound_set_pitch(int permille
)
247 pitch_ratio
= permille
;
249 dsp_configure(DSP_SWITCH_FREQUENCY
, (int *)dsp
->codec_frequency
);
252 /* Convert at most count samples to the internal format, if needed. Returns
253 * number of samples ready for further processing. Updates src to point
254 * past the samples "consumed" and dst is set to point to the samples to
255 * consume. Note that for mono, dst[0] equals dst[1], as there is no point
256 * in processing the same data twice.
258 static int convert_to_internal(const char* src
[], int count
, int32_t* dst
[])
260 count
= MIN(SAMPLE_BUF_SIZE
/ 2, count
);
262 if ((dsp
->sample_depth
<= NATIVE_DEPTH
)
263 || (dsp
->stereo_mode
== STEREO_INTERLEAVED
))
265 dst
[0] = &sample_buf
[0];
266 dst
[1] = (dsp
->stereo_mode
== STEREO_MONO
)
267 ? dst
[0] : &sample_buf
[SAMPLE_BUF_SIZE
/ 2];
271 dst
[0] = (int32_t*) src
[0];
272 dst
[1] = (int32_t*) ((dsp
->stereo_mode
== STEREO_MONO
) ? src
[0] : src
[1]);
275 if (dsp
->sample_depth
<= NATIVE_DEPTH
)
277 short* s0
= (short*) src
[0];
278 int32_t* d0
= dst
[0];
279 int32_t* d1
= dst
[1];
280 int scale
= WORD_SHIFT
;
283 if (dsp
->stereo_mode
== STEREO_INTERLEAVED
)
285 for (i
= 0; i
< count
; i
++)
287 *d0
++ = *s0
++ << scale
;
288 *d1
++ = *s0
++ << scale
;
291 else if (dsp
->stereo_mode
== STEREO_NONINTERLEAVED
)
293 short* s1
= (short*) src
[1];
295 for (i
= 0; i
< count
; i
++)
297 *d0
++ = *s0
++ << scale
;
298 *d1
++ = *s1
++ << scale
;
303 for (i
= 0; i
< count
; i
++)
305 *d0
++ = *s0
++ << scale
;
309 else if (dsp
->stereo_mode
== STEREO_INTERLEAVED
)
311 int32_t* s0
= (int32_t*) src
[0];
312 int32_t* d0
= dst
[0];
313 int32_t* d1
= dst
[1];
316 for (i
= 0; i
< count
; i
++)
323 if (dsp
->stereo_mode
== STEREO_NONINTERLEAVED
)
325 src
[0] += count
* dsp
->sample_bytes
;
326 src
[1] += count
* dsp
->sample_bytes
;
328 else if (dsp
->stereo_mode
== STEREO_INTERLEAVED
)
330 src
[0] += count
* dsp
->sample_bytes
* 2;
334 src
[0] += count
* dsp
->sample_bytes
;
340 static void resampler_set_delta(int frequency
)
342 resample_data
[current_codec
].delta
= (unsigned long)
343 frequency
* 65536LL / NATIVE_FREQUENCY
;
346 /* Linear interpolation resampling that introduces a one sample delay because
347 * of our inability to look into the future at the end of a frame.
350 /* TODO: we really should have a separate set of resample functions for both
351 mono and stereo to avoid all this internal branching and looping. */
352 static long downsample(int32_t **dst
, int32_t **src
, int count
,
353 struct resample_data
*r
)
355 long phase
= r
->phase
;
356 long delta
= r
->delta
;
358 int32_t *d
[2] = { dst
[0], dst
[1] };
359 int pos
= phase
>> 16;
361 int num_channels
= dsp
->stereo_mode
== STEREO_MONO
? 1 : 2;
363 for (j
= 0; j
< num_channels
; j
++) {
364 last_sample
= r
->last_sample
[j
];
365 /* Do we need last sample of previous frame for interpolation? */
368 last_sample
= src
[j
][pos
- 1];
370 *d
[j
]++ = last_sample
+ FRACMUL((phase
& 0xffff) << 15,
371 src
[j
][pos
] - last_sample
);
375 while ((pos
= phase
>> 16) < count
)
377 for (j
= 0; j
< num_channels
; j
++)
378 *d
[j
]++ = src
[j
][pos
- 1] + FRACMUL((phase
& 0xffff) << 15,
379 src
[j
][pos
] - src
[j
][pos
- 1]);
384 /* Wrap phase accumulator back to start of next frame. */
385 r
->phase
= phase
- (count
<< 16);
387 r
->last_sample
[0] = src
[0][count
- 1];
388 r
->last_sample
[1] = src
[1][count
- 1];
392 static long upsample(int32_t **dst
, int32_t **src
, int count
, struct resample_data
*r
)
394 long phase
= r
->phase
;
395 long delta
= r
->delta
;
396 int32_t *d
[2] = { dst
[0], dst
[1] };
399 int num_channels
= dsp
->stereo_mode
== STEREO_MONO
? 1 : 2;
401 while ((pos
= phase
>> 16) == 0)
403 for (j
= 0; j
< num_channels
; j
++)
404 *d
[j
]++ = r
->last_sample
[j
] + FRACMUL((phase
& 0xffff) << 15,
405 src
[j
][pos
] - r
->last_sample
[j
]);
410 while ((pos
= phase
>> 16) < count
)
412 for (j
= 0; j
< num_channels
; j
++)
413 *d
[j
]++ = src
[j
][pos
- 1] + FRACMUL((phase
& 0xffff) << 15,
414 src
[j
][pos
] - src
[j
][pos
- 1]);
419 /* Wrap phase accumulator back to start of next frame. */
420 r
->phase
= phase
- (count
<< 16);
422 r
->last_sample
[0] = src
[0][count
- 1];
423 r
->last_sample
[1] = src
[1][count
- 1];
427 /* Resample count stereo samples. Updates the src array, if resampling is
428 * done, to refer to the resampled data. Returns number of stereo samples
429 * for further processing.
431 static inline int resample(int32_t* src
[], int count
)
435 if (dsp
->frequency
!= NATIVE_FREQUENCY
)
437 int32_t* dst
[2] = {&resample_buf
[0], &resample_buf
[RESAMPLE_BUF_SIZE
/ 2]};
439 if (dsp
->frequency
< NATIVE_FREQUENCY
)
441 new_count
= upsample(dst
, src
, count
,
442 &resample_data
[current_codec
]);
446 new_count
= downsample(dst
, src
, count
,
447 &resample_data
[current_codec
]);
451 if (dsp
->stereo_mode
!= STEREO_MONO
)
464 static inline long clip_sample(int32_t sample
, int32_t min
, int32_t max
)
470 else if (sample
< min
)
478 /* The "dither" code to convert the 24-bit samples produced by libmad was
479 * taken from the coolplayer project - coolplayer.sourceforge.net
482 static long dither_sample(int32_t sample
, int32_t bias
, int32_t mask
,
483 struct dither_data
* dither
)
490 /* Noise shape and bias */
492 sample
+= dither
->error
[0] - dither
->error
[1] + dither
->error
[2];
493 dither
->error
[2] = dither
->error
[1];
494 dither
->error
[1] = dither
->error
[0] / 2;
496 output
= sample
+ bias
;
500 random
= dither
->random
* 0x0019660dL
+ 0x3c6ef35fL
;
501 sample
+= (random
& mask
) - (dither
->random
& mask
);
502 dither
->random
= random
;
504 /* Clip and quantize */
508 sample
= clip_sample(sample
, min
, max
);
509 output
= clip_sample(output
, min
, max
) & ~mask
;
513 dither
->error
[0] = sample
- output
;
518 void dsp_set_crossfeed(bool enable
)
520 dsp
->crossfeed_enabled
= enable
;
523 void dsp_set_crossfeed_direct_gain(int gain
)
525 /* Work around bug in get_replaygain_int which returns 0 for 0 dB */
527 crossfeed_data
.gain
= 0x7fffffff;
529 crossfeed_data
.gain
= get_replaygain_int(gain
* -10) << 7;
532 void dsp_set_crossfeed_cross_params(long lf_gain
, long hf_gain
, long cutoff
)
534 long g1
= get_replaygain_int(lf_gain
* -10) << 3;
535 long g2
= get_replaygain_int(hf_gain
* -10) << 3;
537 filter_bishelf_coefs(0xffffffff/NATIVE_FREQUENCY
*cutoff
, g1
, g2
,
538 crossfeed_data
.coefs
);
541 /* Applies crossfeed to the stereo signal in src.
542 * Crossfeed is a process where listening over speakers is simulated. This
543 * is good for old hard panned stereo records, which might be quite fatiguing
544 * to listen to on headphones with no crossfeed.
546 #ifndef DSP_HAVE_ASM_CROSSFEED
547 void apply_crossfeed(int32_t* src
[], int count
)
549 int32_t *hist_l
= &crossfeed_data
.history
[0];
550 int32_t *hist_r
= &crossfeed_data
.history
[2];
551 int32_t *delay
= &crossfeed_data
.delay
[0][0];
552 int32_t *coefs
= &crossfeed_data
.coefs
[0];
553 int32_t gain
= crossfeed_data
.gain
;
554 int di
= crossfeed_data
.index
;
560 for (i
= 0; i
< count
; i
++) {
564 /* Filter delayed sample from left speaker */
565 ACC_INIT(acc
, delay
[di
*2], coefs
[0]);
566 ACC(acc
, hist_l
[0], coefs
[1]);
567 ACC(acc
, hist_l
[1], coefs
[2]);
568 /* Save filter history for left speaker */
569 hist_l
[1] = GET_ACC(acc
);
570 hist_l
[0] = delay
[di
*2];
571 /* Filter delayed sample from right speaker */
572 ACC_INIT(acc
, delay
[di
*2 + 1], coefs
[0]);
573 ACC(acc
, hist_r
[0], coefs
[1]);
574 ACC(acc
, hist_r
[1], coefs
[2]);
575 /* Save filter history for right speaker */
576 hist_r
[1] = GET_ACC(acc
);
577 hist_r
[0] = delay
[di
*2 + 1];
579 delay
[di
*2 + 1] = right
;
580 /* Now add the attenuated direct sound and write to outputs */
581 src
[0][i
] = FRACMUL(left
, gain
) + hist_r
[1];
582 src
[1][i
] = FRACMUL(right
, gain
) + hist_l
[1];
584 /* Wrap delay line index if bigger than delay line size */
588 /* Write back local copies of data we've modified */
589 crossfeed_data
.index
= di
;
593 /* Combine all gains to a global gain. */
594 static void set_gain(void)
596 dsp
->gain
= DEFAULT_GAIN
;
600 dsp
->gain
= dsp
->replaygain
;
603 if (dsp
->eq_enabled
&& dsp
->eq_precut
)
605 dsp
->gain
= (long) (((int64_t) dsp
->gain
* dsp
->eq_precut
) >> 24);
608 if (dsp
->gain
== DEFAULT_GAIN
)
619 * Use to enable the equalizer.
621 * @param enable true to enable the equalizer
623 void dsp_set_eq(bool enable
)
625 dsp
->eq_enabled
= enable
;
629 * Update the amount to cut the audio before applying the equalizer.
631 * @param precut to apply in decibels (multiplied by 10)
633 void dsp_set_eq_precut(int precut
)
635 dsp
->eq_precut
= get_replaygain_int(precut
* -10);
640 * Synchronize the equalizer filter coefficients with the global settings.
642 * @param band the equalizer band to synchronize
644 void dsp_set_eq_coefs(int band
)
648 unsigned long cutoff
, q
;
650 /* Adjust setting pointer to the band we actually want to change */
651 setting
= &global_settings
.eq_band0_cutoff
+ (band
* 3);
653 /* Convert user settings to format required by coef generator functions */
654 cutoff
= 0xffffffff / NATIVE_FREQUENCY
* (*setting
++);
655 q
= ((*setting
++) << 16) / 10; /* 16.16 */
656 gain
= ((*setting
++) << 16) / 10; /* s15.16 */
661 /* NOTE: The coef functions assume the EMAC unit is in fractional mode,
662 which it should be, since we're executed from the main thread. */
664 /* Assume a band is disabled if the gain is zero */
666 eq_data
.enabled
[band
] = 0;
669 eq_ls_coefs(cutoff
, q
, gain
, eq_data
.filters
[band
].coefs
);
671 eq_hs_coefs(cutoff
, q
, gain
, eq_data
.filters
[band
].coefs
);
673 eq_pk_coefs(cutoff
, q
, gain
, eq_data
.filters
[band
].coefs
);
675 eq_data
.enabled
[band
] = 1;
679 /* Apply EQ filters to those bands that have got it switched on. */
680 static void eq_process(int32_t **x
, unsigned num
)
683 unsigned int channels
= dsp
->stereo_mode
!= STEREO_MONO
? 2 : 1;
686 /* filter configuration currently is 1 low shelf filter, 3 band peaking
687 filters and 1 high shelf filter, in that order. we need to know this
688 so we can choose the correct shift factor.
690 for (i
= 0; i
< 5; i
++) {
691 if (eq_data
.enabled
[i
]) {
692 if (i
== 0 || i
== 4) /* shelving filters */
693 shift
= EQ_SHELF_SHIFT
;
695 shift
= EQ_PEAK_SHIFT
;
696 eq_filter(x
, &eq_data
.filters
[i
], num
, channels
, shift
);
701 /* Apply a constant gain to the samples (e.g., for ReplayGain). May update
702 * the src array if gain was applied.
703 * Note that this must be called before the resampler.
705 static void apply_gain(int32_t* _src
[], int _count
)
709 int32_t** src
= _src
;
711 int32_t* s0
= src
[0];
712 int32_t* s1
= src
[1];
713 long gain
= dsp
->gain
;
720 d
= &sample_buf
[SAMPLE_BUF_SIZE
/ 2];
724 for (i
= 0; i
< count
; i
++)
725 FRACMUL_8_LOOP(s
, gain
, s1
, d
);
729 src
[1] = &sample_buf
[0];
736 for (i
= 0; i
< count
; i
++)
737 FRACMUL_8_LOOP(s
, gain
, s0
, d
);
741 void channels_set(int value
)
743 channels_mode
= value
;
746 void stereo_width_set(int value
)
748 long width
, straight
, cross
;
750 width
= value
* 0x7fffff / 100;
752 straight
= (0x7fffff + width
) / 2;
753 cross
= straight
- width
;
755 /* straight = (1 + width) / (2 * width) */
756 straight
= ((int64_t)(0x7fffff + width
) << 22) / width
;
757 cross
= straight
- 0x7fffff;
759 sw_gain
= straight
<< 8;
760 sw_cross
= cross
<< 8;
763 /* Implements the different channel configurations and stereo width.
764 * We might want to combine this with the write_samples stage for efficiency,
765 * but for now we'll just let it stay as a stage of its own.
767 static void channels_process(int32_t **src
, int num
)
770 int32_t *sl
= src
[0], *sr
= src
[1];
772 if (channels_mode
== SOUND_CHAN_STEREO
)
774 switch (channels_mode
) {
775 case SOUND_CHAN_MONO
:
776 for (i
= 0; i
< num
; i
++)
777 sl
[i
] = sr
[i
] = sl
[i
]/2 + sr
[i
]/2;
779 case SOUND_CHAN_CUSTOM
:
780 for (i
= 0; i
< num
; i
++) {
781 int32_t left_sample
= sl
[i
];
783 sl
[i
] = FRACMUL(sl
[i
], sw_gain
) + FRACMUL(sr
[i
], sw_cross
);
784 sr
[i
] = FRACMUL(sr
[i
], sw_gain
) + FRACMUL(left_sample
, sw_cross
);
787 case SOUND_CHAN_MONO_LEFT
:
788 for (i
= 0; i
< num
; i
++)
791 case SOUND_CHAN_MONO_RIGHT
:
792 for (i
= 0; i
< num
; i
++)
795 case SOUND_CHAN_KARAOKE
:
796 for (i
= 0; i
< num
; i
++) {
797 int32_t left_sample
= sl
[i
]/2;
799 sl
[i
] = left_sample
- sr
[i
]/2;
800 sr
[i
] = sr
[i
]/2 - left_sample
;
806 static void write_samples(short* dst
, int32_t* src
[], int count
)
808 int32_t* s0
= src
[0];
809 int32_t* s1
= src
[1];
810 int scale
= dsp
->frac_bits
+ 1 - NATIVE_DEPTH
;
812 if (dsp
->dither_enabled
)
814 long bias
= (1L << (dsp
->frac_bits
- NATIVE_DEPTH
));
815 long mask
= (1L << scale
) - 1;
819 *dst
++ = (short) (dither_sample(*s0
++, bias
, mask
, &dither_data
[0])
821 *dst
++ = (short) (dither_sample(*s1
++, bias
, mask
, &dither_data
[1])
827 long min
= dsp
->clip_min
;
828 long max
= dsp
->clip_max
;
832 *dst
++ = (short) (clip_sample(*s0
++, min
, max
) >> scale
);
833 *dst
++ = (short) (clip_sample(*s1
++, min
, max
) >> scale
);
838 /* Process and convert src audio to dst based on the DSP configuration,
839 * reading size bytes of audio data. dst is assumed to be large enough; use
840 * dst_get_dest_size() to get the required size. src is an array of
841 * pointers; for mono and interleaved stereo, it contains one pointer to the
842 * start of the audio data; for non-interleaved stereo, it contains two
843 * pointers, one for each audio channel. Returns number of bytes written to
846 long dsp_process(char* dst
, const char* src
[], long size
)
853 #if defined(CPU_COLDFIRE) && !defined(SIMULATOR)
854 /* set emac unit for dsp processing, and save old macsr, we're running in
855 codec thread context at this point, so can't clobber it */
856 unsigned long old_macsr
= coldfire_get_macsr();
857 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
860 dsp
= &dsp_conf
[current_codec
];
862 factor
= (dsp
->stereo_mode
!= STEREO_MONO
) ? 2 : 1;
863 size
/= dsp
->sample_bytes
* factor
;
864 dsp_set_replaygain(false);
868 samples
= convert_to_internal(src
, size
, tmp
);
870 apply_gain(tmp
, samples
);
871 samples
= resample(tmp
, samples
);
872 if (dsp
->crossfeed_enabled
&& dsp
->stereo_mode
!= STEREO_MONO
)
873 apply_crossfeed(tmp
, samples
);
875 eq_process(tmp
, samples
);
876 if (dsp
->stereo_mode
!= STEREO_MONO
)
877 channels_process(tmp
, samples
);
878 write_samples((short*) dst
, tmp
, samples
);
880 dst
+= samples
* sizeof(short) * 2;
883 #if defined(CPU_COLDFIRE) && !defined(SIMULATOR)
884 /* set old macsr again */
885 coldfire_set_macsr(old_macsr
);
887 return written
* sizeof(short) * 2;
890 /* Given size bytes of input data, calculate the maximum number of bytes of
891 * output data that would be generated (the calculation is not entirely
892 * exact and rounds upwards to be on the safe side; during resampling,
893 * the number of samples generated depends on the current state of the
896 /* dsp_input_size MUST be called afterwards */
897 long dsp_output_size(long size
)
899 dsp
= &dsp_conf
[current_codec
];
901 if (dsp
->sample_depth
> NATIVE_DEPTH
)
906 if (dsp
->frequency
!= NATIVE_FREQUENCY
)
908 size
= (long) ((((unsigned long) size
* NATIVE_FREQUENCY
)
909 + (dsp
->frequency
- 1)) / dsp
->frequency
);
912 /* round to the next multiple of 2 (these are shorts) */
913 size
= (size
+ 1) & ~1;
915 if (dsp
->stereo_mode
== STEREO_MONO
)
920 /* now we have the size in bytes for two resampled channels,
921 * and the size in (short) must not exceed RESAMPLE_BUF_SIZE to
922 * avoid resample buffer overflow. One must call dsp_input_size()
923 * to get the correct input buffer size. */
924 if (size
> RESAMPLE_BUF_SIZE
*2)
925 size
= RESAMPLE_BUF_SIZE
*2;
930 /* Given size bytes of output buffer, calculate number of bytes of input
931 * data that would be consumed in order to fill the output buffer.
933 long dsp_input_size(long size
)
935 dsp
= &dsp_conf
[current_codec
];
937 /* convert to number of output stereo samples. */
940 /* Mono means we need half input samples to fill the output buffer */
941 if (dsp
->stereo_mode
== STEREO_MONO
)
944 /* size is now the number of resampled input samples. Convert to
945 original input samples. */
946 if (dsp
->frequency
!= NATIVE_FREQUENCY
)
948 /* Use the real resampling delta =
949 * (unsigned long) dsp->frequency * 65536 / NATIVE_FREQUENCY, and
950 * round towards zero to avoid buffer overflows. */
951 size
= ((unsigned long)size
*
952 resample_data
[current_codec
].delta
) >> 16;
955 /* Convert back to bytes. */
956 if (dsp
->sample_depth
> NATIVE_DEPTH
)
964 int dsp_stereo_mode(void)
966 dsp
= &dsp_conf
[current_codec
];
968 return dsp
->stereo_mode
;
971 bool dsp_configure(int setting
, void *value
)
973 dsp
= &dsp_conf
[current_codec
];
977 case DSP_SET_FREQUENCY
:
978 memset(&resample_data
[current_codec
], 0,
979 sizeof(struct resample_data
));
980 /* Fall through!!! */
981 case DSP_SWITCH_FREQUENCY
:
982 dsp
->codec_frequency
= ((long) value
== 0) ? NATIVE_FREQUENCY
: (long) value
;
983 /* Account for playback speed adjustment when settingg dsp->frequency
984 if we're called from the main audio thread. Voice UI thread should
985 not need this feature.
987 if (current_codec
== CODEC_IDX_AUDIO
)
988 dsp
->frequency
= pitch_ratio
* dsp
->codec_frequency
/ 1000;
990 dsp
->frequency
= dsp
->codec_frequency
;
991 resampler_set_delta(dsp
->frequency
);
994 case DSP_SET_CLIP_MIN
:
995 dsp
->clip_min
= (long) value
;
998 case DSP_SET_CLIP_MAX
:
999 dsp
->clip_max
= (long) value
;
1002 case DSP_SET_SAMPLE_DEPTH
:
1003 dsp
->sample_depth
= (long) value
;
1005 if (dsp
->sample_depth
<= NATIVE_DEPTH
)
1007 dsp
->frac_bits
= WORD_FRACBITS
;
1008 dsp
->sample_bytes
= sizeof(short);
1009 dsp
->clip_max
= ((1 << WORD_FRACBITS
) - 1);
1010 dsp
->clip_min
= -((1 << WORD_FRACBITS
));
1014 dsp
->frac_bits
= (long) value
;
1015 dsp
->sample_bytes
= 4; /* samples are 32 bits */
1016 dsp
->clip_max
= (1 << (long)value
) - 1;
1017 dsp
->clip_min
= -(1 << (long)value
);
1022 case DSP_SET_STEREO_MODE
:
1023 dsp
->stereo_mode
= (long) value
;
1027 dsp
->dither_enabled
= false;
1028 dsp
->stereo_mode
= STEREO_NONINTERLEAVED
;
1029 dsp
->clip_max
= ((1 << WORD_FRACBITS
) - 1);
1030 dsp
->clip_min
= -((1 << WORD_FRACBITS
));
1031 dsp
->track_gain
= 0;
1032 dsp
->album_gain
= 0;
1033 dsp
->track_peak
= 0;
1034 dsp
->album_peak
= 0;
1035 dsp
->codec_frequency
= dsp
->frequency
= NATIVE_FREQUENCY
;
1036 dsp
->sample_depth
= NATIVE_DEPTH
;
1037 dsp
->frac_bits
= WORD_FRACBITS
;
1038 dsp
->new_gain
= true;
1042 memset(dither_data
, 0, sizeof(dither_data
));
1043 dsp
->dither_enabled
= (bool) value
;
1046 case DSP_SET_TRACK_GAIN
:
1047 dsp
->track_gain
= (long) value
;
1048 dsp
->new_gain
= true;
1051 case DSP_SET_ALBUM_GAIN
:
1052 dsp
->album_gain
= (long) value
;
1053 dsp
->new_gain
= true;
1056 case DSP_SET_TRACK_PEAK
:
1057 dsp
->track_peak
= (long) value
;
1058 dsp
->new_gain
= true;
1061 case DSP_SET_ALBUM_PEAK
:
1062 dsp
->album_peak
= (long) value
;
1063 dsp
->new_gain
= true;
1073 void dsp_set_replaygain(bool always
)
1075 dsp
= &dsp_conf
[current_codec
];
1077 if (always
|| dsp
->new_gain
)
1081 dsp
->new_gain
= false;
1083 if (global_settings
.replaygain
|| global_settings
.replaygain_noclip
)
1086 = ((global_settings
.replaygain_type
== REPLAYGAIN_TRACK
)
1087 || ((global_settings
.replaygain_type
== REPLAYGAIN_SHUFFLE
)
1088 && global_settings
.playlist_shuffle
));
1089 long peak
= (track_mode
|| !dsp
->album_peak
)
1090 ? dsp
->track_peak
: dsp
->album_peak
;
1092 if (global_settings
.replaygain
)
1094 gain
= (track_mode
|| !dsp
->album_gain
)
1095 ? dsp
->track_gain
: dsp
->album_gain
;
1097 if (global_settings
.replaygain_preamp
)
1099 long preamp
= get_replaygain_int(
1100 global_settings
.replaygain_preamp
* 10);
1102 gain
= (long) (((int64_t) gain
* preamp
) >> 24);
1108 /* So that noclip can work even with no gain information. */
1109 gain
= DEFAULT_GAIN
;
1112 if (global_settings
.replaygain_noclip
&& (peak
!= 0)
1113 && ((((int64_t) gain
* peak
) >> 24) >= DEFAULT_GAIN
))
1115 gain
= (((int64_t) DEFAULT_GAIN
<< 24) / peak
);
1118 if (gain
== DEFAULT_GAIN
)
1120 /* Nothing to do, disable processing. */
1126 /* Store in S8.23 format to simplify calculations. */
1127 dsp
->replaygain
= gain
;