one thing I like to do with my animations is tap out the bpm with the spacebar - I seem to be much better at finding the beat than 100% of the software I've used to do it, could be something to do with the fact that I'm a human being and have evolved over the last few million years to do just that.

Anyway, here's my example code, feel free to clean it up / use it for god/evil:

(clear)

;--- trig constants

(define PI 3.14159265)
(define HALF_PI (* PI 0.5))
(define TWO_PI (* PI 2))


;--- key press variables, for timing

(define lastKeyPress 0)
(define lastKeyPresses (list 0 0 0))


;--- calculate the bpm based on the time of last key press
;--- and a list of previous key presses

(define (calc-bpm max-interval)
    (let ([dt (- (time) lastKeyPress)])
        ;--- only count this beat if within max-interval ms
        (cond [(> dt 0.1)  ; also don't count if interval is too short
        (cond [(< dt max-interval)
(set! lastKeyPresses (append (cdr lastKeyPresses) (list dt)))
            ]
            [else (set! lastKeyPresses (list dt dt dt))]
        )
            (set! lastKeyPress (time))
        ])

    )
   (display lastKeyPresses)(newline)

   ;-- return median time for multiplying by (time)
   (let [(t (median lastKeyPresses))]
       (cond [(> t 0) (/ 1 t)]
            [else 0])
    )
)

(define (median l)
    (cond [(null? l) 0]
[else (list-ref (sort l <) (inexact->exact (round (/ (length l) 2))))]
        )
    )

(define cubey (build-cube))


;-- gimmie a number btw 0 and 1 based on time

(define (time-ramp tscale tmax)
    (/ (fmod (* (- (time) lastKeyPress) tscale) tmax) tmax)
    )

;-- gimmie a number based on time and sine curve - 0 to 1

(define (time-curve tscale tmax)
    (sin (* HALF_PI (time-ramp tscale tmax)))
    )


(define msPerBeat 1)

(define (do-something)
    (define num-objs 6)
    (define half-num-objs (/ (- num-objs 1) 2))

    (when (key-pressed " ") (set! msPerBeat (calc-bpm 2500)))

    ;-- generate a 4 beat long ramp, use to rotate cubes
    (let* ([base-ramp (time-ramp msPerBeat 4)]
            [ramp1 (* base-ramp 360)]
            )
;        (display ramp1)(newline)
        (with-state
            (colour (vector 1 1 0))
            (for ([i (in-range num-objs)])
                (let [(x (* 9 (/ (- i half-num-objs) half-num-objs)))
                      (y 0)
                      (z 0)]
                    (with-state
                        (identity)
                        (rotate (vector ramp1 0 0))
                        (translate (vector x y z))
                        (draw-instance cubey)
                    )
                   )
            )
         )

      )
)

(every-frame (do-something))

Reply via email to