"Music"


Well, if you are a real musician don't read further.

If you just want your Arduino to produce some sound, get any MIDI file you like, send it through a midi2txt converter, strip everything but the notes (preceded by a "+" sign). Use lower case letters for the notes as the capitals are predefined by the Arduino system. And let the Arduino make some noise by means of its tone() function.

It could look like this (it is divided into three files):

the program:

#include "chromatic.h"
#include <avr/pgmspace.h>
PROGMEM byte noteArray[] = { 
#include "elise.h"  // <--- the song
};

void setup() {
  Serial.begin(9600);
  Serial.println(__FILE__);
  Serial.println(a8);
  // calculate the frequencies:
  float q[12];
  for (int i = 1; i < 12; i++) q[i] = pow(2,i/12.0);
  float freq[a8+1];
  freq[a0] = 27.5;
  for (int i = 0; i < a8; i = i + 12) { // well-tempered:
    for (int j = 1; j < 12; j++)  freq[i+j] = freq[i] * q[j];
    freq[i+12] = freq[i] * 2;
  }
  // now play:
  const byte speaker = 7;
  pinMode(speaker, OUTPUT);
  int dly = 150;  
/* test: 
  for (int i = 3; i < a8; i++) {
    tone(speaker, freq[i]); // chromatic scale *
    delay(dly);
  }
  /* */
  int length = sizeof(noteArray);
  for (int i = 0; i < length; i++) {
    float f = freq[pgm_read_byte( &noteArray[i] )];
    tone(speaker, (int) f);
    delay(dly);
  }
  noTone(speaker);
}

void loop() {}

the notes "chromatic.h":

enum { a0, /* = 27.5 Hz matches exactly */ b0, 
c1, cs1, d1, ds1, e1, f1, fs1, g1, gs1, a1, as1, b1, 
c2, cs2, d2, ds2, e2, f2, fs2, g2, gs2, a2, as2, b2, 
c3, cs3, d3, ds3, e3, f3, fs3, g3, gs3, a3, as3, b3, 
c4, cs4, d4, ds4, e4, f4, fs4, g4, gs4, a4, as4, b4, 
c5, cs5, d5, ds5, e5, f5, fs5, g5, gs5, a5, as5, b5, 
c6, cs6, d6, ds6, e6, f6, fs6, g6, gs6, a6, as6, b6, 
c7, cs7, d7, ds7, e7, f7, fs7, g7, gs7, a7, as7, b7, 
c8, cs8, d8, ds8, e8, f8, fs8, g8, gs8, a8};

the song "elise.h":

e5,ds5,e5,ds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c5,a2,e3,a3,e4,d5,c5,b4,e2,e3,e4,e4,e5,e4,e5,e5,e6,ds5,e5,ds5,e5,ds5,e5,b4,d5,c5,a4,a2,e3,a3,c4,e4,a4,b4,e2,e3,gs3,e4,gs4,b4,c5,a2,e3,a3,e4,e5,ds5,e5,ds5,e5,b4,d5,c5,a4,a2,e3,a3,c4,e4,a4,b4,e2,e3,gs3,e4,c5,b4,c4,a4,a2,a1

Sorry for the length of the data line.

And: yes, it looks like it is safe to connect an eight ohms speaker directly to an Arduino port. Without a resistor or an amplifier.

But as always: no warranties given.

Additional files

chromatic.h
elise.h



contact: nji(at)gmx.de