(define (buf-rms data) (let ((len (vct-length data))) (sqrt (/ (dot-product data data) len)))) (define (seconds->samples secs) "innebygget versjon er buggy" (inexact->exact (round (* secs (srate))))) (define (samples->seconds samps) "innebygget versjon er buggy" (/ samps (srate))) (define* (parse-attacks #&key (start 0) (len 512) (on 0.2) (off 0.2) (file 0) (channel 0) (dur -1) (min-duration most-negative-fixnum) (bad '())) (let ((maxdur (and file (frames file)))) (if (negative? dur) (set! dur (samples->seconds maxdur))) (format #t "dur: ~A ~&" dur) (let* ((times '()) (*srate* (srate file)) (beg (seconds->samples start)) (end (seconds->samples (min (+ start dur) maxdur))) (buf (make-vct len)) ; window for amp averaging (sum 0.0) (val 0.0) (avr 0.0) (j 0) (k -1) (on? #f) (middle 0) (ontime 0.0) (offtime 0.0)) ;; fill buf with first window (set! j 0) (do ((i beg (+ i len))) ((>= i end) #f) (set! buf (samples->vct i len file channel)) (set! avr (buf-rms buf)) (if (not on?) (if (>= avr on) (begin (set! ontime (samples->seconds (1+ (- i len)))) (set! on? #t)) #f) (begin (if (and (>= i middle) (<= avr off)) (let ((sdur 0)) (set! offtime (samples->seconds (- i len))) (set! sdur (- offtime ontime)) (while (< sdur min-duration) (set! bad (cons k bad))) (set! times (cons (list ontime offtime sdur) times)) (set! on? #f)) #f)))) (if (or times (not (null? bad))) (if (not (null? bad)) (list (reverse times) (reverse bad)) (reverse times)) #f))))