TIME2SAMPLES macro moved to core.h.
[ahxm.git] / wav.c
blob9596650029dfe69275b932d9ada1795de97ac15c
1 /*
3 wav.c
5 Copyright (C) 2003 Angel Ortega <angel@triptico.com>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 http://www.triptico.com
25 #include "config.h"
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <math.h>
32 #include "core.h"
33 #include "input.h"
34 #include "output.h"
35 #include "generator.h"
36 #include "instrument.h"
38 /*******************
39 Data
40 ********************/
42 struct channel
44 int * wave;
45 double vol;
46 double dvol;
50 #define NUM_GENERATORS 64
51 struct generator _generators[NUM_GENERATORS];
54 struct effect_delay
56 int * lbuffer;
57 int * rbuffer;
58 int size;
59 double vol;
60 int cursor;
64 /*******************
65 Code
66 ********************/
68 #define TIME2SAMPLES(s) ((s) * _frequency)
70 void init_delay(struct effect_delay * e, int size, double vol)
72 /* free previously allocated buffer */
73 /* if(e->lbuffer != NULL) free(e->lbuffer);
74 if(e->rbuffer != NULL) free(e->rbuffer);
76 e->lbuffer=(int *) malloc(size * sizeof(int));
77 memset(e->lbuffer, '\0', size * sizeof(int));
78 e->rbuffer=(int *) malloc(size * sizeof(int));
79 memset(e->rbuffer, '\0', size * sizeof(int));
81 e->size=size;
82 e->vol=vol;
83 e->cursor=0;
87 void process_delay(struct effect_delay * e, int * lsample, int * rsample)
89 int l,r;
91 l=e->lbuffer[e->cursor];
92 r=e->lbuffer[e->cursor];
94 l=*lsample + (int)(e->vol * (double)l);
95 r=*rsample + (int)(e->vol * (double)r);
97 e->lbuffer[e->cursor]=*lsample;
98 e->rbuffer[e->cursor]=*rsample;
100 if(++e->cursor == e->size) e->cursor=0;
102 *lsample=l;
103 *rsample=r;
107 void process_reverb(struct effect_delay * e, int * lsample, int * rsample)
109 int l,r;
111 l=e->lbuffer[e->cursor];
112 r=e->lbuffer[e->cursor];
114 e->lbuffer[e->cursor]=*lsample + (int)(e->vol * (double)l);
115 e->rbuffer[e->cursor]=*rsample + (int)(e->vol * (double)r);
117 l+=*lsample;
118 r+=*rsample;
120 if(++e->cursor == e->size) e->cursor=0;
122 *lsample=l;
123 *rsample=r;
127 int main(void)
129 int * ls;
130 int * rs;
131 int size;
132 FILE * f;
133 int n, m;
134 struct effect_delay e;
135 struct instrument i;
137 _linear_interpolation=1;
138 _frequency=44100;
140 memset(_generators, '\0', sizeof(_generators));
141 build_note_frequencies();
143 load_pat_file("samples/acpiano.pat");
144 /* load_pat_file("/cdrom/orchhit.pat"); */
145 /* return(0); */
147 load_wav_file("samples/01c1.wav", &size, &ls, &rs);
149 init_instrument(&i);
150 add_instrument_layer(&i, 36, 0, 128, ls, rs,
151 size, 0, size, 44100 / 3);
153 instrument_play_note(&i, 36, 1, 1);
154 /* instrument_play_note(&i, 39, 1, 1); */
157 load_wav_file("samples/amen1.wav", &size, &ls, &rs);
159 generator_play(&_generators[0], ls, rs, size, 1, 0, size, 0.2, 1, 4410);
161 load_wav_file("samples/01c1.wav", &size, &ls, &rs);
162 generator_play(&_generators[1], ls, rs, size,
163 _note_freq[24] / _note_freq[36], 0, size, 1, 1, 44100 / 3);
165 load_wav_file("samples/05c3.wav", &size, &ls, &rs);
167 generator_play(&_generators[2], ls, rs, size,
168 _note_freq[36] / _note_freq[36], 0, size, 0.5, 0.5, 0);
170 generator_play(&_generators[3], ls, rs, size,
171 _note_freq[39] / _note_freq[36], 0, size, 0.5, 0.5, 0);
173 generator_play(&_generators[4], ls, rs, size,
174 _note_freq[43] / _note_freq[36], 0, size, 0.5, 0.5, 0);
176 load_wav_file("samples/as-al017.wav", &size, &ls, &rs);
177 generator_play(&_generators[5], ls, rs, size, 1, 0, size, 2, 2, 0);
180 f=popen("sox -V -t raw -s -r 44100 -c 2 -w - qq.wav","w");
183 if(output_open("oss", "/dev/dsp") < 0)
185 printf("Error: can't init driver\n");
186 return(1);
189 init_delay(&e, 44100 / 10, 0.5);
191 for(n=TIME2SAMPLES(15);n > 0;n--)
193 int l,r;
195 l=r=0;
197 for(m=0;m < NUM_GENERATORS;m++)
198 generator(&_generators[m], &l, &r);
200 generate_instrument(&i, &l, &r);
202 if(n < TIME2SAMPLES(13))
203 process_reverb(&e, &l, &r);
205 output_write(l, r);
207 /* if(n < TIME2SAMPLES(14))
208 generator_release(&_generators[1]); */
211 output_close();
213 return(0);