In event1_to_event2(), the tempo variable has been renamed to wpm_tempo
[ahxm.git] / core.c
blob729dd0efac0fa560051fcebfa2e1ab1547ea995d
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2004 Angel Ortega <angel@triptico.com>
6 core.c - Core functions
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <math.h>
32 #include "core.h"
34 /*******************
35 Data
36 ********************/
38 /* main output frequency */
39 int _frequency=44100;
41 /* interpolation type: 0, none; 1, linear; 2, cubic spline; 3, lagrange */
42 int _interpolation=1;
44 /* output channels */
45 int _n_channels=2;
47 /* note frequencies */
48 double _middle_A_freq=440.0;
49 static double _note_frequency[NUM_NOTES];
51 /* debug level */
52 int _debug=1;
54 /*******************
55 Code
56 ********************/
58 /**
59 * note_frequency - MIDI note to frequency converter
60 * @note: the MIDI note
62 * Accepts a MIDI note number (range 0 to 127) and
63 * returns its frequency in Hz.
65 double note_frequency(int note)
67 int n;
69 if(!VALID_NOTE(note))
70 return(0);
72 /* builds the table if empty */
73 if(_note_frequency[0] == 0.0)
75 for(n=0;n < NUM_NOTES;n++)
76 _note_frequency[n]=(_middle_A_freq / 32.0) *
77 pow(2.0, (((double)n - 9.0) / 12.0));
80 return(_note_frequency[note]);
84 /**
85 * _get_sample - Reads a sample from a wave buffer.
86 * @wave: the wave PCM data
87 * @size: size of the wave in samples
88 * @offset: sample number to be returned
90 * Returns the sample number @offset from the @wave buffer
91 * of @size size, doing interpolation if @offset
92 * is not an integer.
94 float _get_sample(float * wave, double size, double offset)
96 float d, s1, s2, r;
97 int o;
99 o=(int) offset;
101 switch(_interpolation)
103 case 0:
104 /* no interpolation */
105 r=wave[o];
106 break;
108 case 1:
109 /* linear interpolation */
110 if(offset > size - 2)
111 r=wave[o];
112 else
114 d=(float)(offset - floor(offset));
115 s1=wave[o];
116 s2=(wave[o + 1] - s1) * d;
118 r=s1 + s2;
121 break;
123 case 2:
124 /* cubic spline (borrowed from timidity) */
125 if(offset < 1 || offset > size - 3)
126 r=wave[o];
127 else
129 float s0, s3, t;
131 s0=wave[o - 1];
132 s1=wave[o];
133 s2=wave[o + 1];
134 s3=wave[o + 2];
136 t=s2;
138 d=(float)(offset - floor(offset));
140 s2=(6.0 * s2 +
141 ((5.0 * s3 - 11.0 * s2 + 7.0 * s1 - s0) / 4.0) *
142 (d + 1.0) * (d - 1.0)) * d;
144 s1=((6.0 * s1 +
145 ((5.0 * s0 - 11.0 * s1 + 7.0 * t - s3) / 4.0) *
146 d * (d - 2.0)) * (1.0 - d) + s2) / 6.0;
148 r=s1;
151 break;
153 case 3:
154 /* lagrange (borrowed from timidity) */
155 if(offset < 1 || offset > size - 3)
156 r=wave[o];
157 else
159 float s0, s3;
161 s0=wave[o - 1];
162 s1=wave[o];
163 s2=wave[o + 1];
164 s3=wave[o + 2];
166 d=(float)(offset - floor(offset));
168 s3 += -3.0 * s2 + 3.0 * s1 - s0;
169 s3 *= (d - 2.0) / 6.0;
170 s3 += s2 - 2.0 * s1 + s0;
171 s3 *= (d - 1.0) / 2.0;
172 s3 += s1 - s0;
173 s3 *= d;
174 s3 += s0;
176 r=s3;
179 break;
182 return(r);
186 float get_sample(struct ss_wave * w, int channel, double offset)
188 return(_get_sample(w->wave[channel], w->size, offset));
193 * wave_resample - Resamples a wave.
194 * @freq: original frequency of the wave
195 * @size: size of the wave in samples
196 * @n_channels: number of channels
197 * @wave: the wave PCM data
199 * Resamples the @wave from its original @freq frequency to
200 * the global output frequency. The resampled wave is stored
201 * in @wave.
203 * Returns the conversion ratio; the original size (and other
204 * pointers to the data as loop starts and ends) should be
205 * divided by this number to match the new wave.
207 double wave_resample(int freq, double size, int n_channels, float * wave[])
209 double ratio;
210 int new_size;
211 float * old_wave;
212 int n,m;
213 double i;
215 ratio=(double) freq / (double) _frequency;
216 new_size=(int)(size / ratio);
218 printf("wave_resample: ratio %f\n", (float)ratio);
220 for(n=0;n < n_channels;n++)
222 old_wave=wave[n];
223 wave[n]=(float *) malloc(new_size * sizeof(float));
225 for(m=i=0;m < new_size;m++,i+=ratio)
226 wave[n][m]=get_sample(old_wave, size, i);
228 /* free the old wave */
229 free(old_wave);
232 return(ratio);
237 * tempo_from_wave - Calculates a tempo from a wave
238 * @w: the wave
239 * @note: note to calculate the tempo from
240 * @beats: number of beats the tempo should match
242 * Calculates the optimal tempo for the @w wave, playing the @note,
243 * to last @beats.
245 double tempo_from_wave(struct ss_wave * w, int note, double beats)
247 double d;
249 d=note_frequency(note) / w->base_freq;
251 /* get the length of a whole, in seconds */
252 d *= w->size / w->s_rate;
254 /* convert to minutes */
255 d *= 60.0;
257 /* convert to the desired beats */
258 d *= 4.0 / beats;
260 return(d);
265 * pitch_from_wave - Calculates a pitch from a wave
266 * @w: the wave
267 * @tempo: current tempo
268 * @beats: number of beats the frequency should match
270 * Calculates the optimal frequency (pitch) for the @w wave, at @tempo,
271 * to last @beats.
273 double pitch_from_wave(struct ss_wave * w, double tempo, double beats)
275 double d;
277 /* calculate number of seconds the wave lasts */
278 d=w->size / (double) w->s_rate;
280 /* convert to minutes and then to bpms */
281 d /= 60.0;
282 d *= tempo;
284 return(w->base_freq * d / beats);