Factor out the duplicate code from bufadvance in rebuffer_handle()
[Rockbox.git] / apps / dsp.h
blob4e57adfc12e862292aba573baecb479905494fe1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
20 #ifndef _DSP_H
21 #define _DSP_H
23 #include <stdlib.h>
24 #include <stdbool.h>
26 #define NATIVE_FREQUENCY 44100
27 enum
29 STEREO_INTERLEAVED = 0,
30 STEREO_NONINTERLEAVED,
31 STEREO_MONO,
32 STEREO_NUM_MODES,
35 enum
37 CODEC_SET_FILEBUF_WATERMARK = 1,
38 CODEC_SET_FILEBUF_CHUNKSIZE,
39 CODEC_SET_FILEBUF_PRESEEK,
40 DSP_SWITCH_CODEC,
41 DSP_SET_FREQUENCY,
42 DSP_SWITCH_FREQUENCY,
43 DSP_SET_SAMPLE_DEPTH,
44 DSP_SET_STEREO_MODE,
45 DSP_RESET,
46 DSP_FLUSH,
47 DSP_SET_TRACK_GAIN,
48 DSP_SET_ALBUM_GAIN,
49 DSP_SET_TRACK_PEAK,
50 DSP_SET_ALBUM_PEAK,
51 DSP_CROSSFEED
54 enum {
55 DSP_CALLBACK_SET_PRESCALE = 0,
56 DSP_CALLBACK_SET_BASS,
57 DSP_CALLBACK_SET_TREBLE,
58 DSP_CALLBACK_SET_CHANNEL_CONFIG,
59 DSP_CALLBACK_SET_STEREO_WIDTH
62 /* A bunch of fixed point assembler helper macros */
63 #if defined(CPU_COLDFIRE)
64 /* These macros use the Coldfire EMAC extension and need the MACSR flags set
65 * to fractional mode with no rounding.
68 /* Multiply two S.31 fractional integers and return the sign bit and the
69 * 31 most significant bits of the result.
71 #define FRACMUL(x, y) \
72 ({ \
73 long t; \
74 asm ("mac.l %[a], %[b], %%acc0\n\t" \
75 "movclr.l %%acc0, %[t]\n\t" \
76 : [t] "=r" (t) : [a] "r" (x), [b] "r" (y)); \
77 t; \
80 /* Multiply two S.31 fractional integers, and return the 32 most significant
81 * bits after a shift left by the constant z. NOTE: Only works for shifts of
82 * up to 8 on Coldfire!
84 #define FRACMUL_SHL(x, y, z) \
85 ({ \
86 long t, t2; \
87 asm ("mac.l %[a], %[b], %%acc0\n\t" \
88 "moveq.l %[d], %[t]\n\t" \
89 "move.l %%accext01, %[t2]\n\t" \
90 "and.l %[mask], %[t2]\n\t" \
91 "lsr.l %[t], %[t2]\n\t" \
92 "movclr.l %%acc0, %[t]\n\t" \
93 "asl.l %[c], %[t]\n\t" \
94 "or.l %[t2], %[t]\n\t" \
95 : [t] "=&d" (t), [t2] "=&d" (t2) \
96 : [a] "r" (x), [b] "r" (y), [mask] "d" (0xff), \
97 [c] "i" ((z)), [d] "i" (8 - (z))); \
98 t; \
101 /* Multiply one S.31-bit and one S8.23 fractional integer and return the
102 * sign bit and the 31 most significant bits of the result. Load next value
103 * to multiply with into x from s (and increase s); x must contain the
104 * initial value.
106 #define FRACMUL_8_LOOP(x, y, s, d) \
108 long t, t2; \
109 asm volatile ("mac.l %[a], %[b], (%[src])+, %[a], %%acc0\n\t" \
110 "move.l %%accext01, %[t2]\n\t" \
111 "movclr.l %%acc0, %[t]\n\t" \
112 "asl.l #8, %[t]\n\t" \
113 "move.b %[t2], %[t]\n\t" \
114 "move.l %[t], (%[dst])+\n\t" \
115 : [a] "+r" (x), [src] "+a" (s), [dst] "+a" (d), \
116 [t] "=r" (t), [t2] "=r" (t2) \
117 : [b] "r" (y)); \
120 #define ACC(acc, x, y) \
121 (void)acc; \
122 asm ("mac.l %[a], %[b], %%acc0" \
123 : : [a] "i,r" (x), [b] "i,r" (y));
125 #define GET_ACC(acc) \
126 ({ \
127 long t; \
128 (void)acc; \
129 asm ("movclr.l %%acc0, %[t]" \
130 : [t] "=r" (t)); \
131 t; \
134 #define ACC_INIT(acc, x, y) ACC(acc, x, y)
136 #elif defined(CPU_ARM)
138 /* Multiply two S.31 fractional integers and return the sign bit and the
139 * 31 most significant bits of the result.
141 #define FRACMUL(x, y) \
142 ({ \
143 long t, t2; \
144 asm ("smull %[t], %[t2], %[a], %[b]\n\t" \
145 "mov %[t2], %[t2], asl #1\n\t" \
146 "orr %[t], %[t2], %[t], lsr #31\n\t" \
147 : [t] "=&r" (t), [t2] "=&r" (t2) \
148 : [a] "r" (x), [b] "r" (y)); \
149 t; \
152 /* Multiply two S.31 fractional integers, and return the 32 most significant
153 * bits after a shift left by the constant z.
155 #define FRACMUL_SHL(x, y, z) \
156 ({ \
157 long t, t2; \
158 asm ("smull %[t], %[t2], %[a], %[b]\n\t" \
159 "mov %[t2], %[t2], asl %[c]\n\t" \
160 "orr %[t], %[t2], %[t], lsr %[d]\n\t" \
161 : [t] "=&r" (t), [t2] "=&r" (t2) \
162 : [a] "r" (x), [b] "r" (y), \
163 [c] "M" ((z) + 1), [d] "M" (31 - (z))); \
164 t; \
167 #define ACC_INIT(acc, x, y) acc = FRACMUL(x, y)
168 #define ACC(acc, x, y) acc += FRACMUL(x, y)
169 #define GET_ACC(acc) acc
171 /* Multiply one S.31-bit and one S8.23 fractional integer and store the
172 * sign bit and the 31 most significant bits of the result to d (and
173 * increase d). Load next value to multiply with into x from s (and
174 * increase s); x must contain the initial value.
176 #define FRACMUL_8_LOOP(x, y, s, d) \
177 ({ \
178 long t, t2; \
179 asm volatile ("smull %[t], %[t2], %[a], %[b]\n\t" \
180 "mov %[t2], %[t2], asl #9\n\t" \
181 "orr %[d], %[t2], %[t], lsr #23\n\t" \
182 : [d] "=&r" (*(d)++), [t] "=&r" (t), [t2] "=&r" (t2) \
183 : [a] "r" (x), [b] "r" (y)); \
184 x = *(s)++; \
187 #else
189 #define ACC_INIT(acc, x, y) acc = FRACMUL(x, y)
190 #define ACC(acc, x, y) acc += FRACMUL(x, y)
191 #define GET_ACC(acc) acc
192 #define FRACMUL(x, y) (long) (((((long long) (x)) * ((long long) (y))) >> 31))
193 #define FRACMUL_SHL(x, y, z) \
194 ((long)(((((long long) (x)) * ((long long) (y))) >> (31 - (z)))))
195 #define FRACMUL_8_LOOP(x, y, s, d) \
196 ({ \
197 long t = x; \
198 x = *(s)++; \
199 *(d)++ = (long) (((((long long) (t)) * ((long long) (y))) >> 23)); \
202 #endif
204 #define DIV64(x, y, z) (long)(((long long)(x) << (z))/(y))
206 int dsp_process(char *dest, const char *src[], int count);
207 int dsp_input_count(int count);
208 int dsp_output_count(int count);
209 int dsp_stereo_mode(void);
210 bool dsp_configure(int setting, intptr_t value);
211 void dsp_set_replaygain(void);
212 void dsp_set_crossfeed(bool enable);
213 void dsp_set_crossfeed_direct_gain(int gain);
214 void dsp_set_crossfeed_cross_params(long lf_gain, long hf_gain, long cutoff);
215 void dsp_set_eq(bool enable);
216 void dsp_set_eq_precut(int precut);
217 void dsp_set_eq_coefs(int band);
218 void sound_set_pitch(int r);
219 int sound_get_pitch(void);
220 int dsp_callback(int msg, intptr_t param);
221 void dsp_set_channel_config(int value);
222 void dsp_set_stereo_width(int value);
223 void dsp_dither_enable(bool enable);
225 #endif