Skip to content
Permalink
Browse files

improve op_random

  • Loading branch information
boqs committed Nov 8, 2017
1 parent fa4940a commit b80c957cdd43953353c56f4f7f1711f7ccf588e5
Showing with 64 additions and 16 deletions.
  1. +46 −11 apps/bees/src/ops/op_random.c
  2. +5 −2 apps/bees/src/ops/op_random.h
  3. +13 −3 utils/pd/bees_ops_grid.pd
@@ -3,31 +3,34 @@

//-------------------------------------------------
//---- static func declare
static void countBits(op_random_t* random);
static void op_random_in_min(op_random_t* min, const io_t v);
static void op_random_in_max(op_random_t* max, const io_t v);
static void op_random_in_trig(op_random_t* trig, const io_t v);
static void op_random_in_seed(op_random_t* seed, const io_t v);

// pickle / unpickle
static u8* op_random_pickle(op_random_t* op, u8* dst);
static const u8* op_random_unpickle(op_random_t* op, const u8* src);

//-------------------------------------------------
//---- static vars
static const char* op_random_instring = "MIN\0 MAX\0 TRIG\0 ";
static const char* op_random_instring = "MIN\0 MAX\0 TRIG\0 SEED\0 ";
static const char* op_random_outstring = "VAL ";
static const char* op_random_opstring = "RAND";

static op_in_fn op_random_in_fn[3] = {
static op_in_fn op_random_in_fn[4] = {
(op_in_fn)&op_random_in_min,
(op_in_fn)&op_random_in_max,
(op_in_fn)&op_random_in_trig
(op_in_fn)&op_random_in_trig,
(op_in_fn)&op_random_in_seed
};

//-------------------------------------------------
//---- external func define
void op_random_init(void* mem) {
op_random_t* random = (op_random_t*)mem;
random->super.numInputs = 3;
random->super.numInputs = 4;
random->super.numOutputs = 1;
random->outs[0] = -1;

@@ -45,35 +48,65 @@ void op_random_init(void* mem) {
random->in_val[0] = &(random->min);
random->in_val[1] = &(random->max);
random->in_val[2] = &(random->trig);
random->in_val[3] = &(random->seed);

random->min = 0;
random->max = 1;
random->trig = 0;
random->seed = 111;

random->a = 0x19660d;
random->c = 0x3c6ef35f;
random->x = 111; // FIXME seed... do this better
countBits(random);
}

//-------------------------------------------------
//---- static func define

static void countBits(op_random_t* random) {
random->range = random->max - random->min;
u16 n = random->range;
int counter = 0;
while(n) {
counter ++;
n = n >> 1;
}
random->bitShift = 25 - counter;
}

static void op_random_in_min(op_random_t* random, const io_t v) {
random->min = v;
if(v <= random->max) {
random->min = v;
}
else {
random->min = random->max;
}
countBits(random);
}

static void op_random_in_max(op_random_t* random, const io_t v) {
random->max = v;
if(v >= random->min) {
random->max = v;
}
else {
random->max = random->min;
}
countBits(random);
}

static void op_random_in_seed(op_random_t* random, const io_t v) {
random->seed = v;
random->x = v;
}

static void op_random_in_trig(op_random_t* random, const io_t v) {
random->x = random->x * random->c + random->a;
random->val =random->x;
if(random->val < 0)
random->val *= -1;
random->val = (random->val % ((random->max - random->min) + 1)) + random->min;
random->val = (random->x >> random->bitShift);
random->val %= random->range;
random->val += random->min;
/* printf("%d\n", random->val); */
net_activate(random, 0, random->val);
// if(v > 0) { random->trig = OP_ONE; } else { random->trig = 0; }
}


@@ -82,13 +115,15 @@ u8* op_random_pickle(op_random_t* op, u8* dst) {
dst = pickle_io(op->min, dst);
dst = pickle_io(op->max, dst);
dst = pickle_io(op->trig, dst);
dst = pickle_io(op->seed, dst);
return dst;
}

const u8* op_random_unpickle(op_random_t* op, const u8* src) {
src = unpickle_io(src, &(op->min));
src = unpickle_io(src, &(op->max));
src = unpickle_io(src, &(op->trig));
src = unpickle_io(src, &(op->seed));
return src;
}

@@ -12,11 +12,14 @@ typedef struct op_random_struct {
volatile io_t min;
volatile io_t max;
volatile io_t trig;
volatile io_t * in_val[3]; // min, max, trig
volatile io_t seed;
volatile io_t * in_val[4]; // min, max, trig, seed
op_out_t outs[1];

u32 a,c,x;
u16 range;
u8 bitShift;
} op_random_t;
void op_random_init(void* mem);

#endif // header guard
#endif // header guard
@@ -1,4 +1,4 @@
#N canvas 0 29 1918 1169 10;
#N canvas 0 47 1364 719 10;
#X obj 37 37 bees_op;
#X obj 277 119 BEES_MP;
#X obj 373 120 BEES_GRID;
@@ -73,7 +73,7 @@
#X obj 848 415 BEES_ADD;
#X obj 866 379 BEES_MUL;
#X msg 898 346 5;
#X obj 204 326 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0
#X obj 204 326 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 1
1;
#X obj 610 908 BEES_POLY;
#X floatatom 591 952 5 0 0 0 - - -, f 5;
@@ -91,10 +91,15 @@
#X obj 534 751 t f f;
#X obj 591 750 t f f;
#X obj 656 750 t f f;
#X obj 696 839 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 1
#X obj 696 839 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0
1;
#X obj 948 476 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0
1;
#X obj 106 604 BEES_RAND;
#X msg 79 485 0;
#X msg 117 489 16;
#X floatatom 118 685 5 0 0 0 - - -, f 5;
#X msg 185 519 1;
#X connect 2 0 7 0;
#X connect 2 1 8 0;
#X connect 2 2 9 0;
@@ -119,6 +124,7 @@
#X connect 20 0 18 3;
#X connect 21 0 23 2;
#X connect 21 0 31 2;
#X connect 21 0 89 3;
#X connect 22 0 21 2;
#X connect 23 0 24 0;
#X connect 23 1 25 0;
@@ -199,3 +205,7 @@
#X connect 86 1 83 0;
#X connect 87 0 74 4;
#X connect 88 0 65 4;
#X connect 89 0 92 0;
#X connect 90 0 89 1;
#X connect 91 0 89 2;
#X connect 93 0 89 3;

0 comments on commit b80c957

Please sign in to comment.