While I was fiddling around with getmetatable() on various Codea metatables, I discovered quite a few undocumented and unmentioned functions of sound() instances. When I run a debug print of all its values, it says this:
fadeTo(volume, time) - tweens the volume to the specified over the time specified
panTo(pan, time) - tweens the pan to the specified over the time specified
pitchTo(pitch, time) - tweens the pitch to the specified over the time specified
rewind(?) - doesn't seem to take parameters, just stops the sound.
stop() - stops the sound
stopActions() - stops all tweens
stopFade() - stops volume tweening
stopPan() - stops pan tweening
stopPitch() - stops pitch tweening
Here’s the code I used to find them:
-- Metatesting
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
parameter.action("Play Normal", function()
if obj ~= nil then
obj:stop()
end
obj = sound("Game Sounds One:Wind 1")
end)
parameter.action("Play Edited", function()
if obj ~= nil then
obj:stop()
end
obj = sound("Game Sounds One:Wind 1")
obj.volume = 1
obj:fadeTo(0, 2)
tween.delay(1.5, function()
obj:stopFade()
end)
end)
parameter.action("Stop Sounds", function()
if obj ~= nil then
obj:stop()
end
end)
local obj = sound("Game Sounds One:Wind 1")
obj:stop()
local str = "tostring = \"" .. tostring(obj) .. "\"\
type = \"" .. type(obj) .. "\"\
getmetatable =\
\
"
for k, v in pairs(getmetatable(obj)) do
str = str .. k .. ": " .. tostring(v) .. "\
"
end
print(str)
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
end
@SkyTheCoder Awesome, you’ve got an inquisitive mind haven’t you. I’ve been using tween.sequence to replicate pitchTo(), do they all work as described?
@NatTheCoder These functions aren’t documented anywhere and even Simeon and John themselves down at TLL didn’t say anything about them. How did you or anyone else know them?
@JakAttak (in response to first comment) You can’t tween metatables, can you?
@SkyTheCoder nice find, I forgot we implemented these (and obviously forgot to document them). They are missing some error checking, but basically they work as you describe. Also note that these APIs might change in the future.
Is there any way to buffer the sound/music? I managed to make kind of a launchpad, but there is a little gap between the loops… Anyway, it sounds great!
@Majormorgan Here’s an example that starts the sound when you tap the screen, then stops the sound when you tap the screen again. Slide to parameter switch to use a timer. The sound will start at 100 and stop at 200. Tapping the screen while the timer is running will reset the count and stop the sound if it’s playing.
function setup()
parameter.boolean("use_timer",false,set)
cnt=0
end
function draw()
background(40, 40, 50)
fill(255)
if use_timer then
text("sound starts at 100",WIDTH/2,HEIGHT-150)
text("sound stops at 200",WIDTH/2,HEIGHT-175)
text("tap screen to reset count",WIDTH/2,HEIGHT-220)
cnt=cnt+1
text("count "..cnt,WIDTH/2,HEIGHT-100)
if cnt==100 then
s=sound("Game Sounds One:Crowd Cheer")
elseif cnt==200 then
s:stop()
s=nill
end
else
if s==nil then
text("Tap screen to start the sound",WIDTH/2,HEIGHT/2)
else
text("Tap screen to stop the sound",WIDTH/2,HEIGHT/2)
end
end
end
function set()
cnt=0
if s~=nil then
s:stop()
end
s=nil
end
function touched(t)
if t.state==BEGAN then
if not use_timer then
if s==nil then
s=sound("Game Sounds One:Crowd Cheer")
else
s:stop()
s=nil
end
else
set()
end
end
end
@Majormorgan I know that this is completely irrelevant, but you should start getting into the habit of keeping everything properly indented. It really does help a lot