aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2021-05-16 11:40:47 +0200
committerThomas White <taw@physics.org>2021-05-16 11:40:47 +0200
commit417ebc04afe1fb98c6df4480be8725877697069a (patch)
treed95a5876000c3010eb6cf0826045fc016f2c3528
parent34d48b4402ec09feacc70bf33a10e107cf830c7d (diff)
Restore effects using clocks
-rw-r--r--examples/demo.scm11
-rw-r--r--guile/starlet/clock.scm19
-rw-r--r--guile/starlet/effects.scm16
3 files changed, 25 insertions, 21 deletions
diff --git a/examples/demo.scm b/examples/demo.scm
index 11f0803..a49bf7b 100644
--- a/examples/demo.scm
+++ b/examples/demo.scm
@@ -93,15 +93,14 @@
;; Functions can be assigned to parameters
-;; (temporarily disabled - time parameter is gone, should use
-;; clock objects instead)
-;;(at foh1 'intensity (lambda (time)
-;; (* 50
-;; (+ 1 (sin (* 2 time))))))
+(let ((clock (make-clock)))
+ (at foh1 'intensity (lambda ()
+ (* 50
+ (+ 1 (sin (* 2 (elapsed-time clock))))))))
;; Effects library
-(at floor2 'intensity 100)
+(at floor2 'intensity (flash 1.3))
(at floor2 'colour (make-colour-cmy 0 0 100))
(at floor2 'pan 0)
(at floor2 'tilt (sinewave 0.5 100 170))
diff --git a/guile/starlet/clock.scm b/guile/starlet/clock.scm
index 46dd316..29782a0 100644
--- a/guile/starlet/clock.scm
+++ b/guile/starlet/clock.scm
@@ -32,6 +32,7 @@
clock-stopped?
clock-reversed?
+ elapsed-time
elapsed-fraction))
@@ -45,7 +46,7 @@
;; "Real" clocks are straightforward objects for measuring differences in time
;; between now and some point in the past, allowing for temporarily "pausing"
;; the clock. The time difference cannot be negative: if the start time is in
-;; the future, then "time-elapsed" will return 0.
+;; the future, then "elapsed-time" will return 0.
(define-class <starlet-clock> (<object>)
(start-real-time
@@ -74,7 +75,7 @@
(make <starlet-clock>))
-(define-method (time-elapsed (clock <starlet-clock>))
+(define-method (elapsed-time (clock <starlet-clock>))
(if (clock-stopped? clock)
(get-start-elapsed-time clock)
(max 0
@@ -91,13 +92,13 @@
;; Stop the clock running
(define-method (stop-clock! (clock <starlet-clock>))
- (set-start-elapsed-time! clock (time-elapsed clock))
+ (set-start-elapsed-time! clock (elapsed-time clock))
(set-clock-stopped! clock #t))
;; Start the clock running (forwards)
(define-method (start-clock! (clock <starlet-clock>))
- (set-start-elapsed-time! clock (time-elapsed clock))
+ (set-start-elapsed-time! clock (elapsed-time clock))
(set-start-real-time! clock (time-now))
(set-clock-reversed! clock #f)
(set-clock-stopped! clock #f))
@@ -105,7 +106,7 @@
;; Start the clock running, backwards
(define-method (reverse-clock! (clock <starlet-clock>))
- (set-start-elapsed-time! clock (time-elapsed clock))
+ (set-start-elapsed-time! clock (elapsed-time clock))
(set-start-real-time! clock (time-now))
(set-clock-reversed! clock #t)
(set-clock-stopped! clock #f))
@@ -139,18 +140,18 @@
(clock-reversed? (get-parent-clock clock)))
-(define-method (time-elapsed (clock <starlet-delayed-clock>))
- (max 0 (- (time-elapsed (get-parent-clock clock))
+(define-method (elapsed-time (clock <starlet-delayed-clock>))
+ (max 0 (- (elapsed-time (get-parent-clock clock))
(get-delay-time clock))))
(define-method (elapsed-fraction (clock <starlet-delayed-clock>))
(if (= (get-duration clock) 0)
- (if (> (time-elapsed clock) 0)
+ (if (> (elapsed-time clock) 0)
1.0
0.0)
(min 1.0
- (/ (time-elapsed clock)
+ (/ (elapsed-time clock)
(get-duration clock)))))
diff --git a/guile/starlet/effects.scm b/guile/starlet/effects.scm
index fcffb3c..c14f5a0 100644
--- a/guile/starlet/effects.scm
+++ b/guile/starlet/effects.scm
@@ -19,6 +19,7 @@
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
(define-module (starlet effects)
+ #:use-module (starlet clock)
#:export (flash
sinewave))
@@ -32,12 +33,15 @@
0))
(define (flash hz)
- (lambda (time)
- (square-wave time hz)))
+ (let ((clock (make-clock)))
+ (lambda ()
+ (square-wave (elapsed-time clock)
+ hz))))
(define (sinewave hz range-min range-max)
- (lambda (time)
- (+ range-min
- (* (/ (- range-max range-min) 2)
- (+ 1 (sin (* 2 pi hz time)))))))
+ (let ((clock (make-clock)))
+ (lambda ()
+ (+ range-min
+ (* (/ (- range-max range-min) 2)
+ (+ 1 (sin (* 2 pi hz (elapsed-time clock)))))))))