[Fwd: Re: [Tutor] Re: transposing musical sequences]

Lloyd Hugh Allen lha2@columbia.edu
Fri, 01 Mar 2002 18:46:00 -0500


From: Lloyd Hugh Allen <l.h.allen@mail.verizon.net>
Subject: Re: [Tutor] Re: transposing musical sequences
To: kp87@lycos.com

If you have notes stored as complex-valued numbers with the octave
stored as the real part of the variable and the pitch stored as the
complex part, how's this?

def normalize(complex_pitch):
	#print complex_pitch.real
	#print complex_pitch.imag
	complex_pitch = (complex_pitch + complex_pitch.imag//12) % 12j
	#print complex_pitch
	return complex_pitch

(remove the # for debugging) (if only I knew how to access the real and
imaginary components as integers without sending them through
float-land--although that shouldn't be an issue) (Also, please note that
this is Python 2.2 code--the // breaks on earlier) (the \ is only for
line continuation) (enough extra comments).

The original incarnation was 

	complex_pitch = (complex_pitch.real+complex_pitch.imag//12 + \
			 complex_pitch.imag%12 * 1j)

kevin parks wrote:
> 
> Hi. I sent this message to the list a while back and Danny ways kind enough to answer. Now i have a very different question.
> 
> In computer music there are several ways to represent musical pitch. I am working with several of these. One such way is related to the bottom function. It is called octave point pitch class because the first number represents the octave followed by a dot and the next numbers represent the pitch class (there are 12: 0->11). All items (added or subtracted) are modulo 12 (%12)
> 
> 7.11  # B octave 7
> 8.00  # C octave 8  THIS HERE IS MIDDLE C (a.k.a C4), MIDI: 60, ca.261.626 Hz....
> 8.01  # C#/Db octave 8
> 8.02  # D octave 8
> .
> .
> .
> 8.11  # B octave 8
> 9.00  # C octave 9  (here we go again)
> 
> So my question is this. The little fucntion below works ok for the pitch class (c,c#,d,d#,e,f,g,g#,a,a#,b) part, but...
>              0,1,2,3,4,5,6,7,8, 9,10,11
> 
> I now need to add a part that takes care of the octave business. so that if i go below zero (transpose down) or above 11 (transpose up) it increments or decrements the octave number. I am not sure how to add this part without doing a test on the transposition factor and calulating how many 12s fit in it, etc. In otherwords how do i do this more efficently without having lots of flags and conditionals and test on each invocation. I just know that there has to be a better way...
> 
> I suppose that the best way would be to make this a pitch class and have Xpose be a method (and add other methods, later...) musical pitch, like ratios, fractions, points (x, y), i suppose, are prime candidates for classes. But I am *just* getting to the part about objects and classes and methods in my python studies....
> 
> but i am guessing somthing like (vague & imaginary fake python code w/ syntax errors sure to follow):
> 
> >>> z=[pch(8.01), pch(8.05), pch(7.07), pch(6.00)]
> >>> x = pch.xpose([1, 2, 3], 11)
> >>> foo = pch.tomidi(x)
> >>> bar = pch.tocps(x)
> 
> etc. & co.....is eventually where this is heading...
> 
> If you can make any heads or tails of this question, please help spoon feed this newbie an answer. OOP answers and vanilla examples both welcome, most important is getting a working engine *and* a new undertanding of how to approach the problem.
> 
> Thank you muchly for tolerating all my dumb questions over this
> winter break.
> 
> best,
> 
> kevin parks
> seoul, korea
> 
> >def Xpose(seq,n, oct=12, sort_flag=0):
> >    ''' take a sequence and tranpose it modulo some number
> >    '''
> >    mylist=[]
> >    for i in seq:
> >        x = (i+n)%oct
> >        mylist.append(x) # to append or extend?
> >    if sort_flag:
> >        mylist.sort() # to sort or not sort
> >    return mylist
> >
> >#-- (i am at an internet cafe with no interpreter so there could
> ># be a type, but i hope not, Imaginary session follows:
> >
> >>>> x = [0, 2, 4, 5, 7, 9, 11]
> >>>> c = Xpose(x, 4, 12, 0)
> >>>> c
> >[4, 6, 8, 9, 11, 1, 3]
> >>>> d = Xpose(x, 4, 12, 1) # sorted now
> >>>> d
> >[1, 3, 4, 6, 8, 9, 11]
> >
> ># cool. It works, and just in case you are curious i just turned a
> ># C major scale into E major
> 
> 2,000,000,000 Web Pages--you only need 1. Save time with My Lycos.
> http://my.lycos.com
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor