Wiki
Clone wikiCore / sound
Back to Beyond the Codea in-app reference
sound() function
Introduction
The 'Sounds' chapter of Codea's in-app reference documents most of the sound()
function. The function takes one or two parameters and, depending on their values, either plays a sound or encodes a table of parameters into a string that is returned by the function.
sound("encode", parameterTable)
If there are two parameters and the first parameter is "encode"
, the function sound()
assumes that the second parameter is a table specifying the parameters to use when generating a sound. It encodes the parameters in the table into a string that can be used with sound("data", parameterString)
.
The Codea API provides a global variable ENCODE
that refers to the string "encode"
.
For example:
supportedOrientations(LANDSCAPE_ANY)
function setup()
-- Add parameters. Function 'changed' is called when they are created or if they change
parameter.integer("waveform", 0, 3, 0, changed)
parameter.number("attackTime", 0, 3, 0.5, changed)
parameter.number("sustainTime", 0, 3, 1, changed)
parameter.number("sustainPunch", 0, 3, 0.0, changed)
parameter.number("decayTime", 0, 3, 0.4, changed)
parameter.number("startFrequency", 0, 2, 0.9, changed)
parameter.number("minFrequency", 0, 1, 0, changed)
parameter.number("slide", 0.9, 1, 0.96, changed)
parameter.number("deltaSlide", 0, 1, 0.6, changed)
parameter.number("vibratoDepth", 0, 1, 0.5, changed)
parameter.number("vibratoSpeed", 0, 1, 0.5, changed)
parameter.number("changeAmount", 0, 1, 0.6, changed)
parameter.number("changeSpeed", 0, 1, 0.6, changed)
parameter.number("squareDuty", 0, 1, 0.5, changed)
parameter.number("dutySweep", 0, 1, 0.5, changed)
parameter.number("repeatSpeed", 0, 1, 0.5, changed)
parameter.number("phaserSweep", 0, 1, 0.5, changed)
parameter.number("lowpassFilterCutoff", 0, 1, 0.5, changed)
parameter.number("lowpassFilterCutoffSweep", 0, 1, 0.5, changed)
parameter.number("lowpassFilterResonance", 0, 1, 0.5, changed)
parameter.number("highpassFilterCutoff", 0, 1, 0.5, changed)
parameter.number("highpassFilterCutoffSweep", 0, 1, 0.5, changed)
parameter.number("volume", 0, 1, 1.0, changed)
isSetup = true
changed() -- Initialise the parameterString
textWrapWidth(WIDTH * 0.9)
fontSize(32)
fill(255)
end
function draw()
background(0)
text(parameterString, WIDTH / 2, HEIGHT / 2)
end
function changed()
if not isSetup then return end -- All variables need to exist
parameterTable = {
Waveform = waveform,
AttackTime = math.pow(attackTime - 0.5, 3),
SustainTime = math.pow(sustainTime - 0.5, 2),
SustainPunch = math.pow(sustainPunch, 2),
DecayTime = decayTime - 0.5,
StartFrequency = math.pow(startFrequency, 2),
MinimumFrequency = minFrequency,
Slide = math.pow(slide, 5) - 0.5,
DeltaSlide = deltaSlide - 0.5,
VibratoDepth = math.pow(vibratoDepth - 0.5, 3),
VibratoSpeed = vibratoSpeed - 0.5,
ChangeAmount = changeAmount - 0.5,
SquareDuty = squareDuty - 0.5,
DutySweep = dutySweep - 0.5,
RepeatSpeed = repeatSpeed - 0.5,
PhaserSweep = phaserSweep - 0.5,
LowPassFilterCutoff = 1 - math.pow(lowpassFilterCutoff, 3),
LowPassFilterCutoffSweep = math.pow(lowpassFilterCutoffSweep - 0.5, 3),
LowPassFilterResonance = lowpassFilterResonance - 0.5,
HighPassFilterCutoff = math.pow(highpassFilterCutoff, 3),
HighPassFilterCutoffSweep = math.pow(highpassFilterCutoffSweep - 0.5, 3),
Volume = volume
}
parameterString = sound(ENCODE, parameterTable)
end
sound("decode", ...)
As of Codea version 1.5.1, does nothing and returns nil
.
The Codea API provides a global variable DECODE
that refers to the string "decode"
.
Updated