Version 1.0.10 RELEASED.
[ahxm.git] / ss_core.c
blob811ca347fea1a0e9e75f1c0d70d59a8b64e3dd14
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)
73 for(n = 0;n < 128;n++)
74 note_frequency[n] = (ss_middle_A_freq / 32.0) *
75 pow(2.0, (((double)n - 9.0) / 12.0));
78 return(note_frequency[note]);
82 /**
83 * ss_alloc_wave - Allocates a wave structure.
84 * @size: size in frames
85 * @n_channels: number of channels
86 * @s_rate: sampling rate
87 * @p_size: size of the sound page
89 * Allocates a wave structure. If @p_size is -1, it's assumed to be the
90 * same as @size (so the sound will live entirely in memory).
92 struct ss_wave * ss_alloc_wave(int size, int n_channels, int s_rate, int p_size)
94 struct ss_wave * w = NULL;
96 if(p_size == -1) p_size = size;
98 if((w = (struct ss_wave *)malloc(sizeof(struct ss_wave))) != NULL)
100 memset(w, '\0', sizeof(struct ss_wave));
102 w->size = (double) size;
103 w->p_size = p_size;
104 w->n_channels = n_channels;
105 w->s_rate = s_rate;
107 /* alloc space for the pointers to the waves */
108 w->wave = (sample_t **)malloc(n_channels * sizeof(sample_t *));
109 memset(w->wave, '\0', n_channels * sizeof(sample_t *));
112 return(w);
117 * ss_free_wave - Frees a wave structure.
118 * @w: the wave structure
120 * Frees a struct ss_wave allocated by ss_alloc_wave().
122 void ss_free_wave(struct ss_wave * w)
124 if(w->wave != NULL)
126 int n;
128 /* frees the buffers */
129 for(n = 0;n < w->n_channels;n++)
130 if(w->wave[n] != NULL)
131 free(w->wave[n]);
133 /* frees the space for the pointers to the waves */
134 free(w->wave);
136 /* if it has a filename, also free it */
137 if(w->filename != NULL) free(w->filename);
140 /* frees the wave itself */
141 free(w);
145 void ss_prepare_wave(struct ss_wave * w)
146 /* prepares a wave file for usage (creates the page buffers) */
148 int n;
150 /* alloc space for the waves themselves */
151 for(n = 0;n < w->n_channels;n++)
153 w->wave[n] = (sample_t *)realloc(w->wave[n],
154 w->p_size * sizeof(sample_t));
156 memset(w->wave[n], '\0', w->p_size * sizeof(sample_t));
161 static void ss_load_page(struct ss_wave * w, int offset)
162 /* loads a page from a wave file into memory */
164 FILE * f;
165 int s;
167 /* set the offset to some samples behind the
168 wanted offset (to avoid possible page bounces) */
169 if((w->p_offset = offset - 441) < 0)
170 w->p_offset = 0;
172 /* too much page faults for this wave? */
173 if(w->page_faults >= 8)
175 /* increase space */
176 if((w->p_size *= 2) > (int) w->size)
178 /* if this resize is too much, just
179 set it to load the full wave */
180 w->p_size = (int) w->size;
181 w->p_offset = 0;
184 /* trigger a page resizing and restart statistics */
185 w->page_faults = 0;
188 if(w->page_faults == 0)
189 ss_prepare_wave(w);
191 if((f = fopen(w->filename, "r")) == NULL)
193 fprintf(stderr, "Can't open '%s'\n", w->filename);
194 return;
197 if(verbose >= 3)
198 printf("load_page [%s,%d,%d,%d]\n", w->filename,
199 w->p_offset, w->p_size, w->page_faults);
201 /* calculate the frame size */
202 s = w->p_offset * (w->bits / 8) * w->n_channels;
204 /* move there */
205 fseek(f, w->f_pos + s, SEEK_SET);
207 /* fill the page */
208 load_pcm_wave(f, w);
210 fclose(f);
212 w->page_faults++;
216 static sample_t ss_pick_sample(struct ss_wave * w, int channel, double offset)
217 /* picks a sample from a ss_wave, forcing a call to ss_load_page() if
218 the wanted sample is not in memory */
220 int o;
221 sample_t * wave;
223 o = (int) offset;
225 /* is the wanted sample not in memory? */
226 if(o < w->p_offset || o > w->p_offset + w->p_size)
227 ss_load_page(w, o);
229 wave = w->wave[channel];
230 return(wave[o - w->p_offset]);
235 * ss_get_sample - Reads a sample from a wave.
236 * @wave: the wave
237 * @channel: the channel
238 * @offset: sample number to be returned
240 * Returns the sample number @offset from the @channel of the @wave. @Offset
241 * can be a non-integer value.
243 sample_t ss_get_sample(struct ss_wave * w, int channel, double offset)
245 sample_t d, t, r = 0.0;
246 sample_t s0, s1, s2, s3;
248 /* take care of wrappings */
249 if(offset < 0) offset += w->size;
251 /* pick sample at offset */
252 s1 = ss_pick_sample(w, channel, offset);
254 switch(ss_interpolation)
256 case 0:
257 /* no interpolation */
258 r = s1;
259 break;
261 case 1:
262 /* linear interpolation */
263 if(offset > w->size - 2)
264 r = s1;
265 else
267 d = (sample_t)(offset - floor(offset));
268 s2 = (ss_pick_sample(w, channel, offset + 1) - s1) * d;
270 r = s1 + s2;
273 break;
275 case 2:
276 /* cubic spline (borrowed from timidity) */
277 if(offset < 1 || offset > w->size - 3)
278 r = s1;
279 else
281 s0 = ss_pick_sample(w, channel, offset - 1);
282 s2 = ss_pick_sample(w, channel, offset + 1);
283 s3 = ss_pick_sample(w, channel, offset + 2);
285 t = s2;
287 d = (sample_t)(offset - floor(offset));
289 s2 = (6.0 * s2 +
290 ((5.0 * s3 - 11.0 * s2 + 7.0 * s1 - s0) / 4.0) *
291 (d + 1.0) * (d - 1.0)) * d;
293 s1 = ((6.0 * s1 +
294 ((5.0 * s0 - 11.0 * s1 + 7.0 * t - s3) / 4.0) *
295 d * (d - 2.0)) * (1.0 - d) + s2) / 6.0;
297 r = s1;
300 break;
302 case 3:
303 /* lagrange (borrowed from timidity) */
304 if(offset < 1 || offset > w->size - 3)
305 r = s1;
306 else
308 s0 = ss_pick_sample(w, channel, offset - 1);
309 s2 = ss_pick_sample(w, channel, offset + 1);
310 s3 = ss_pick_sample(w, channel, offset + 2);
312 d = (sample_t)(offset - floor(offset));
314 s3 += -3.0 * s2 + 3.0 * s1 - s0;
315 s3 *= (d - 2.0) / 6.0;
316 s3 += s2 - 2.0 * s1 + s0;
317 s3 *= (d - 1.0) / 2.0;
318 s3 += s1 - s0;
319 s3 *= d;
320 s3 += s0;
322 r = s3;
325 break;
328 return(r);
333 * ss_tempo_from_wave - Calculates a tempo from a wave
334 * @w: the wave
335 * @note: note to calculate the tempo from
336 * @len: whole notes the tempo should match
338 * Calculates the optimal tempo for the @w wave, playing the @note,
339 * to last @len whole notes.
341 double ss_tempo_from_wave(struct ss_wave * w, int note, double len)
343 double d;
345 d = ss_note_frequency(note) / w->base_freq;
347 /* get the length of a whole, in seconds */
348 d *= w->s_rate / w->size;
350 /* convert to minutes */
351 d *= 60.0;
353 /* then to bpm,s */
354 d *= 4.0;
355 d *= len;
357 return(d);
362 * ss_pitch_from_tempo - Calculates a pitch from a tempo
363 * @w: the wave
364 * @tempo: current tempo
365 * @len: desired length in whole notes
367 * Calculates the optimal frequency (pitch) for the @w wave, at @tempo,
368 * to last @len whole notes.
370 double ss_pitch_from_tempo(struct ss_wave * w, double tempo, double len)
372 double d;
374 /* calculate number of seconds the wave lasts */
375 d = w->size / (double) w->s_rate;
377 /* convert to minutes, then to wpms */
378 d /= 60.0;
379 d *= (tempo / 4.0);
381 return(w->base_freq * d * len);