#langracket/base
(require"define-memo.rkt")
;; this module figures out what number of cycles comes "close"
;; to being a whole number of frames.
(providecycles-to-use)
;; experiments suggest that for all numbers between 1.0 and 22050.0,
;; it's pretty easy to find a number of cycles whose cumulative period
;; when rounded to an integer number of frames is off by less than
;; this fraction of a cycle.
(definethresh4e-5)
(define/memo (cycles-to-usefreqsample-rate)
(when (not (and (real?freq) (<0freq)))
(raise-type-error'cycles-to-use"positive real number"0freqsample-rate))
(defineperiod (/sample-ratefreq))
(definefail (add1 (ceilingfreq)))
(letloop ([i1])
(cond [(<faili)
(error'cycle-search"couldn't find good number of cycles for frequency ~s"freq)]
[(< (/ (abs (- (round (*iperiod)) (*iperiod)))
period)
thresh)
i]
[else (loop (add1i))])))