OK - think I’ve figured out the pitch command. Pitch=1 is unchanged. Pitch=2 is an octave higher and pitch=0.5 is an octave lower.
Here is a demo using a simple piano setup. Play the keys or play one of the 3 demo songs. Adjust the tempo and sound used.
-- Simple Piano
-- by West
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
-- Use this function to perform your initial setup
function setup()
counter=0
songplay=0
whitenotes={1,3,5,6,8,10,12,13,15,17,18,20,22,24,25,27,29}
blacknotes={2,4,0,7,9,11,0,14,16,0,19,21,23,0,26,28}
--number corresponds to the white and black(#/flats) above ; 0 is a rest
songlist={}
table.insert(songlist,{song={1,0,0,3,5,0,0,1,5,0,1,0,5,0,0,0,3,0,0,5,6,6,5,3,6,0,0,0,0,0,0,0,
5,0,0,6,8,0,0,5,8,0,5,0,8,0,0,0,6,0,0,8,10,10,8,6,10,0,0,0,0,0,0,0,8,
0,0,1,3,5,6,8,10,0,0,0,0,0,0,0,10,0,0,3,5,6,8,10,12,0,0,0,0,0,0,0,
12,0,0,5,6,8,10,12,13,0,0,0,0,0,10,6,5,0,1,0,8,0,5,0,8,0,6,0,5,0,3,0,1,0,0,0,0,0,0,0},name="Do-Re-Me"})
table.insert(songlist,{song={8,0,8,0,10,0,12,0,8,0,12,0,10,0,0,0,8,0,8,0,10,0,12,0,8,0,0,0,7,0,0,0,
8,0,8,0,10,0,12,0,13,0,12,0,10,0,8,0,7,0,3,0,5,0,7,0,8,0,0,0,8,0,0,0
},name="Yankee Doodle"})
--yes the low G is an octave too high!
table.insert(songlist,{song={5,0,5,0,6,0,8,0,8,0,6,0,5,0,3,0,1,0,1,0,3,0,5,0,5,0,0,3,3,0,0,0,
5,0,5,0,6,0,8,0,8,0,6,0,5,0,3,0,1,0,1,0,3,0,5,0,3,0,0,1,1,0,0,0,
3,0,3,0,5,0,1,0,3,0,5,6,5,0,1,0,3,0,5,6,5,0,3,0,1,0,3,0,8,0,0,0,
5,0,5,0,6,0,8,0,8,0,6,0,5,0,3,0,1,0,1,0,3,0,5,0,3,0,0,1,1,0,0,0
},name="Ode to joy"})
songpos=1
notes={}
lastnote=0
blackplayed=0
tempo=9
pitch=1
numberofkeys=16
currentsong=2
currentinstrument=1
instrument={}
table.insert(instrument,{inst="Game Sounds One:Bell 2",name="Bell"})
table.insert(instrument,{inst="Game Sounds One:Pop 1",name="Pop"})
table.insert(instrument,{inst="Game Sounds One:Zapper 1",name="Zap"})
table.insert(instrument,{inst="A Hero's Quest:Hurt 4",name="Ouch"})
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
if songplay==0 then
sprite("Cargo Bot:Play Button",WIDTH*0.95,HEIGHT*0.975,0.1*WIDTH,-40)
else
sprite("Cargo Bot:Stop Button",WIDTH*0.95,HEIGHT*0.975,0.1*WIDTH,-40)
end
-- This sets the line thickness
strokeWidth(5)
fill(255)
--display song name
text(songlist[currentsong].name,WIDTH/2,HEIGHT*0.975)
--display current instrument
text(instrument[currentinstrument].name,WIDTH/2,HEIGHT*0.1)
--display tempo and tempo control
text("Tempo",WIDTH*0.95,HEIGHT*0.12)
text(21-tempo,WIDTH*0.91,HEIGHT*0.04)
sprite("Cargo Bot:Command Grab",WIDTH*0.95,HEIGHT*0.02,50,34)
sprite("Cargo Bot:Command Grab",WIDTH*0.95,HEIGHT*0.06,50,-34)
if counter>=tempo and songplay==1 then
if songlist[currentsong].song[songpos]>0 then
getPitch(songlist[currentsong].song[songpos])
end
counter=0
songpos = songpos + 1
if songpos>#songlist[currentsong].song then songpos=1 end
end
counter = counter + 1
for i=0,numberofkeys do
keywidth=WIDTH/numberofkeys
stroke(177, 173, 173, 255)
fill(240, 239, 239, 255)
rect(i*keywidth,200,keywidth,300)
end
for b=0,numberofkeys do
bkeywidth=WIDTH/numberofkeys
stroke(149, 149, 149, 255)
fill(48, 48, 48, 255)
if b~=2 and b~=6 and b~=9 and b~=13 then
rect(3*WIDTH/(numberofkeys*4)+b*bkeywidth,300,bkeywidth/2,200)
end
end
end
function touched(t)
--check black notes
blackplayed=0
for b=0,numberofkeys do
if t.x>3*WIDTH/(numberofkeys*4)+b*bkeywidth and t.x<3*WIDTH/(numberofkeys*4)+b*bkeywidth+bkeywidth/2 and t.y>300 and t.y<500 and b~=2 and b~=6 and b~=9 and b~=13 then
if t.state==BEGAN then
getPitch(blacknotes[b+1])
lastnote=blacknotes[b+1]
end
if t.state==MOVING and lastnote~=blacknotes[b+1] then
getPitch(blacknotes[b+1])
lastnote=blacknotes[b+1]
end
blackplayed=1
end
end
--check white notes
for i=0,numberofkeys do
keywidth=WIDTH/numberofkeys
if t.x>i*keywidth and t.x<(i+1)*keywidth and t.y>200 and t.y<500 and blackplayed==0 then
if t.state==BEGAN and lastnote==0 then
getPitch(whitenotes[i+1])
lastnote=whitenotes[i+1]
blackplayed=0
elseif t.state==MOVING and (whitenotes[i+1]~=lastnote and blackplayed==0) then
getPitch(whitenotes[i+1])
lastnote=whitenotes[i+1]
blackplayed=0
end
end
end
if t.state==ENDED or t.y>500 or t.y<200 then
lastnote=0
blackplayed=0
end
--start/stop button for song
if t.state==ENDED and t.x>WIDTH*0.9 and t.y>HEIGHT*0.9 then
songplay = songplay + 1
if songplay>1 then
songplay=0
songpos=1
end
end
--advance to next song
if t.state==ENDED and t.x<WIDTH*0.9 and t.y>HEIGHT*0.9 then
songpos=1
currentsong = currentsong + 1
if currentsong>#songlist then currentsong=1 songpos=1 end
end
--change instrument
if t.state==ENDED and t.y<HEIGHT*0.1 and t.x<WIDTH*0.9 then
currentinstrument = currentinstrument + 1
if currentinstrument>#instrument then
currentinstrument=1
end
end
-- change tempo
if t.state==ENDED and t.x>WIDTH*0.9 and t.y<HEIGHT*0.1 then
if t.y<HEIGHT*0.05 then
tempo = tempo +1
if tempo>20 then tempo=20 end
else
tempo = tempo - 1
if tempo<1 then tempo=1 end
end
end
end
function getPitch(n)
--octave shift
octaveshift=2
pitch=2^(((n-1)/12)-octaveshift)
--a pitch of 1 is the base pitch of the sound.
--a pitch of 2 is one octave higher
--a pitch of 0.5 is one octave lower
sound(instrument[currentinstrument].inst,1.0,pitch,0.0,false)
end