Pages: [1]   Go Down
Author Topic: "Sliding" sound?  (Read 751 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int speakerPin = 8;

int length = 2; // the number of notes
char notes[] = "cb "; // a space represents a rest
int 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);
  }
}
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Pages: [1]   Go Up
Jump to: