1 /*(LGPL)
2 ---------------------------------------------------------------------------
3 	a_pitch.h - Simple fixed point linear pitch table.
4 ---------------------------------------------------------------------------
5  * Copyright (C) 2001. 2002, David Olofson
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or (at
10  * your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #ifndef	_PITCHTAB_H_
23 #define	_PITCHTAB_H_
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 extern int *__pitchtab;
30 
31 /*
32  * 'middle_c' is the output value you want with an input of
33  * 60; MIDI standard middle C. The table will start 5 octaves
34  * down from there, and will contain 129 semitone steps. (The
35  * last step is for sub-semitone interpolation.)
36  */
37 int ptab_init(int middle_c);
38 
39 void ptab_close(void);
40 
41 /*
42  * Interpolated linear frequency -> frequency factor lookup.
43  *
44  * 'pith' is a 16:16 fixed point linear frequency value.
45  *
46  * Returns engine pitch as 16:16 fixed point.
47  */
ptab_convert(int pitch)48 static inline int ptab_convert(int pitch)
49 {
50 	int bfrac;
51 	int btone = pitch >> 16;
52 	if(btone < 0)
53 		return __pitchtab[0];
54 	else if(btone > 127)
55 		return __pitchtab[128];
56 	bfrac = (pitch & 0xffff) >> 8;
57 	return (__pitchtab[btone+1] * bfrac +
58 			__pitchtab[btone] * (256-bfrac)) >> 8;
59 }
60 
61 
62 /*
63  * Interpolated linear frequency -> frequency factor lookup,
64  * MIDI style.
65  *
66  * 'pith' is a MIDI pitch code (0..127; 60 <==> middle C,
67  * which maps to the original sample rate of the current
68  * waveform).
69  *
70  * 'bend' is a 16:16 fixed point value in the same unit.
71  * That is, the integer part of bend is added to pitch,
72  * whereas the fraction part is used for fractional tone
73  * interpolation.
74  *
75  * Returns engine pitch as 16:16 fixed point.
76  */
77 #define	ptab_convert_midi(p, b)	ptab_convert((b) + ((p)<<16))
78 #if 0
79 static inline int ptab_convert_midi(int pitch, int bend)
80 {
81 	int bfrac;
82 	int btone = pitch + (bend >> 16);
83 	if(btone < 0)
84 		return __pitchtab[0];
85 	else if(btone > 127)
86 		return __pitchtab[128];
87 	bfrac = (bend & 0xffff) >> 8;
88 	return (__pitchtab[btone+1] * bfrac +
89 			__pitchtab[btone] * (256-bfrac)) >> 8;
90 }
91 #endif
92 
93 /*
94  * Simple semitone resolution lookup - nonchecking.
95  */
96 #define	_ptab_lookup(p)	(__pitchtab[p])
97 
98 /*
99  * ...and with checking.
100  */
101 #define	ptab_lookup(p)	(__pitchtab ? _ptab_lookup(p) : 65536)
102 
103 #ifdef __cplusplus
104 };
105 #endif
106 
107 #endif	/*_PITCHTAB_H_*/
108