Update Fluidsynth to v2.0.2
[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 /*
26  *  phase
27  */
28
29 #define FLUID_INTERP_BITS        8
30 #define FLUID_INTERP_BITS_MASK   0xff000000
31 #define FLUID_INTERP_BITS_SHIFT  24
32
33
34 #define FLUID_FRACT_MAX ((double)4294967296.0)
35
36 /* fluid_phase_t
37 * Purpose:
38 * Playing pointer for voice playback
39 *
40 * When a sample is played back at a different pitch, the playing pointer in the
41 * source sample will not advance exactly one sample per output sample.
42 * This playing pointer is implemented using fluid_phase_t.
43 * It is a 64 bit number. The higher 32 bits contain the 'index' (number of
44 * the current sample), the lower 32 bits the fractional part.
45 */
46 typedef uint64_t fluid_phase_t;
47
48 /* Purpose:
49  * Set a to b.
50  * a: fluid_phase_t
51  * b: fluid_phase_t
52  */
53 #define fluid_phase_set(a,b) a=b;
54
55 #define fluid_phase_set_int(a, b)    ((a) = ((uint64_t)(b)) << 32)
56
57 /* Purpose:
58  * Sets the phase a to a phase increment given in b.
59  * For example, assume b is 0.9. After setting a to it, adding a to
60  * the playing pointer will advance it by 0.9 samples. */
61 #define fluid_phase_set_float(a, b) \
62   (a) = (((uint64_t)(b)) << 32) \
63   | (uint32_t) (((double)(b) - (int)(b)) * (double)FLUID_FRACT_MAX)
64
65 /* create a fluid_phase_t from an index and a fraction value */
66 #define fluid_phase_from_index_fract(index, fract) \
67   ((((uint64_t)(index)) << 32) + (fract))
68
69 /* Purpose:
70  * Return the index and the fractional part, respectively. */
71 #define fluid_phase_index(_x) \
72   ((unsigned int)((_x) >> 32))
73 #define fluid_phase_fract(_x) \
74   ((uint32_t)((_x) & 0xFFFFFFFF))
75
76 /* Get the phase index with fractional rounding */
77 #define fluid_phase_index_round(_x) \
78   ((unsigned int)(((_x) + 0x80000000) >> 32))
79
80
81 /* Purpose:
82  * Takes the fractional part of the argument phase and
83  * calculates the corresponding position in the interpolation table.
84  * The fractional position of the playing pointer is calculated with a quite high
85  * resolution (32 bits). It would be unpractical to keep a set of interpolation
86  * coefficients for each possible fractional part...
87  */
88 #define fluid_phase_fract_to_tablerow(_x) \
89   ((unsigned int)(fluid_phase_fract(_x) & FLUID_INTERP_BITS_MASK) >> FLUID_INTERP_BITS_SHIFT)
90
91 #define fluid_phase_double(_x) \
92   ((double)(fluid_phase_index(_x)) + ((double)fluid_phase_fract(_x) / FLUID_FRACT_MAX))
93
94 /* Purpose:
95  * Advance a by a step of b (both are fluid_phase_t).
96  */
97 #define fluid_phase_incr(a, b)  a += b
98
99 /* Purpose:
100  * Subtract b from a (both are fluid_phase_t).
101  */
102 #define fluid_phase_decr(a, b)  a -= b
103
104 /* Purpose:
105  * Subtract b samples from a.
106  */
107 #define fluid_phase_sub_int(a, b)  ((a) -= (uint64_t)(b) << 32)
108
109 /* Purpose:
110  * Creates the expression a.index++. */
111 #define fluid_phase_index_plusplus(a)  (((a) += 0x100000000LL)
112
113 #endif  /* _FLUID_PHASE_H */