Jingle

Jingle

  • Comments 7

As an attempt to spread some holiday joy, Satish Mathew and I wrote the following script.  Hope you enjoy it! 

- Marcel Ortiz

 ########################################################################################

$notes = write-output `
  4A4 4A4 2A4        4A4 4A4 2A4        4A4 4C4 4F3 8G3             1A4 `
  4Bb4 4Bb4 4Bb4 8Bb4     4Bb4 4A4 4A4 8A4 8A4    4A4 4G3 4G3 4A4    2G3 2C4 `
  4A4 4A4 2A4       4A4 4A4 2A4    4A4 4C4 4F3 4G3     1A4        4Bb4 4Bb4 4Bb4 4Bb4 `
  4Bb4 4A4 4A4 8A4 8A4    4C4 4C4 4Bb4 4G3     1F3     4C3 4A4 4G3 4F3     2C3 8C3 8C3  `
  4C3 4A4 4G3 4F3       1D3      4D3 4Bb4 4A4 4G3     '1E3'    4C4 4C4 4Bb4 4G3 `
  1A4     4C3 4A4 4G3 4F3    1C3     4C3 4A4 4G3 4F3     1D3 `
  4D3 4Bb3 4A4 4G3         4C4 4C4 4C4 8C4 8C4    4D4 4C4 4Bb4 4G3    4F3 2C4   4A4 4A4 2A4 `
  4A4 4A4 2A4      4A4 4C4 4C3 8G3      1A4     4Bb4 4Bb4 4Bb4 8Bb4      4Bb4 4A4 4A4 8A4 8A4 `
  4A4 4G3 4G3 4A4       2G3 2C4     4A4 4A4 2A4     4A4 4A4 2A4    4A4 4C4 4F3 8G3 `
  1A4       4Bb4 4Bb4 4Bb4 4Bb4      4Bb4 4A4 4A4 8A4 8A4      4C4 4C4 4Bb4 4G3     1F3

function Play([int] $freq, [int] $duration)
{
  [console]::Beep($freq, $duration);
}

 

#
# Note is given by fn=f0 * (a)^n
# a is the twelth root of 2
# n is the number of half steps from f0, positive or negative.
# f0 used here is A4 at 440 Hz
#
$f0 = 440;
$a = [math]::pow(2,(1/12)); # Twelth root of 2
function GetNoteFreq([string]$note)
{
  # n is the number of half steps from the fixed note.
  $note -match '([A-G#]{1,2})(\d+)' | out-null
  $octave = ([int] $matches[2]) - 4;
  $n = $octave * 12 + ( GetHalfStepsFromA $matches[1] );
  $freq = $f0 * [math]::Pow($a, $n);

  return $freq;
}

 

function GetHalfStepsFromA([string] $note)
{
  switch($note)
  {
    'A'  { 0 }
    'A#' { 1 }
    'Bb' { 1 }
    'B'  { 2 }
    'C'  { 3 }
    'C#' { 4 }
    'Db' { 4 }
    'D'  { 5 }
    'D#' { 6 }
    'Eb' { 6 }
    'E'  { 7 }
    'F'  { 8 }
    'F#' { 9 }
    'Gb' { 9 }
    'G'  { 10 }
    'G#' { 11 }
    'Ab' { 11 }
  }
}

 

$StandardDuration = 1000;
foreach($note in $notes)
{

  $note -match '(\d)(.+)' | out-null
  $duration = $StandardDuration / ([int] $matches[1]);
  $playNote = $matches[2];
  $freq = GetNoteFreq $playNote;

  write-host $playNote;
  Play $freq $duration
  start-sleep -milli 50
}

Leave a Comment
  • Please add 1 and 6 and type the answer here:
  • Post
  • Hi, Marcel,

    That's a cool jingle all the way to the Xmas. Are all those A1 B2 stuff MIDI signal?

    Ying

  • Hey Ying,

    Nah, no midi.  I wish!  :)  Those are just the notes for the song in our own encoding. The first number is the reciprocal of the duration and the rest is the note and octave (which we use for getting the frequency).  For example, 4A4 is a quarter A note.  Our apologies to musicians for our clumsy interpretation of the music. :)

  • That is great. Will have to show that off at work tomorrow :)

    PowerShell is on my hit list for 2007. Well actually everything .NET is ;)

  • In the display of true geekness, Satish Mathew and Marcel Ortiz have created a PowerShell script that...

  • shameless plugging for something related but ...

    Here's a really simple script to run through all the shared folders in your current domain looking for files of certain type, say *.mp3?

    http://rougedelimiter.blogspot.com/2006/12/powershell-network-scan-script.html

  • Hey Marcel, I wish I could run that script. I remember you fumbling with a guitar back in college, curious to see how much of a grasp of music you really had. Pato!

  • [link] How would one know the translation of notes to the proper format for PowerShell to be able to

Page 1 of 1 (7 items)