git ssb

0+

cel / foostudio



Tree: fdc9644582b3e2b875598b772c5bdda6c74ef9e5

Files: fdc9644582b3e2b875598b772c5bdda6c74ef9e5 / studio.c

1399 bytesRaw
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
11struct tune_dl {
12 void *handle;
13 ino_t inode;
14 struct tune *tune;
15};
16
17static 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
51int 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}
76

Built with git-ssb-web