git ssb

0+

cel / foostudio



Commit dc2fadcea45c02d204f58e09305b7bcb0c89b025

Fix stuff

cel committed on 11/4/2016, 8:15:35 PM
Parent: 033d2aa1890af2ff80fde819e83b034484c47cbc

Files changed

foobuf.cchanged
foobuf.hchanged
tune.cchanged
tune.hchanged
foobuf.cView
@@ -5,20 +5,25 @@
55
66 int foobuf_init(struct foobuf **bufp, size_t len)
77 {
88 struct foobuf *buf = *bufp;
9- if (!buf) buf = calloc(sizeof *buf, 1);
10- if (!buf) return 1;
11- if (buf->len != len) {
12- buf->data = realloc(buf->data, len * sizeof(buf->data[0]));
9 + if (!buf) {
10 + buf = calloc(sizeof *buf, 1);
11 + if (!buf) return 1;
12 + *bufp = buf;
13 + buf->data = calloc(len, sizeof(float));
14 + if (!buf->data) return 1;
15 + buf->len = len;
16 + } else if (buf->len != len) {
17 + buf->data = realloc(buf->data, len * sizeof(float));
18 + if (!buf->data) return 1;
1319 if (len > buf->len) {
1420 memset(&buf->data[buf->len], 0, len - buf->len);
1521 } else {
1622 if (buf->offset >= len) buf->offset = 0;
1723 }
1824 buf->len = len;
1925 }
20- *bufp = buf;
2126 return 0;
2227 }
2328
2429 float foobuf_read(struct foobuf *buf, size_t offset)
@@ -35,4 +40,11 @@
3540 offset++;
3641 if (offset == buf->len) offset = 0;
3742 buf->offset = offset;
3843 }
44 +
45 +int _struct_init(void **ptr, size_t size)
46 +{
47 + if (*ptr) return 0;
48 + if ((*ptr = calloc(1, size))) return 0;
49 + return 1;
50 +}
foobuf.hView
@@ -8,4 +8,9 @@
88
99 int foobuf_init(struct foobuf **, size_t len);
1010 float foobuf_read(struct foobuf *buf, size_t offset);
1111 void foobuf_write(struct foobuf *buf, float val);
12 +
13 +int _struct_init(void **, size_t);
14 +
15 +#define struct_init(ptr) _struct_init((void **)ptr, sizeof(**ptr))
16 +#define array_init(ptr, len) _struct_init((void **)ptr, sizeof(**ptr) * len)
tune.cView
@@ -1,21 +1,55 @@
11 #include <math.h>
22 #include <malloc.h>
3 +#include <unistd.h>
34 #include "tune.h"
45 #include "foobuf.h"
6 +#include <err.h>
57
6-static float keymap[256] = {0};
8 +static float notes[256] = {0};
9 +static float dampen[256] = {0};
710
8-float play(void *obj, double time, float in, unsigned char midi_cmd[3])
11 +static float freqs[256] = {0};
12 +static int got_freqs = 0;
13 +
14 +// static float buf[48000] = {0};
15 +
16 +float get_freq(int i)
917 {
18 + if (!got_freqs) {
19 + for (int i = 0; i < 256; i++) {
20 + freqs[i] = 440. * exp2((i-69) / 12.);
21 + }
22 + got_freqs = 1;
23 + }
24 + return freqs[i];
25 +}
26 +
27 +float saw(float x, float t)
28 +{
29 + return fmodf(t, 1./x) * x*2. - 1.;
30 +}
31 +
32 +float play(void **obj, double time, float in, unsigned char midi_cmd[3])
33 +{
34 + // return in * 10;
35 + // return sin(2. * M_PI * time * 440.) * 0.1 + in * 0.9;
1036 struct {
1137 struct foobuf *buf;
12- float *keymap;
13- } *state = obj;
38 + // float *keymap;
39 + } **statep = (void *)obj, *state;
1440
15- struct_init(&state);
16- // foobuf_init(&state->buf, 48000);
41 + if (struct_init(statep)) {
42 + warn("foobuf_init");
43 + return 0;
44 + }
45 + state = *statep;
46 + if (foobuf_init(&state->buf, 48000)) {
47 + warn("foobuf_init");
48 + return 0;
49 + }
1750 // array_init(&state->keymap, 256);
51 +
1852 /*
1953 static float keymap[256];
2054 if (!state->keymap) {
2155 state->keymap = keymap;
@@ -27,29 +61,38 @@
2761
2862 if (midi_cmd) {
2963 // float val = sin(2. * M_PI * (float)i/buf_size * freq);
3064 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;
65 + // printf("key %u\n", midi_cmd[1]);
66 + notes[midi_cmd[1]] += (float)midi_cmd[2]/255.;
67 + dampen[midi_cmd[1]] = 0;
68 + } else if (midi_cmd[0] == 0x80) {
69 + dampen[midi_cmd[1]] += (float)midi_cmd[2]/255.;
3470 } else {
3571 // printf("midi %x %x %x\n", buf[0], buf[1], buf[2]);
3672 }
73 + }
3774
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- }
75 + float total = 0;
76 +
77 + for (int i = 0; i < 256; i++) {
78 + if (notes[i] > 0.01) {
79 + float freq = get_freq(i);
80 + total += notes[i];
81 + out += notes[i] * saw(freq, time);
82 + // out += notes[i] * sin(2. * M_PI * time * freq);
83 + notes[i] *= 0.99999 * (1 - dampen[i] * 0.001);
4684 }
85 + if (dampen[i] > 0.001) dampen[i] *= 0.9999;
4786 }
87 +
4888 // return sin(2. * M_PI * time * 440.) * 0.25 + in * 0.75;
4989
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;
90 + // out += foobuf_read(state->buf, 500)*0.9;
91 + out += in*180;
92 + // out = in*0.1 + foobuf_read(state->buf, 48000)*0.9;
93 + foobuf_write(state->buf, out);
94 + // return out;
95 + return out * 0.25;
96 + // return out / total;
97 + // return 1/(1. + exp(-out * 4)) * 0.99;
5598 }
tune.hView
@@ -1,15 +1,5 @@
11 #pragma once
22
3-typedef float play_fn(void *, double time, float in, unsigned char midi_cmd[3]);
3 +typedef float play_fn(void **, double time, float in, unsigned char midi_cmd[3]);
44
5-#define struct_init(ptr) _struct_init((void **)ptr, sizeof(*ptr))
6-#define array_init(ptr, len) _struct_init((void **)ptr, sizeof(*ptr) * len)
7-
8-static inline int _struct_init(void **ptr, size_t size)
9-{
10- if (*ptr) return 0;
11- if ((*ptr = calloc(size, 1))) return 0;
12- return 1;
13-}
14-
155 extern play_fn play;

Built with git-ssb-web