Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93410

Re: "normalizing" a value

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: "normalizing" a value
Date 2015-07-01 23:36 -0400
Organization IISS Elusive Unicorn
References <fd370a7b-ea6a-48e5-90f4-9d86b89a745e@googlegroups.com> <mailman.230.1435800465.3674.python-list@python.org> <948e2800-e52b-429a-9e00-f268fcff5085@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.233.1435808221.3674.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, 1 Jul 2015 18:49:34 -0700 (PDT), bvdp <bob@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@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

"normalizing" a value bvdp <bob@mellowood.ca> - 2015-07-01 17:12 -0700
  Re: "normalizing" a value MRAB <python@mrabarnett.plus.com> - 2015-07-02 01:30 +0100
  Re: "normalizing" a value Paul Rubin <no.email@nospam.invalid> - 2015-07-01 17:36 -0700
  Re: "normalizing" a value Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-02 01:06 +0000
  Re: "normalizing" a value random832@fastmail.us - 2015-07-01 21:27 -0400
    Re: "normalizing" a value bvdp <bob@mellowood.ca> - 2015-07-01 18:49 -0700
      Re: "normalizing" a value random832@fastmail.us - 2015-07-01 22:23 -0400
        Re: "normalizing" a value bvdp <bob@mellowood.ca> - 2015-07-01 19:41 -0700
      Re: "normalizing" a value Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-07-01 23:36 -0400
        Re: "normalizing" a value bvdp <bob@mellowood.ca> - 2015-07-02 10:03 -0700
  Re: "normalizing" a value Steven D'Aprano <steve@pearwood.info> - 2015-07-02 12:15 +1000
    Re: "normalizing" a value bvdp <bob@mellowood.ca> - 2015-07-01 19:42 -0700
  Re: "normalizing" a value Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-07-02 05:41 +0100
  Re: "normalizing" a value Skip Montanaro <skip.montanaro@gmail.com> - 2015-07-02 10:05 -0500

csiph-web