int32 note
int32 s
bin12 p1
int i;
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;