Advertisement
Guest User

Untitled

a guest
Aug 1st, 2018
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #param format is "note,octave,beat" like "b,4,h" or for a rest its "r,h"
  2.  
  3. function play-music {
  4.  
  5.     [cmdletbinding()]
  6.     param(
  7.         [parameter(mandatory = $true)]
  8.         [array]$note
  9.     )
  10.  
  11.     $notes = "c","c#","d","d#","e","f","f#","g","g#","a","a#","b"
  12.  
  13.     #credit to /u/wrktrway for this method
  14.     $beat = @{
  15.         'w.' = 2000
  16.         w    = 1600
  17.         'h.' = 1000
  18.         h    = 800
  19.         'q.' = 600
  20.         q    = 400
  21.         'e.' = 300
  22.         e    = 200
  23.         's.' = 150
  24.         s    = 100
  25.     }
  26.  
  27.     foreach($n in $note){
  28.  
  29.         $letter = $n.split(",")[0]
  30.         $octave = $n.split(",")[1] #or time for rests
  31.         $timing = $n.split(",")[2]
  32.  
  33.         if($letter -like "r"){
  34.  
  35.             start-sleep -milliseconds ($beat[$octave]) #.octave is the time for the rest because im lazy.
  36.  
  37.         }else{
  38.  
  39.             #The following formulas are one that i figured out, and the other was from wikipedia.
  40.             #the formula to get the frequency is: f = 440(2^(n-49/12)) where n is the numeric value of the note on the key board 1 trough 88 for a standard key board
  41.             #the formula that i figured out to sovle for n so as to not need a look up table for the notes/octaves to frequency relation is: n = 12(octave - 1)+(index+4) where index is the note index of the $notes array starting with c.
  42.             #i cant remember why i made the array start with c instead of a like the note index chart..... (maybe because the octave doesnt corespond directly with a multiple of 12... idk i figured it out once and forgot)
  43.             #so octave - 1 * 12 is meant to start the count at the begining number of each octave then add the index of the note and add 4 because the keyboard index starts on a not c
  44.  
  45.             $freq = [math]::round((440 * ([math]::pow(2,((($octave - 1) * 12) + (($notes.indexof($letter) + 4)) - 49) / 12))),2)
  46.             [console]::Beep($freq,($beat[$timing]))
  47.         }
  48.     }
  49. }
  50.  
  51. play-music -note 'b,4,q','r,s','a#,5,q','r,s','g#,5,w.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement