rndWeightPatt

Weighted random pattern generator Based on the RndWeidthNote module (earlier version to be more precise), but without the ability to save multiple chance tables. This does mean that it's still fixed to the 12 possible possitions of the 12 notes/octave. So it can be used as a note generator, but also as a random value generator. when using as a kind random value generator for 8 possible positions, just leave the last 4 controls to zero.
Author: Johannes Taelman
License: BSD
Github: sss/patt/rndWeightPatt.axo

Inlets

int32 count

int32 length

int32 maxOct

int32 minOct

int32 pattern

bool32 trig

bool32 rnd

Outlets

frac32 random

frac32 repeating

Parameters

frac32.u.mapvsl C

frac32.u.mapvsl cis

frac32.u.mapvsl D

frac32.u.mapvsl dis

frac32.u.mapvsl E

frac32.u.mapvsl F

frac32.u.mapvsl fis

frac32.u.mapvsl G

frac32.u.mapvsl gis

frac32.u.mapvsl A

frac32.u.mapvsl ais

frac32.u.mapvsl B

Attributes

spinner maxlength

spinner maxpatterns

Declaration
uint32_t chance[13];
uint32_t melody[attr_maxpatterns][attr_maxlength];
int i;
int j;
int gtrig;
int32_t select;
int32_t note;
int count;
int rtrig;
int32_t Melody;
int oct[attr_maxpatterns][attr_maxlength];
int prev;
int init;
Init
init = 1;
Control Rate
chance[0] = 0;
chance[1] = param_C;
chance[2] = param_cis;
chance[3] = param_D;
chance[4] = param_dis;
chance[5] = param_E;
chance[6] = param_F;
chance[7] = param_fis;
chance[8] = param_G;
chance[9] = param_gis;
chance[10] = param_A;
chance[11] = param_ais;
chance[12] = param_B;

int length = inlet_length > attr_maxlength ? attr_maxlength : inlet_length;
count = inlet_count - (inlet_count / length) * length;
count = count < 0 ? count + length : count;

int pattern =
    inlet_pattern - (inlet_pattern / attr_maxpatterns) * attr_maxpatterns;
pattern = pattern < 0 ? pattern + attr_maxpatterns : pattern;

int32_t total = 0;
for (i = 0; i < 13; i++) {
  total += chance[i];
}
for (i = 2; i < 13; i++) {
  chance[i] = chance[i] + chance[i - 1];
}
if ((inlet_trig) && !gtrig) {
  select = ___SMMUL(((uint32_t)(GenerateRandomNumber())) >> 1, total) << 1;
}

int diff = inlet_maxOct - inlet_minOct;
if ((inlet_rnd > 0) && !rtrig) {
  rtrig = 1;
  for (i = 0; i < length; i++) {
    melody[pattern][i] =
        ___SMMUL(((uint32_t)(GenerateRandomNumber())) >> 1, total << 1);
    oct[pattern][i] =
        inlet_minOct * 12 +
        (___SMMUL(((uint32_t)(GenerateRandomNumber())) >> 1, (diff << 1) + 1)) *
            12;
  }
} else if (inlet_rnd == 0) {
  rtrig = 0;
}

if (init > 0) {
  for (j = 0; j < attr_maxpatterns; j++) {
    for (i = 0; i < attr_maxlength; i++) {
      melody[j][i] =
          ___SMMUL(((uint32_t)(GenerateRandomNumber())) >> 1, total << 1);
      oct[j][i] = inlet_minOct * 12 +
                  (___SMMUL(((uint32_t)(GenerateRandomNumber())) >> 1,
                            (diff << 1) + 1)) *
                      12;
    }
  }
}
init = 0;

if ((inlet_trig) && !gtrig) {
  gtrig = 1;
  for (i = 11; i >= 0; i--) {
    note = ((select >= chance[i]) && (select < chance[i + 1])) > 0 ? i : note;
  }
  note +=
      inlet_minOct * 12 +
      (___SMMUL(((uint32_t)(GenerateRandomNumber())) >> 1, (diff << 1) + 1)) *
          12;
} else if (inlet_trig == 0) {
  gtrig = 0;
}

if (!(prev == count)) {
  for (i = 11; i >= 0; i--) {
    Melody = ((melody[pattern][count] >= chance[i]) &&
              (melody[pattern][count] < chance[i + 1])) > 0
                 ? i
                 : Melody;
  }
  Melody += oct[pattern][count];
}

outlet_random = note << 21;
outlet_repeating = Melody << 21;
prev = count;

Privacy

© 2024 Zrna Research