Introduce names for header and row color in the tables so that they are the same...
[maemo-rb.git] / apps / dsp.c
blobc8c8ddee75422c4332fc29f26c887fe7b50a6b20
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"
38 #include "pcmbuf.h"
40 /* Define LOGF_ENABLE to enable logf output in this file */
41 /*#define LOGF_ENABLE*/
42 #include "logf.h"
44 /* 16-bit samples are scaled based on these constants. The shift should be
45 * no more than 15.
47 #define WORD_SHIFT 12
48 #define WORD_FRACBITS 27
50 #define NATIVE_DEPTH 16
51 /* If the small buffer size changes, check the assembly code! */
52 #define SMALL_SAMPLE_BUF_COUNT 256
53 #define DEFAULT_GAIN 0x01000000
55 /* enums to index conversion properly with stereo mode and other settings */
56 enum
58 SAMPLE_INPUT_LE_NATIVE_I_STEREO = STEREO_INTERLEAVED,
59 SAMPLE_INPUT_LE_NATIVE_NI_STEREO = STEREO_NONINTERLEAVED,
60 SAMPLE_INPUT_LE_NATIVE_MONO = STEREO_MONO,
61 SAMPLE_INPUT_GT_NATIVE_I_STEREO = STEREO_INTERLEAVED + STEREO_NUM_MODES,
62 SAMPLE_INPUT_GT_NATIVE_NI_STEREO = STEREO_NONINTERLEAVED + STEREO_NUM_MODES,
63 SAMPLE_INPUT_GT_NATIVE_MONO = STEREO_MONO + STEREO_NUM_MODES,
64 SAMPLE_INPUT_GT_NATIVE_1ST_INDEX = STEREO_NUM_MODES
67 enum
69 SAMPLE_OUTPUT_MONO = 0,
70 SAMPLE_OUTPUT_STEREO,
71 SAMPLE_OUTPUT_DITHERED_MONO,
72 SAMPLE_OUTPUT_DITHERED_STEREO
75 /****************************************************************************
76 * NOTE: Any assembly routines that use these structures must be updated
77 * if current data members are moved or changed.
79 struct resample_data
81 uint32_t delta; /* 00h */
82 uint32_t phase; /* 04h */
83 int32_t last_sample[2]; /* 08h */
84 /* 10h */
87 /* This is for passing needed data to assembly dsp routines. If another
88 * dsp parameter needs to be passed, add to the end of the structure
89 * and remove from dsp_config.
90 * If another function type becomes assembly optimized and requires dsp
91 * config info, add a pointer paramter of type "struct dsp_data *".
92 * If removing something from other than the end, reserve the spot or
93 * else update every implementation for every target.
94 * Be sure to add the offset of the new member for easy viewing as well. :)
95 * It is the first member of dsp_config and all members can be accessesed
96 * through the main aggregate but this is intended to make a safe haven
97 * for these items whereas the c part can be rearranged at will. dsp_data
98 * could even moved within dsp_config without disurbing the order.
100 struct dsp_data
102 int output_scale; /* 00h */
103 int num_channels; /* 04h */
104 struct resample_data resample_data; /* 08h */
105 int32_t clip_min; /* 18h */
106 int32_t clip_max; /* 1ch */
107 int32_t gain; /* 20h - Note that this is in S8.23 format. */
108 /* 24h */
111 /* No asm...yet */
112 struct dither_data
114 long error[3]; /* 00h */
115 long random; /* 0ch */
116 /* 10h */
119 struct crossfeed_data
121 int32_t gain; /* 00h - Direct path gain */
122 int32_t coefs[3]; /* 04h - Coefficients for the shelving filter */
123 int32_t history[4]; /* 10h - Format is x[n - 1], y[n - 1] for both channels */
124 int32_t delay[13][2]; /* 20h */
125 int32_t *index; /* 88h - Current pointer into the delay line */
126 /* 8ch */
129 /* Current setup is one lowshelf filters three peaking filters and one
130 * highshelf filter. Varying the number of shelving filters make no sense,
131 * but adding peaking filters is possible.
133 struct eq_state
135 char enabled[5]; /* 00h - Flags for active filters */
136 struct eqfilter filters[5]; /* 08h - packing is 4? */
137 /* 10ch */
140 /* Include header with defines which functions are implemented in assembly
141 code for the target */
142 #include <dsp_asm.h>
144 /* Typedefs keep things much neater in this case */
145 typedef void (*sample_input_fn_type)(int count, const char *src[],
146 int32_t *dst[]);
147 typedef int (*resample_fn_type)(int count, struct dsp_data *data,
148 const int32_t *src[], int32_t *dst[]);
149 typedef void (*sample_output_fn_type)(int count, struct dsp_data *data,
150 const int32_t *src[], int16_t *dst);
152 /* Single-DSP channel processing in place */
153 typedef void (*channels_process_fn_type)(int count, int32_t *buf[]);
154 /* DSP local channel processing in place */
155 typedef void (*channels_process_dsp_fn_type)(int count, struct dsp_data *data,
156 int32_t *buf[]);
157 /* DSP processes that return a value */
158 typedef int (*return_fn_type)(int count, int32_t *buf[]);
161 ***************************************************************************/
163 struct dsp_config
165 struct dsp_data data; /* Config members for use in asm routines */
166 long codec_frequency; /* Sample rate of data coming from the codec */
167 long frequency; /* Effective sample rate after pitch shift (if any) */
168 int sample_depth;
169 int sample_bytes;
170 int stereo_mode;
171 int32_t tdspeed_percent; /* Speed% * PITCH_SPEED_PRECISION */
172 bool tdspeed_active; /* Timestretch is in use */
173 int frac_bits;
174 long limiter_preamp; /* limiter amp gain in S7.24 format */
175 #ifdef HAVE_SW_TONE_CONTROLS
176 /* Filter struct for software bass/treble controls */
177 struct eqfilter tone_filter;
178 #endif
179 /* Functions that change depending upon settings - NULL if stage is
180 disabled */
181 sample_input_fn_type input_samples;
182 resample_fn_type resample;
183 sample_output_fn_type output_samples;
184 /* These will be NULL for the voice codec and is more economical that
185 way */
186 channels_process_dsp_fn_type apply_gain;
187 channels_process_fn_type apply_crossfeed;
188 channels_process_fn_type eq_process;
189 channels_process_fn_type channels_process;
190 return_fn_type limiter_process;
193 /* General DSP config */
194 static struct dsp_config dsp_conf[2] IBSS_ATTR; /* 0=A, 1=V */
195 /* Dithering */
196 static struct dither_data dither_data[2] IBSS_ATTR; /* 0=left, 1=right */
197 static long dither_mask IBSS_ATTR;
198 static long dither_bias IBSS_ATTR;
199 /* Crossfeed */
200 struct crossfeed_data crossfeed_data IDATA_ATTR = /* A */
202 .index = (int32_t *)crossfeed_data.delay
205 /* Equalizer */
206 static struct eq_state eq_data; /* A */
208 /* Software tone controls */
209 #ifdef HAVE_SW_TONE_CONTROLS
210 static int prescale; /* A/V */
211 static int bass; /* A/V */
212 static int treble; /* A/V */
213 #endif
215 /* Settings applicable to audio codec only */
216 static int32_t pitch_ratio = PITCH_SPEED_100;
217 static int channels_mode;
218 long dsp_sw_gain;
219 long dsp_sw_cross;
220 static bool dither_enabled;
221 static long eq_precut;
222 static long track_gain;
223 static bool new_gain;
224 static long album_gain;
225 static long track_peak;
226 static long album_peak;
227 static long replaygain;
228 static bool crossfeed_enabled;
230 /* limiter */
231 static int count_adjust;
232 static bool limiter_buffer_active;
233 static bool limiter_buffer_full;
234 static bool limiter_buffer_emptying;
235 static int32_t limiter_buffer[2][LIMITER_BUFFER_SIZE] IBSS_ATTR;
236 static int32_t *start_lim_buf[2] IBSS_ATTR,
237 *end_lim_buf[2] IBSS_ATTR;
238 static uint16_t lim_buf_peak[LIMITER_BUFFER_SIZE] IBSS_ATTR;
239 static uint16_t *start_peak IBSS_ATTR,
240 *end_peak IBSS_ATTR;
241 static uint16_t out_buf_peak[LIMITER_BUFFER_SIZE] IBSS_ATTR;
242 static uint16_t *out_buf_peak_index IBSS_ATTR;
243 static uint16_t release_peak IBSS_ATTR;
244 static int32_t in_samp IBSS_ATTR,
245 samp0 IBSS_ATTR;
247 static void reset_limiter_buffer(struct dsp_config *dsp);
248 static int limiter_buffer_count(bool buf_count);
249 static int limiter_process(int count, int32_t *buf[]);
250 static uint16_t get_peak_value(int32_t sample);
252 /* The clip_steps array essentially stores the results of fp_factor from
253 * 0 to 12 dB, in 48 equal steps, in S3.28 format. */
254 const long clip_steps[49] ICONST_ATTR = { 0x10000000,
255 0x10779AFA, 0x10F2B409, 0x1171654C, 0x11F3C9A0, 0x1279FCAD,
256 0x13041AE9, 0x139241A2, 0x14248EF9, 0x14BB21F9, 0x15561A92,
257 0x15F599A0, 0x1699C0F9, 0x1742B36B, 0x17F094CE, 0x18A38A01,
258 0x195BB8F9, 0x1A1948C5, 0x1ADC619B, 0x1BA52CDC, 0x1C73D51D,
259 0x1D488632, 0x1E236D3A, 0x1F04B8A1, 0x1FEC982C, 0x20DB3D0E,
260 0x21D0D9E2, 0x22CDA2BE, 0x23D1CD41, 0x24DD9099, 0x25F12590,
261 0x270CC693, 0x2830AFD3, 0x295D1F37, 0x2A925471, 0x2BD0911F,
262 0x2D1818B3, 0x2E6930AD, 0x2FC42095, 0x312931EC, 0x3298B072,
263 0x3412EA24, 0x35982F3A, 0x3728D22E, 0x38C52808, 0x3A6D8847,
264 0x3C224CD9, 0x3DE3D264, 0x3FB2783F};
265 /* The gain_steps array essentially stores the results of fp_factor from
266 * 0 to -12 dB, in 48 equal steps, in S3.28 format. */
267 const long gain_steps[49] ICONST_ATTR = { 0x10000000,
268 0xF8BC9C0, 0xF1ADF94, 0xEAD2988, 0xE429058, 0xDDAFD68,
269 0xD765AC1, 0xD149309, 0xCB59186, 0xC594210, 0xBFF9112,
270 0xBA86B88, 0xB53BEF5, 0xB017965, 0xAB18964, 0xA63DDFE,
271 0xA1866BA, 0x9CF1397, 0x987D507, 0x9429BEE, 0x8FF599E,
272 0x8BDFFD3, 0x87E80B0, 0x840CEBE, 0x804DCE8, 0x7CA9E76,
273 0x792070E, 0x75B0AB0, 0x7259DB2, 0x6F1B4BF, 0x6BF44D5,
274 0x68E4342, 0x65EA5A0, 0x63061D6, 0x6036E15, 0x5D7C0D3,
275 0x5AD50CE, 0x5841505, 0x55C04B8, 0x535176A, 0x50F44D9,
276 0x4EA84FE, 0x4C6D00E, 0x4A41E78, 0x48268DF, 0x461A81C,
277 0x441D53E, 0x422E985, 0x404DE62};
279 #define AUDIO_DSP (dsp_conf[CODEC_IDX_AUDIO])
280 #define VOICE_DSP (dsp_conf[CODEC_IDX_VOICE])
282 /* The internal format is 32-bit samples, non-interleaved, stereo. This
283 * format is similar to the raw output from several codecs, so the amount
284 * of copying needed is minimized for that case.
287 #define RESAMPLE_RATIO 4 /* Enough for 11,025 Hz -> 44,100 Hz */
289 static int32_t small_sample_buf[SMALL_SAMPLE_BUF_COUNT] IBSS_ATTR;
290 static int32_t small_resample_buf[SMALL_SAMPLE_BUF_COUNT * RESAMPLE_RATIO] IBSS_ATTR;
292 static int32_t *big_sample_buf = NULL;
293 static int32_t *big_resample_buf = NULL;
294 static int big_sample_buf_count = -1; /* -1=unknown, 0=not available */
296 static int sample_buf_count;
297 static int32_t *sample_buf;
298 static int32_t *resample_buf;
300 #define SAMPLE_BUF_LEFT_CHANNEL 0
301 #define SAMPLE_BUF_RIGHT_CHANNEL (sample_buf_count/2)
302 #define RESAMPLE_BUF_LEFT_CHANNEL 0
303 #define RESAMPLE_BUF_RIGHT_CHANNEL (sample_buf_count/2 * RESAMPLE_RATIO)
306 /* Clip sample to signed 16 bit range */
307 static inline int32_t clip_sample_16(int32_t sample)
309 if ((int16_t)sample != sample)
310 sample = 0x7fff ^ (sample >> 31);
311 return sample;
314 int32_t sound_get_pitch(void)
316 return pitch_ratio;
319 void sound_set_pitch(int32_t percent)
321 pitch_ratio = percent;
322 dsp_configure(&AUDIO_DSP, DSP_SWITCH_FREQUENCY,
323 AUDIO_DSP.codec_frequency);
326 static void tdspeed_setup(struct dsp_config *dspc)
328 /* Assume timestretch will not be used */
329 dspc->tdspeed_active = false;
330 sample_buf = small_sample_buf;
331 resample_buf = small_resample_buf;
332 sample_buf_count = SMALL_SAMPLE_BUF_COUNT;
334 if(!dsp_timestretch_available())
335 return; /* Timestretch not enabled or buffer not allocated */
336 if (dspc->tdspeed_percent == 0)
337 dspc->tdspeed_percent = PITCH_SPEED_100;
338 if (!tdspeed_config(
339 dspc->codec_frequency == 0 ? NATIVE_FREQUENCY : dspc->codec_frequency,
340 dspc->stereo_mode != STEREO_MONO,
341 dspc->tdspeed_percent))
342 return; /* Timestretch not possible or needed with these parameters */
344 /* Timestretch is to be used */
345 dspc->tdspeed_active = true;
346 sample_buf = big_sample_buf;
347 sample_buf_count = big_sample_buf_count;
348 resample_buf = big_resample_buf;
351 void dsp_timestretch_enable(bool enabled)
353 /* Hook to set up timestretch buffer on first call to settings_apply() */
354 if (big_sample_buf_count < 0) /* Only do something on first call */
356 if (enabled)
358 /* Set up timestretch buffers */
359 big_sample_buf_count = SMALL_SAMPLE_BUF_COUNT * RESAMPLE_RATIO;
360 big_sample_buf = small_resample_buf;
361 big_resample_buf = (int32_t *) buffer_alloc(big_sample_buf_count * RESAMPLE_RATIO * sizeof(int32_t));
363 else
365 /* Not enabled at startup, "big" buffers will never be available */
366 big_sample_buf_count = 0;
368 tdspeed_setup(&AUDIO_DSP);
372 void dsp_set_timestretch(int32_t percent)
374 AUDIO_DSP.tdspeed_percent = percent;
375 tdspeed_setup(&AUDIO_DSP);
378 int32_t dsp_get_timestretch()
380 return AUDIO_DSP.tdspeed_percent;
383 bool dsp_timestretch_available()
385 return (global_settings.timestretch_enabled && big_sample_buf_count > 0);
388 /* Convert count samples to the internal format, if needed. Updates src
389 * to point past the samples "consumed" and dst is set to point to the
390 * samples to consume. Note that for mono, dst[0] equals dst[1], as there
391 * is no point in processing the same data twice.
394 /* convert count 16-bit mono to 32-bit mono */
395 static void sample_input_lte_native_mono(
396 int count, const char *src[], int32_t *dst[])
398 const int16_t *s = (int16_t *) src[0];
399 const int16_t * const send = s + count;
400 int32_t *d = dst[0] = dst[1] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
401 int scale = WORD_SHIFT;
403 while (s < send)
405 *d++ = *s++ << scale;
408 src[0] = (char *)s;
411 /* convert count 16-bit interleaved stereo to 32-bit noninterleaved */
412 static void sample_input_lte_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 + 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];
419 int scale = WORD_SHIFT;
421 while (s < send)
423 int32_t slr = *s++;
424 #ifdef ROCKBOX_LITTLE_ENDIAN
425 *dl++ = (slr >> 16) << scale;
426 *dr++ = (int32_t)(int16_t)slr << scale;
427 #else /* ROCKBOX_BIG_ENDIAN */
428 *dl++ = (int32_t)(int16_t)slr << scale;
429 *dr++ = (slr >> 16) << scale;
430 #endif
433 src[0] = (char *)s;
436 /* convert count 16-bit noninterleaved stereo to 32-bit noninterleaved */
437 static void sample_input_lte_native_ni_stereo(
438 int count, const char *src[], int32_t *dst[])
440 const int16_t *sl = (int16_t *) src[0];
441 const int16_t *sr = (int16_t *) src[1];
442 const int16_t * const slend = sl + count;
443 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
444 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
445 int scale = WORD_SHIFT;
447 while (sl < slend)
449 *dl++ = *sl++ << scale;
450 *dr++ = *sr++ << scale;
453 src[0] = (char *)sl;
454 src[1] = (char *)sr;
457 /* convert count 32-bit mono to 32-bit mono */
458 static void sample_input_gt_native_mono(
459 int count, const char *src[], int32_t *dst[])
461 dst[0] = dst[1] = (int32_t *)src[0];
462 src[0] = (char *)(dst[0] + count);
465 /* convert count 32-bit interleaved stereo to 32-bit noninterleaved stereo */
466 static void sample_input_gt_native_i_stereo(
467 int count, const char *src[], int32_t *dst[])
469 const int32_t *s = (int32_t *)src[0];
470 const int32_t * const send = s + 2*count;
471 int32_t *dl = dst[0] = &sample_buf[SAMPLE_BUF_LEFT_CHANNEL];
472 int32_t *dr = dst[1] = &sample_buf[SAMPLE_BUF_RIGHT_CHANNEL];
474 while (s < send)
476 *dl++ = *s++;
477 *dr++ = *s++;
480 src[0] = (char *)send;
483 /* convert 32 bit-noninterleaved stereo to 32-bit noninterleaved stereo */
484 static void sample_input_gt_native_ni_stereo(
485 int count, const char *src[], int32_t *dst[])
487 dst[0] = (int32_t *)src[0];
488 dst[1] = (int32_t *)src[1];
489 src[0] = (char *)(dst[0] + count);
490 src[1] = (char *)(dst[1] + count);
494 * sample_input_new_format()
496 * set the to-native sample conversion function based on dsp sample parameters
498 * !DSPPARAMSYNC
499 * needs syncing with changes to the following dsp parameters:
500 * * dsp->stereo_mode (A/V)
501 * * dsp->sample_depth (A/V)
503 static void sample_input_new_format(struct dsp_config *dsp)
505 static const sample_input_fn_type sample_input_functions[] =
507 [SAMPLE_INPUT_LE_NATIVE_I_STEREO] = sample_input_lte_native_i_stereo,
508 [SAMPLE_INPUT_LE_NATIVE_NI_STEREO] = sample_input_lte_native_ni_stereo,
509 [SAMPLE_INPUT_LE_NATIVE_MONO] = sample_input_lte_native_mono,
510 [SAMPLE_INPUT_GT_NATIVE_I_STEREO] = sample_input_gt_native_i_stereo,
511 [SAMPLE_INPUT_GT_NATIVE_NI_STEREO] = sample_input_gt_native_ni_stereo,
512 [SAMPLE_INPUT_GT_NATIVE_MONO] = sample_input_gt_native_mono,
515 int convert = dsp->stereo_mode;
517 if (dsp->sample_depth > NATIVE_DEPTH)
518 convert += SAMPLE_INPUT_GT_NATIVE_1ST_INDEX;
520 dsp->input_samples = sample_input_functions[convert];
524 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO
525 /* write mono internal format to output format */
526 static void sample_output_mono(int count, struct dsp_data *data,
527 const int32_t *src[], int16_t *dst)
529 const int32_t *s0 = src[0];
530 const int scale = data->output_scale;
531 const int dc_bias = 1 << (scale - 1);
533 while (count-- > 0)
535 int32_t lr = clip_sample_16((*s0++ + dc_bias) >> scale);
536 *dst++ = lr;
537 *dst++ = lr;
540 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_MONO */
542 /* write stereo internal format to output format */
543 #ifndef DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO
544 static void sample_output_stereo(int count, struct dsp_data *data,
545 const int32_t *src[], int16_t *dst)
547 const int32_t *s0 = src[0];
548 const int32_t *s1 = src[1];
549 const int scale = data->output_scale;
550 const int dc_bias = 1 << (scale - 1);
552 while (count-- > 0)
554 *dst++ = clip_sample_16((*s0++ + dc_bias) >> scale);
555 *dst++ = clip_sample_16((*s1++ + dc_bias) >> scale);
558 #endif /* DSP_HAVE_ASM_SAMPLE_OUTPUT_STEREO */
561 * The "dither" code to convert the 24-bit samples produced by libmad was
562 * taken from the coolplayer project - coolplayer.sourceforge.net
564 * This function handles mono and stereo outputs.
566 static void sample_output_dithered(int count, struct dsp_data *data,
567 const int32_t *src[], int16_t *dst)
569 const int32_t mask = dither_mask;
570 const int32_t bias = dither_bias;
571 const int scale = data->output_scale;
572 const int32_t min = data->clip_min;
573 const int32_t max = data->clip_max;
574 const int32_t range = max - min;
575 int ch;
576 int16_t *d;
578 for (ch = 0; ch < data->num_channels; ch++)
580 struct dither_data * const dither = &dither_data[ch];
581 const int32_t *s = src[ch];
582 int i;
584 for (i = 0, d = &dst[ch]; i < count; i++, s++, d += 2)
586 int32_t output, sample;
587 int32_t random;
589 /* Noise shape and bias (for correct rounding later) */
590 sample = *s;
591 sample += dither->error[0] - dither->error[1] + dither->error[2];
592 dither->error[2] = dither->error[1];
593 dither->error[1] = dither->error[0]/2;
595 output = sample + bias;
597 /* Dither, highpass triangle PDF */
598 random = dither->random*0x0019660dL + 0x3c6ef35fL;
599 output += (random & mask) - (dither->random & mask);
600 dither->random = random;
602 /* Round sample to output range */
603 output &= ~mask;
605 /* Error feedback */
606 dither->error[0] = sample - output;
608 /* Clip */
609 if ((uint32_t)(output - min) > (uint32_t)range)
611 int32_t c = min;
612 if (output > min)
613 c += range;
614 output = c;
617 /* Quantize and store */
618 *d = output >> scale;
622 if (data->num_channels == 2)
623 return;
625 /* Have to duplicate left samples into the right channel since
626 pcm buffer and hardware is interleaved stereo */
627 d = &dst[0];
629 while (count-- > 0)
631 int16_t s = *d++;
632 *d++ = s;
637 * sample_output_new_format()
639 * set the from-native to ouput sample conversion routine
641 * !DSPPARAMSYNC
642 * needs syncing with changes to the following dsp parameters:
643 * * dsp->stereo_mode (A/V)
644 * * dither_enabled (A)
646 static void sample_output_new_format(struct dsp_config *dsp)
648 static const sample_output_fn_type sample_output_functions[] =
650 sample_output_mono,
651 sample_output_stereo,
652 sample_output_dithered,
653 sample_output_dithered
656 int out = dsp->data.num_channels - 1;
658 if (dsp == &AUDIO_DSP && dither_enabled)
659 out += 2;
661 dsp->output_samples = sample_output_functions[out];
665 * Linear interpolation resampling that introduces a one sample delay because
666 * of our inability to look into the future at the end of a frame.
668 #ifndef DSP_HAVE_ASM_RESAMPLING
669 static int dsp_downsample(int count, struct dsp_data *data,
670 const int32_t *src[], int32_t *dst[])
672 int ch = data->num_channels - 1;
673 uint32_t delta = data->resample_data.delta;
674 uint32_t phase, pos;
675 int32_t *d;
677 /* Rolled channel loop actually showed slightly faster. */
680 /* Just initialize things and not worry too much about the relatively
681 * uncommon case of not being able to spit out a sample for the frame.
683 const int32_t *s = src[ch];
684 int32_t last = data->resample_data.last_sample[ch];
686 data->resample_data.last_sample[ch] = s[count - 1];
687 d = dst[ch];
688 phase = data->resample_data.phase;
689 pos = phase >> 16;
691 /* Do we need last sample of previous frame for interpolation? */
692 if (pos > 0)
693 last = s[pos - 1];
695 while (pos < (uint32_t)count)
697 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last);
698 phase += delta;
699 pos = phase >> 16;
700 last = s[pos - 1];
703 while (--ch >= 0);
705 /* Wrap phase accumulator back to start of next frame. */
706 data->resample_data.phase = phase - (count << 16);
707 return d - dst[0];
710 static int dsp_upsample(int count, struct dsp_data *data,
711 const int32_t *src[], int32_t *dst[])
713 int ch = data->num_channels - 1;
714 uint32_t delta = data->resample_data.delta;
715 uint32_t phase, pos;
716 int32_t *d;
718 /* Rolled channel loop actually showed slightly faster. */
721 /* Should always be able to output a sample for a ratio up to RESAMPLE_RATIO */
722 const int32_t *s = src[ch];
723 int32_t last = data->resample_data.last_sample[ch];
725 data->resample_data.last_sample[ch] = s[count - 1];
726 d = dst[ch];
727 phase = data->resample_data.phase;
728 pos = phase >> 16;
730 while (pos == 0)
732 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[0] - last);
733 phase += delta;
734 pos = phase >> 16;
737 while (pos < (uint32_t)count)
739 last = s[pos - 1];
740 *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last);
741 phase += delta;
742 pos = phase >> 16;
745 while (--ch >= 0);
747 /* Wrap phase accumulator back to start of next frame. */
748 data->resample_data.phase = phase & 0xffff;
749 return d - dst[0];
751 #endif /* DSP_HAVE_ASM_RESAMPLING */
753 static void resampler_new_delta(struct dsp_config *dsp)
755 dsp->data.resample_data.delta = (unsigned long)
756 dsp->frequency * 65536LL / NATIVE_FREQUENCY;
758 if (dsp->frequency == NATIVE_FREQUENCY)
760 /* NOTE: If fully glitch-free transistions from no resampling to
761 resampling are desired, last_sample history should be maintained
762 even when not resampling. */
763 dsp->resample = NULL;
764 dsp->data.resample_data.phase = 0;
765 dsp->data.resample_data.last_sample[0] = 0;
766 dsp->data.resample_data.last_sample[1] = 0;
768 else if (dsp->frequency < NATIVE_FREQUENCY)
769 dsp->resample = dsp_upsample;
770 else
771 dsp->resample = dsp_downsample;
774 /* Resample count stereo samples. Updates the src array, if resampling is
775 * done, to refer to the resampled data. Returns number of stereo samples
776 * for further processing.
778 static inline int resample(struct dsp_config *dsp, int count, int32_t *src[])
780 int32_t *dst[2] =
782 &resample_buf[RESAMPLE_BUF_LEFT_CHANNEL],
783 &resample_buf[RESAMPLE_BUF_RIGHT_CHANNEL],
786 count = dsp->resample(count, &dsp->data, (const int32_t **)src, dst);
788 src[0] = dst[0];
789 src[1] = dst[dsp->data.num_channels - 1];
791 return count;
794 static void dither_init(struct dsp_config *dsp)
796 memset(dither_data, 0, sizeof (dither_data));
797 dither_bias = (1L << (dsp->frac_bits - NATIVE_DEPTH));
798 dither_mask = (1L << (dsp->frac_bits + 1 - NATIVE_DEPTH)) - 1;
801 void dsp_dither_enable(bool enable)
803 struct dsp_config *dsp = &AUDIO_DSP;
804 dither_enabled = enable;
805 sample_output_new_format(dsp);
808 /* Applies crossfeed to the stereo signal in src.
809 * Crossfeed is a process where listening over speakers is simulated. This
810 * is good for old hard panned stereo records, which might be quite fatiguing
811 * to listen to on headphones with no crossfeed.
813 #ifndef DSP_HAVE_ASM_CROSSFEED
814 static void apply_crossfeed(int count, int32_t *buf[])
816 int32_t *hist_l = &crossfeed_data.history[0];
817 int32_t *hist_r = &crossfeed_data.history[2];
818 int32_t *delay = &crossfeed_data.delay[0][0];
819 int32_t *coefs = &crossfeed_data.coefs[0];
820 int32_t gain = crossfeed_data.gain;
821 int32_t *di = crossfeed_data.index;
823 int32_t acc;
824 int32_t left, right;
825 int i;
827 for (i = 0; i < count; i++)
829 left = buf[0][i];
830 right = buf[1][i];
832 /* Filter delayed sample from left speaker */
833 acc = FRACMUL(*di, coefs[0]);
834 acc += FRACMUL(hist_l[0], coefs[1]);
835 acc += FRACMUL(hist_l[1], coefs[2]);
836 /* Save filter history for left speaker */
837 hist_l[1] = acc;
838 hist_l[0] = *di;
839 *di++ = left;
840 /* Filter delayed sample from right speaker */
841 acc = FRACMUL(*di, coefs[0]);
842 acc += FRACMUL(hist_r[0], coefs[1]);
843 acc += FRACMUL(hist_r[1], coefs[2]);
844 /* Save filter history for right speaker */
845 hist_r[1] = acc;
846 hist_r[0] = *di;
847 *di++ = right;
848 /* Now add the attenuated direct sound and write to outputs */
849 buf[0][i] = FRACMUL(left, gain) + hist_r[1];
850 buf[1][i] = FRACMUL(right, gain) + hist_l[1];
852 /* Wrap delay line index if bigger than delay line size */
853 if (di >= delay + 13*2)
854 di = delay;
856 /* Write back local copies of data we've modified */
857 crossfeed_data.index = di;
859 #endif /* DSP_HAVE_ASM_CROSSFEED */
862 * dsp_set_crossfeed(bool enable)
864 * !DSPPARAMSYNC
865 * needs syncing with changes to the following dsp parameters:
866 * * dsp->stereo_mode (A)
868 void dsp_set_crossfeed(bool enable)
870 crossfeed_enabled = enable;
871 AUDIO_DSP.apply_crossfeed = (enable && AUDIO_DSP.data.num_channels > 1)
872 ? apply_crossfeed : NULL;
875 void dsp_set_crossfeed_direct_gain(int gain)
877 crossfeed_data.gain = get_replaygain_int(gain * 10) << 7;
878 /* If gain is negative, the calculation overflowed and we need to clamp */
879 if (crossfeed_data.gain < 0)
880 crossfeed_data.gain = 0x7fffffff;
883 /* Both gains should be below 0 dB */
884 void dsp_set_crossfeed_cross_params(long lf_gain, long hf_gain, long cutoff)
886 int32_t *c = crossfeed_data.coefs;
887 long scaler = get_replaygain_int(lf_gain * 10) << 7;
889 cutoff = 0xffffffff/NATIVE_FREQUENCY*cutoff;
890 hf_gain -= lf_gain;
891 /* Divide cutoff by sqrt(10^(hf_gain/20)) to place cutoff at the -3 dB
892 * point instead of shelf midpoint. This is for compatibility with the old
893 * crossfeed shelf filter and should be removed if crossfeed settings are
894 * ever made incompatible for any other good reason.
896 cutoff = fp_div(cutoff, get_replaygain_int(hf_gain*5), 24);
897 filter_shelf_coefs(cutoff, hf_gain, false, c);
898 /* Scale coefs by LF gain and shift them to s0.31 format. We have no gains
899 * over 1 and can do this safely
901 c[0] = FRACMUL_SHL(c[0], scaler, 4);
902 c[1] = FRACMUL_SHL(c[1], scaler, 4);
903 c[2] <<= 4;
906 /* Apply a constant gain to the samples (e.g., for ReplayGain).
907 * Note that this must be called before the resampler.
909 #ifndef DSP_HAVE_ASM_APPLY_GAIN
910 static void dsp_apply_gain(int count, struct dsp_data *data, int32_t *buf[])
912 const int32_t gain = data->gain;
913 int ch;
915 for (ch = 0; ch < data->num_channels; ch++)
917 int32_t *d = buf[ch];
918 int i;
920 for (i = 0; i < count; i++)
921 d[i] = FRACMUL_SHL(d[i], gain, 8);
924 #endif /* DSP_HAVE_ASM_APPLY_GAIN */
926 /* Combine all gains to a global gain. */
927 static void set_gain(struct dsp_config *dsp)
929 /* gains are in S7.24 format */
930 dsp->data.gain = DEFAULT_GAIN;
932 /* Replay gain not relevant to voice */
933 if (dsp == &AUDIO_DSP && replaygain)
935 dsp->data.gain = replaygain;
938 if (dsp->eq_process && eq_precut)
940 dsp->data.gain = fp_mul(dsp->data.gain, eq_precut, 24);
943 /* only preamp for the limiter if limiter is active and sample depth
944 * allows safe pre-amping (12 dB is OK with 29 or less frac bits) */
945 if ((dsp->limiter_preamp) && (dsp->frac_bits <= 29))
947 dsp->data.gain = fp_mul(dsp->data.gain, dsp->limiter_preamp, 24);
950 #ifdef HAVE_SW_VOLUME_CONTROL
951 if (global_settings.volume < SW_VOLUME_MAX ||
952 global_settings.volume > SW_VOLUME_MIN)
954 int vol_gain = get_replaygain_int(global_settings.volume * 100);
955 dsp->data.gain = (long) (((int64_t) dsp->data.gain * vol_gain) >> 24);
957 #endif
959 if (dsp->data.gain == DEFAULT_GAIN)
961 dsp->data.gain = 0;
963 else
965 dsp->data.gain >>= 1; /* convert gain to S8.23 format */
968 dsp->apply_gain = dsp->data.gain != 0 ? dsp_apply_gain : NULL;
972 * Update the amount to cut the audio before applying the equalizer.
974 * @param precut to apply in decibels (multiplied by 10)
976 void dsp_set_eq_precut(int precut)
978 eq_precut = get_replaygain_int(precut * -10);
979 set_gain(&AUDIO_DSP);
983 * Synchronize the equalizer filter coefficients with the global settings.
985 * @param band the equalizer band to synchronize
987 void dsp_set_eq_coefs(int band)
989 const int *setting;
990 long gain;
991 unsigned long cutoff, q;
993 /* Adjust setting pointer to the band we actually want to change */
994 setting = &global_settings.eq_band0_cutoff + (band * 3);
996 /* Convert user settings to format required by coef generator functions */
997 cutoff = 0xffffffff / NATIVE_FREQUENCY * (*setting++);
998 q = *setting++;
999 gain = *setting++;
1001 if (q == 0)
1002 q = 1;
1004 /* NOTE: The coef functions assume the EMAC unit is in fractional mode,
1005 which it should be, since we're executed from the main thread. */
1007 /* Assume a band is disabled if the gain is zero */
1008 if (gain == 0)
1010 eq_data.enabled[band] = 0;
1012 else
1014 if (band == 0)
1015 eq_ls_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
1016 else if (band == 4)
1017 eq_hs_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
1018 else
1019 eq_pk_coefs(cutoff, q, gain, eq_data.filters[band].coefs);
1021 eq_data.enabled[band] = 1;
1025 /* Apply EQ filters to those bands that have got it switched on. */
1026 static void eq_process(int count, int32_t *buf[])
1028 static const int shifts[] =
1030 EQ_SHELF_SHIFT, /* low shelf */
1031 EQ_PEAK_SHIFT, /* peaking */
1032 EQ_PEAK_SHIFT, /* peaking */
1033 EQ_PEAK_SHIFT, /* peaking */
1034 EQ_SHELF_SHIFT, /* high shelf */
1036 unsigned int channels = AUDIO_DSP.data.num_channels;
1037 int i;
1039 /* filter configuration currently is 1 low shelf filter, 3 band peaking
1040 filters and 1 high shelf filter, in that order. we need to know this
1041 so we can choose the correct shift factor.
1043 for (i = 0; i < 5; i++)
1045 if (!eq_data.enabled[i])
1046 continue;
1047 eq_filter(buf, &eq_data.filters[i], count, channels, shifts[i]);
1052 * Use to enable the equalizer.
1054 * @param enable true to enable the equalizer
1056 void dsp_set_eq(bool enable)
1058 AUDIO_DSP.eq_process = enable ? eq_process : NULL;
1059 set_gain(&AUDIO_DSP);
1062 static void dsp_set_stereo_width(int value)
1064 long width, straight, cross;
1066 width = value * 0x7fffff / 100;
1068 if (value <= 100)
1070 straight = (0x7fffff + width) / 2;
1071 cross = straight - width;
1073 else
1075 /* straight = (1 + width) / (2 * width) */
1076 straight = ((int64_t)(0x7fffff + width) << 22) / width;
1077 cross = straight - 0x7fffff;
1080 dsp_sw_gain = straight << 8;
1081 dsp_sw_cross = cross << 8;
1085 * Implements the different channel configurations and stereo width.
1088 /* SOUND_CHAN_STEREO mode is a noop so has no function - just outline one for
1089 * completeness. */
1090 #if 0
1091 static void channels_process_sound_chan_stereo(int count, int32_t *buf[])
1093 /* The channels are each just themselves */
1094 (void)count; (void)buf;
1096 #endif
1098 #ifndef DSP_HAVE_ASM_SOUND_CHAN_MONO
1099 static void channels_process_sound_chan_mono(int count, int32_t *buf[])
1101 int32_t *sl = buf[0], *sr = buf[1];
1103 while (count-- > 0)
1105 int32_t lr = *sl/2 + *sr/2;
1106 *sl++ = lr;
1107 *sr++ = lr;
1110 #endif /* DSP_HAVE_ASM_SOUND_CHAN_MONO */
1112 #ifndef DSP_HAVE_ASM_SOUND_CHAN_CUSTOM
1113 static void channels_process_sound_chan_custom(int count, int32_t *buf[])
1115 const int32_t gain = dsp_sw_gain;
1116 const int32_t cross = dsp_sw_cross;
1117 int32_t *sl = buf[0], *sr = buf[1];
1119 while (count-- > 0)
1121 int32_t l = *sl;
1122 int32_t r = *sr;
1123 *sl++ = FRACMUL(l, gain) + FRACMUL(r, cross);
1124 *sr++ = FRACMUL(r, gain) + FRACMUL(l, cross);
1127 #endif /* DSP_HAVE_ASM_SOUND_CHAN_CUSTOM */
1129 static void channels_process_sound_chan_mono_left(int count, int32_t *buf[])
1131 /* Just copy over the other channel */
1132 memcpy(buf[1], buf[0], count * sizeof (*buf));
1135 static void channels_process_sound_chan_mono_right(int count, int32_t *buf[])
1137 /* Just copy over the other channel */
1138 memcpy(buf[0], buf[1], count * sizeof (*buf));
1141 #ifndef DSP_HAVE_ASM_SOUND_CHAN_KARAOKE
1142 static void channels_process_sound_chan_karaoke(int count, int32_t *buf[])
1144 int32_t *sl = buf[0], *sr = buf[1];
1146 while (count-- > 0)
1148 int32_t ch = *sl/2 - *sr/2;
1149 *sl++ = ch;
1150 *sr++ = -ch;
1153 #endif /* DSP_HAVE_ASM_SOUND_CHAN_KARAOKE */
1155 static void dsp_set_channel_config(int value)
1157 static const channels_process_fn_type channels_process_functions[] =
1159 /* SOUND_CHAN_STEREO = All-purpose index for no channel processing */
1160 [SOUND_CHAN_STEREO] = NULL,
1161 [SOUND_CHAN_MONO] = channels_process_sound_chan_mono,
1162 [SOUND_CHAN_CUSTOM] = channels_process_sound_chan_custom,
1163 [SOUND_CHAN_MONO_LEFT] = channels_process_sound_chan_mono_left,
1164 [SOUND_CHAN_MONO_RIGHT] = channels_process_sound_chan_mono_right,
1165 [SOUND_CHAN_KARAOKE] = channels_process_sound_chan_karaoke,
1168 if ((unsigned)value >= ARRAYLEN(channels_process_functions) ||
1169 AUDIO_DSP.stereo_mode == STEREO_MONO)
1171 value = SOUND_CHAN_STEREO;
1174 /* This doesn't apply to voice */
1175 channels_mode = value;
1176 AUDIO_DSP.channels_process = channels_process_functions[value];
1179 #if CONFIG_CODEC == SWCODEC
1181 #ifdef HAVE_SW_TONE_CONTROLS
1182 static void set_tone_controls(void)
1184 filter_bishelf_coefs(0xffffffff/NATIVE_FREQUENCY*200,
1185 0xffffffff/NATIVE_FREQUENCY*3500,
1186 bass, treble, -prescale,
1187 AUDIO_DSP.tone_filter.coefs);
1188 /* Sync the voice dsp coefficients */
1189 memcpy(&VOICE_DSP.tone_filter.coefs, AUDIO_DSP.tone_filter.coefs,
1190 sizeof (VOICE_DSP.tone_filter.coefs));
1192 #endif
1194 /* Hook back from firmware/ part of audio, which can't/shouldn't call apps/
1195 * code directly.
1197 int dsp_callback(int msg, intptr_t param)
1199 switch (msg)
1201 #ifdef HAVE_SW_TONE_CONTROLS
1202 case DSP_CALLBACK_SET_PRESCALE:
1203 prescale = param;
1204 set_tone_controls();
1205 break;
1206 /* prescaler is always set after calling any of these, so we wait with
1207 * calculating coefs until the above case is hit.
1209 case DSP_CALLBACK_SET_BASS:
1210 bass = param;
1211 break;
1212 case DSP_CALLBACK_SET_TREBLE:
1213 treble = param;
1214 break;
1215 #ifdef HAVE_SW_VOLUME_CONTROL
1216 case DSP_CALLBACK_SET_SW_VOLUME:
1217 set_gain(&AUDIO_DSP);
1218 break;
1219 #endif
1220 #endif
1221 case DSP_CALLBACK_SET_CHANNEL_CONFIG:
1222 dsp_set_channel_config(param);
1223 break;
1224 case DSP_CALLBACK_SET_STEREO_WIDTH:
1225 dsp_set_stereo_width(param);
1226 break;
1227 default:
1228 break;
1230 return 0;
1232 #endif
1234 /* Process and convert src audio to dst based on the DSP configuration,
1235 * reading count number of audio samples. dst is assumed to be large
1236 * enough; use dsp_output_count() to get the required number. src is an
1237 * array of pointers; for mono and interleaved stereo, it contains one
1238 * pointer to the start of the audio data and the other is ignored; for
1239 * non-interleaved stereo, it contains two pointers, one for each audio
1240 * channel. Returns number of bytes written to dst.
1242 int dsp_process(struct dsp_config *dsp, char *dst, const char *src[], int count)
1244 int32_t *tmp[2];
1245 static long last_yield;
1246 long tick;
1247 int written = 0;
1249 #if defined(CPU_COLDFIRE)
1250 /* set emac unit for dsp processing, and save old macsr, we're running in
1251 codec thread context at this point, so can't clobber it */
1252 unsigned long old_macsr = coldfire_get_macsr();
1253 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
1254 #endif
1256 if (new_gain)
1257 dsp_set_replaygain(); /* Gain has changed */
1259 /* Perform at least one yield before starting */
1260 last_yield = current_tick;
1261 yield();
1263 /* Testing function pointers for NULL is preferred since the pointer
1264 will be preloaded to be used for the call if not. */
1265 while (count > 0)
1267 int samples = MIN(sample_buf_count/2, count);
1268 count -= samples;
1270 dsp->input_samples(samples, src, tmp);
1272 if (dsp->tdspeed_active)
1273 samples = tdspeed_doit(tmp, samples);
1275 int chunk_offset = 0;
1276 while (samples > 0)
1278 int32_t *t2[2];
1279 t2[0] = tmp[0]+chunk_offset;
1280 t2[1] = tmp[1]+chunk_offset;
1282 int chunk = MIN(sample_buf_count/2, samples);
1283 chunk_offset += chunk;
1284 samples -= chunk;
1286 if (dsp->apply_gain)
1287 dsp->apply_gain(chunk, &dsp->data, t2);
1289 if (dsp->resample && (chunk = resample(dsp, chunk, t2)) <= 0)
1290 break; /* I'm pretty sure we're downsampling here */
1292 if (dsp->apply_crossfeed)
1293 dsp->apply_crossfeed(chunk, t2);
1295 if (dsp->eq_process)
1296 dsp->eq_process(chunk, t2);
1298 #ifdef HAVE_SW_TONE_CONTROLS
1299 if ((bass | treble) != 0)
1300 eq_filter(t2, &dsp->tone_filter, chunk,
1301 dsp->data.num_channels, FILTER_BISHELF_SHIFT);
1302 #endif
1304 if (dsp->channels_process)
1305 dsp->channels_process(chunk, t2);
1307 if (dsp->limiter_process)
1308 chunk = dsp->limiter_process(chunk, t2);
1310 dsp->output_samples(chunk, &dsp->data, (const int32_t **)t2, (int16_t *)dst);
1312 written += chunk;
1313 dst += chunk * sizeof (int16_t) * 2;
1315 /* yield at least once each tick */
1316 tick = current_tick;
1317 if (TIME_AFTER(tick, last_yield))
1319 last_yield = tick;
1320 yield();
1325 #if defined(CPU_COLDFIRE)
1326 /* set old macsr again */
1327 coldfire_set_macsr(old_macsr);
1328 #endif
1329 return written;
1332 /* Given count number of input samples, calculate the maximum number of
1333 * samples of output data that would be generated (the calculation is not
1334 * entirely exact and rounds upwards to be on the safe side; during
1335 * resampling, the number of samples generated depends on the current state
1336 * of the resampler).
1338 /* dsp_input_size MUST be called afterwards */
1339 int dsp_output_count(struct dsp_config *dsp, int count)
1341 if (dsp->tdspeed_active)
1342 count = tdspeed_est_output_size();
1343 if (dsp->resample)
1345 count = (int)(((unsigned long)count * NATIVE_FREQUENCY
1346 + (dsp->frequency - 1)) / dsp->frequency);
1349 /* Now we have the resampled sample count which must not exceed
1350 * RESAMPLE_BUF_RIGHT_CHANNEL to avoid resample buffer overflow. One
1351 * must call dsp_input_count() to get the correct input sample
1352 * count.
1354 if (count > RESAMPLE_BUF_RIGHT_CHANNEL)
1355 count = RESAMPLE_BUF_RIGHT_CHANNEL;
1357 /* If the limiter buffer is filling, some or all samples will
1358 * be captured by it, so expect fewer samples coming out. */
1359 if (limiter_buffer_active && !limiter_buffer_full)
1361 int empty_space = limiter_buffer_count(false);
1362 count_adjust = MIN(empty_space, count);
1363 count -= count_adjust;
1366 return count;
1369 /* Given count output samples, calculate number of input samples
1370 * that would be consumed in order to fill the output buffer.
1372 int dsp_input_count(struct dsp_config *dsp, int count)
1374 /* If the limiter buffer is filling, the output count was
1375 * adjusted downward. This adjusts it back so that input
1376 * count is not affected.
1378 if (limiter_buffer_active && !limiter_buffer_full)
1379 count += count_adjust;
1381 /* count is now the number of resampled input samples. Convert to
1382 original input samples. */
1383 if (dsp->resample)
1385 /* Use the real resampling delta =
1386 * dsp->frequency * 65536 / NATIVE_FREQUENCY, and
1387 * round towards zero to avoid buffer overflows. */
1388 count = (int)(((unsigned long)count *
1389 dsp->data.resample_data.delta) >> 16);
1392 if (dsp->tdspeed_active)
1393 count = tdspeed_est_input_size(count);
1395 return count;
1398 static void dsp_set_gain_var(long *var, long value)
1400 *var = value;
1401 new_gain = true;
1404 static void dsp_update_functions(struct dsp_config *dsp)
1406 sample_input_new_format(dsp);
1407 sample_output_new_format(dsp);
1408 if (dsp == &AUDIO_DSP)
1409 dsp_set_crossfeed(crossfeed_enabled);
1412 intptr_t dsp_configure(struct dsp_config *dsp, int setting, intptr_t value)
1414 switch (setting)
1416 case DSP_MYDSP:
1417 switch (value)
1419 case CODEC_IDX_AUDIO:
1420 return (intptr_t)&AUDIO_DSP;
1421 case CODEC_IDX_VOICE:
1422 return (intptr_t)&VOICE_DSP;
1423 default:
1424 return (intptr_t)NULL;
1427 case DSP_SET_FREQUENCY:
1428 memset(&dsp->data.resample_data, 0, sizeof (dsp->data.resample_data));
1429 /* Fall through!!! */
1430 case DSP_SWITCH_FREQUENCY:
1431 dsp->codec_frequency = (value == 0) ? NATIVE_FREQUENCY : value;
1432 /* Account for playback speed adjustment when setting dsp->frequency
1433 if we're called from the main audio thread. Voice UI thread should
1434 not need this feature.
1436 if (dsp == &AUDIO_DSP)
1437 dsp->frequency = pitch_ratio * dsp->codec_frequency / PITCH_SPEED_100;
1438 else
1439 dsp->frequency = dsp->codec_frequency;
1441 resampler_new_delta(dsp);
1442 tdspeed_setup(dsp);
1443 break;
1445 case DSP_SET_SAMPLE_DEPTH:
1446 dsp->sample_depth = value;
1448 if (dsp->sample_depth <= NATIVE_DEPTH)
1450 dsp->frac_bits = WORD_FRACBITS;
1451 dsp->sample_bytes = sizeof (int16_t); /* samples are 16 bits */
1452 dsp->data.clip_max = ((1 << WORD_FRACBITS) - 1);
1453 dsp->data.clip_min = -((1 << WORD_FRACBITS));
1455 else
1457 dsp->frac_bits = value;
1458 dsp->sample_bytes = sizeof (int32_t); /* samples are 32 bits */
1459 dsp->data.clip_max = (1 << value) - 1;
1460 dsp->data.clip_min = -(1 << value);
1463 dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
1464 sample_input_new_format(dsp);
1465 dither_init(dsp);
1466 break;
1468 case DSP_SET_STEREO_MODE:
1469 dsp->stereo_mode = value;
1470 dsp->data.num_channels = value == STEREO_MONO ? 1 : 2;
1471 dsp_update_functions(dsp);
1472 tdspeed_setup(dsp);
1473 break;
1475 case DSP_RESET:
1476 dsp->stereo_mode = STEREO_NONINTERLEAVED;
1477 dsp->data.num_channels = 2;
1478 dsp->sample_depth = NATIVE_DEPTH;
1479 dsp->frac_bits = WORD_FRACBITS;
1480 dsp->sample_bytes = sizeof (int16_t);
1481 dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH;
1482 dsp->data.clip_max = ((1 << WORD_FRACBITS) - 1);
1483 dsp->data.clip_min = -((1 << WORD_FRACBITS));
1484 dsp->codec_frequency = dsp->frequency = NATIVE_FREQUENCY;
1486 if (dsp == &AUDIO_DSP)
1488 track_gain = 0;
1489 album_gain = 0;
1490 track_peak = 0;
1491 album_peak = 0;
1492 new_gain = true;
1495 dsp_update_functions(dsp);
1496 resampler_new_delta(dsp);
1497 tdspeed_setup(dsp);
1498 reset_limiter_buffer(dsp);
1499 break;
1501 case DSP_FLUSH:
1502 memset(&dsp->data.resample_data, 0,
1503 sizeof (dsp->data.resample_data));
1504 resampler_new_delta(dsp);
1505 dither_init(dsp);
1506 tdspeed_setup(dsp);
1507 reset_limiter_buffer(dsp);
1508 break;
1510 case DSP_SET_TRACK_GAIN:
1511 if (dsp == &AUDIO_DSP)
1512 dsp_set_gain_var(&track_gain, value);
1513 break;
1515 case DSP_SET_ALBUM_GAIN:
1516 if (dsp == &AUDIO_DSP)
1517 dsp_set_gain_var(&album_gain, value);
1518 break;
1520 case DSP_SET_TRACK_PEAK:
1521 if (dsp == &AUDIO_DSP)
1522 dsp_set_gain_var(&track_peak, value);
1523 break;
1525 case DSP_SET_ALBUM_PEAK:
1526 if (dsp == &AUDIO_DSP)
1527 dsp_set_gain_var(&album_peak, value);
1528 break;
1530 default:
1531 return 0;
1534 return 1;
1537 void dsp_set_replaygain(void)
1539 long gain = 0;
1541 new_gain = false;
1543 if ((global_settings.replaygain_type != REPLAYGAIN_OFF) ||
1544 global_settings.replaygain_noclip)
1546 bool track_mode = get_replaygain_mode(track_gain != 0,
1547 album_gain != 0) == REPLAYGAIN_TRACK;
1548 long peak = (track_mode || !album_peak) ? track_peak : album_peak;
1550 if (global_settings.replaygain_type != REPLAYGAIN_OFF)
1552 gain = (track_mode || !album_gain) ? track_gain : album_gain;
1554 if (global_settings.replaygain_preamp)
1556 long preamp = get_replaygain_int(
1557 global_settings.replaygain_preamp * 10);
1559 gain = (long) (((int64_t) gain * preamp) >> 24);
1563 if (gain == 0)
1565 /* So that noclip can work even with no gain information. */
1566 gain = DEFAULT_GAIN;
1569 if (global_settings.replaygain_noclip && (peak != 0)
1570 && ((((int64_t) gain * peak) >> 24) >= DEFAULT_GAIN))
1572 gain = (((int64_t) DEFAULT_GAIN << 24) / peak);
1575 if (gain == DEFAULT_GAIN)
1577 /* Nothing to do, disable processing. */
1578 gain = 0;
1582 /* Store in S7.24 format to simplify calculations. */
1583 replaygain = gain;
1584 set_gain(&AUDIO_DSP);
1587 /** RESET THE LIMITER BUFFER
1588 * Force the limiter buffer to its initial state and discard
1589 * any samples held there. */
1590 static void reset_limiter_buffer(struct dsp_config *dsp)
1592 if (dsp == &AUDIO_DSP)
1594 int i;
1595 logf(" reset_limiter_buffer");
1596 for (i = 0; i < 2; i++)
1597 start_lim_buf[i] = end_lim_buf[i] = limiter_buffer[i];
1598 start_peak = end_peak = lim_buf_peak;
1599 limiter_buffer_full = false;
1600 limiter_buffer_emptying = false;
1601 release_peak = 0;
1605 /** OPERATE THE LIMITER BUFFER
1606 * Handle all samples entering or exiting the limiter buffer. */
1607 static inline int set_limiter_buffer(int count, int32_t *buf[])
1609 int32_t *in_buf[] = {buf[0], buf[1]},
1610 *out_buf[] = {buf[0], buf[1]};
1611 int empty_space, i, out_count;
1612 const long clip_max = AUDIO_DSP.data.clip_max;
1613 const int ch = AUDIO_DSP.data.num_channels - 1;
1614 out_buf_peak_index = out_buf_peak;
1616 if (limiter_buffer_emptying)
1617 /** EMPTY THE BUFFER
1618 * since the empty flag has been set, assume no inbound samples and
1619 return all samples in the limiter buffer to the outbound buffer */
1621 count = limiter_buffer_count(true);
1622 out_count = count;
1623 logf(" Emptying limiter buffer: %d", count);
1624 while (count-- > 0)
1626 for (i = 0; i <= ch; i++)
1628 /* move samples in limiter buffer to output buffer */
1629 *out_buf[i]++ = *start_lim_buf[i]++;
1630 if (start_lim_buf[i] == &limiter_buffer[i][LIMITER_BUFFER_SIZE])
1631 start_lim_buf[i] = limiter_buffer[i];
1632 /* move limiter buffer peak values to output peak values */
1633 if (i == 0)
1635 *out_buf_peak_index++ = *start_peak++;
1636 if (start_peak == &lim_buf_peak[LIMITER_BUFFER_SIZE])
1637 start_peak = lim_buf_peak;
1641 reset_limiter_buffer(&AUDIO_DSP);
1643 else /* limiter buffer NOT emptying */
1645 if (count <= 0) return 0;
1647 empty_space = limiter_buffer_count(false);
1649 if (empty_space > 0)
1650 /** FILL BUFFER
1651 * use as many inbound samples as necessary to fill the buffer */
1653 /* don't try to fill with more samples than available */
1654 if (empty_space > count)
1655 empty_space = count;
1656 logf(" Filling limiter buffer: %d", empty_space);
1657 while (empty_space-- > 0)
1659 for (i = 0; i <= ch; i++)
1661 /* put inbound samples in the limiter buffer */
1662 in_samp = *in_buf[i]++;
1663 *end_lim_buf[i]++ = in_samp;
1664 if (end_lim_buf[i] == &limiter_buffer[i][LIMITER_BUFFER_SIZE])
1665 end_lim_buf[i] = limiter_buffer[i];
1666 if (in_samp < 0) /* make positive for comparison */
1667 in_samp = -in_samp - 1;
1668 if (in_samp <= clip_max)
1669 in_samp = 0; /* disregard if not clipped */
1670 if (i == 0)
1671 samp0 = in_samp;
1672 if (i == ch)
1674 /* assign peak value for each inbound sample pair */
1675 *end_peak++ = ((samp0 > 0) || (in_samp > 0)) ?
1676 get_peak_value(MAX(samp0, in_samp)) : 0;
1677 if (end_peak == &lim_buf_peak[LIMITER_BUFFER_SIZE])
1678 end_peak = lim_buf_peak;
1681 count--;
1683 /* after buffer fills, the remaining inbound samples are cycled */
1686 limiter_buffer_full = (end_lim_buf[0] == start_lim_buf[0]);
1687 out_count = count;
1689 /** CYCLE BUFFER
1690 * return buffered samples and backfill limiter buffer with new ones.
1691 * The buffer is always full when cycling. */
1692 while (count-- > 0)
1694 for (i = 0; i <= ch; i++)
1696 /* copy incoming sample */
1697 in_samp = *in_buf[i]++;
1698 /* put limiter buffer sample into outbound buffer */
1699 *out_buf[i]++ = *start_lim_buf[i]++;
1700 /* put incoming sample on the end of the limiter buffer */
1701 *end_lim_buf[i]++ = in_samp;
1702 /* ring buffer pointer wrap */
1703 if (start_lim_buf[i] == &limiter_buffer[i][LIMITER_BUFFER_SIZE])
1704 start_lim_buf[i] = limiter_buffer[i];
1705 if (end_lim_buf[i] == &limiter_buffer[i][LIMITER_BUFFER_SIZE])
1706 end_lim_buf[i] = limiter_buffer[i];
1707 if (in_samp < 0) /* make positive for comparison */
1708 in_samp = -in_samp - 1;
1709 if (in_samp <= clip_max)
1710 in_samp = 0; /* disregard if not clipped */
1711 if (i == 0)
1713 samp0 = in_samp;
1714 /* assign outgoing sample its associated peak value */
1715 *out_buf_peak_index++ = *start_peak++;
1716 if (start_peak == &lim_buf_peak[LIMITER_BUFFER_SIZE])
1717 start_peak = lim_buf_peak;
1719 if (i == ch)
1721 /* assign peak value for each inbound sample pair */
1722 *end_peak++ = ((samp0 > 0) || (in_samp > 0)) ?
1723 get_peak_value(MAX(samp0, in_samp)) : 0;
1724 if (end_peak == &lim_buf_peak[LIMITER_BUFFER_SIZE])
1725 end_peak = lim_buf_peak;
1731 return out_count;
1734 /** RETURN LIMITER BUFFER COUNT
1735 * If argument is true, returns number of samples in the buffer,
1736 * otherwise, returns empty space remaining */
1737 static int limiter_buffer_count(bool buf_count)
1739 int count;
1740 if (limiter_buffer_full)
1741 count = LIMITER_BUFFER_SIZE;
1742 else if (end_lim_buf[0] >= start_lim_buf[0])
1743 count = (end_lim_buf[0] - start_lim_buf[0]);
1744 else
1745 count = (end_lim_buf[0] - start_lim_buf[0]) + LIMITER_BUFFER_SIZE;
1746 return buf_count ? count : (LIMITER_BUFFER_SIZE - count);
1749 /** FLUSH THE LIMITER BUFFER
1750 * Empties the limiter buffer into the buffer pointed to by the argument
1751 * and returns the number of samples in that buffer */
1752 int dsp_flush_limiter_buffer(char *dest)
1754 if ((!limiter_buffer_active) || (limiter_buffer_count(true) <= 0))
1755 return 0;
1757 logf(" dsp_flush_limiter_buffer");
1758 int32_t flush_buf[2][LIMITER_BUFFER_SIZE];
1759 int32_t *src[2] = {flush_buf[0], flush_buf[1]};
1761 limiter_buffer_emptying = true;
1762 int count = limiter_process(0, src);
1763 AUDIO_DSP.output_samples(count, &AUDIO_DSP.data,
1764 (const int32_t **)src, (int16_t *)dest);
1765 return count;
1768 /** GET PEAK VALUE
1769 * Return a small value representing how much the sample is clipped. This
1770 * should only be called if a sample is actually clipped. Sample is a
1771 * positive value.
1773 static uint16_t get_peak_value(int32_t sample)
1775 const int frac_bits = AUDIO_DSP.frac_bits;
1776 int mid,
1777 hi = 48,
1778 lo = 0;
1780 /* shift sample into 28 frac bit range for comparison */
1781 if (frac_bits > 28)
1782 sample >>= (frac_bits - 28);
1783 if (frac_bits < 28)
1784 sample <<= (28 - frac_bits);
1786 /* if clipped out of range, return maximum value */
1787 if (sample >= clip_steps[48])
1788 return 48 * 90;
1790 /* find amount of sample clipping on the table */
1793 mid = (hi + lo) / 2;
1794 if (sample < clip_steps[mid])
1795 hi = mid;
1796 else if (sample > clip_steps[mid])
1797 lo = mid;
1798 else
1799 return mid * 90;
1801 while (hi > (lo + 1));
1803 /* interpolate linearly between steps (less accurate but faster) */
1804 return ((hi-1) * 90) + (((sample - clip_steps[hi-1]) * 90) /
1805 (clip_steps[hi] - clip_steps[hi-1]));
1808 /** SET LIMITER
1809 * Called by the menu system to configure the limiter process */
1810 void dsp_set_limiter(int limiter_level)
1812 if (limiter_level > 0)
1814 if (!limiter_buffer_active)
1816 /* enable limiter process */
1817 AUDIO_DSP.limiter_process = limiter_process;
1818 limiter_buffer_active = true;
1820 /* limiter preamp is a gain factor in S7.24 format */
1821 long old_preamp = AUDIO_DSP.limiter_preamp;
1822 long new_preamp = fp_factor((((long)limiter_level << 24) / 10), 24);
1823 if (old_preamp != new_preamp)
1825 AUDIO_DSP.limiter_preamp = new_preamp;
1826 set_gain(&AUDIO_DSP);
1827 logf(" Limiter enable: Yes\tLimiter amp: %.8f",
1828 (float)AUDIO_DSP.limiter_preamp / (1 << 24));
1831 else
1833 /* disable limiter process*/
1834 if (limiter_buffer_active)
1836 AUDIO_DSP.limiter_preamp = (1 << 24);
1837 set_gain(&AUDIO_DSP);
1838 /* pcmbuf_flush_limiter_buffer(); */
1839 limiter_buffer_active = false;
1840 AUDIO_DSP.limiter_process = NULL;
1841 reset_limiter_buffer(&AUDIO_DSP);
1842 logf(" Limiter enable: No\tLimiter amp: %.8f",
1843 (float)AUDIO_DSP.limiter_preamp / (1 << 24));
1848 /** LIMITER PROCESS
1849 * Checks pre-amplified signal for clipped samples and smoothly reduces gain
1850 * around the clipped samples using a preset attack/release schedule.
1852 static int limiter_process(int count, int32_t *buf[])
1854 /* Limiter process passes through if limiter buffer isn't active, or the
1855 * sample depth is too large for safe pre-amping */
1856 if ((!limiter_buffer_active) || (AUDIO_DSP.frac_bits > 29))
1857 return count;
1859 count = set_limiter_buffer(count, buf);
1861 if (count <= 0)
1862 return 0;
1864 const int attack_slope = 15; /* 15:1 ratio between attack and release */
1865 const int buffer_count = limiter_buffer_count(true);
1867 int i, ch;
1868 uint16_t max_peak = 0,
1869 gain_peak,
1870 gain_rem;
1871 long gain;
1873 /* step through limiter buffer in reverse order, in order to find the
1874 * appropriate max_peak for modifying the output buffer */
1875 for (i = buffer_count - 1; i >= 0; i--)
1877 const uint16_t peak_i = lim_buf_peak[(start_peak - lim_buf_peak + i) %
1878 LIMITER_BUFFER_SIZE];
1879 /* if no attack slope, nothing to do */
1880 if ((peak_i == 0) && (max_peak == 0)) continue;
1881 /* if new peak, start attack slope */
1882 if (peak_i >= max_peak)
1884 max_peak = peak_i;
1886 /* keep sloping */
1887 else
1889 if (max_peak > attack_slope)
1890 max_peak -= attack_slope;
1891 else
1892 max_peak = 0;
1895 /* step through output buffer the same way, but this time modifying peak
1896 * values to create a smooth attack slope. */
1897 for (i = count - 1; i >= 0; i--)
1899 /* if no attack slope, nothing to do */
1900 if ((out_buf_peak[i] == 0) && (max_peak == 0)) continue;
1901 /* if new peak, start attack slope */
1902 if (out_buf_peak[i] >= max_peak)
1904 max_peak = out_buf_peak[i];
1906 /* keep sloping */
1907 else
1909 if (max_peak > attack_slope)
1910 max_peak -= attack_slope;
1911 else
1912 max_peak = 0;
1913 out_buf_peak[i] = max_peak;
1916 /* Now step forward through the output buffer, and modify the peak values
1917 * to establish a smooth, slow release slope.*/
1918 for (i = 0; i < count; i++)
1920 /* if no release slope, nothing to do */
1921 if ((out_buf_peak[i] == 0) && (release_peak == 0)) continue;
1922 /* if new peak, start release slope */
1923 if (out_buf_peak[i] >= release_peak)
1925 release_peak = out_buf_peak[i];
1927 /* keep sloping */
1928 else
1930 release_peak--;
1931 out_buf_peak[i] = release_peak;
1934 /* Implement the limiter: adjust gain of the outbound samples by the gain
1935 * amounts in the gain steps array corresponding to the peak values. */
1936 for (ch = 0; ch < AUDIO_DSP.data.num_channels; ch++)
1938 int32_t *d = buf[ch];
1939 for (i = 0; i < count; i++)
1941 if (out_buf_peak[i] > 0)
1943 gain_peak = (out_buf_peak[i] + 1) / 90;
1944 gain_rem = (out_buf_peak[i] + 1) % 90;
1945 gain = gain_steps[gain_peak];
1946 if ((gain_peak < 48) && (gain_rem > 0))
1947 gain -= gain_rem * ((gain_steps[gain_peak] -
1948 gain_steps[gain_peak + 1]) / 90);
1949 d[i] = FRACMUL_SHL(d[i], gain, 3);
1953 return count;