diff options
author | James <olpc@localhost.localdomain> | 2007-01-21 11:55:11 (GMT) |
---|---|---|
committer | James <olpc@localhost.localdomain> | 2007-01-21 11:55:11 (GMT) |
commit | 2fcb4c0d3d2a42dd89a6aab92838dda675230de1 (patch) | |
tree | 99670e7de9b0e90c96ba7226ef69ca703b1b24fe /Util/Clooper | |
parent | e2eab95f6e89691198c61ea01e10a041099ba782 (diff) |
steps toward Clooper
Diffstat (limited to 'Util/Clooper')
-rw-r--r-- | Util/Clooper/Makefile | 5 | ||||
-rw-r--r-- | Util/Clooper/__init__.py | 0 | ||||
-rw-r--r-- | Util/Clooper/cmd_csound.cpp | 169 |
3 files changed, 163 insertions, 11 deletions
diff --git a/Util/Clooper/Makefile b/Util/Clooper/Makefile index 22e78c9..91badbb 100644 --- a/Util/Clooper/Makefile +++ b/Util/Clooper/Makefile @@ -4,7 +4,7 @@ CFLAGS+=-g -Wall -Werror --std=c99 -fPIC PYTHON=/usr/include/python2.4 -all : _ttest.so +all : _ttest.so cmd_csound %.o: %.c %.h $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ @@ -21,5 +21,8 @@ ttest_wrap.c: ttest.i ttest_wrap.o: ttest_wrap.c gcc -fPIC -I$(PYTHON) -o $@ -c $< +cmd_csound: cmd_csound.cpp + g++ -o $@ $^ -lcsound + _ttest.so : ttest_wrap.o ttest.o gcc -shared -o $@ $^ diff --git a/Util/Clooper/__init__.py b/Util/Clooper/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Util/Clooper/__init__.py diff --git a/Util/Clooper/cmd_csound.cpp b/Util/Clooper/cmd_csound.cpp index 90a522b..591fa26 100644 --- a/Util/Clooper/cmd_csound.cpp +++ b/Util/Clooper/cmd_csound.cpp @@ -1,19 +1,168 @@ #include <stdio.h> -#include "csound.hpp" +#include <csound/csound.hpp> -int main( int argc, char ** argv) +#include <pthread.h> +#include <stdio.h> +#include <time.h> +#include <unistd.h> +#include <sys/time.h> + +struct TamTamSound { - CSOUND * csound = csoundCreate(0); - csoundInitialize(&argc, &argv, 0); + void * ThreadID; + CSOUND * csound; + enum {CONTINUE, STOP} PERF_STATUS; + int playnote; + + TamTamSound(char * orc) + : playnote(0) + { + csound = csoundCreate(NULL); + int argc = 2; + char * prog = "fake_progname"; + char ** argv = (char **) malloc(argc * sizeof(char*)); + argv[0] = prog; + argv[1] = orc; + + csoundInitialize(&argc, &argv, 0); + int result = csoundCompile( csound, argc, argv); + free(argv); + if (!result) + { + PERF_STATUS = CONTINUE; + ThreadID = csoundCreateThread(csThread, (void*)this); + } + else + { + fprintf(stderr, "ERROR: csoundCompile() returned %i\n", result); + csoundDestroy(csound); + csound = NULL; + ThreadID = NULL; + } + } + ~TamTamSound() + { + if (csound) + { + PERF_STATUS = STOP; + uintptr_t rval = csoundJoinThread(ThreadID); + if (1) fprintf(stderr, "INFO: thread returned %zu\n", rval); + csoundDestroy(csound); + } + } + static double pytime(const struct timeval * tv) + { + return (double) tv->tv_sec + (double) tv->tv_usec / 1000000.0; + } + uintptr_t thread_fn() + { + struct timeval tv; + double t_prev, t_this; + double m = 0.0; - int rval = csoundCompile(csound, argc, argv); + int loops = 0; + + while ( (csoundPerformKsmps(csound) == 0) + && (PERF_STATUS == CONTINUE)) + { + gettimeofday(&tv, 0); + double t_this = pytime(&tv); + if (loops) + { + if (m < t_this - t_prev) + { + m = t_this - t_prev; + fprintf(stderr, "maximum lag %lf\n", m); + } + } + ++loops; + t_prev = t_this; + if (playnote) + { + play_note(playnote); + playnote = 0; + } + } + csoundDestroy(csound); + return 1; + } + static uintptr_t csThread(void *clientData) + { + return ((TamTamSound*)clientData)->thread_fn(); + } - if (!rval) + void load_instrument(int table, const char * fname) { - while (csoundPerformKsmps(csound) == 0) - ; + if (!csound) + { + fprintf(stderr, "ERROR: TamTamSound::%s() csound not loaded\n", __FUNCTION__); + return; + } + char str[512]; + sprintf(str, "f%d 0 0 -1 \"%s\" 0 0 0", table, fname); + if (1) fprintf(stdout, "%s\n", str); + csoundInputMessage(csound, str); } - csoundDestroy(csound); - return rval; + void play_note(int table) + { + if (!csound) + { + fprintf(stderr, "ERROR: TamTamSound::%s() csound not loaded\n", __FUNCTION__); + return; + } + MYFLT p[16]; + p[0] = 0.0; + p[1] = 5003.1; + p[2] = 0.0; + p[3] = 0.5; + p[4] = 1.0; + p[5] = 0.0; + p[6] = 0.8; + p[7] = 0.5; + p[8] = (MYFLT) table; + p[9] = 0.002; + p[10] = 0.05; + p[11] = 0.0; + p[12] = 1000.0; + p[13] = 0.0; + p[14] = 0.0; + p[15] = 0.0; + csoundScoreEvent(csound, 'i', p+1, 15); + + } + void setMasterVolume(MYFLT vol) + { + MYFLT *p; + if (!(csoundGetChannelPtr(csound, &p, "masterVolume", CSOUND_CONTROL_CHANNEL | CSOUND_INPUT_CHANNEL))) + *p = (MYFLT) vol; + else + { + fprintf(stderr, "ERROR: failed to set master volume\n"); + } + } + + bool good() + { + return csound != NULL; + } +}; + +int main( int argc, char ** argv) +{ + int userInput = 200; + TamTamSound * tt = new TamTamSound(argv[1]); + tt->setMasterVolume(30.0); + + while ((userInput != 0) && (tt->good())) + { + fprintf(stderr, "Enter a pitch\n"); + scanf("%i", &userInput); + tt->load_instrument(5083, "/home/olpc/tamtam/Resources/Sounds/sitar"); + scanf("%i", &userInput); + tt->playnote = (5083); + } + + delete tt; + return 0; } |