Skip to content

Instantly share code, notes, and snippets.

@tnlogy
Created February 4, 2013 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tnlogy/4707774 to your computer and use it in GitHub Desktop.
Save tnlogy/4707774 to your computer and use it in GitHub Desktop.
Notes in Codea
-- 4 play
-- Use this function to perform your initial setup
function setup()
displayMode(FULLSCREEN)
createPitchTable()
parameter.integer("X", 1,10,1,play)
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 createPitchTable()
multiplier = 1.0296
pitchTable = {1}
pitch = pitchTable[1]
semitoneModifier = 0
gsNoteOrder = "CDEFGABcdefgab"
gsTonalSystem = "22122212212221"
-- 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
-- This function gets called once y frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
local x =math.floor((ElapsedTime*WIDTH*.25)%WIDTH)
local hh = HEIGHT/2
for i,v in ipairs(song) do
local s = 20
if x > v.x then
local d = math.abs(x-v.x)
if d <400 then
s = s + d
else
s = math.max(20, s+(800-d))
end
end
ellipse(v.x, v.p*1000, s)
if math.abs(x-v.x) < 5 then
sound(DATA, v.data)
end
end
stroke(219, 3, 245, 255)
line(x,0,x,HEIGHT)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment