1 /*
2     wdatutil.h - Code to generate wavedata dl containing pre-calculated
3                  wavetables.
4 
5     Copyright (C) 2003  Mike Rawes
6 
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21 
22 #ifndef blop_wdatutil_h
23 #define blop_wdatutil_h
24 
25 #include <stdio.h>
26 #include <ladspa.h>
27 #include "math_func.h"
28 #include "wavedata.h"
29 
30 #define WAVE_TYPE_COUNT         3
31 
32 extern char *wave_names[];
33 extern char *wave_descriptions[];
34 extern unsigned long wave_first_harmonics[];
35 extern unsigned long wave_harmonic_intervals[];
36 
37 /* Get actual maximum harmonic from given harmonic, h, and wavetype, w */
38 #define ACTUAL_HARM(h,w) h - (h - wave_first_harmonics[w]) % wave_harmonic_intervals[w]
39 /* Get minimum harmonic content in given wavetype, w */
40 #define MIN_HARM(w) wave_first_harmonics[w]
41 /* Get minimum extra harmonic content possible in given wavetype, w */
42 #define MIN_EXTRA_HARM(w) wave_harmonic_intervals[w]
43 /* Get frequency from MIDI note, n */
44 #define FREQ_FROM_NOTE(n) 6.875f * POWF (2.0f, (float)(n + 3) / 12.0f)
45 /* Get max harmonic from given frequency, f, at sample rate, r */
46 #define HARM_FROM_FREQ(f,r) (unsigned long)((float)r / f / 2.0f)
47 
48 /*
49  * A single wavetable will have a range of pitches at which their samples
50  * may be played back.
51  *
52  * The upper bound is determined by the maximum harmonic present in the
53  * waveform - above this frequency, the higher harmonics will alias.
54  *
55  * The lower bound is chosen to be the higher bound of the previous wavetable
56  * (or a pre-defined limit if there is no such table).
57  */
58 
59 typedef enum
60 {
61 	SAW,
62 	SQUARE,
63 	PARABOLA
64 } Wavetype;
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
70 /*******************************************************************************
71  *  Description: Allocate new wavedata struct
72  *
73  *    Arguments: sample_rate       Sample rate to use when generating data
74  *
75  *      Returns: Pointer to wavedata on success
76  *               NULL (0) on failure
77  *
78  *        Notes: No wavetables are allocated. Use wavedata_add_table
79  ******************************************************************************/
80 Wavedata *
81 wavedata_new (unsigned long sample_rate);
82 
83 /*******************************************************************************
84  *  Description: Destroy allocated wavedata and any tables
85  *
86  *    Arguments: w      Wavedata struct to cleanup
87  ******************************************************************************/
88 void
89 wavedata_cleanup (Wavedata * w);
90 
91 /*******************************************************************************
92  *     Description: Add new wavetable information to wavedata file object
93  *
94  *       Arguments: w                   Wavedata to add table to
95  *                  sample_count        Number of samples in wavetable
96  *                  harmonics           Maximum harmonics present in table
97  *
98  *         Returns:  0 on success
99  *                  -1 otherwise
100  ******************************************************************************/
101 int
102 wavedata_add_table (Wavedata * w,
103                     unsigned long sample_count,
104                     unsigned long harmonics);
105 
106 /*******************************************************************************
107  *     Description: Initialise all wavetables in wavedata with a waveform
108  *                  generated from Fourier series.
109  *
110  *       Arguments: w               Wavedata to generate data for
111  *                  wavetype        Wavetype to generate
112  *                  gibbs_comp      Compensation for Gibbs' effect:
113  *                                    0.0: none (wave will overshoot)
114  *                                    1.0: full (wave will not overshoot)
115  *
116  *           Notes: Compensation for Gibbs' Effect will reduce the degree
117  *                   of overshoot and ripple at the transitions. A value of 1.0
118  *                   will pretty much eliminate it.
119  ******************************************************************************/
120 void
121 wavedata_generate_tables (Wavedata * w,
122                           Wavetype wavetype,
123                           float gibbs_comp);
124 
125 /*******************************************************************************
126  *     Description: Write wavedata as a c header file
127  *
128  *       Arguments: w           Wavedata to write
129  *                  wdat_fp     Pointer to output file
130  *                  prefix      Prefix to use in declarations. If this is null
131  *                               declarations are prefixed with 'wdat'.
132  *
133  *         Returns:  0 on success
134  *                  -1 otherwise
135  ******************************************************************************/
136 int
137 wavedata_write (Wavedata * w,
138                 FILE * wdat_fp,
139                 char * prefix);
140 
141 #ifdef __cplusplus
142 } /* extern "C" { */
143 #endif
144 
145 #endif /* blop_wdatutil_h */
146