[Tutor] Re: transposing musical sequences

kevin parks kp87@lycos.com
Sat, 02 Mar 2002 21:08:15 +0900


I don't have an interpreter here so i can't actually post
working code, but i know that at the heart of my problem i can
just use %12 to extract the pitch and /12 to get to the octave part

octpch is the base note (the note we are starting with, eg 8.00
n is the number of half steps we want to transpose up or down

xpose(octpch, n):
    oct = octpch # something here to separate the stuff before the dot
    pch = octpch # followed by something code here to split the part after the dot
    pch = ((pch +n) % 12/100) #update the pitch
    oct = (oct +n) /12   # update the oct part
    return (the two items joined back together

Also i am guessing that transposing down somehow involves the 12s compliment
(12-n)

maybe down is (12-n)% 12

I am at a net cafe so i have to wait till i get home to try this ideas out. The came
to me last night *after* i sent my query! (it started to perculate in my brain as i waited
to get to sleep)

Sorry for all the thinking out loud here....

cheers,

kevin


---


On Sat, 02 Mar 2002 03:21:36  
 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
>


2,000,000,000 Web Pages--you only need 1. Save time with My Lycos.
http://my.lycos.com