There's various ways that this may be calculated in Nyquist, but one easy method is:
Code: Select all
(expt 2 (/ 12.0))So if we have a frequency of 440 Hz (A4) and we want to calculate the frequency of the note that is one semi-tone higher (A#4)
Code: Select all
(* 440 (expt 2 (/ 12.0)))To calculate the frequency 2 semi-tones above 440 we need to calculate ((semi-tone)^2) and multiply that by our base frequency of 440
so this gives us:
440 * [{(2 ^ (1/12)} 2]
which in Nyquist we can write as:
Code: Select all
(* 440 (expt (expt 2 (/ 12.0)) 2.0))Code: Select all
(defun fcalc (f0 n)
(* f0 (expt (expt 2 (/ 12.0)) n)))
However, Nyquist has been written specifically with music in mind, so for many applications there is an even easier way.
Nyquist provides two functions:
(hz-to-step) and (step-to-hz)
As a convention that is used in the MIDI 1.0 specification, the note A4 (which is the standard pitch that orchestras tune to and almost everything else uses as the tuning reference) is assigned the number 69 and corresponds to the frequency 440 Hz.
Code: Select all
(step-to-hz 69) ; returns 440.0
(hz-to-step 440) ; returns 69
Code: Select all
(step-to-hz (+ 2 (hz-to-step 440)))
Code: Select all
(defun fcalc2 (f0 n)
(step-to-hz (+ n (hz-to-step f0))))
1/3 octave is 4 semi-tones (12 semi-tone steps to an octave) so we can iterate through a loop:
Code: Select all
(defun fcalc (f0 n)
(step-to-hz (+ n (hz-to-step f0))))
(setf frequency-list
(do* ((count 0 (setq count (1+ count)))(freq 20)(flist (list freq)))
((> freq 20000)(reverse flist))
(setq freq (round (fcalc freq 4)))
(setf flist (cons freq flist))))
(format NIL "~a" frequency-list)