Browse Source

patch loading

vampirefrog 3 years ago
parent
commit
9b25b01751
3 changed files with 63 additions and 13 deletions
  1. 4
    13
      main.c
  2. 58
    0
      synth.c
  3. 1
    0
      synth.h

+ 4
- 13
main.c View File

@@ -245,19 +245,10 @@ int main(int argc, char **argv) {
245 245
 	sr = jack_get_sample_rate(client);
246 246
 
247 247
 	synth_init(&synth);
248
-	synth.osc_env.attack = 1;
249
-	synth.osc_env.decay = 0;
250
-	synth.osc_env.sustain = 100;
251
-	synth.osc_env.release = 100;
252
-	synth.filter_env.attack = 1;
253
-	synth.filter_env.decay = 300;
254
-	synth.filter_env.sustain = 0;
255
-	synth.filter_env.release = 0;
256
-	synth.filter_eg_intensity = .9;
257
-	synth.filter_kbd_track = .5;
258
-	sine_osc_set_freq(&synth.lfo_osc, 6);
259
-	synth.pitch_bend_range = 12; // semitones
260
-	synth.monophonic = 1;
248
+
249
+	if(optind < argc) {
250
+		synth_load_patch(&synth, argv[optind]);
251
+	}
261 252
 
262 253
 	if (jack_activate(client)) {
263 254
 		fprintf (stderr, "cannot activate client");

+ 58
- 0
synth.c View File

@@ -191,3 +191,61 @@ void synth_pitch_bend(struct Synth *s, int16_t bend) {
191 191
 void synth_set_lfo_depth(struct Synth *s, uint8_t mod) {
192 192
 	s->lfo_depth = mod / 127.0;
193 193
 }
194
+
195
+void synth_load_patch(struct Synth *s, const char *filename) {
196
+	FILE *f = fopen(filename, "r");
197
+	if(!f) {
198
+		perror(filename);
199
+		return;
200
+	}
201
+	char buf[256];
202
+	while(!feof(f)) {
203
+		fgets(buf, sizeof(buf), f);
204
+		char *tok = strtok(buf, " \t");
205
+		if(tok) {
206
+			char *val = strtok(NULL, " \t");
207
+			if(val) {
208
+				if(!strcmp(tok, "lfo_freq")) {
209
+					sine_osc_set_freq(&s->lfo_osc, strtof(val, NULL));
210
+				} else if(!strcmp(tok, "osc_env.attack")) {
211
+					s->osc_env.attack = strtof(val, NULL);
212
+				} else if(!strcmp(tok, "osc_env.decay")) {
213
+					s->osc_env.decay = strtof(val, NULL);
214
+				} else if(!strcmp(tok, "osc_env.sustain")) {
215
+					s->osc_env.sustain = strtof(val, NULL);
216
+				} else if(!strcmp(tok, "osc_env.release")) {
217
+					s->osc_env.release = strtof(val, NULL);
218
+				} else if(!strcmp(tok, "filter_env.attack")) {
219
+					printf("filter_env.attack %f\n", strtof(val, NULL));
220
+					s->filter_env.attack = strtof(val, NULL);
221
+				} else if(!strcmp(tok, "filter_env.decay")) {
222
+					printf("filter_env.decay %f\n", strtof(val, NULL));
223
+					s->filter_env.decay = strtof(val, NULL);
224
+				} else if(!strcmp(tok, "filter_env.sustain")) {
225
+					s->filter_env.sustain = strtof(val, NULL);
226
+				} else if(!strcmp(tok, "filter_env.release")) {
227
+					s->filter_env.release = strtof(val, NULL);
228
+				} else if(!strcmp(tok, "filter_eg_intensity")) {
229
+					s->filter_eg_intensity  = strtof(val, NULL);
230
+				} else if(!strcmp(tok, "filter_kbd_track")) {
231
+					s->filter_kbd_track = strtof(val, NULL);
232
+				} else if(!strcmp(tok, "pitch_bend_range")) {
233
+					s->pitch_bend_range = strtof(val, NULL);
234
+				} else if(!strcmp(tok, "monophonic")) {
235
+					s->monophonic = atoi(val);
236
+				} else if(!strcmp(tok, "unison_spread")) {
237
+					s->unison_spread = strtof(val, NULL);
238
+				} else if(!strcmp(tok, "stereo_spread")) {
239
+					s->stereo_spread = strtof(val, NULL);
240
+				} else if(!strcmp(tok, "cutoff")) {
241
+					s->cutoff = strtof(val, NULL);
242
+				} else if(!strcmp(tok, "resonance")) {
243
+					for(int i = 0; i < SYNTH_NUM_VOICES; i++) {
244
+						filter_set_resonance(&s->voices[i].filter, strtof(val, NULL));
245
+					}
246
+				}
247
+			}
248
+		}
249
+	}
250
+	fclose(f);
251
+}

+ 1
- 0
synth.h View File

@@ -37,6 +37,7 @@ struct Synth {
37 37
 };
38 38
 
39 39
 void synth_init(struct Synth *synth);
40
+void synth_load_patch(struct Synth *synth, const char *filename);
40 41
 void synth_note_on(struct Synth *synth, uint8_t note, uint8_t velocity);
41 42
 void synth_note_off(struct Synth *synth, uint8_t note, uint8_t velocity);
42 43
 void synth_render_sample(struct Synth *synth, float *out);

Loading…
Cancel
Save