Go Down

### Topic: "Sliding" sound? (Read 1 time)previous topic - next topic

#### arduinokov

##### Jan 06, 2013, 11:37 pm
Hi,

I am using a code to play two notes. I would like to slide from one note to the next being able to control the overall time it takes to get from one note to the next. (Like the ascending part of a siren).

Code: [Select]
`int speakerPin = 8;int length = 2; // the number of noteschar notes[] = "cb "; // a space represents a restint beats[] = { 5, 5 };int tempo = 500;void playTone(int tone, int duration) {  for (long i = 0; i < duration * 1000L; i += tone * 8) {    digitalWrite(speakerPin, HIGH);    delayMicroseconds(tone);    digitalWrite(speakerPin, LOW);    delayMicroseconds(tone);  }}void playNote(char note, int duration) {  char names[] = { 'c', 'b'};  int tones[] = { 1915, 1014 };  // play the tone corresponding to the note name    for (int i = 0; i < 8; i++) {    if (names[i] == note) {      playTone(tones[i], duration);    }  }}void setup() {  pinMode(speakerPin, OUTPUT);}void loop() {  for (int i = 0; i < length; i++) {    if (notes[i] == ' ') {      delay(beats[i] * tempo); // rest    } else {      playNote(notes[i], beats[i] * tempo);    }    // pause between notes    delay(tempo / 2);   }}`

#### billroy

#1
##### Jan 06, 2013, 11:47 pm
To slide from one note to the other one must adjust the frequency during the playing of the note.

You know how long you want the transition to take, and you know the starting and ending frequencies.  Split the time between the start and end of the note into N steps. Compute the required frequency change per step.  Adjust the frequency by adding the frequency change per step after each 1/N step of the note duration has elapsed.

-br

Go Up

Please enter a valid email to subscribe