You can save yourself some time by copying numbers from the midiTable
- which has MIDI pitches for all notes 0 - 127. Note 60 is middle C,
then it moves in semitones.
Mapping from frequency:
value = frequency(Hz) * 65536 / 31250
Mapping from MIDI note:
value = (65536 / 31250) * 440 * 2^((note-69)/12)
Those constants are:
65536: The counter for phase angle is 16 bits wide, so 65536 is one
full waveform cycle.
31250: Sample rate of the output in Hz.
440: Frequency of A above middle C. Most instruments standardise on
this being exactly 440Hz: "A440" tuning.
69: MIDI note of A above middle C.
12: Semitones in an octave
2: An octave note is a doubling of frequency.
Some worked examples:
A above middle C is 440Hz. Table entry is 440 * 65536 / 31500 =
922.75, so use the nearest whole number, "923".
Middle C is 261.63Hz. Table entry is: 261.625 * 65536 / 31250 =
548.67, so use "549".
You are limited to the values 1 (0.48Hz) to 32767 (15624.52Hz) -
although I doubt you want to be going up that high anyway! Past 32768,
pitch starts going down again.
Hope that helps!
Peter