Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #param format is "note,octave,beat" like "b,4,h" or for a rest its "r,h"
- function play-music {
- [cmdletbinding()]
- param(
- [parameter(mandatory = $true)]
- [array]$note
- )
- $notes = "c","c#","d","d#","e","f","f#","g","g#","a","a#","b"
- #credit to /u/wrktrway for this method
- $beat = @{
- 'w.' = 2000
- w = 1600
- 'h.' = 1000
- h = 800
- 'q.' = 600
- q = 400
- 'e.' = 300
- e = 200
- 's.' = 150
- s = 100
- }
- foreach($n in $note){
- $letter = $n.split(",")[0]
- $octave = $n.split(",")[1] #or time for rests
- $timing = $n.split(",")[2]
- if($letter -like "r"){
- start-sleep -milliseconds ($beat[$octave]) #.octave is the time for the rest because im lazy.
- }else{
- #The following formulas are one that i figured out, and the other was from wikipedia.
- #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
- #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.
- #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)
- #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
- $freq = [math]::round((440 * ([math]::pow(2,((($octave - 1) * 12) + (($notes.indexof($letter) + 4)) - 49) / 12))),2)
- [console]::Beep($freq,($beat[$timing]))
- }
- }
- }
- 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