[bgreco.net]: PS C:\> Get-Hertz

Have you ever wanted to be able get the frequency (in Hz) of notes from the command line?

I thought not. Nevertheless, I wrote a handy function that lets you do just that. All I've used it so far is writing little songs that can be played using [console]::Beep()

Usage: The function takes a single parameter, string representing a note in scientific pitch notation.

Examples of valid inputs: C4 (middle C), A4, D#3, Gb4.

function Get-Hertz([string] $Pitch) {
	$notes = 'Cb', 'C', 'C#|Db', 'D', 'D#|Eb', 'E|Fb', 'E#|F', 'F#|Gb', 'G', 'G#|Ab', 'A', 'A#|Bb', 'B', 'B#'
	$note, $octave = $pitch.Substring(0, $pitch.Length - 1), $pitch[-1]
	$note_offset = $notes.IndexOf(($notes | ? { "|$_|" -like "*|$note|*" })) - 10
	$octave_offset = [int]::Parse($octave) - 4
	$offset = 12 * $octave_offset + $note_offset
	440 * [math]::Pow(2, 1/12 * $offset)
}

Here's an example of the function being used to play the Star Wars theme from the console.

$starwars = '
G3,2 G3,2 G3,2
C4,12 G4,12
F4,2 E4,2 D4,2 C5,12 G4,6
F4,2 E4,2 D4,2 C5,12 G4,6
F4,2 E4,2 F4,2 D4,12 G3,2 G3,2 G3,2
C4,12 G4,12
F4,2 E4,2 D4,2 C5,12 G4,6
F4,2 E4,2 D4,2 C5,12 G4,6
F4,2 E4,2 F4,2 D4,12 G3,4 G3,2
A3,9 A3,3 F4,3 E4,3 D4,3 C4,3
C4,2 D4,2 E4,2 D4,4 A3,2 B3,6 G3,3 G3,3
A3,9 A3,3 F4,3 E4,3 D4,3 C4,3
G4,6 D4,12 G3,3 G3,3
A3,9 A3,3 F4,3 E4,3 D4,3 C4,3
C4,2 D4,2 E4,2 D4,4 A3,2 B3,6 G4,4 G4,2
C5,3 Bb4,3 Ab4,3 G4,3 F4,3 Eb4,3 D4,3 C4,3
G4,18'

foreach($note in (-split $starwars)) {
	$pitch, $duration = $note -split ','
	[console]::Beep((Get-Hertz $pitch), [int]::Parse($duration) * 80)
}