The author of Instructables under the nickname rgco came up with a four-voice 1.5-octave synthesizer on Arduino Uno. For comparison, the widespread children's synthesizers that differ from each other only in design and made by the same chip are three-voice. True, they can automatically accompany the music with rhythm, memorize one-voice melodies and imitate the voices of animals. But homemade sounds nicer. Why?
It's all about the parameters. Sampling frequency 31 kHz, 9 bit, 4 voices, FM synthesis with a time-varying sweep, ADSR envelope, 12 virtual instruments, 18 keys spanning 1.5 octaves.
The master composes the synthesizer circuit in Fritzing:
In it, it simulates the location of components on a breadboard type breadboard and the connections between them:
And collects the scheme in real life:
Just “pour and use” is the simplest thing you can do with the design. 18 keys (from BEFORE the fourth octave to FA fifth) - to play, the nineteenth switches the virtual instruments in a ring: piano, xylophone, guitar, cymbals, bells, funky, vibrato, metal, violin, bass, trumpet, accordion. The nature of the sound is affected by the duration of the keystrokes, but not the effort, unlike professional synthesizers. But the developer wants you to try programming your virtual tools too. Each of them is given by ten parameters. The wizard tells you what parameter it depends on.
ldness - volume
If this parameter is less than 64, overload and related distortions are excluded. But if you program such a virtual instrument that does not sound at maximum volume for long, the value of this parameter may exceed 64, since all four voices simultaneously sound infrequently.
pitch0 - pitch shift
The range corresponds to the above, if you set this parameter to 12. Decrease moves down, increase - up. Zero corresponds to a shift down exactly one octave, 24 - up exactly one octave.
ADSR_a - volume slew rate from zero to maximum
The smaller, the slower.For example, 8192 corresponds to 4 ms, 256 to 128 ms.
ADSR_d - speed to decrease the volume from the maximum to the one specified by the ADSR_s parameter.
The principle is the same.
ADSR_s - the value to which the volume decreases with a long press of the key
For example, 256 - the volume remains maximum all the time while the key is held. 192 - the volume drops to 80 percent and remains so while the key is held down. 0 - volume decreases to zero, even if the key is held down.
ADSR_r - speed to decrease the volume to zero after releasing the key
The principle is the same as for the ADSR_a and ADSR_d parameters.
FM_inc - ratio of modulation frequency to tone
With a value of 256, this ratio is 1: 1, with 512 - 2: 1, with 128 - 1: 2, the rest is similar. If this parameter is set to a multiple of 64, the sound will be anharmonic.
FM_a1 - the range of frequency modulation at the beginning of a note
256 corresponds to a wide range of harmonics. With a decrease, the tone is cleaner, with an increase in harmonics it becomes more.
FM_a2 - sweep frequency modulation at the end of a note
The principle is the same. For most real instruments, harmonics decay faster than the fundamental tone. If you do the opposite, you get unusual sounds.
FM_dec - transition speed of the frequency modulation range from the one specified by the FM_a1 parameter to the one specified by the FM_a2 parameter
The principle is the same as for the parameters ADSR_a, ADSR_d and ADSR_r.
A PWM frequency of 31,250 Hz is obtained by dividing the clock frequency (16 MHz) by 512. The program is designed in such a way that events are linked to time without interrupts. Instead, a timer overflow bit is involved. In order for the program to manage to do everything that is required of it, only integers of 8 and 16 bit length are used. The sine wave is presented in the form of a table of 8-bit numbers. Ancillary operations — reading key states, changing virtual instruments, selecting voices, calculating notes parameters that change in time — are distributed among 15 procedures, which take 0.48 ms to complete. At this time, some operations are performed with 32-bit precision, which is necessary to multiply two 16-bit binary numbers.
A simple sine wave sounds dull because there are no harmonics in its spectrum. FM synthesis allows you to receive them and change their spectrum over time, simulating the sound of real instruments. Multiple frequencies give an anharmonic sound that occurs, for example, in bells. The device well imitates the change in spectrum inherent in real instruments over time, when some harmonics decay faster than others.