1 /*
2 ---------------------------------------------------------------------------
3 	a_pitch.c - 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 #include <stdlib.h>
23 #include "a_globals.h"
24 #include "a_pitch.h"
25 
26 /*
27  * Start pitch, based on the assumption that 1.0 (original
28  * sample playback speed) gives you a middle C (MIDI #60).
29  */
30 #define	FIVE_OCTAVES		(1 << 5)
31 
32 /* 1 + twelfth root of two */
33 #define	SEMITONE_MULTIPLIER	1.0594630943592953
34 
35 int *__pitchtab = NULL;
36 
ptab_init(int middle_c)37 int ptab_init(int middle_c)
38 {
39 	int p;
40 	double pitch;
41 	free(__pitchtab);
42 	__pitchtab = malloc(129 * sizeof(int));
43 	if(!__pitchtab)
44 		return -1;
45 
46 	pitch = (double)middle_c / FIVE_OCTAVES;
47 	for(p = 0; p < 129; ++p)
48 	{
49 		__pitchtab[p] = (int)pitch;
50 		pitch *= SEMITONE_MULTIPLIER;
51 	}
52 	return 0;
53 }
54 
ptab_close(void)55 void ptab_close(void)
56 {
57 	free(__pitchtab);
58 	__pitchtab = NULL;
59 }
60 
61