drawscale

Scale object. Any incoming value will be scaled to the closest note in the scale.
Author: Jaffasplaffa
License: BSD
Github: jaffa/scale/drawscale.axo

Inlets

int32 note

Outlets

int32 s

Parameters

bin12 p1

Declaration
int i;
Control Rate
int Step = (inlet_note + 120) % 12;

int ScaleOut;
if (param_p1 & (1 << Step)) {
  ScaleOut = Step;
} else {
  int closest_higher = -1, closest_lower = -1;
  // look for closest lower
  for (int i = Step - 1; i >= 0; i -= 1) {
    if (param_p1 & (1 << i)) {
      closest_lower = Step - i;
      break;
    }
  }
  // look for closest higher
  for (int i = Step + 1; i <= 11; i += 1) {
    if (param_p1 & (1 << i)) {
      closest_higher = i - Step;
      break;
    }
  }

  if (closest_lower != -1 && closest_higher != -1) {
    if (closest_lower < closest_higher) {
      ScaleOut = Step - closest_lower;
    } else {
      ScaleOut = Step + closest_higher;
    }
  } else if (closest_lower != -1) {
    ScaleOut = Step - closest_lower;
  } else if (closest_higher != -1) {
    ScaleOut = Step + closest_higher;
  } else {
    ScaleOut = 0;
  }
}

int Octave = ((inlet_note + 120) / 12) * 12;

outlet_s = ScaleOut + Octave - 120;

Privacy

© 2024 Zrna Research