Skip to content

Instantly share code, notes, and snippets.

@fredbogg
Created November 6, 2011 16:48
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 fredbogg/1343158 to your computer and use it in GitHub Desktop.
Save fredbogg/1343158 to your computer and use it in GitHub Desktop.
A polyphonic diatonic keyboard for Codea / Codify
-- Scale.  This script makes a diatonic keyboard that plays polyphonically
-- By Fred.
-- v1.0
-- Use this function to perform your initial setup
function setup()
    
    notes={c3=85,d3=321,e3=48,f3=63,g3=60,a3=68,b3=11,c4=84}
    
    scale={"c3","d3","e3","f3","g3","a3","b3","c4"}
    numSteps=8
    
    for key, value in pairs(notes) do
        print("notes."..key.."="..value)
    end
    
    rectMode(CENTER)
    touches={}
    
end
function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
    else
        touches[touch.id] = touch
    end
end
-- This function gets called once every frame
function draw()
    
    heldNotes={}
    
    for k,touch in pairs(touches) do
        
        x = touch.x
        x = math.ceil(x/WIDTH*numSteps)
       
        if x>0 then
            table.insert(heldNotes,x)
            playSound()
        end
    end
         -- Draw keys   
        background(10,10,20)
        fill(255, 255, 255, 255)
        stroke(0, 0, 0, 255)
        strokeWidth(2)
        -- white notes
        for i=1,numSteps do
            fill(255, 255, 255, 255)
            rect((i-1)*(WIDTH/numSteps),HEIGHT-300,WIDTH/numSteps,300)
        end
        -- red notes if played
        for i, v in ipairs(heldNotes) do
            fill(211, 74, 86, 255)
            rect((v-1)*(WIDTH/numSteps),HEIGHT-300,WIDTH/numSteps,300)
        end
        -- black notes for show only at this stage:)
        for i=1,numSteps do
            if i ~= 3 and i ~= 7 then
                fill(0, 0, 0, 255)
                rectMode(CENTER)
                rect((i-1)*(WIDTH/numSteps)+(WIDTH/numSteps),HEIGHT-50,WIDTH/(numSteps*1.3),200)
            end
        end
end
function playSound()
    
    for key, value in pairs(heldNotes) do
        keyPressed=scale[x]
        sound(SOUND_BLIT,notes[keyPressed])
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment