Traditional music boxes play one or two tunes very well, but are not very interactive. Put differently, they have a high quality of synthesis, but a fixed-pattern note sequencer and fixed tonal quality. I wanted to build a device which would play an interesting music-like note sequence, which constantly changed and evolved, with settable timbre, tempo, and beat.
To synthesize nice sounding musical notes you need to control spectral content of the note, the rise time (attack), fall time (decay), and the change in spectral content during attack and decay. Also it is nice to have at least two independent musical voices. And all of this has to be done using the modest arithmetic capability of an 8-bit microcontroller. We use Atmel atmega-series MCUs. The scheme used for the attack and decay envelope was to generate the product of two exponentials, a saturating rise exponential and an exponential decay. The process to convert this complicated sounding envelope into fixed point shifts and adds (and one integer multiply) will be covered below. The spectral content of the notes was enriched using FM modulation, which is widely used for musical and special effects. Since directly computing sine waveforms is also mathematically heavy, direct digital synthesis (DDS) was used to produce waveforms. The DDS scheme explained below is used to generate both the FM modulating sine wave signal and the fundamental sine wave for the musical note.
WINAVR GCC program. (Circuit Cellar version) For Markov music box running on atmega644 or atmega1284 at 16 MHz. Pin B.3 is the PWM output connected through a 10,000 radian/sec lowpass filter to amplified speakers. Port C is hooked to 8 pushbuttons which control transition matrix, tempo, beat pattern, and timbre for each voice.
Published in Circuit Cellar Magazine, #272, pp 28-32, March 2013 (Manuscript) Full algorithm details are in the manuscript.
Markov Music Box Examples and code:
http://people.ece.cornell.edu/land/courses/ece4760…For more detail: Markov Music Box