git ssb

0+

cel / foostudio



Commit f6edf5f603f7e436d7f0b8b086f82dcb49a62317

Add foobuf (resizeable circular buffer)

cel committed on 10/27/2016, 7:02:05 PM
Parent: 239935fea99de16fbc7d43693de7db919bedb955

Files changed

Makefilechanged
ccdl.cchanged
foostudio.cchanged
foobuf.cadded
foobuf.hadded
MakefileView
@@ -1,11 +1,13 @@
11 BIN = foostudio
2-CFLAGS = -Wall -Wextra -pedantic -g
2 +CFLAGS = -Wall -Wextra -pedantic -g -Werror
33 LDLIBS = -ldl -lasound -lm -lpthread
44
5-$(BIN): foostudio.o ccdl.o
5 +OBJ = foostudio.o ccdl.o foobuf.o
66
7-foostudio.o ccdl.o: Makefile
7 +$(BIN): $(OBJ)
88
9 +$(OBJ): Makefile
10 +
911 clean:
1012 $(RM) $(wildcard *.o *.so) $(BIN)
1113
ccdl.cView
@@ -46,9 +46,9 @@
4646 if (child == 0) {
4747 char *cc = getenv("CC");
4848 if (!cc) cc = "cc";
4949 char *const argv[] = {
50- cc, (char *)src, "-o", (char *)lib,
50 + cc, (char *)src, "foobuf.o", "-o", (char *)lib,
5151 "-fPIC", "-shared", "-lm",
5252 NULL};
5353 if (execvp(argv[0], argv)) err(1, "execvp");
5454 exit(0);
foostudio.cView
@@ -124,9 +124,9 @@
124124 if (play) {
125125 for (size_t i = 0; i < period_size; i++) {
126126 time += step;
127127 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);
129129 }
130130 } else {
131131 time += step * period_size;
132132 }
foobuf.cView
@@ -1,0 +1,35 @@
1 +#include <malloc.h>
2 +#include <string.h>
3 +#include <stdio.h>
4 +#include "foobuf.h"
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 +}
foobuf.hView
@@ -1,0 +1,11 @@
1 +#pragma once
2 +
3 +struct foobuf {
4 + size_t len;
5 + size_t offset;
6 + float *data;
7 +};
8 +
9 +struct foobuf *foobuf(struct foobuf *buf, size_t len);
10 +float foobuf_read(struct foobuf *buf, size_t offset);
11 +void foobuf_write(struct foobuf *buf, float val);

Built with git-ssb-web