1 /* FluidSynth - A Software Synthesizer
3 * Copyright (C) 2003 Peter Hanappe and others.
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.
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.
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
22 #ifndef _FLUID_PHASE_H
23 #define _FLUID_PHASE_H
29 #define FLUID_INTERP_BITS 8
30 #define FLUID_INTERP_BITS_MASK 0xff000000
31 #define FLUID_INTERP_BITS_SHIFT 24
34 #define FLUID_FRACT_MAX ((double)4294967296.0)
38 * Playing pointer for voice playback
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.
46 typedef uint64_t fluid_phase_t;
53 #define fluid_phase_set(a,b) a=b;
55 #define fluid_phase_set_int(a, b) ((a) = ((uint64_t)(b)) << 32)
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)
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))
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))
76 /* Get the phase index with fractional rounding */
77 #define fluid_phase_index_round(_x) \
78 ((unsigned int)(((_x) + 0x80000000) >> 32))
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...
88 #define fluid_phase_fract_to_tablerow(_x) \
89 ((unsigned int)(fluid_phase_fract(_x) & FLUID_INTERP_BITS_MASK) >> FLUID_INTERP_BITS_SHIFT)
91 #define fluid_phase_double(_x) \
92 ((double)(fluid_phase_index(_x)) + ((double)fluid_phase_fract(_x) / FLUID_FRACT_MAX))
95 * Advance a by a step of b (both are fluid_phase_t).
97 #define fluid_phase_incr(a, b) a += b
100 * Subtract b from a (both are fluid_phase_t).
102 #define fluid_phase_decr(a, b) a -= b
105 * Subtract b samples from a.
107 #define fluid_phase_sub_int(a, b) ((a) -= (uint64_t)(b) << 32)
110 * Creates the expression a.index++. */
111 #define fluid_phase_index_plusplus(a) (((a) += 0x100000000LL)
113 #endif /* _FLUID_PHASE_H */