Hey all!
I’m currently working on a theremin in Codea. This is not a serious app, but it did teach me a lot about codea… Especially sound. Anyway, here is my code for main:
--Theremin
--by Brooks MacBeth
--started on March 23, 2012
--current form finished March 28, 2012
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
print("This is my Theremin... enjoy!!!")
-- This is where the multi-touches get stored...
touches = {}
-- Just setting up the text positions...
textSize(50)
textAlign(CENTER)
end
-- Copied this from the multi-touch template given to us...
function touched(touch)
if touch.state == ENDED then
touches[touch.id] = nil
else
touches[touch.id] = touch
end
end
function drawBackground()
stroke(255, 255, 255, 255)
fill(255, 255, 255, 255)
line(WIDTH/2, 0, WIDTH/2, HEIGHT)
text("Volume", WIDTH/4, HEIGHT - 55)
text("Pitch" , WIDTH/4 * 3, HEIGHT - 55)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
drawBackground()
-- Detect the touches and produce the sounds...
volume = nil
pitch = nil
for k,touch in pairs(touches) do
-- Use the touch id as the random seed
math.randomseed(touch.id)
-- This ensures the same fill color is used for the same id
fill(math.random(255),math.random(255),math.random(255))
-- Draw ellipse at touch position
ellipse(touch.x, touch.y, 100, 100)
if touch.x < WIDTH/2 then
volume = touch.y/HEIGHT
else
pitch = touch.y/HEIGHT
end
Sound:make(pitch, volume)
end
end
And for the sound class…
Sound = class()
function Sound:init(x)
-- you can accept and set parameters here
self.x = x
end
function Sound:make(freq, vol)
if freq == nil then
freq = nil
end
if vol == nil then
vol = 0.5
end
sound({StartFrequency = freq,
AttackTime = 0,
SustainTime = 0.14,
SustainPunch = 0,
DecayTime = 0,
MinimumFrequency = 0,
Slide = 0.5,
DeltaSlide = 0,
VibratoDepth = 0.5,
VibratoSpeed = 0.5,
ChangeAmount = 0,
ChangeSpeed = 0,
SquareDuty = 0,
DutySweep = 0,
RepeatSpeed = 0,
PhaserSweep = 0,
LowPassFilterCutoff = 1,
LowPassFilterCutoffSweep = 0.5,
LowPassFilterResonance = 0.5,
HighPassFilterCutoff = 0.5,
HighPassFilterCutoffSweep = 0.5,
Waveform = 2,
Volume = vol})
end
function Sound:draw()
-- Codea does not automatically call this method
end
function Sound:touched(touch)
-- Codea does not automatically call this method
end
Please let me know what you think. I used the Newton Cradle project posted on here not too long ago to get the first sounds produced, and then after playing with the sound slider project I got to the sound it produces now. I still wand a better, smoother sound throughout, but i think it sounds pretty good right now.
Brooks