How to randomly generate notes in musical keys, across an 84 key kslider?

Benson's icon
Max Patch
Copy patch and select New From Clipboard in Max.

I want my patch to select random notes from a klsider, but only in a specific key. Like the first patch below is doing, but for an 84 key kslider instead of just an octave.

In my patch I have it randomly generating notes but not in a specific key. I've specified the midi notes of the 2 keys (C major and Db major), but don't know how to integrate the two pathways so that it randomly generates the notes from the keys, respectively. I'd like to be able to select the keys from a umenu.

Max Patch
Copy patch and select New From Clipboard in Max.

Here is what I have so far:

Roman Thilenius's icon

i would first generate random numbers in C major and then change the key and convert to minor where needed.

well, actually ... i would probably write it all in one line. :D

metro 500
|
expr random(0\,49)
|
[expr ( (($i1%7)==1)*2 + (($i1%7)==2)*(4 - ($i2!=0)) + (($i1%7)==3)*5 + (($i1%7)==4)*7 + (($i1%7)==5)*(9 - ($i2!=0)) + (($i1%7)==6)*(11 - ($i2!=0)) ) + (($i1/7)*12) + $i3]
|

second inlet: 0 or 1 for major/minor (use a toggle?)

third inlet: transpose key, 0 = C, 2 = D,...

double_UG's icon
Max Patch
Copy patch and select New From Clipboard in Max.
double_UG's icon
Max Patch
Copy patch and select New From Clipboard in Max.
Peter Castine's icon

Roman suggested
> [expr ( (($i1%7)==1)*2 + (($i1%7)==2)*(4 – ($i2!=0)) + (($i1%7)==3)*5 + (($i1%7)==4)*7 + (($i1%7)==5)*(9 – ($i2!=0)) + (($i1%7)==6)*(11 – ($i2!=0)) ) + (($i1/7)*12) + $i3]

Might I propose the following, more compact substitute?

[expr ($i1*12)/7]

Does essentially the same thing (integer arithmetic can do some truly magnificent things when you know how to use it!-) You can add an offset if you want a different scale.

The trick has been posted before.

Roman Thilenius's icon

that does not do the same as mine i think.

[expr ($i1*12)/7] gives you neither minor nor major.

for example, if the input is 1, [expr ($i1*12)/7] gives you 1, which is C#.

Roman Thilenius's icon

ok i se how it can work when you use expr ((($i1+1)*12)/7)-1 as basis - at least for natural minor.

it is not only shorter, it also allows to integrate the random() :)

Roman Thilenius's icon

sorry now i forgot about this.

[expr (((($i1+((($f2==1)*5)+1))*12)/7)-((($f2==1)*9)+1))+$i3] would be my final suggestion and is fairly optimized.

$2 is 0 1 for major minor and $3 the offset/transposition.

p.s.: shame over me. my own abstractions mostly use coll. :D