Updated TODO.
[ahxm.git] / ss_core.c
blobfebcd1e7d90cce54bcfb37dbe1bd3e4f775d9b40
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2007 Angel Ortega <angel@triptico.com>
6 ss_core.c - Softsynth 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 <string.h>
31 #include <math.h>
33 #include "ahxm.h"
35 /*******************
36 Data
37 ********************/
39 /* main output frequency */
40 int ss_frequency = 44100;
42 /* interpolation type: 0, none; 1, linear; 2, cubic spline; 3, lagrange */
43 int ss_interpolation = 3;
45 /* output channels */
46 int ss_nchannels = -1;
48 /* note frequencies */
49 double ss_middle_A_freq = 440.0;
50 static double note_frequency[128];
52 /*******************
53 Code
54 ********************/
56 /**
57 * ss_note_frequency - MIDI note to frequency converter
58 * @note: the MIDI note
60 * Accepts a MIDI note number (range 0 to 127) and
61 * returns its frequency in Hz.
63 double ss_note_frequency(int note)
65 int n;
67 if (note < 0 || note > 127)
68 return 0;
70 /* builds the table if empty */
71 if (note_frequency[0] == 0.0) {
72 for (n = 0; n < 128; n++)
73 note_frequency[n] = (ss_middle_A_freq / 32.0) *
74 pow(2.0, (((double) n - 9.0) / 12.0));
77 return note_frequency[note];
81 /**
82 * ss_alloc_wave - Allocates a wave structure.
83 * @size: size in frames
84 * @n_channels: number of channels
85 * @s_rate: sampling rate
86 * @p_size: size of the sound page
88 * Allocates a wave structure. If @p_size is -1, it's assumed to be the
89 * same as @size (so the sound will live entirely in memory).
91 struct ss_wave *ss_alloc_wave(int size, int n_channels, int s_rate, int p_size)
93 struct ss_wave *w = NULL;
95 if (p_size == -1)
96 p_size = size;
98 if ((w = (struct ss_wave *) malloc(sizeof(struct ss_wave))) != NULL) {
99 memset(w, '\0', sizeof(struct ss_wave));
101 w->size = (double) size;
102 w->p_size = p_size;
103 w->n_channels = n_channels;
104 w->s_rate = s_rate;
106 /* alloc space for the pointers to the waves */
107 w->wave = (sample_t **) malloc(n_channels * sizeof(sample_t *));
108 memset(w->wave, '\0', n_channels * sizeof(sample_t *));
111 return w;
116 * ss_free_wave - Frees a wave structure.
117 * @w: the wave structure
119 * Frees a struct ss_wave allocated by ss_alloc_wave().
121 void ss_free_wave(struct ss_wave *w)
123 if (w->wave != NULL) {
124 int n;
126 /* frees the buffers */
127 for (n = 0; n < w->n_channels; n++)
128 if (w->wave[n] != NULL)
129 free(w->wave[n]);
131 /* frees the space for the pointers to the waves */
132 free(w->wave);
134 /* if it has a filename, also free it */
135 if (w->filename != NULL)
136 free(w->filename);
139 /* frees the wave itself */
140 free(w);
144 void ss_prepare_wave(struct ss_wave *w)
145 /* prepares a wave file for usage (creates the page buffers) */
147 int n;
149 /* alloc space for the waves themselves */
150 for (n = 0; n < w->n_channels; n++) {
151 w->wave[n] = (sample_t *) realloc(w->wave[n], w->p_size * sizeof(sample_t));
153 memset(w->wave[n], '\0', w->p_size * sizeof(sample_t));
158 static void ss_load_page(struct ss_wave *w, int offset)
159 /* loads a page from a wave file into memory */
161 FILE *f;
162 int s;
164 /* set the offset to some samples behind the
165 wanted offset (to avoid possible page bounces) */
166 if ((w->p_offset = offset - 441) < 0)
167 w->p_offset = 0;
169 /* too much page faults for this wave? */
170 if (w->page_faults >= 8) {
171 /* increase space */
172 if ((w->p_size *= 2) > (int) w->size) {
173 /* if this resize is too much, just
174 set it to load the full wave */
175 w->p_size = (int) w->size;
176 w->p_offset = 0;
179 /* trigger a page resizing and restart statistics */
180 w->page_faults = 0;
183 if (w->page_faults == 0)
184 ss_prepare_wave(w);
186 if ((f = fopen(w->filename, "r")) == NULL) {
187 fprintf(stderr, "Can't open '%s'\n", w->filename);
188 return;
191 if (verbose >= 3)
192 printf("load_page [%s,%d,%d,%d]\n", w->filename,
193 w->p_offset, w->p_size, w->page_faults);
195 /* calculate the frame size */
196 s = w->p_offset * (w->bits / 8) * w->n_channels;
198 /* move there */
199 fseek(f, w->f_pos + s, SEEK_SET);
201 /* fill the page */
202 load_pcm_wave(f, w);
204 fclose(f);
206 w->page_faults++;
210 static sample_t ss_pick_sample(struct ss_wave *w, int channel, double offset)
211 /* picks a sample from a ss_wave, forcing a call to ss_load_page() if
212 the wanted sample is not in memory */
214 int o;
215 sample_t *wave;
217 o = (int) offset;
219 /* is the wanted sample not in memory? */
220 if (o < w->p_offset || o > w->p_offset + w->p_size)
221 ss_load_page(w, o);
223 wave = w->wave[channel];
224 return wave[o - w->p_offset];
229 * ss_get_sample - Reads a sample from a wave.
230 * @wave: the wave
231 * @channel: the channel
232 * @offset: sample number to be returned
234 * Returns the sample number @offset from the @channel of the @wave. @Offset
235 * can be a non-integer value.
237 sample_t ss_get_sample(struct ss_wave * w, int channel, double offset)
239 sample_t d, t, r = 0.0;
240 sample_t s0, s1, s2, s3;
242 /* take care of wrappings */
243 if (offset < 0)
244 offset += w->size;
246 /* pick sample at offset */
247 s1 = ss_pick_sample(w, channel, offset);
249 switch (ss_interpolation) {
250 case 0:
251 /* no interpolation */
252 r = s1;
253 break;
255 case 1:
256 /* linear interpolation */
257 if (offset > w->size - 2)
258 r = s1;
259 else {
260 d = (sample_t) (offset - floor(offset));
261 s2 = (ss_pick_sample(w, channel, offset + 1) - s1) * d;
263 r = s1 + s2;
266 break;
268 case 2:
269 /* cubic spline (borrowed from timidity) */
270 if (offset < 1 || offset > w->size - 3)
271 r = s1;
272 else {
273 s0 = ss_pick_sample(w, channel, offset - 1);
274 s2 = ss_pick_sample(w, channel, offset + 1);
275 s3 = ss_pick_sample(w, channel, offset + 2);
277 t = s2;
279 d = (sample_t) (offset - floor(offset));
281 s2 = (6.0 * s2 +
282 ((5.0 * s3 - 11.0 * s2 + 7.0 * s1 - s0) / 4.0) *
283 (d + 1.0) * (d - 1.0)) * d;
285 s1 = ((6.0 * s1 +
286 ((5.0 * s0 - 11.0 * s1 + 7.0 * t - s3) / 4.0) *
287 d * (d - 2.0)) * (1.0 - d) + s2) / 6.0;
289 r = s1;
292 break;
294 case 3:
295 /* lagrange (borrowed from timidity) */
296 if (offset < 1 || offset > w->size - 3)
297 r = s1;
298 else {
299 s0 = ss_pick_sample(w, channel, offset - 1);
300 s2 = ss_pick_sample(w, channel, offset + 1);
301 s3 = ss_pick_sample(w, channel, offset + 2);
303 d = (sample_t) (offset - floor(offset));
305 s3 += -3.0 * s2 + 3.0 * s1 - s0;
306 s3 *= (d - 2.0) / 6.0;
307 s3 += s2 - 2.0 * s1 + s0;
308 s3 *= (d - 1.0) / 2.0;
309 s3 += s1 - s0;
310 s3 *= d;
311 s3 += s0;
313 r = s3;
316 break;
319 return r;
324 * ss_tempo_from_wave - Calculates a tempo from a wave
325 * @w: the wave
326 * @note: note to calculate the tempo from
327 * @len: whole notes the tempo should match
329 * Calculates the optimal tempo for the @w wave, playing the @note,
330 * to last @len whole notes.
332 double ss_tempo_from_wave(struct ss_wave *w, int note, double len)
334 double d;
336 d = ss_note_frequency(note) / w->base_freq;
338 /* get the length of a whole, in seconds */
339 d *= w->s_rate / w->size;
341 /* convert to minutes */
342 d *= 60.0;
344 /* then to bpm,s */
345 d *= 4.0;
346 d *= len;
348 return d;
353 * ss_pitch_from_tempo - Calculates a pitch from a tempo
354 * @w: the wave
355 * @tempo: current tempo
356 * @len: desired length in whole notes
358 * Calculates the optimal frequency (pitch) for the @w wave, at @tempo,
359 * to last @len whole notes.
361 double ss_pitch_from_tempo(struct ss_wave *w, double tempo, double len)
363 double d;
365 /* calculate number of seconds the wave lasts */
366 d = w->size / (double) w->s_rate;
368 /* convert to minutes, then to wpms */
369 d /= 60.0;
370 d *= (tempo / 4.0);
372 return w->base_freq * d * len;