git ssb

0+

cel / foostudio



Commit fdc9644582b3e2b875598b772c5bdda6c74ef9e5

Initial commit

Charles Lehner committed on 10/14/2016, 5:19:02 AM

Files changed

Makefileadded
studio.cadded
tune.cadded
tune.hadded
MakefileView
@@ -1,0 +1,14 @@
1 +CFLAGS = -std=c99 -fPIC
2 +LDLIBS = -ldl -lm
3 +
4 +all: studio tune.so
5 +
6 +studio: studio.c tune.h
7 + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)
8 +
9 +tune.so : tune.c tune.h
10 + $(CC) $(CFLAGS) -shared $(LDFLAGS) -o $@ $< $(LDLIBS)
11 +
12 +clean:
13 + $(RM) *.o *.so studio
14 +
studio.cView
@@ -1,0 +1,75 @@
1 +#define _DEFAULT_SOURCE
2 +#include <dlfcn.h>
3 +#include <err.h>
4 +#include <stdbool.h>
5 +#include <stdio.h>
6 +#include <sys/stat.h>
7 +#include <sys/types.h>
8 +#include <unistd.h>
9 +#include "tune.h"
10 +
11 +struct tune_dl {
12 + void *handle;
13 + ino_t inode;
14 + struct tune *tune;
15 +};
16 +
17 +static int tune_dl_load(struct tune_dl *dl, const char *libfname)
18 +{
19 + struct stat st;
20 + if (stat(libfname, &st) < 0) {
21 + warn("stat");
22 + return -1;
23 + }
24 + if (dl->inode == st.st_ino) {
25 + // file unchanged
26 + return 0;
27 + }
28 + // reload the library
29 + if (dl->handle) {
30 + dlclose(dl->handle);
31 + dl->inode = 0;
32 + }
33 + dl->handle = dlopen(libfname, RTLD_NOW);
34 + if (dl->handle == NULL) {
35 + // warnx("dlopen: %s", dlerror());
36 + return 0;
37 + }
38 + dl->tune = dlsym(dl->handle, "TUNE");
39 + if (dl->tune == NULL) {
40 + warn("dlsym");
41 + dlclose(dl->handle);
42 + return 0;
43 + }
44 + dl->inode = st.st_ino;
45 + if (dl->tune->reload) {
46 + dl->tune->reload(dl->tune);
47 + }
48 + return 0;
49 +}
50 +
51 +int main(int argc, char *argv[])
52 +{
53 + struct tune_dl dl = {0};
54 + const char *fname = "./tune.so";
55 + if (tune_dl_load(&dl, fname) < 0) {
56 + return 1;
57 + }
58 + if (dl.tune->init) {
59 + dl.tune->init(dl.tune);
60 + }
61 + int t = 0;
62 + while (1) {
63 + (void) tune_dl_load(&dl, fname);
64 + if (dl.handle && dl.tune->play) {
65 + double sample = dl.tune->play(dl.tune, t);
66 + printf("%d %f\n", t, sample);
67 + }
68 + (void) tune_dl_load(&dl, fname);
69 + usleep(100000);
70 + t++;
71 + }
72 + if (dl.tune->deinit) {
73 + dl.tune->deinit(dl.tune);
74 + }
75 +}
tune.cView
@@ -1,0 +1,26 @@
1 +#include <math.h>
2 +#include "tune.h"
3 +
4 +static void tune_init(struct tune *tune)
5 +{
6 +}
7 +
8 +static void tune_reload(struct tune *tune)
9 +{
10 +}
11 +
12 +static double tune_play(struct tune *tune, int time)
13 +{
14 + return sin(time);
15 +}
16 +
17 +static void tune_deinit(struct tune *tune)
18 +{
19 +}
20 +
21 +const struct tune TUNE = {
22 + .init = tune_init,
23 + .reload = tune_reload,
24 + .play = tune_play,
25 + .deinit = tune_deinit,
26 +};
tune.hView
@@ -1,0 +1,10 @@
1 +#pragma once
2 +
3 +struct tune {
4 + void (*init)(struct tune *);
5 + void (*deinit)(struct tune *);
6 + void (*reload)(struct tune *);
7 + double (*play)(struct tune *, int time);
8 +};
9 +
10 +extern const struct tune TUNE;

Built with git-ssb-web