From 939515986c20c32dbebc8a95ebd10b5f192877b9 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 10 Jul 2015 09:32:38 +0200 Subject: Make test input module work in a more logical way. --- drumgizmo/input/test/test.cc | 48 ++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'drumgizmo/input/test') diff --git a/drumgizmo/input/test/test.cc b/drumgizmo/input/test/test.cc index b7b4a7d..225a7a2 100644 --- a/drumgizmo/input/test/test.cc +++ b/drumgizmo/input/test/test.cc @@ -31,7 +31,11 @@ class Test { public: - Test() { p = 0.1; instr = -1; len = -1; } + Test() + : p(0.1) + , instr(-1) + {} + ~Test() {} bool init(int instruments, char *inames[]); @@ -48,11 +52,12 @@ public: private: float p; int instr; - int len; + int num_instruments; }; bool Test::init(int instruments, char *inames[]) { + num_instruments = instruments; return true; } @@ -60,7 +65,6 @@ void Test::setParm(std::string parm, std::string value) { if(parm == "p") p = atof(value.c_str()); if(parm == "instr") instr = atoi(value.c_str()); - if(parm == "len") len = atoi(value.c_str()); } bool Test::start() @@ -76,23 +80,37 @@ void Test::pre() { } +#define BUFFER_MAX 1000 event_t *Test::run(size_t pos, size_t nsamples, size_t *nevents) { - if((float)rand() / (float)RAND_MAX > p) { - *nevents = 0; - return NULL; - } + *nevents = 0; + event_t *evs = (event_t *)malloc(sizeof(event_t) * BUFFER_MAX); + + for(size_t i = 0; i < nsamples; ++i) { + if((float)rand() / (float)RAND_MAX > p) { + continue; + } + + evs[*nevents].type = TYPE_ONSET; - *nevents = 1; - event_t *evs = (event_t *)malloc(sizeof(event_t)); - evs[0].type = TYPE_ONSET; - if(len != -1 && (int)pos > len * 44100) evs[0].type = TYPE_STOP; + if(instr != -1) { + // Use specified instrument + evs[*nevents].instrument = instr; + } else { + // Use random instrument + evs[*nevents].instrument = rand() % num_instruments; + } - if(instr != -1) evs[0].instrument = instr; - else evs[0].instrument = rand() % 32; + evs[*nevents].velocity = (float)rand()/(float)RAND_MAX; + evs[*nevents].offset = nsamples?rand()%nsamples:0; + + (*nevents)++; + + if(*nevents == BUFFER_MAX) { + break; + } + } - evs[0].velocity = (float)rand()/(float)RAND_MAX; - evs[0].offset = nsamples?rand()%nsamples:0; return evs; } -- cgit v1.2.3