clean up a-pong

This commit is contained in:
Robin Gareus 2016-08-21 01:36:50 +02:00
parent 31ad7236b0
commit d5f55246cc

View file

@ -20,29 +20,30 @@ function dsp_params ()
} }
end end
local gametime local fps -- audio samples per game-step
local fps local game_time -- counts up to fps
local ball_x, ball_y local ball_x, ball_y -- ball position [0..1]
local dx, dy local dx, dy -- current ball speed
local pingsound local lost_sound -- audio-sample counter for game-over [0..3*fps]
local lotsound local ping_sound -- audio-sample counter for ping-sound [0..fps]
local pingnote local ping_phase -- ping note phase
local ping_pitch
function dsp_init (rate) function dsp_init (rate)
self:shmem ():allocate (4) self:shmem ():allocate (4)
self:shmem ():clear () self:shmem ():clear ()
fps = rate / 25 fps = rate / 25
pingnote = 352 / rate ping_pitch = 752 / rate
ball_x = 0.5 ball_x = 0.5
ball_y = 0 ball_y = 0
dx = 0.011 dx = 0.011
dy = 0.021 dy = 0.0237
end end
function dsp_configure (ins, outs) function dsp_configure (ins, outs)
gametime = fps game_time = fps -- start the ball immediately (notfiy GUI)
pingsound = fps ping_sound = fps -- set to end of synth cycle
lostsound = 3 * fps lost_sound = 3 * fps
end end
function dsp_run (ins, outs, n_samples) function dsp_run (ins, outs, n_samples)
@ -50,34 +51,38 @@ function dsp_run (ins, outs, n_samples)
local shmem = self:shmem () local shmem = self:shmem ()
local state = shmem:to_float (0):array () -- "cast" into lua-table local state = shmem:to_float (0):array () -- "cast" into lua-table
local changed = false local changed = false -- flag to notify GUI on every game-step
gametime = gametime + n_samples game_time = game_time + n_samples
-- simple game engine -- simple game engine
while gametime > fps do while game_time > fps do
changed = true changed = true
gametime = gametime - fps game_time = game_time - fps
-- move the ball
ball_x = ball_x + dx ball_x = ball_x + dx
ball_y = ball_y + dy ball_y = ball_y + dy
-- reflect left/right
if ball_x >= 1 or ball_x <= 0 then dx = -dx end if ball_x >= 1 or ball_x <= 0 then dx = -dx end
-- single player (reflect top) -- TODO "stereo" version, 2 ctrls
if ball_y <= 0 then dy = - dy end if ball_y <= 0 then dy = - dy end
if ball_y > 1 then if ball_y > 1 then
local bar = ctrl[1] local bar = ctrl[1] -- get bar position
if math.abs (bar - ball_x) < 0.1 then if math.abs (bar - ball_x) < 0.1 then
dy = - dy dy = - dy
ball_y = 1.0 ball_y = 1.0
dx = dx + 0.1 * (bar - ball_x) dx = dx + 0.1 * (bar - ball_x)
-- queue sound (unless it's playing) -- queue sound (unless it's playing)
if (pingsound > fps) then if (ping_sound >= fps) then
pingsound = 0 ping_sound = 0
ping_phase = 0
end end
phase = 0
else else
-- game over -- game over
lostsound = 0 lost_sound = 0
ball_y = 0 ball_y = 0
dx = 0.011 dx = 0.011
end end
@ -85,31 +90,38 @@ function dsp_run (ins, outs, n_samples)
end end
-- simple synth -- TODO Optimize -- simple synth -- TODO Optimize
if pingsound <= fps then if ping_sound < fps then
for s = 1, n_samples do local abufs = {}
pingsound = pingsound + 1 for c = 1,#outs do
if pingsound > fps then goto note_end end abufs[c] = outs[c]:array();
phase = phase + pingnote
local snd = 0.7 * math.sin(6.283185307 * phase) * math.sin (3.141592 * pingsound / fps)
for c = 1,#outs do
-- don't copy this code, it's quick/dirty and not efficient
outs[c]:array()[s] = outs[c]:array()[s] + snd
end
::note_end::
end end
for s = 1, n_samples do
ping_sound = ping_sound + 1
ping_phase = ping_phase + ping_pitch
local snd = 0.7 * math.sin(6.283185307 * ping_phase) * math.sin (3.141592 * ping_sound / fps)
for c = 1,#outs do
abufs[c][s] = abufs[c][s] + snd
end
if ping_sound >= fps then goto ping_end end
end
::ping_end::
end end
if lostsound <= 3 * fps then if lost_sound < 3 * fps then
local abufs = {}
for c = 1,#outs do
abufs[c] = outs[c]:array();
end
for s = 1, n_samples do for s = 1, n_samples do
lostsound = lostsound + 1 lost_sound = lost_sound + 1
if lostsound > 3 * fps then goto noise_end end -- -12dBFS white noise
local snd = 0.5 * (math.random () - 0.5) local snd = 0.5 * (math.random () - 0.5)
for c = 1,#outs do for c = 1,#outs do
-- don't copy this code, it's quick/dirty and not efficient abufs[c][s] = abufs[c][s] + snd
outs[c]:array()[s] = outs[c]:array()[s] + snd
end end
::noise_end:: if lost_sound >= 3 * fps then goto noise_end end
end end
::noise_end::
end end
if changed then if changed then