summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorRobin Gareus <robin@gareus.org>2016-08-21 22:57:18 +0200
committerRobin Gareus <robin@gareus.org>2016-08-21 23:02:37 +0200
commit970241b654c97143179968944259be340023917f (patch)
tree9cc638dc61cb883ff5a48d9600f4db13357b42a1 /scripts
parent9e981367e2cf27cf0d19914e03f55a8c405c7188 (diff)
more musical pong. uhm ping.
Diffstat (limited to 'scripts')
-rw-r--r--scripts/_pong.lua34
1 files changed, 24 insertions, 10 deletions
diff --git a/scripts/_pong.lua b/scripts/_pong.lua
index 5cf4495ec2..453a7b3e9d 100644
--- a/scripts/_pong.lua
+++ b/scripts/_pong.lua
@@ -26,6 +26,7 @@ end
-- Game State (for this instance)
-- NOTE: these variables are for the DSP part (not shared with the GUI instance)
+local sample_rate -- sample-rate
local fps -- audio samples per game-step
local game_time -- counts up to fps
local game_score
@@ -41,6 +42,7 @@ function dsp_init (rate)
self:shmem ():allocate (3)
self:shmem ():clear ()
-- initialize some variables
+ sample_rate = rate
fps = rate / 25
ping_pitch = 752 / rate
ball_x = 0.5
@@ -53,6 +55,15 @@ function dsp_init (rate)
lost_sound = 3 * fps
end
+function queue_beep ()
+ -- queue 'ping' sound (unless one is already playing to prevent clicks)
+ if (ping_sound >= fps) then
+ ping_sound = 0
+ ping_phase = 0
+ local midi_note = math.floor (60 + math.random () * 24)
+ ping_pitch = (440 / 32) * 2^((midi_note - 10.0) / 12.0) / sample_rate
+ end
+end
-- callback: process "n_samples" of audio
-- ins, outs are http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
@@ -84,13 +95,20 @@ function dsp_run (ins, outs, n_samples)
ball_y = ball_y + dy * ctrl[3]
-- reflect left/right
- if ball_x >= 1 or ball_x <= 0 then dx = -dx end
- -- keep the ball in the field
- if ball_x >= 1 then ball_x = 1 end
- if ball_x <= 0 then ball_x = 0 end
+ if ball_x >= 1 or ball_x <= 0 then
+ dx = -dx
+ queue_beep ()
+ end
-- single player (reflect top) -- TODO "stereo" version, 2 ctrls :)
- if ball_y <= 0 then dy = - dy y = 0 end
+ if ball_y <= 0 then
+ dy = - dy y = 0
+ queue_beep ()
+ end
+
+ -- keep the ball in the field at all times
+ if ball_x >= 1 then ball_x = 1 end
+ if ball_x <= 0 then ball_x = 0 end
-- bottom edge
if ball_y > 1 then
@@ -102,12 +120,8 @@ function dsp_run (ins, outs, n_samples)
dx = dx - 0.04 * (bar - ball_x)
-- make sure it's moving (not stuck on borders)
if math.abs (dx) < 0.0001 then dx = 0.0001 end
- -- queue 'ping' sound (unless it's already playing to prevent clicks)
- if (ping_sound >= fps) then
- ping_sound = 0
- ping_phase = 0
- end
game_score = game_score + 1
+ queue_beep ()
else
-- game over, reset game
lost_sound = 0 -- re-start noise