Updated documentation (Closes: #1014).
[ahxm.git] / core.c
blob154d0a02903c2f72df7562c69aba07447755c5e2
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 /*******************
52 Code
53 ********************/
55 /**
56 * note_frequency - MIDI note to frequency converter
57 * @note: the MIDI note
59 * Accepts a MIDI note number (range 0 to 127) and
60 * returns its frequency in Hz.
62 double note_frequency(int note)
64 int n;
66 if(!VALID_NOTE(note))
67 return(0);
69 /* builds the table if empty */
70 if(_note_frequency[0] == 0.0)
72 for(n=0;n < NUM_NOTES;n++)
73 _note_frequency[n]=(_middle_A_freq / 32.0) *
74 pow(2.0, (((double)n - 9.0) / 12.0));
77 return(_note_frequency[note]);
81 /**
82 * _get_sample - Reads a sample from a wave buffer.
83 * @wave: the wave PCM data
84 * @size: size of the wave in samples
85 * @offset: sample number to be returned
87 * Returns the sample number @offset from the @wave buffer
88 * of @size size, doing interpolation if @offset
89 * is not an integer.
91 float _get_sample(float * wave, double size, double offset)
93 float d, s1, s2, r;
94 int o;
96 o=(int) offset;
98 switch(_interpolation)
100 case 0:
101 /* no interpolation */
102 r=wave[o];
103 break;
105 case 1:
106 /* linear interpolation */
107 if(offset > size - 2)
108 r=wave[o];
109 else
111 d=(float)(offset - floor(offset));
112 s1=wave[o];
113 s2=(wave[o + 1] - s1) * d;
115 r=s1 + s2;
118 break;
120 case 2:
121 /* cubic spline (borrowed from timidity) */
122 if(offset < 1 || offset > size - 3)
123 r=wave[o];
124 else
126 float s0, s3, t;
128 s0=wave[o - 1];
129 s1=wave[o];
130 s2=wave[o + 1];
131 s3=wave[o + 2];
133 t=s2;
135 d=(float)(offset - floor(offset));
137 s2=(6.0 * s2 +
138 ((5.0 * s3 - 11.0 * s2 + 7.0 * s1 - s0) / 4.0) *
139 (d + 1.0) * (d - 1.0)) * d;
141 s1=((6.0 * s1 +
142 ((5.0 * s0 - 11.0 * s1 + 7.0 * t - s3) / 4.0) *
143 d * (d - 2.0)) * (1.0 - d) + s2) / 6.0;
145 r=s1;
148 break;
150 case 3:
151 /* lagrange (borrowed from timidity) */
152 if(offset < 1 || offset > size - 3)
153 r=wave[o];
154 else
156 float s0, s3;
158 s0=wave[o - 1];
159 s1=wave[o];
160 s2=wave[o + 1];
161 s3=wave[o + 2];
163 d=(float)(offset - floor(offset));
165 s3 += -3.0 * s2 + 3.0 * s1 - s0;
166 s3 *= (d - 2.0) / 6.0;
167 s3 += s2 - 2.0 * s1 + s0;
168 s3 *= (d - 1.0) / 2.0;
169 s3 += s1 - s0;
170 s3 *= d;
171 s3 += s0;
173 r=s3;
176 break;
179 return(r);
183 float get_sample(struct ss_wave * w, int channel, double offset)
185 return(_get_sample(w->wave[channel], w->size, offset));
190 * wave_resample - Resamples a wave.
191 * @freq: original frequency of the wave
192 * @size: size of the wave in samples
193 * @n_channels: number of channels
194 * @wave: the wave PCM data
196 * Resamples the @wave from its original @freq frequency to
197 * the global output frequency. The resampled wave is stored
198 * in @wave.
200 * Returns the conversion ratio; the original size (and other
201 * pointers to the data as loop starts and ends) should be
202 * divided by this number to match the new wave.
204 double wave_resample(int freq, double size, int n_channels, float * wave[])
206 double ratio;
207 int new_size;
208 float * old_wave;
209 int n,m;
210 double i;
212 ratio=(double) freq / (double) _frequency;
213 new_size=(int)(size / ratio);
215 printf("wave_resample: ratio %f\n", (float)ratio);
217 for(n=0;n < n_channels;n++)
219 old_wave=wave[n];
220 wave[n]=(float *) malloc(new_size * sizeof(float));
222 for(m=i=0;m < new_size;m++,i+=ratio)
223 wave[n][m]=get_sample(old_wave, size, i);
225 /* free the old wave */
226 free(old_wave);
229 return(ratio);
234 * tempo_from_wave - Calculates a tempo from a wave
235 * @w: the wave
236 * @note: note to calculate the tempo from
237 * @beats: number of beats the tempo should match
239 * Calculates the optimal tempo for the @w wave, playing the @note,
240 * to last @beats.
242 double tempo_from_wave(struct ss_wave * w, int note, double beats)
244 double d;
246 d=note_frequency(note) / w->base_freq;
248 /* get the length of a whole, in seconds */
249 d *= w->size / w->s_rate;
251 /* convert to minutes */
252 d *= 60.0;
254 /* convert to the desired beats */
255 d *= 4.0 / beats;
257 return(d);
262 * pitch_from_wave - Calculates a pitch from a wave
263 * @w: the wave
264 * @tempo: current tempo
265 * @beats: number of beats the frequency should match
267 * Calculates the optimal frequency (pitch) for the @w wave, at @tempo,
268 * to last @beats.
270 #if 0
271 double pitch_from_wave(struct ss_wave * w, double tempo, double beats)
274 #endif