Update Fluidsynth to 2.0.1
[ardour.git] / libs / fluidsynth / src / fluid_phase.h
1 /* FluidSynth - A Software Synthesizer
2  *
3  * Copyright (C) 2003  Peter Hanappe and others.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; either version 2.1 of
8  * the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  */
20
21
22 #ifndef _FLUID_PHASE_H
23 #define _FLUID_PHASE_H
24
25 #include "config.h"
26
27 /*
28  *  phase
29  */
30
31 #define FLUID_INTERP_BITS        8
32 #define FLUID_INTERP_BITS_MASK   0xff000000
33 #define FLUID_INTERP_BITS_SHIFT  24
34 #define FLUID_INTERP_MAX         256
35
36 #define FLUID_FRACT_MAX ((double)4294967296.0)
37
38 /* fluid_phase_t
39 * Purpose:
40 * Playing pointer for voice playback
41 *
42 * When a sample is played back at a different pitch, the playing pointer in the
43 * source sample will not advance exactly one sample per output sample.
44 * This playing pointer is implemented using fluid_phase_t.
45 * It is a 64 bit number. The higher 32 bits contain the 'index' (number of
46 * the current sample), the lower 32 bits the fractional part.
47 */
48 typedef uint64_t fluid_phase_t;
49
50 /* Purpose:
51  * Set a to b.
52  * a: fluid_phase_t
53  * b: fluid_phase_t
54  */
55 #define fluid_phase_set(a,b) a=b;
56
57 #define fluid_phase_set_int(a, b)    ((a) = ((uint64_t)(b)) << 32)
58
59 /* Purpose:
60  * Sets the phase a to a phase increment given in b.
61  * For example, assume b is 0.9. After setting a to it, adding a to
62  * the playing pointer will advance it by 0.9 samples. */
63 #define fluid_phase_set_float(a, b) \
64   (a) = (((uint64_t)(b)) << 32) \
65   | (uint32_t) (((double)(b) - (int)(b)) * (double)FLUID_FRACT_MAX)
66
67 /* create a fluid_phase_t from an index and a fraction value */
68 #define fluid_phase_from_index_fract(index, fract) \
69   ((((uint64_t)(index)) << 32) + (fract))
70
71 /* Purpose:
72  * Return the index and the fractional part, respectively. */
73 #define fluid_phase_index(_x) \
74   ((unsigned int)((_x) >> 32))
75 #define fluid_phase_fract(_x) \
76   ((uint32_t)((_x) & 0xFFFFFFFF))
77
78 /* Get the phase index with fractional rounding */
79 #define fluid_phase_index_round(_x) \
80   ((unsigned int)(((_x) + 0x80000000) >> 32))
81
82
83 /* Purpose:
84  * Takes the fractional part of the argument phase and
85  * calculates the corresponding position in the interpolation table.
86  * The fractional position of the playing pointer is calculated with a quite high
87  * resolution (32 bits). It would be unpractical to keep a set of interpolation
88  * coefficients for each possible fractional part...
89  */
90 #define fluid_phase_fract_to_tablerow(_x) \
91   ((unsigned int)(fluid_phase_fract(_x) & FLUID_INTERP_BITS_MASK) >> FLUID_INTERP_BITS_SHIFT)
92
93 #define fluid_phase_double(_x) \
94   ((double)(fluid_phase_index(_x)) + ((double)fluid_phase_fract(_x) / FLUID_FRACT_MAX))
95
96 /* Purpose:
97  * Advance a by a step of b (both are fluid_phase_t).
98  */
99 #define fluid_phase_incr(a, b)  a += b
100
101 /* Purpose:
102  * Subtract b from a (both are fluid_phase_t).
103  */
104 #define fluid_phase_decr(a, b)  a -= b
105
106 /* Purpose:
107  * Subtract b samples from a.
108  */
109 #define fluid_phase_sub_int(a, b)  ((a) -= (uint64_t)(b) << 32)
110
111 /* Purpose:
112  * Creates the expression a.index++. */
113 #define fluid_phase_index_plusplus(a)  (((a) += 0x100000000LL)
114
115 #endif  /* _FLUID_PHASE_H */