Skip to content

Instantly share code, notes, and snippets.

@CodeaLuis
Created September 28, 2013 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeaLuis/6745793 to your computer and use it in GitHub Desktop.
Save CodeaLuis/6745793 to your computer and use it in GitHub Desktop.
Sounds
---------------------------------
--Sound class
--clase importada 
---------------------------------
Sound = class()
function Sound:init()
    -- you can accept and set parameters here
      createPitchTable()
    
    
    song = {
        {x=100,n="e"},
        {x=200,n="g"},
        {x=300,n="c"},
        {x=500,n="E"},
        {x=600,n="G"},
        {x=700,n="C"},
    }
    
    for i,v in ipairs(song) do
        v.x = v.x * .8
        v.p = convertNoteToPitch(v.n)
        v.data = sound(ENCODE, { Waveform = SOUND_SINEWAVE, StartFrequency = v.p
            })
            
    end
    
end
function Sound:draw()
    -- Codea does not automatically call this method
       fill(bolass)
    local x =math.floor((ElapsedTime*WIDTH*.15)%WIDTH)
    local hh = HEIGHT/2
    for i,v in ipairs(song) do
        local s = 0
        if x > v.x then
            local d = math.abs(x-v.x)
            if d <200 then
                s = s + d
            else
                s = math.max(0, s+(400-d))
            end
    end
        --math.randomseed(s)
  --  fill(255,255,255,80)
        ellipse(v.x*1.7, v.p*940, s)
        if math.abs(x-v.x) < 6 then
            sound(DATA, v.data)
        end
    end
end
function createPitchTable()
    multiplier = 1.0296
    
    pitchTable = {1}
    pitch = pitchTable[1]
    semitoneModifier = 0
    
    gsNoteOrder = "CDEFGABcdefgab"
    gsTonalSystem = "22122212221221"
    -- There are 88 keys on a piano, so we will start from our highest note and go down.
    -- We calculate the notes ourselves and put them in a table. 
    for i = 88, 1, -1 do
        pitch = pitch / multiplier
        table.insert(pitchTable,1,pitch)
    end
end
 
function convertNoteToPitch(n)
    semitoneModifier = 0
                gsNoteBeingPlayed = n
            for j = 1, #gsNoteBeingPlayed do
                local currentChar = string.sub(gsNoteBeingPlayed,j,j)
                
                if currentChar == "_" then 
                    semitoneModifier = semitoneModifier - 1
                end
                
                if currentChar == "\^" then 
                    semitoneModifier = semitoneModifier + 1
                    currentChar = "%^"
                end
                -- NB need to implement naturals =
                
                
                -- if the current char is a note
                if string.find("abcdefg",string.lower(currentChar)) ~= nil then
                    
                    -- modify octave
                    -- search through the next characters for , and '
                    local nextCharIndex = 1
                    local nextChar = string.sub(gsNoteBeingPlayed,j+nextCharIndex,j+nextCharIndex)
                    if nextChar == "," then 
                        semitoneModifier = semitoneModifier - 12
                    end
                    if nextChar == "'" then 
                        semitoneModifier = semitoneModifier + 12
                    end
                    
                    pos = string.find(gsNoteOrder,currentChar)
                    local tonalModifier = string.sub(gsTonalSystem, 1, pos - 1)
                    
                    for i = 1, #tonalModifier do
        semitoneModifier = semitoneModifier + tonumber(string.sub(tonalModifier,i,i))
                    end
                    
                end
                            
            end
            
            pos = semitoneModifier + 44
        
            pitch = pitchTable[pos]
            
    return pitch
end
 
 
function play()
    local note = string.sub(gsNoteOrder,X,X)
    print(note)
    sound({
        Waveform = SOUND_SINEWAVE,
        StartFrequency = convertNoteToPitch(note) 
    })
end
function Sound:touched(touch)
    -- Codea does not automatically call this method
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment