Created
November 3, 2011 10:48
-
-
Save fredbogg/1336235 to your computer and use it in GitHub Desktop.
Play music in Codea / Codify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Play Music. This plays polyphonically, using the draw function and a frame counter for timing. | |
-- By Fred. | |
-- Use this function to perform your initial setup | |
function setup() | |
-- initialise variables and clear screen | |
frame = 0 | |
measure = 1 | |
tuneLength = 0 | |
background(0, 0, 0, 0) | |
-- This slidable parameter adjusts the number of frames between musical measures. | |
-- It controls the speed of the music. | |
iparameter("pauseAmount",0,100,10) | |
-- These are the names of the notes and their (trial and error) equivalent seed numbers | |
-- for the sound() function. The digit tells us which octave it is. | |
-- Further mapping of the seed address space would expand our range and improve tonal quality. | |
notes = {b2=118,c3=85,d3=321,e3=48,f3=63,g3=60,a3=68,b3=11,c4=84} | |
-- This is the tune we want to play, in a multidimensional array. | |
-- Each element of the table is a table of what notes should be played for each | |
-- sequential measure. This allows chords, and rests as an empty table. | |
-- Minuet in G by JS Bach (transposed to C) | |
tune={{"c3","e3","g3"},{},{"c3"},{"d3"},{"e3"},{"f3"},{"c3","e3","g3"},{},{"c3"},{},{"c3"},{},{"c3","f3","a3"},{},{"f3"},{"g3"},{"a3"},{"b3"},{"c3","e3","g3","c4"},{},{"c3"},{},{"c3"},{},{"b2","d3","f3"},{},{"g3"},{"f3"},{"e3"},{"d3"},{"c3","e3"},{},{"f3"},{"e3"},{"d3"},{"c3"},{"b2","d3"},{},{"c3","e3"},{"d3"},{"c3"},{"b2"},{"c3"}} | |
-- List the notes available and their assigned seeds | |
print("These are the notes and sound() seeds.") | |
for key, value in pairs(notes) do | |
print("notes."..key.." = "..value) | |
end | |
-- Count the length of the tune so that later we don't crash at the end. | |
for key, value in pairs(tune) do | |
tuneLength = tuneLength + 1 | |
end | |
end | |
-- This function gets called once every frame | |
function draw() | |
-- If the number of frames specified by the pauseAmount slider has passed, we play the next | |
-- element in the tune table. This is the next musical measure. This works better than | |
-- trying implement a pause function that holds up the whole program. | |
if frame % pauseAmount == 0 and measure <= tuneLength then | |
readMusic() | |
-- Reset frame counter rather than going to infinity and beyond. | |
frame = 0 | |
end | |
frame = frame + 1 | |
end | |
-- Start the music again if touched. | |
function touched(touch) | |
if CurrentTouch.state == BEGAN then | |
measure = 1 | |
end | |
end | |
function readMusic() | |
-- Based on the current measure count, find the table of notes to be played now from the | |
-- tune table. Because we are using pairs, the notes making up this chord will not | |
-- necessarily be played in order, but it's pretty quick. | |
for key, value in pairs(tune[measure]) do | |
-- The seed given here to the sound() function determines the pseudorandom | |
-- qualities of the sound. The notes table approximates the notes of a scale | |
-- with certain seed numbers. Your listening mileage may vary. | |
sound(SOUND_BLIT,notes[value]) | |
end | |
-- Increment our measure count so next time we play the following measure of music. | |
measure = measure + 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment