Support for GUS patches started.
[ahxm.git] / wav.c
blob69b9e266b8f2760af858f27e597eb2c8c7eb14b3
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 "input.h"
33 #include "output.h"
34 #include "generator.h"
35 #include "instrument.h"
37 /*******************
38 Data
39 ********************/
41 /* main output frequency */
42 int _frequency=44100;
44 struct channel
46 int * wave;
47 double vol;
48 double dvol;
52 #define NUM_GENERATORS 64
53 struct generator _generators[NUM_GENERATORS];
56 struct effect_delay
58 int * lbuffer;
59 int * rbuffer;
60 int size;
61 double vol;
62 int cursor;
66 /*******************
67 Code
68 ********************/
70 #define TIME2SAMPLES(s) ((s) * _frequency)
72 void init_delay(struct effect_delay * e, int size, double vol)
74 /* free previously allocated buffer */
75 /* if(e->lbuffer != NULL) free(e->lbuffer);
76 if(e->rbuffer != NULL) free(e->rbuffer);
78 e->lbuffer=(int *) malloc(size * sizeof(int));
79 memset(e->lbuffer, '\0', size * sizeof(int));
80 e->rbuffer=(int *) malloc(size * sizeof(int));
81 memset(e->rbuffer, '\0', size * sizeof(int));
83 e->size=size;
84 e->vol=vol;
85 e->cursor=0;
89 void process_delay(struct effect_delay * e, int * lsample, int * rsample)
91 int l,r;
93 l=e->lbuffer[e->cursor];
94 r=e->lbuffer[e->cursor];
96 l=*lsample + (int)(e->vol * (double)l);
97 r=*rsample + (int)(e->vol * (double)r);
99 e->lbuffer[e->cursor]=*lsample;
100 e->rbuffer[e->cursor]=*rsample;
102 if(++e->cursor == e->size) e->cursor=0;
104 *lsample=l;
105 *rsample=r;
109 void process_reverb(struct effect_delay * e, int * lsample, int * rsample)
111 int l,r;
113 l=e->lbuffer[e->cursor];
114 r=e->lbuffer[e->cursor];
116 e->lbuffer[e->cursor]=*lsample + (int)(e->vol * (double)l);
117 e->rbuffer[e->cursor]=*rsample + (int)(e->vol * (double)r);
119 l+=*lsample;
120 r+=*rsample;
122 if(++e->cursor == e->size) e->cursor=0;
124 *lsample=l;
125 *rsample=r;
129 int main(void)
131 int * ls;
132 int * rs;
133 int size;
134 FILE * f;
135 int n, m;
136 struct effect_delay e;
138 memset(_generators, '\0', sizeof(_generators));
139 build_note_frequencies();
141 load_pat_file("samples/acpiano.pat");
142 return(0);
145 load_wav_file("samples/amen1.wav", &size, &ls, &rs);
147 generator_play(&_generators[0], ls, rs, size, 1, 0, size, 0.2, 1, 4410);
149 load_wav_file("samples/01c1.wav", &size, &ls, &rs);
150 generator_play(&_generators[1], ls, rs, size,
151 _note_freq[24] / _note_freq[36], 0, size, 1, 1, 44100 / 3);
153 load_wav_file("samples/05c3.wav", &size, &ls, &rs);
155 generator_play(&_generators[2], ls, rs, size,
156 _note_freq[36] / _note_freq[36], 0, size, 0.5, 0.5, 0);
158 generator_play(&_generators[3], ls, rs, size,
159 _note_freq[39] / _note_freq[36], 0, size, 0.5, 0.5, 0);
161 generator_play(&_generators[4], ls, rs, size,
162 _note_freq[43] / _note_freq[36], 0, size, 0.5, 0.5, 0);
164 load_wav_file("samples/as-al017.wav", &size, &ls, &rs);
165 generator_play(&_generators[5], ls, rs, size, 1, 0, size, 2, 2, 0);
168 f=popen("sox -V -t raw -s -r 44100 -c 2 -w - qq.wav","w");
171 if(output_open("oss", "/dev/dsp") < 0)
173 printf("Error: can't init driver\n");
174 return(1);
177 init_delay(&e, 44100 / 10, 0.5);
179 for(n=TIME2SAMPLES(15);n > 0;n--)
181 int l,r;
183 l=r=0;
185 for(m=0;m < NUM_GENERATORS;m++)
186 generator(&_generators[m], &l, &r);
188 if(n < TIME2SAMPLES(13))
189 process_reverb(&e, &l, &r);
191 output_write(l, r);
193 /* if(n < TIME2SAMPLES(14))
194 generator_release(&_generators[1]); */
197 output_close();
199 return(0);