Generate tone patterns over time from a string representation of a melody.
Many physical systems require generating patterns of control outputs over time. For example, a dishwasher or washing machine controller is primarily a clock which times the phases of spraying or agitation. There is some sensor feedback for process transitions based on physical state (e.g. the water level reaching a threshold), but much of the process is simply timed.
Performance machines, especially musical ones, also frequently utilize timed sequences. It is also convenient for these machines to have a representation of the pattern which can easily be used for authoring and editing the performances.
This exercise introduces a very simple pattern language which represents a rhythmic tone pattern as a fixed string of text. The pattern string is interpreted by the program. This data-driven approach allows more compact representation than the equivalent code sequence. It also integrates much more easily alongside simultaneous processes such as sensor data acquisition.
This exercise uses a C++ class named MelodyGenerator to implement the tone and rhythm pattern generator. This was written as one class for simplicity, but logically might be broken out into separate ToneGenerator and PatternGenerator classes. This class was also incorporated directly into the sketch but would more conventionally be provided in separate files as a generic re-usable component.