Showing posts with label Music. Show all posts
Showing posts with label Music. Show all posts

Wednesday, May 29, 2019

Playing music with PowerShell

While continuing my journey to continue learning Microsoft PowerShell, I thought back to the early days of making music with computers. I have worked on many projects throughout the years, using several languages, and making simple music has been a repeated challenge that I have used to help learn timing, loops, functions and debugging. While doing so in PowerShell, I located a site that had already published frequency information at https://sl.advdat.com/NotesAndFrequencies, the related .NET information at https://sl.advdat.com/2WghUEV  and finally the PowerShell call '[System.Console]::Beep(frequency,length in milliseconds)'.  After compiling this information, I took the examples I found and produced the following code:

##########Begin Script##############


# Notes
$Dsharp1 = 38.891
$E1 = 41.203
$F1 = 43.654
$Fsharp1 = 46.249
$G1 = 48.999
$Gsharp1 = 51.913
$A1 = 55
$Asharp1 = 58.27
$B1 = 61.735

$C2 = 65.406
$Csharp2 = 69.296
$D2 = 73.416
$Dsharp2 = 77.782
$E2 = 82.407
$F2 = 87.307
$Fsharp2 = 92.499
$G2 = 97.999
$Gsharp2 = 103.826
$A2 = 110
$Asharp2 = 116.541
$B2 = 123.471

$C3 = 130.813
$Csharp3 = 138.591
$D3 = 146.832
$Dsharp3 = 155.563
$E3 = 164.814
$F3 = 174.614
$Fsharp3 = 184.997
$G3 = 195.998
$Gsharp3 = 207.652
$A3 = 220
$Asharp3 = 233.082
$B3 = 246.942

$C4 = 261.626 #Middle C
$Csharp4 = 277.183
$D4 = 293.665
$Dsharp4 = 311.127
$E4 = 329.628
$F4 = 349.228
$Fsharp4 = 369.994
$G4 = 391.995
$Gsharp4 = 415.305
$A4 = 440
$Asharp4 = 466.164
$B4 = 493.883

$C5 = 523.251
$Csharp5 = 554.365
$D5 = 587.33
$Dsharp5 = 622.254
$E5 = 659.255
$F5 = 698.456
$Fsharp5 = 739.989
$G5 = 783.991
$Gsharp5 = 830.609
$A5 = 880
$Asharp5 = 932.328
$B5 = 987.767

$C6 = 1046.502
$Csharp6 = 1108.731
$D6 = 1174.659
$Dsharp6 = 1244.508
$E6 = 1318.51
$F6 = 1396.913
$Fsharp6 = 1479.978
$G6 = 1567.982
$Gsharp6 = 1661.219
$A6 = 1760
$Asharp6 = 1864.655
$B6 = 1975.533

$C7 = 2093.005
$Csharp7 = 2217.461
$D7 = 2349.318
$Dsharp7 = 2489.016
$E7 = 2637.021
$F7 = 2793.826
$Fsharp7 = 2959.955
$G7 = 3135.964

# Note Types 60bpm Whole=4000ms

$w=4000 #whole note
$h=$w/2 #half note
$q=$h/2 #quarter note
$e=$q/2 #eighth note
$s=$e/2 #sixteenth note

#Play Middle C
[System.Console]::Beep($C4,$W) # Middle C

Function CMScale{

[System.Console]::Beep($c4,$q)
[System.Console]::Beep($d4,$q)
[System.Console]::Beep($e4,$q)
[System.Console]::Beep($F4,$q)
[System.Console]::Beep($g4,$q)
[System.Console]::Beep($a4,$q)
[System.Console]::Beep($b4,$q)
[System.Console]::Beep($c5,$q)
}


#Call Function
CMScale
##########End Script##############

With this information, you too can create simple music on the console. I have been working through translating some old simplified sheet music, just to see what I can do. It's a bit more enjoyable than the old Hello World scripts we have usually started with. However, I have noted several examples on GitHub as well:

Tetris - https://gist.github.com/HelgeSverre/33361e8a283624dfbbd6
SuperMario - https://gist.github.com/davewilson/5612674

Happy scripting!