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 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.
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.
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
22 #ifndef _FLUID_PHASE_H
23 #define _FLUID_PHASE_H
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
38 #define FLUID_FRACT_MAX ((double)4294967296.0)
42 * Playing pointer for voice playback
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.
50 typedef unsigned long long fluid_phase_t;
57 #define fluid_phase_set(a,b) a=b;
59 #define fluid_phase_set_int(a, b) ((a) = ((unsigned long long)(b)) << 32)
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)
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))
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))
80 /* Get the phase index with fractional rounding */
81 #define fluid_phase_index_round(_x) \
82 ((unsigned int)(((_x) + 0x80000000) >> 32))
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...
92 #define fluid_phase_fract_to_tablerow(_x) \
93 ((unsigned int)(fluid_phase_fract(_x) & FLUID_INTERP_BITS_MASK) >> FLUID_INTERP_BITS_SHIFT)
95 #define fluid_phase_double(_x) \
96 ((double)(fluid_phase_index(_x)) + ((double)fluid_phase_fract(_x) / FLUID_FRACT_MAX))
99 * Advance a by a step of b (both are fluid_phase_t).
101 #define fluid_phase_incr(a, b) a += b
104 * Subtract b from a (both are fluid_phase_t).
106 #define fluid_phase_decr(a, b) a -= b
109 * Subtract b samples from a.
111 #define fluid_phase_sub_int(a, b) ((a) -= (unsigned long long)(b) << 32)
114 * Creates the expression a.index++. */
115 #define fluid_phase_index_plusplus(a) (((a) += 0x100000000LL)
117 #endif /* _FLUID_PHASE_H */