Musique - happy birthday ♫

Description

Créez un Buzzer jouant la mélodie "Happy Birthday". Une initiative sympa pour surprendre quelqu'un le jour de son anniversaire ! // Il vous faut : · Une Base · Une Brique Buzzer // Difficulté : ★★☆


/* Happy birthday hack
 * Johan Euphrosine 2009
 * Thingz 2014
 */

#include <math.h>  // requires an Atmega168 chip 
#include "Son.h"

Son buzzer;

#define outpin 9   // audio out to speaker or amp
int ptime;
int   k,  x, dur, freq, t; 
int i, j;


float ps;         // variable for pow pitchShift routine

float noteval;

// note values for two octave scale
// divide them by powers of two to generate other octaves
float A     = 14080;
float AS    = 14917.2;
float B     = 15804.3;
float C     = 16744;
float CS    = 17739.7;
float D     = 18794.5;
float DS    = 19912.1;
float E     = 21096.2;
float F     = 22350.6;
float FS    = 23679.6;
float G     = 25087.7;
float GS    = 26579.5;
float A2_    = 28160;
float A2S   = 29834.5;
float B2    = 31608.5;
float C2    = 33488.1;
float C2S   = 35479.4;
float D2    = 37589.1;
float D2S   = 39824.3;
float E2    = 42192.3;
float F2    = 44701.2;
float F2S   = 47359.3;
float G2    = 50175.4;
float G2S   = 53159;
float A3_    = 56320;

//octaves  - corresponds to piano octaves
float oct8 = 4;
float oct7 = 8;
float oct6 = 16;
float oct5 = 32;
float oct4 = 64;
float oct3 = 128;
float oct2 = 256;
float oct1 = 512;
float oct0 = 1024;

//rhythm values
int wh = 1024;
int h  = 512;
int dq = 448;
int q = 256;
int qt = 170;
int de = 192;
int e = 128;
int et = 85;
int dsx = 96;
int sx = 64;
int thx = 32;

// major scale just for demo, hack this

float happyBirthday[] = {
G, 0, G, A2_, 0, 0, 
G, 0, 0, C2, 0, 0,
B2, 0, 0, 0, 0, 0,
G, 0, G, A2_, 0, 0,
G, 0, 0, D2, 0, 0, 
C2, 0, 0, 0, 0, 0,
G, 0, G, G2, 0, 0,
E2, 0, 0, C2, 0, 0, 
B2, 0, 0, A2_, 0, 0,
F2, 0, F2, E2, 0, 0,
C2, 0, 0, D2, 0, 0,
C2, 0, 0, 0, 0, 0,
};

void setup() { 
} 


void loop(){   
 for(i= 0; i<=11; i++){ 
     int value = HIGH;

    ps = (float)i / 12;         // choose new transpose interval every loop
    for(x= 0; x<=12*6; x++){
      noteval = (happyBirthday[x] / oct4) * pow(2,ps);    // transpose scale up 12 tones
// pow function generates transposition
// eliminate " * pow(2,ps) " to cut out transpose routine

      dur = 100;

     

      if ((int)noteval > 0) {
        //freqout((int)noteval, dur);
        buzzer.sonne(noteval);
        attendre(dur);
      } else {
        attendre(100);
      }
    }
  }
}