This is part of a bigger project that I’ll write up at some later date. These functions from it might be of some use to anyone who wants to identify a pitch from an audio input. The whole topic of pitch identification is a long story with lots of maths, but eventually you will want to convert between a frequency and a note.

Note Index

These functions are intended within the scope of all the notes on a standard keyboard – using the SPN notation, c0 through B8 (108 notes) where c0 has noteIndex 0, and b8 has noteIndex 107, and use a tempered scaled based on the standard A4 at 440hz.

You can supply either a frequency, or a note index, and get back this ‘note’ object. Both sharp and flat scale notations are given so you can use the appropriate one for the key. This one is for Ab (or G#) at SPN7.

The code

Given a note index (0-107), make a note object

Figure out the frequency of a given note index

Generate a note object for the nearest note that corresponds to this frequency

For more like this see Google Apps Scripts Snippets
Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.