Computer Science Canada

Fixing turing's Music.Play

Author:  beard0 [ Mon Oct 31, 2005 4:36 pm ]
Post subject:  Fixing turing's Music.Play

Got the idea for this from an old topic that was brought up again, but implimented it in a more useful way.

I made a module containing Play that is compatible with Turing's Music.Play, but doesn't cut off the last note. It also adds some features.
note: to acheive dotted ntoes, use the [x,y] fraction notation that I have defined. Eg. dotted quarter = 1/4 + 1/2(1/4) = 1/4 + 1/8 = 3/8, therefore, use [3,8].

code:
module MyMusic
    import Str, Music
    export SetZero, SetWhole, Play, BGPlay
    var zeroNote : nat := 262
    var octave := 0
    var whole : nat := 1000
    var curDuration := 1 / 4
    var waiting := ""

    fcn note (x : int) : int
        result round (zeroNote * 2 ** (x / 12 + octave))
    end note

    proc SetZero (n : nat)
        zeroNote := n
    end SetZero

    proc SetWhole (n : nat)
        whole := n
    end SetWhole

    proc Play (m : string)
        var n : int
        var doplay := false
        var i : int := 0
        loop
            i += 1
            exit when i > length (m)
            doplay := false
            if m (i) = ">" then
                octave += 1
            elsif m (i) = "<" then
                octave -= 1
            elsif Str.Upper (m (i)) = "C" then
                n := 0
                doplay := true
            elsif Str.Upper (m (i)) = "D" then
                n := 2
                doplay := true
            elsif Str.Upper (m (i)) = "E" then
                n := 4
                doplay := true
            elsif Str.Upper (m (i)) = "F" then
                n := 5
                doplay := true
            elsif Str.Upper (m (i)) = "G" then
                n := 7
                doplay := true
            elsif Str.Upper (m (i)) = "A" then
                n := 9
                doplay := true
            elsif Str.Upper (m (i)) = "B" then
                n := 11
                doplay := true
            elsif Str.Upper (m (i)) = "P" then
                Time.Delay (round (curDuration * whole))
            elsif m (i) = "1" then
                curDuration := 1
            elsif m (i) = "2" then
                curDuration := 1 / 2
            elsif m (i) = "4" then
                curDuration := 1 / 4
            elsif m (i) = "8" then
                curDuration := 1 / 8
            elsif m (i) = "6" then
                curDuration := 1 / 16
            elsif m (i) = "[" then
                if i + 4 <= length (m) and index (m (i + 1 .. *), "]") - 1 > index (m (i + 1 .. *), ",") and index (m (i + 1 .. *), ",") > 1 then
                    if strintok (m (i + 1 .. i + index (m (i + 2 .. *), ","))) and strintok (m (i + index (m (i .. *), ",") .. i + index (m (i + 2 .. *), "]"))) then
                        curDuration := strint (m (i + 1 .. i + index (m (i + 2 .. *), ","))) / strint (m (i + index (m (i .. *), ",") .. i + index (m (i + 2 .. *), "]")))
                        i += index (m (i + 1 .. *), "]")
                    end if
                end if
            end if
            if i + 1 <= length (m) and m (i + 1) = "+" then
                n += 1
                i += 1
            elsif i + 1 <= length (m) and m (i + 1) = "-" then
                n -= 1
                i += 1
            end if
            if doplay then
                Music.Sound (note (n), round (curDuration * whole))
            end if
        end loop
    end Play

    process BGPlay (score : string)
        var callnum := Rand.Int (0, 99999)
        loop
            exit when index (waiting, "[" + intstr (callnum) + "]") = 0
            callnum := Rand.Int (0, 99999)
        end loop
        waiting += "[" + intstr (callnum) + "]"
        loop
            exit when index (waiting, "[" + intstr (callnum) + "]") = 1
        end loop
        Play (score)
        waiting := waiting (length (intstr (callnum)) + 3 .. *)
    end BGPlay

end MyMusic

MyMusic.Play ("6>gpf+p8f[3,8]e-6p8e")
MyMusic.Play ("8p<g+a>4c<8a>cd<")
MyMusic.Play ("4p8>g6f+p8f4e-8e")
MyMusic.Play ("6p[5,16]>c8c2c<")

Author:  jamonathin [ Tue Nov 01, 2005 8:34 pm ]
Post subject: 

I dont have sound at school nor Turing at home, so i cant really test it, but i know what you mean about cutting off the last note. Everything in your code looks good, and its good to see that you added in dotted notes, gj Smile

Author:  [Gandalf] [ Wed Nov 02, 2005 12:28 am ]
Post subject: 

Yep, very nice code, and very functional. Think it's possible to add things like ties and slurs now?
It's too bad we are stuck with messing around with frequencies when we could be handling midis, making nicer music. But, hey, what's one more thing to complain about, eh? Smile

Author:  beard0 [ Wed Nov 02, 2005 3:57 pm ]
Post subject: 

[Gandalf] wrote:
Think it's possible to add things like ties and slurs now?


To achieve a tie, just add the two note length values, and use [x,y].
Acheiving slurs would be a little more difficult... the only thing I can think to do would be to make regular notes a little stacattoed, by placing small rests between them, and removing the rests for slurs. To actually slur wouldn't really be possible, as we're dealing with frquencies, unless what you're after is a trombone-style slur, which might be kinda fun.


: