"normalizing" a value

bvdp bob at mellowood.ca
Thu Jul 2 19:03:41 CEST 2015


On Wednesday, July 1, 2015 at 8:37:18 PM UTC-7, Dennis Lee Bieber wrote:
> On Wed, 1 Jul 2015 18:49:34 -0700 (PDT), bvdp <bob at mellowood.ca> declaimed
> the following:
> 
> >
> >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.
> >
> 
> Modulo will give you the "note", but throws out the octave.
> 
> Your original post specified -50..50, which spans only 101 values, whereas
> MIDI spans 128 values -- so what do you really consider out of range? And
> what MIDI value is -50 supposed to map against. Is input 0 supposed to
> represent middle-C with negative values being notes on the bass clef and
> positive values being treble clef?
> 
> MIDI middle-C is note 60. Presuming your 0 is supposed to be middle-C, I'd
> do the transformation as:
> 
> midiNote = invalue + 60
> if midiNote < 0: midiNote = midiNote % 12
> if midiNote > 127: midiNote = (midiNote % 12) + 115
> 
> which actually means input values of -60..+67 are shifted directly to a
> midi note number, and values outside of that range are shadowed as the
> lowest or highest octave.
> 
> >>> for i in range(-70, 80, 4):
> ... 	midiNote = i + 60
> ... 	if midiNote < 0: midiNote = midiNote % 12
> ... 	if midiNote > 127: midiNote = ((midiNote - 5) % 12) + 113
> ... 	print i, midiNote, "CcDdEFfGgAaB"[midiNote % 12], divmod(midiNote,
> 12)
> ... 
> -70 2 D (0, 2)
> -66 6 f (0, 6)
> -62 10 a (0, 10)
> -58 2 D (0, 2)
> -54 6 f (0, 6)
> -50 10 a (0, 10)
> -46 14 D (1, 2)
> -42 18 f (1, 6)
> -38 22 a (1, 10)
> -34 26 D (2, 2)
> -30 30 f (2, 6)
> -26 34 a (2, 10)
> -22 38 D (3, 2)
> -18 42 f (3, 6)
> -14 46 a (3, 10)
> -10 50 D (4, 2)
> -6 54 f (4, 6)
> -2 58 a (4, 10)
> 2 62 D (5, 2)
> 6 66 f (5, 6)
> 10 70 a (5, 10)
> 14 74 D (6, 2)
> 18 78 f (6, 6)
> 22 82 a (6, 10)
> 26 86 D (7, 2)
> 30 90 f (7, 6)
> 34 94 a (7, 10)
> 38 98 D (8, 2)
> 42 102 f (8, 6)
> 46 106 a (8, 10)
> 50 110 D (9, 2)
> 54 114 f (9, 6)
> 58 118 a (9, 10)
> 62 122 D (10, 2)
> 66 126 f (10, 6)
> 70 118 a (9, 10)
> 74 122 D (10, 2)
> 78 114 f (9, 6)
> >>> 
> 
> The oddity for the >127 case is that 127 is not an even octave break point.
> divmod() gives you the "octave" and the note.
> -- 
> 	Wulfraed                 Dennis Lee Bieber         AF6VN
>     wlfraed at ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Thanks. I like your solution for values <0 >127. Makes more sense than mindlessly adding/subtracting 12 to bring into range. In this case I'm just trying to take an out-of-range note to the top/bottom of the valid midi range.


More information about the Python-list mailing list