|Consider an oscillator in your standard analog synthesizer. It cycles through its waveform endlessly. Let's say you want to model this using C#. Why not use the
IEnumerator interface? The
MoveNext method moves the oscillator to the next sample of the waveform. The
Reset method resets the oscillator to the beginning of the cycle of its waveform. And the
Current property represents the current output of the oscillator.
In addition to implementing the
IEnumerator interface, an Oscillator class could implement methods allowing you to modulate it's amplitude and frequency; it could have
ModulateFrequency methods that take a double value.
In the case of
ModulateAmplitude, the Oscillator would multiply the specified value to its current amplitude. In the case of
ModulateFrequency, the Oscillator would add the specified value to its current index into the waveform buffer. When
MoveNext is called, it calculates its current value based on the modulated amplitude value and modulated frequency value.
Since this is an oscillator,
MoveNext would never return false. However, we could model other synth components using the
IEnumerator interface, such as an ADSR envelope. It this case,
MoveNext would eventually return false when the envelope has reached the end of its release segment.
I've written code to implement the above descriptions, and I've liked the results. Hopefully, I'll post an article with an accompanying demo project sometime before the end of the year.