[Csnd] note/pitch quantisation to scale

Hey hey,
I'd like to "quantise" both MIDI notes and frequencies to musical scales. I suppose cpstun is a tool for the latter.

But is there a good realtime capable method to quantise MIDI notes to a MIDI note scale? I thought about a table lookup with 127 elements, which is created from some kind of short description.

Does anyone have ideas on these topics or a good pointer? Most of my research discovered such features in DAWs.

Best wishes and many thanks,


Not sure I understand what you need but a lookup table to take MIDI number to frequency is fast and simple.

If on the other hand you have a frequency and you want to quantise it to a scale then a table can be searched to find the nearest value, either linear search or binary.

Get TypeApp for Android

how would you do this search ("either linear search or binary")?

Not knowing about cpstun, I just calculated:
ifreq = girats[giscale[idegree]] * 2 ^ ioct * gifund ; NOT ‘ifreq mtof ikey’
where ‘girats’ was an array of just-tuning ratios for one octave (one for each ‘idegree’ of a scale), and ioct was the octave of the midi note being played.

The degree was midi 12, the octave (int (midi/12)) -- Or int(midi 8), if I’d simplified a scale down to 7 degrees mapped to the white keys, say. (Convenient for a pianistically-impaired player like me): gidegree[] fillarray 0, 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6

Any help?

Thanks all. I'm going with the lookup table for MIDI notes.

For pitches I'm slowly leaning towards one solution, but it will be limited to the western scale. It's not completely worked out yet, but:
iPch = cpspch(iFreqInHz)
iOct = int(iPch)
iOriginalDegree = frac(iPch)
iQuantisedNote = int(iOriginalDegree * 100) ; (ranges 0-11.x)
Then use a lookup table, create a new pch pitch and convert back to cps. Does that sound reasonable?

Best wishes,


Dec 31 2021, Forrest Curo has written:

The method I’ve been using for a few years in the Implication Organ, which supports switching between multiple tunings, is to define each tuning using GEN51:

giWTP = ftgen(201, 0, 128, -51,
12, 2, 297.989, 63,
1, 567/512, 9/8, 147/128, 21/16, 1323/1024, 189/128, 3/2, 49/32, 7/4, 441/256, 63/32, 2)

and then use tablekt to convert incoming MIDI notes into frequencies

kfreq = tablekt(knote, gk_tuning)

This has been working quite well for me. Complete source code here: https://github.com/DaveSeidel/music-src/tree/master/implication-organ

To be clear, the variable “gk_tuning” points to one of the GEN51-generated tables of frequency values, and “knote” in the MIDI notes number.