This example plays major and minor chords.
In the first example, we covered the 12 notes of the chromatic scale, which are the building blocks of Western music.
In the second example we covered 7 of these 12 notes that form the diatonic scale, a set of maximally spaced notes that sound good together.
In this example we're going to filter it down further to sets of 3 or more notes taken from the diatonic scale that form structural harmonies.
As we noted in an earlier example, sound is the result of changes in air pressure. These changes are like waves on a pond and the number of changes per second is called frequency.
Frequency is measured in cycles (waves) per second. The unit of frequency is the Hertz, abbreviated Hz, so 100 cycles per second is 100 Hz.
Ratios are numeric representations of relationships. For example, the A above middle C has a frequency of 440 Hz. The E above that A has a frequency of 660 Hz. We can say that the ratio of the E to the A is 660/440. We can simplify this to 3/2 and write it as a 3:2 ratio.
When notes sound cohesive, stable, and pleasing together, it is not an arbitrary cultural opinion, it is because their underlying sound waves are mathematically locked into perfect alignment.
Every musical note is a physical vibration traveling through the air at a specific speed. When you play multiple notes at the same time, their sound waves overlap.
Clean Ratios (Consonance): If the frequencies of those waves form small, simple integer fractions (like 2:1, 3:2, or 4:3), their wave peaks match up frequently and predictably. The human brain craves order and pattern recognition; it hears this clean synchronization as stable, smooth, and relaxing. We call this consonance.
Complex Ratios (Dissonance): If the frequencies form large, messy fractions (like 16:15), the wave peaks rarely match up. They physically collide and interfere with each other, creating a chaotic, "beating" texture. The brain registers this as friction or tension, which we call dissonance.
Chords are combinations of 3 or more notes. Most chords start with a standard 3-note sequence (a triad) that blends 3 frequencies into a single, mathematically pure wave structure:
Because the human brain enjoys clean mathematical patterns, it hears these overlapping wave cycles as a unified, pleasing harmony ("consonance").
The terms third and fifth represent the spacing between notes, starting at the first note. If we count the first 5 notes starting at C, we get C, D, E, F and G. As you can see, E is third note and G is the fifth note. We can create a chord from these three notes (C, E and G).
In this case, the third is a major third (two whole steps, or four semitones) and the resulting chord is C Major. If instead we drop the pitch of the third by one semitone, we get a minor third (three semitones). This gives us C, Eb and G, which is C minor.
Although the notes Eb (E lowered a semitone) and D# (D raised a semitone) refer to the same pitch, the correct name for this note in the C minor chord is Eb. There are several reasons for this, including:
From a MIDI perspective, it's easy to represent these relationships using the 12 semitones of the chromatic scale instead of the 7 notes (and 5 sharps or flats) of the diatonic scale.
Starting at C on the piano keyboard, we can count the number of black and white keys to get the number of semitones in the third and fifth:
var majorChord = [ 0, 4, 7 ]; // semitone intervals in major chord
var minorChord = [ 0, 3, 7 ]; // semitone intervals in minor chord
These majorChord and minorChord patterns can be applied to any note to create a major or minor chord with that note as the root of the chord.
Just like scales, the internal interval pattern determines the emotional nuance of a chord:
Note that both chords span an exact total of 7 semitones from Root to Fifth. The only difference is the position of the middle note (the Third). Moving that single middle note by just 1 semitone alters the entire emotional landscape.
Major and minor are just two types of chords. We'll cover other types of chords and scale-based chords (Roman numeral notation) in another example.
Press the Copy button below to copy the example to the
clipboard.
// Example: chords.js - Play chords from the diatonic scale
var C3 = 48; // MIDI note 48 is C in the 3rd octave on a piano
var majorChord = [ 0, 4, 7 ]; // semitone intervals in major chord
var minorChord = [ 0, 3, 7 ]; // semitone intervals in minor chord
var channel = 0; // Channels are in range 0 to 15 (MIDI channel 1 to 16)
var velocity = 80; // MIDI velocity is in the range 0 to 127 (note volume)
var delay_ms = 0; // Delay in milliseconds before playing note (for swing)
var duration = 2000; // Chord duration in milliseconds
/**
* Plays a chord starting at the specified root note
*
* @param {number[]} chordType - An array of chord intervals (e.g. minor, major)
* @param {number} root - The starting point for the chord
*/
function playChord(chordType, root)
{
var i;
var note;
// Start notes with no delay to play them simultaneously
for (i = 0; i < chordType.length; i++) {
note = root + chordType[i];
print("Playing note " + note);
ws.fire("note_on", channel, note, velocity, delay_ms);
}
// Pause to let all the notes ring out together
ws.sleep(duration);
// Stop notes to silence the chord
for (i = 0; i < chordType.length; i++) {
note = root + chordType[i];
ws.fire("note_off", channel, note);
}
}
function runExample()
{
print("Playing C-Major chord");
playChord(majorChord, C3);
// Pause for a quarter of a second to let the chord release
ws.sleep(250);
print("Playing C-minor chord");
playChord(minorChord, C3);
}
runExample();