#define _DEFAULT_SOURCE #include #include #include #include #include #include #include #include "tune.h" struct tune_dl { void *handle; ino_t inode; struct tune *tune; }; static int tune_dl_load(struct tune_dl *dl, const char *libfname) { struct stat st; if (stat(libfname, &st) < 0) { warn("stat"); return -1; } if (dl->inode == st.st_ino) { // file unchanged return 0; } // reload the library if (dl->handle) { dlclose(dl->handle); dl->inode = 0; } dl->handle = dlopen(libfname, RTLD_NOW); if (dl->handle == NULL) { // warnx("dlopen: %s", dlerror()); return 0; } dl->tune = dlsym(dl->handle, "TUNE"); if (dl->tune == NULL) { warn("dlsym"); dlclose(dl->handle); return 0; } dl->inode = st.st_ino; if (dl->tune->reload) { dl->tune->reload(dl->tune); } return 0; } int main(int argc, char *argv[]) { struct tune_dl dl = {0}; const char *fname = "./tune.so"; if (tune_dl_load(&dl, fname) < 0) { return 1; } if (dl.tune->init) { dl.tune->init(dl.tune); } int t = 0; while (1) { (void) tune_dl_load(&dl, fname); if (dl.handle && dl.tune->play) { double sample = dl.tune->play(dl.tune, t); printf("%d %f\n", t, sample); } (void) tune_dl_load(&dl, fname); usleep(100000); t++; } if (dl.tune->deinit) { dl.tune->deinit(dl.tune); } }