Now you make sound from nothing. A sine wave is the purest tone, and generating one means sampling the sine function at the right rate. Today you synthesize an exact sine tone.
Generate sine-wave samples from a frequency, amplitude, and sample rate.
A digital tone is just the continuous wave sampled at each tick of the clock. For
a sine of frequency f at sample rate sr, the sample at index n is amp * sin(2*pi*f*n/sr): the fraction f*n/sr is how many full cycles have elapsed by
sample n, times 2*pi turns it into radians, and sin of that scaled by the
amplitude is the value. This is the atom of synthesis - every other waveform is a
variation on stepping a phase forward each sample.
Because samples must be integers, you round the scaled sine, and the spec has to
say so - round here, half away from zero for the rare tie. With f = 1000,
sr = 4000, the phase advances pi/2 per sample, so the four samples land exactly
on sin of 0, pi/2, pi, 3pi/2 - giving [0, 10000, 0, -10000]. (The pi case is
not exactly zero in floating point, but it rounds to 0.) Pick amplitude below full
scale to leave headroom, and you have a clean tone to feed the rest of the chapter.
// sample n = round(amp * sin(2*pi*f*n/sr))func sine(freq, amp, sampleRate, count int) []int {out := make([]int, count)for n := 0; n < count; n++ {phase := 2 * math.Pi * float64(freq) * float64(n) / float64(sampleRate)out[n] = int(math.Round(float64(amp) * math.Sin(phase)))}return out}