One of the joys of our existence: music! But what is music?  It turns out our ears are tuned to certain tone frequencies.  These frequencies make up mathematically defined sets of tones called musical notes. You can generate notes yourself by saying "Do-Re-Me-Fa-Sol-La-Si". You can say those notes in a low pitch (at low frequencies) or very high pitch (at higher frequencies).  In musical notation, the syllables above are given the letters C-D-E-F-G-A-B. In some languages, other symbols are used but the English convention is widespread.

As you go from lower to higher frequencies, you progress through sound octaves.  Low octaves are very low, while the highest octave would be rather high pitched.  Think of a piano, one side has notes at a low frequency, and as you play the keys you hear the "Do-Re-Mi" sequence, repeating to higher and higher frequency tones.

The following is a list of musical note frequencies we can use in our Circuit Playground sketches. The definitions are adapted from the arduino.cc tutorial ToneMelody credited to Brett Hagman. Copy and save to file pitches.h in your sketch folder.

/********************************************************************
 * Musical Notes via https://www.arduino.cc/en/Tutorial/ToneMelody  *
 ********************************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

Now you can see why I picked the two frequencies in previous examples.  440 Hertz is note A4, an A tone in the 4th octave. 1760 Hz is an A note in the 6th octave.

To play a song with the musical notes above, we need a bit more information. How long do we play each tone is important in music, things "sound right" as you play musical note at defined times, creating a tempo or a pace to which the notes are played.  So each note should have both a frequency to play and a duration for how long we should play.  They tempo is set in fractions or multiples of a whole note for example a half note, quarter note, 8th, 16th and so on.  In terms of time, music is played in beats per minute with notes at a faster or slower tempo.

So we'll play a short selection of notes (not quite a song) with several notes.  

// Adafruit Circuit Playground - Melody    Support Open Source, buy at Adafruit
//   2016-08-06 Version 1  by Mike Barela for Adafruit Industries
//   Adapted from melody by Tom Igoe on arduino.cc
// Uses the CircuitPlayground library to easily use the full functionality of the board

#include <Adafruit_CircuitPlayground.h>
#include "pitches.h"

const int numNotes = 8;                     // number of notes we are playing
int melody[] = {                            // specific notes in the melody
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 };

int noteDurations[] = {     // note durations: 4 = quarter note, 8 = eighth note, etc.:
  4, 8, 8, 4, 4, 4, 4, 4 };

void setup() {
  CircuitPlayground.begin();  // initialize the CP library
}

void loop() {
  if(CircuitPlayground.rightButton()) {   // play when we press the right button
    for (int thisNote = 0; thisNote < numNotes; thisNote++) { // play notes of the melody
      // 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];
      CircuitPlayground.playTone(melody[thisNote], noteDuration);

      // to distinguish the notes, set a minimum time between them.
      //   the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
    }
  }
}

You can find songs encoded with tone and duration in many places on the Internet.  

Chiptunes

It is very popular in music and electronics culture to emulate the sounds/music of classic video games. The music was most often on 8 bit machines and the resulting music sounds a bit like the music we're playing. The melodies from classic gaming and remixed music are called chiptunes.

For an in-depth discussion of Chiptunes, you can catch the Adafruit video post Pseudorandom #13 which discusses the chiptunes scene.

Below is a melody from a classic video game - can you name that tune?

// Adafruit Circuit Playground - Theme Song    Support Open Source, buy at Adafruit
//   2016-08-12 Version 1  by Mike Barela for Adafruit Industries

#include <Adafruit_CircuitPlayground.h>
#include "pitches.h"

int melody[] = {                            // specific notes in the melody
NOTE_E7, NOTE_E7, 0, NOTE_E7,
  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,
  NOTE_G6, 0, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,
 
  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,
 
  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0  
 };
int numNotes; // Number of notes in the melody

int noteDurations[] = {     // note durations
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
};

void setup() {
  CircuitPlayground.begin();              // initialize the CP library
  numNotes = sizeof(melody)/sizeof(int);  // number of notes we are playing
}

void loop() {
  if(CircuitPlayground.rightButton()) {   // play when we press the right button
    for (int thisNote = 0; thisNote < numNotes; thisNote++) { // play notes of the melody
      // to calculate the note duration, take one second divided by the note type.
      int noteDuration = 1000 / noteDurations[thisNote];
      CircuitPlayground.playTone(melody[thisNote], noteDuration);

      // to distinguish the notes, set a minimum time between them.
      //   the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
    }
  }
}
Why doesn't our electronic music sound like a rock band?

Most music is polyphonic - multiple notes played together. If we used several Circuit Playground boards playing, we could achieve some of that.

But we still must deal with the physics of the speaker and the way we have been driving, or moving the speaker. Surround sound systems usually have larger speakers which can reproduce a wide range of sounds very well. Due to the construction and size of the Circuit Playground speaker, it is not as good as a name brand in making musical sounds.

We are also driving the speaker with a frequency which is on and off. As you can see in the first example code, we calculate the desired frequency and turn the speaker on then pause, then off, then pause and repeat. This is a square wave which in many speakers sounds pretty good but not "pure" like the sine waves we looke at in the overview. Mathematically, a square wave can be composed of multiple sine waves added together. The wave we want is the fundemental frequency and smaller amplitude sine waves at higher frequencies that we might not want.

Circuit Playground allows us to easily work with sound and music. When we want to explore more band-style music, we can look at other Adafruit products to build great sounding musical circuits.

This guide was first published on Aug 17, 2016. It was last updated on Aug 17, 2016.

This page (The Sound of Music) was last updated on Aug 04, 2016.

Text editor powered by tinymce.