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
38 /* main output frequency */
41 /* interpolation type: 0, none; 1, linear; 2, cubic spline; 3, lagrange */
47 /* note frequencies */
48 double _middle_A_freq
=440.0;
49 static double _note_frequency
[NUM_NOTES
];
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
)
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
]);
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
94 float _get_sample(float * wave
, double size
, double offset
)
99 /* take care of wrappings */
100 if(offset
< 0) offset
+= size
;
104 switch(_interpolation
)
107 /* no interpolation */
112 /* linear interpolation */
113 if(offset
> size
- 2)
117 d
=(float)(offset
- floor(offset
));
119 s2
=(wave
[o
+ 1] - s1
) * d
;
127 /* cubic spline (borrowed from timidity) */
128 if(offset
< 1 || offset
> size
- 3)
141 d
=(float)(offset
- floor(offset
));
144 ((5.0 * s3
- 11.0 * s2
+ 7.0 * s1
- s0
) / 4.0) *
145 (d
+ 1.0) * (d
- 1.0)) * d
;
148 ((5.0 * s0
- 11.0 * s1
+ 7.0 * t
- s3
) / 4.0) *
149 d
* (d
- 2.0)) * (1.0 - d
) + s2
) / 6.0;
157 /* lagrange (borrowed from timidity) */
158 if(offset
< 1 || offset
> size
- 3)
169 d
=(float)(offset
- floor(offset
));
171 s3
+= -3.0 * s2
+ 3.0 * s1
- s0
;
172 s3
*= (d
- 2.0) / 6.0;
173 s3
+= s2
- 2.0 * s1
+ s0
;
174 s3
*= (d
- 1.0) / 2.0;
189 float get_sample(struct ss_wave
* w
, int channel
, double offset
)
191 return(_get_sample(w
->wave
[channel
], w
->size
, offset
));
196 * wave_resample - Resamples a wave.
197 * @freq: original frequency of the wave
198 * @size: size of the wave in samples
199 * @n_channels: number of channels
200 * @wave: the wave PCM data
202 * Resamples the @wave from its original @freq frequency to
203 * the global output frequency. The resampled wave is stored
206 * Returns the conversion ratio; the original size (and other
207 * pointers to the data as loop starts and ends) should be
208 * divided by this number to match the new wave.
210 double wave_resample(int freq
, double size
, int n_channels
, float * wave
[])
218 ratio
=(double) freq
/ (double) _frequency
;
219 new_size
=(int)(size
/ ratio
);
221 printf("wave_resample: ratio %f\n", (float)ratio
);
223 for(n
=0;n
< n_channels
;n
++)
226 wave
[n
]=(float *) malloc(new_size
* sizeof(float));
228 for(m=i=0;m < new_size;m++,i+=ratio)
229 wave[n][m]=get_sample(old_wave, size, i);
231 /* free the old wave */
240 * tempo_from_wave - Calculates a tempo from a wave
242 * @note: note to calculate the tempo from
243 * @len: whole notes the tempo should match
245 * Calculates the optimal tempo for the @w wave, playing the @note,
246 * to last @len whole notes.
248 double tempo_from_wave(struct ss_wave
* w
, int note
, double len
)
252 d
=note_frequency(note
) / w
->base_freq
;
254 /* get the length of a whole, in seconds */
255 d
*= w
->size
/ w
->s_rate
;
257 /* convert to minutes */
260 /* convert to the desired whole notes */
268 * pitch_from_wave - Calculates a pitch from a wave
270 * @tempo: current tempo
271 * @len: desired length in whole notes
273 * Calculates the optimal frequency (pitch) for the @w wave, at @tempo,
274 * to last @len whole notes.
276 double pitch_from_wave(struct ss_wave
* w
, double tempo
, double len
)
280 /* calculate number of seconds the wave lasts */
281 d
=w
->size
/ (double) w
->s_rate
;
283 /* convert to minutes, then to wpms */
287 return(w
->base_freq
* d
* len
);