toneAC v1.2 - Twice the volume, higher quality, higher frequency, etc.

Example 3: Tone Tutorial with fade out tones.
You have to listen to this for yourself. It is not like a piano, because it sounds very computer/robotic.

Use the tone tutorial : http://arduino.cc/en/Tutorial/tone
But connect the loudspeaker to two output pins as is required for the toneAC library.

Replace the setup() function with this:

// Example for toneAC with tone fade out.
// Using the tone tutorial : http://arduino.cc/en/Tutorial/Tone
// public domain

void setup()
{
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    
    // Add some to the length for the pauses between the notes.
    noteDuration += noteDuration/2;
    int i, vol;
    // Setting the volume to 12 is a trick to reduce the code.
    // The maximum volume is 10, but 12 is allowed.
    vol = 12;
    for (i=0; i<=noteDuration; i+=25)
    {
      // Double the frequency to make it sound better.
      toneAC( 2* melody[thisNote], vol, 0, true);
      // fade out
      if (vol > 0)
        vol--;
      delay( 25);
    }
    noToneAC();
  }
}