"normalizing" a value

bvdp bob at mellowood.ca
Thu Jul 2 03:49:34 CEST 2015


On Wednesday, July 1, 2015 at 6:27:57 PM UTC-7, rand... at fastmail.us wrote:
> On Wed, Jul 1, 2015, at 20:12, bvdp wrote:
> > Not sure what this is called (and I'm sure it's not normalize). Perhaps
> > "scaling"?
> > 
> > Anyway, I need to convert various values ranging from around -50 to 50 to
> > an 0 to 12 range (this is part of a MIDI music program). I have a number
> > of places where I do:
> > 
> >    while x < 0: x += 12
> >    while x >= 12: x -= 12
> 
> And this gives you what you want? With e.g. 13=1, 14=2, 22=10, 23=11,
> 24=0, 25 = 1, etc. Seems unusual that that's what you would want.
> 
> Also note this gives an 0 to 11 range for the results, not 0 to 12.
> 
> Anyway, x %= 12 will give the same results.

Thanks guys. Yes, that is exactly what I want. I have a number of places where a MIDI note value is being generated. MIDI should be 0..127, but the process creates notes outside the range. Guess that's another question: if the value I have is <0 or >127 I add/subtract 12 'til it's in range. Don't see using modulo working on this???

As far as the original question: Yes, that's what I need. At times I need to take a note (say 14) and map it into a single octave range. So, the 12 becomes 2. Both 14 and 2 are numeric values for note "d", just an octave apart.

Interesting that negative values translate properly. That's an non-intuitive result to me. Guess I should have studied that math stuff harder way back when!




More information about the Python-list mailing list