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