git ssb

0+

cel / foostudio



Tree: 033d2aa1890af2ff80fde819e83b034484c47cbc

Files: 033d2aa1890af2ff80fde819e83b034484c47cbc / tune.c

1286 bytesRaw
1#include <math.h>
2#include <malloc.h>
3#include "tune.h"
4#include "foobuf.h"
5
6static float keymap[256] = {0};
7
8float play(void *obj, double time, float in, unsigned char midi_cmd[3])
9{
10 struct {
11 struct foobuf *buf;
12 float *keymap;
13 } *state = obj;
14
15 struct_init(&state);
16 // foobuf_init(&state->buf, 48000);
17 // array_init(&state->keymap, 256);
18 /*
19 static float keymap[256];
20 if (!state->keymap) {
21 state->keymap = keymap;
22 for (size_t i = 0; i < 256; i++) keymap[i] = 0;
23 }
24 */
25
26 float out = 0;
27
28 if (midi_cmd) {
29 // float val = sin(2. * M_PI * (float)i/buf_size * freq);
30 if (midi_cmd[0] == 0x90) {
31 keymap[midi_cmd[1]] = (float)midi_cmd[2]/255.;
32 } else if (midi_cmd[0] == 0x90) {
33 keymap[midi_cmd[1]] = 0;
34 } else {
35 // printf("midi %x %x %x\n", buf[0], buf[1], buf[2]);
36 }
37
38 for (size_t i = 0; i < 256; i++) {
39 float val = keymap[i];
40 if (val > 0.01) {
41 float freq = exp2f((float)(i-69) / 12);
42 out += sin(2. * M_PI * time * freq);
43 printf("i %zu %f %f\n", i, (double)freq, val);
44 keymap[i] /= 2;
45 }
46 }
47 }
48 // return sin(2. * M_PI * time * 440.) * 0.25 + in * 0.75;
49
50 // float out = in*0.5 + foobuf_read(state->buf, 0)*0.5;
51 // foobuf_read(state->buf, 0)*0.5;
52 // float out2 = in*0.5 + out*0.5;
53 // foobuf_write(state->buf, out);
54 return out;
55}
56

Built with git-ssb-web