# How to calculate the frequencies of musical notes

*I'm not a musician, and my musical education is basically a "who composed what" type of education. A true musician may find this blog post appalling but I think it may be useful for the Web Audio API fans out there.*

#### A4 or the pitch standard

A4 or the pitch standard (also known as the Stuttgart pitch) has a frequency of 440Hz, and serves as a general tuning standard for musical pitch.

The frequencies of all the other notes can be calculated based on the frequency of A4.

#### The octaves

An A3 note is one octave lower than A4, and A5 is an octave higher. The frequency of A3 is 220Hz (half the frequency of A4), and the frequency of A5 is the double (880Hz).

A0 = 27.5Hz ( A1/2 )

A1 = 55Hz ( A2/2 )

A2 = 110Hz ( A3/2 )

A3 = 220Hz ( A4/2 )

A4 = 440Hz

A5 = 880Hz ( A4*2 )

A6 = 1760Hz ( A5*2 )

A7 = 3520Hz ( A6*2 )

In JavaScript:

` ````
const A4 = 440;
const A3 = A4/2;
//const A2 = A3/2;
//const A2 = A4/(2*2);
//const A2 = A4/(Math.pow(2,2));
const A2 = A4*(Math.pow(2,-2));
const A5 = A4*2;
//const A6 = A5*2;
//const A6 = A4*2*2;
const A6 = A4*(Math.pow(2,2));
```

You get the idea. A for loop would do a very good job:

` ````
const A4 = 440;
const A = [];
for(let i = -4; i < 4; i++){
let a = A4 * Math.pow(2, i);
A.push(a);
}
```

The result is the following array:

[27.5, 55, 110, 220, 440, 880, 1760, 3520];

I don't go below 27.5Hz since the lowest human threshold of hearing is between 16 and 32Hz. I don't go higher than 3520Hz since the next one would be 7040Hz and the highest frequency on a piano 4186Hz. But if you want you can.

#### The notes

Each octave has 12 notes:

C, C#, D, D#, E, F, F#, G, G#, A, A#, B

To calculate the distance between each note in JavaScript:

` ````
const A4Sharp = A4 * Math.pow(2, 1/12);
const B4 = A4 * Math.pow(2, 2/12);
```

While:

` ````
const G4Sharp = A4 * Math.pow(2, -1/12);
const G = A4 * Math.pow(2, -2/12);
```

Knowing this I can create an object for each octave:

` ````
class Octave{
constructor(a){
this.b = a * Math.pow(2, 2/12);
this.aSharp = a * Math.pow(2, 1/12);
this.a = a;
this.gSharp = a * Math.pow(2, -1/12);
this.g = a * Math.pow(2, -2/12);
this.fSharp = a * Math.pow(2, -3/12);
this.f = a * Math.pow(2, -4/12);
this.e = a * Math.pow(2, -5/12);
this.dSharp = a * Math.pow(2, -6/12);
this.d = a * Math.pow(2, -7/12);
this.cSharp = a * Math.pow(2, -8/12);
this.c = a * Math.pow(2, -9/12);
}
}
```

Now it's possible to create several octaves like this:

` ````
let scale = []
for(let i = -4; i < 4; i++){
let a = A4 * Math.pow(2, i);
let octave = new Octave(a);
scale.push(octave);
}
```

The array `scale`

is now the array of all the octaves created, and this is a graphical representation of the frequencies of musical notes.

To get the frequency for C5 - for example - you write:

` ````
console.log(scale[5].c);
```

And if you need the array of all the notes you can write:

` ````
let notes = [];
scale.map((o) => {
for( var note in o){
notes.push( o[note]);
}
});
```

And then you have to sort them because in JavaScript an object is an unordered collection of properties:

` ````
notes.sort((a,b) => a - b);
```

Done!

You may grab the code from this pen:

Also you may compare the results with this list: Physics of Music – Notes

#### Where do I use all this stuff?

I'm using the calculated frequencies of the musical notes in this pen:

but you can come with new ideas, you can create a virtual piano, o even compose some music. Thank you for reading and I hope you find it useful.

nice! And thank you for sharing with us! I have no idea how to use it yet. But looks pretty interesting and enjoyable to play with.

Thank you very much for this post! I have been working on a tuner app for Android, and your post was very helpful. If you are curious, you can see the project here. In particular, the code in the ChromaticOctave and MusicalNote classes located here is based on the code in this post.

Also, as someone who has been playing music for more than 20 years, nothing in the post is appalling. Once again, thank you!