Commit f6edf5f603f7e436d7f0b8b086f82dcb49a62317
Add foobuf (resizeable circular buffer)
cel committed on 10/27/2016, 7:02:05 PMParent: 239935fea99de16fbc7d43693de7db919bedb955
Files changed
Makefile | changed |
ccdl.c | changed |
foostudio.c | changed |
foobuf.c | added |
foobuf.h | added |
Makefile | ||
---|---|---|
@@ -1,11 +1,13 @@ | ||
1 | 1 … | BIN = foostudio |
2 | -CFLAGS = -Wall -Wextra -pedantic -g | |
2 … | +CFLAGS = -Wall -Wextra -pedantic -g -Werror | |
3 | 3 … | LDLIBS = -ldl -lasound -lm -lpthread |
4 | 4 … | |
5 | -$(BIN): foostudio.o ccdl.o | |
5 … | +OBJ = foostudio.o ccdl.o foobuf.o | |
6 | 6 … | |
7 | -foostudio.o ccdl.o: Makefile | |
7 … | +$(BIN): $(OBJ) | |
8 | 8 … | |
9 … | +$(OBJ): Makefile | |
10 … | + | |
9 | 11 … | clean: |
10 | 12 … | $(RM) $(wildcard *.o *.so) $(BIN) |
11 | 13 … |
ccdl.c | ||
---|---|---|
@@ -46,9 +46,9 @@ | ||
46 | 46 … | if (child == 0) { |
47 | 47 … | char *cc = getenv("CC"); |
48 | 48 … | if (!cc) cc = "cc"; |
49 | 49 … | char *const argv[] = { |
50 | - cc, (char *)src, "-o", (char *)lib, | |
50 … | + cc, (char *)src, "foobuf.o", "-o", (char *)lib, | |
51 | 51 … | "-fPIC", "-shared", "-lm", |
52 | 52 … | NULL}; |
53 | 53 … | if (execvp(argv[0], argv)) err(1, "execvp"); |
54 | 54 … | exit(0); |
foostudio.c | ||
---|---|---|
@@ -124,9 +124,9 @@ | ||
124 | 124 … | if (play) { |
125 | 125 … | for (size_t i = 0; i < period_size; i++) { |
126 | 126 … | time += step; |
127 | 127 … | float frame_in = i < (unsigned)frames_in ? buffer[i] : 0; |
128 | - buffer[i] = play(tune_obj, time, frame_in); | |
128 … | + buffer[i] = play(&tune_obj, time, frame_in); | |
129 | 129 … | } |
130 | 130 … | } else { |
131 | 131 … | time += step * period_size; |
132 | 132 … | } |
foobuf.c | |||
---|---|---|---|
@@ -1,0 +1,35 @@ | |||
1 … | + | ||
2 … | + | ||
3 … | + | ||
4 … | + | ||
5 … | + | ||
6 … | +struct foobuf *foobuf(struct foobuf *buf, size_t len) | ||
7 … | +{ | ||
8 … | + if (!buf) buf = calloc(sizeof *buf, 1); | ||
9 … | + if (buf->len != len) { | ||
10 … | + buf->data = realloc(buf->data, len * sizeof(buf->data[0])); | ||
11 … | + if (len > buf->len) { | ||
12 … | + memset(&buf->data[buf->len], 0, len - buf->len); | ||
13 … | + } else { | ||
14 … | + if (buf->offset >= len) buf->offset = 0; | ||
15 … | + } | ||
16 … | + buf->len = len; | ||
17 … | + } | ||
18 … | + return buf; | ||
19 … | +} | ||
20 … | + | ||
21 … | +float foobuf_read(struct foobuf *buf, size_t offset) | ||
22 … | +{ | ||
23 … | + size_t len = buf->len; | ||
24 … | + return buf->data[((buf->offset - offset) + len) % len]; | ||
25 … | +} | ||
26 … | + | ||
27 … | +void foobuf_write(struct foobuf *buf, float val) | ||
28 … | +{ | ||
29 … | + if (!buf || !buf->data) return; | ||
30 … | + size_t offset = buf->offset; | ||
31 … | + buf->data[offset] = val; | ||
32 … | + offset++; | ||
33 … | + if (offset == buf->len) offset = 0; | ||
34 … | + buf->offset = offset; | ||
35 … | +} |
Built with git-ssb-web