2
* Purpose: GRC library version 3.1 internal definitions
4
* GRC3 is a high quality sample rate conversion module that uses fixed point
9
* This file is part of Open Sound System.
11
* Copyright (C) 4Front Technologies 1996-2008.
13
* This this source file is released under GPL v2 license (no other versions).
14
* See the COPYING file included in the main directory of this source
15
* distribution for the license terms and conditions.
19
#ifndef GRC3_H_INCLUDED
20
#define GRC3_H_INCLUDED
22
#if !defined(CONFIG_OSS_GRC_MIN_QUALITY) || CONFIG_OSS_GRC_MIN_QUALITY > 6
23
#define CONFIG_OSS_GRC_MIN_QUALITY 0
26
#if !defined(CONFIG_OSS_GRC_MAX_QUALITY) || CONFIG_OSS_GRC_MAX_QUALITY < CONFIG_OSS_GRC_MIN_QUALITY
27
#define CONFIG_OSS_GRC_MAX_QUALITY 6
31
#if (CONFIG_OSS_GRC_MIN_QUALITY<=1)&&(CONFIG_OSS_GRC_MAX_QUALITY>=0)
32
#define GRC3_COMPILE_L
35
#if (CONFIG_OSS_GRC_MIN_QUALITY<=2)&&(CONFIG_OSS_GRC_MAX_QUALITY>=2)
36
#define GRC3_COMPILE_M
39
#if (CONFIG_OSS_GRC_MIN_QUALITY<=4)&&(CONFIG_OSS_GRC_MAX_QUALITY>=3)
40
#define GRC3_COMPILE_H
43
#if (CONFIG_OSS_GRC_MIN_QUALITY<=6)&&(CONFIG_OSS_GRC_MAX_QUALITY>=5)
44
#define GRC3_COMPILE_P
47
#if (CONFIG_OSS_GRC_MIN_QUALITY<=3)
48
#if (CONFIG_OSS_GRC_MAX_QUALITY>=3)
49
#define DEFAULT_GRC_QUALITY 3
51
#define DEFAULT_GRC_QUALITY CONFIG_OSS_GRC_MAX_QUALITY
54
#define DEFAULT_GRC_QUALITY CONFIG_OSS_GRC_MIN_QUALITY
58
#define GRCinline inline static
59
#define GRCpreg register
60
#define GRCvreg register
63
#define GRC3_MAXHISTORY 4096
70
typedef struct s_grc3state_t
82
int32_t history[GRC3_MAXHISTORY * 2];
90
/*****************************************************************************
92
Tutorial on how to use GRC3 rate conversion
94
1. First, you create an instance of grc3state_t for each channel. If you
95
are working with stereo files - you will need 2 of such instances,
98
The instances may be allocated in either static or dynamic memory - that
99
makes no difference to the convertor. So, if your program has to process
100
one stereo stream, there's no reason why should you use malloc/free to
101
allocate/deallocate structures. Also, in device drivers, you can
102
use static variables as well:
104
static grc3state_t grc[2]; // for two channels
107
2. Before starting any conversion, grc3state_t instances should be initialized
108
properly, and you do this with grc3_setup function. Function itself does
109
not allocate additional memory or change anything except grc3state_t
110
structure, so this is thread safe, and you don't have to do additional
113
If you are doing interleaved audio (stereo/quadro/whatever) conversion,
114
you should do setup on each of the channels, and should have separate
115
instance of grc3state_t for each channel. As you will understand further,
116
such conversion is done separately. And now, the setup function:
118
int grc3_setup( grc3state_t *grc,
122
grc - pointer to grc3state_t instance
123
fromRate - source sample rate
124
toRate - destination sample rate
126
RETURNS - 1 on success, and 0 if conversion is not supported
128
Note, that sample rates itself are not important - the important thing
129
is ratio between those sample rates. So, for example, if you have to
130
convert from 24000Hz to 48000Hz, it's ok to write:
132
result = grc3_setup( &grc[0], 240, 480 );
134
Sometimes (in MIDI synths) it would be desired to use fractional sample
135
rates. For example, setup for conversion from 33100.78 to 48000 may look
138
result = grc3_setup( &grc[0], 3310078, 4800000);
140
Note, that on stereo, GRC3 setup will look like this:
142
static grc3state_t grc[2];
147
grc3_setup( &grc[0], 3310078, 4800000)
148
&& grc3_setup( &grc[1], 3310078, 4800000);
151
Note, that you should not rely on grc3_setup's fast execution or any
152
execution timing. It may contain some massive arithmetic and even huge
153
loops, so avoid putting grc3_setup to inner loops and calling in
154
latency-dependent code.
157
3. Next, before running a stream through grc3_convert function, you should
158
reset each of grc3state_t instance used:
160
int grc3_reset(grc3state_t *grc);
163
grc - pointer to GRC3 instance variable
165
RETURNS - 1 on success, 0 on failure
167
So, for stereo, this appears to be:
169
static grc3state_t grc[2];
173
grc3_reset( &grc[0] );
174
grc3_reset( &grc[1] );
177
4. Finally, doing conversion is easy:
179
int grc3_convert( grc3state_t *grc,
190
grc - pointer to initialized grc3state_t instance; you
191
can specify NULL to check whether a particular
192
domain/quality pair is supported, check return value
194
domain - number of bits in stream;
195
supported values are 8, 16, 32, -16, -32;
197
minus sign stands for swapped-endian conversion; that
198
will do big-endian conversion on little-endian machines
199
and little-endian conversion on big-endian machines
201
quality - quality to use for conversion, supported values are:
203
0 - D lowest quality (normally equals to low quality)
204
1 - L low quality (spline interpolation)
205
2 - M medium quality (lagrange interpolation)
207
4 - HX high quality (high quality with extra precision)
208
5 - P production quality
210
6 - PX production quality (prod quality with extra precision)
211
(PX is currently disabled because it causes a crash)
213
src - source audio buffer
215
dst - destination audio buffer;
217
maxInSize - size of input buffer (in samples per channel!)
219
maxOutSize - size of output buffer (in samples per channel!)
220
(will never overrun this size)
222
interleave - interleave factor; for MONO or non-interleaved data
223
it should be equal to 1;
225
2 - STEREO interleaved audio
226
4 - QUADRO interleaved audio
228
So, basically, this parameter should be equal to number
229
of interleaved channels
231
offset - number of interleaved channel currently processing,
232
starting from 0; for MONO or non-interleaved data
233
it should be equal to 0
236
RETURNS in case of grc != NULL
238
- actual number if INPUT samples processed,
239
or -1 in case if conversion is not supported;
241
For unsupported quality values, it will fall back to
242
"D" quality (the lowest one)
244
also on return it sets:
246
grc->insz == number of input samples processed
247
grc->outsz == number of output samples
251
RETURNS in case of grc == NULL
253
- will return 0 in case quality/domain pair is supported
255
if specified quality and/or bitdepth is not supported,
256
then function will return -1
258
Note, that if quality is not supported but bitdepth is,
259
calling the function with real data will fall back
260
to the worst quality available.
264
5. Interleaved processing of N channels is done like this:
267
static grc3state_t grc[N];
275
grc3_setup( &grc[t], 22050, 48000 );
277
grc3_reset( &grc[t] );
289
&grc[t], // instance pointer
291
16, 4, // numbits, quality
293
in_buffer, // input buffer
294
out_buffer, // input buffer
296
in_samples_count, // number of samples
298
2048, // size of out_buffer
300
N, t // num of channels, channel#
306
// Normally, for interleaved data, ->outsz of all instances will
307
// be the same for the same stream
309
put_sound_somewhere( out_buffer,
310
grc[0]->outsz * N * sizeof(out_buffer[0]) );
315
6. If you use the same storage and the same setup for processing few separate
316
non-related sounds, to prevent the feedback of sound1's tail to sound2's
317
beginning - do grc3_reset on the state instances before calling
320
******************************************************************************/
323
int grc3_setup (grc3state_t * grc, uint32_t fromRate, uint32_t toRate);
325
int grc3_reset (grc3state_t * grc);
327
int grc3_convert (grc3state_t * grc,
328
int domain, int quality,
329
void *src, void *dst,
330
int sz, int bufsz, int inc, int offset);
333
int32_t _clamp24 (int32_t v);