#lang racket/base
(require "define-memo.rkt")
(provide cycles-to-use)
(define thresh 4e-5)
(define/memo (cycles-to-use freq sample-rate)
(when (not (and (real? freq) (< 0 freq)))
(raise-type-error 'cycles-to-use "positive real number" 0
freq sample-rate))
(define period (/ sample-rate freq))
(define fail (add1 (ceiling freq)))
(let loop ([i 1])
(cond [(< fail i)
(error 'cycle-search
"couldn't find good number of cycles for frequency ~s"
freq)]
[(< (/ (abs (- (round (* i period)) (* i period)))
period)
thresh)
i]
[else (loop (add1 i))])))