
Simple synthesizer on Arduino

Hello.
Two weeks ago I purchased an Arduino Duemilanove. He blinked an LED, played with the LCD, and then on the list from the Examples folder. I stopped at the standard example of Play Melody and decided to develop it a little. I must say right away - I’m a teapot in creating and processing sound, therefore I came up with terms and effects for the future synthesizer (if I may call it that) in the process.
Functions

Keys
Only four keys with a difference of about 130 Hz, the frequency increases from left to right. There were no more buttons at hand, I used small ones that came with the Arduino.
the effect
This button changes the frequency multiplier function. The function changes at a given step and at a given interval, and even if the key is not pressed, the effect will continue. For example, when you select the X * X function (where X changes from 0 to 1), you get something like a disco shot. So far, the code has only three effects: sin (x) (0; 3.14), x * x (0; 1), 1-x (0; 1).
Record
Everything is simple here, at each iteration we write the frequency value into the array, and when the array is full, we reproduce its values in a loop. True, playing against the background of the received “sample” does not work, playing the keys creates an additional delay and it is very slow (whoever has the solution, I will be glad). Later I added an LED next to the button, it glows during recording.
Bit
Changes the gap between individual double signals and the width of the signals. If you hold down the key and turn B, the sound is like a gradual stop of the bouncing ball and vice versa.
Frequency
Changes the gaps between individual signals.
Scheme
Six buttons, two potentiometers, speaker and LED + Arduino. The speaker and buttons are connected in a row to the digital inputs 2, 3, 4, 5, 6, 7, 8, the LED to 13; potentiometers to analog inputs 0 and 1;
The code
#include
float M=1;
float x=0;
int F,N,i,pl,d1,d2,d3,d4,d5,d6;
int eff=0;
int limit=0;
float st;
int arr[700];
int timer=0;
int rec=0;
int buf=0;
int beat=0;
void play(int F,int N, float M){
for(i=0;ilimit){
if(buf<4){
buf=eff;
} else {
buf=0;
}
eff=0;
x=0;
}
if(rec==1){
timer++;
arr[timer]=0;
if(pl==0){
delayMicroseconds(F);
}
digitalWrite(13, HIGH);
} else if(rec==2){
timer++;
}
if(eff==0){
pl=0;
}else{
x+=st;
}
if(N<50){
N=50;
eff=buf;
}
if(d1==1){
button(1);
}
if(d2==1){
button(2);
}
if(d3==1){
button(3);
}
if(d4==1){
button(4);
}
if(d5==1){
if(buf>3){
buf=0;
}
eff=buf;
eff++;
pl=1;
}
if(d6==1){
if(rec==0){
rec=1;
}else if(rec==2){
rec=0;
timer=0;
}
}
if(rec==2 && arr[timer]!=0){
play(arr[timer],N,1);
}
if(pl==1){
if(eff==0){
play(F,N,1);
} else if(eff==1){
limit=3.14;
st=0.025;
M=sin(x);
} else if(eff==2){
limit=1.2;
st=0.01;
M=x*x;
} else if(eff==3){
limit=1;
st=0.025;
M=x+0.5;
}
play(F,N,M);
}
if(rec==1 & timer>698){
timer=0;
rec=2;
digitalWrite(13, LOW);
}
if(timer>698){
timer=0;
}
}
Housing

Since this does not pretend to be a finished device, I made a cardboard case, assembled the circuit on a breadboard and placed it inside. The soldering iron remained untouched.
Enhancements
An obvious improvement to the keys is to connect a keyboard where each key changes resistance to a single analog input. You can add mini-jack output for recording directly to a computer or connecting to an amplifier, compiling a whole library of effects ... the list is limited by your imagination.