r/PowerShell • u/wrktrway • Jul 06 '18
Script Sharing I wrote a PowerShell script that uses the Console class Beep() method and allows you to write songs in musical notion. Play-Notes.ps1
It all started when a coworker shared an article (which I sadly cannot find anymore) about the evolution of the Beep() method in computers and why the Beep does not come from your motherboard speaker anymore (because it taking up space in MOBO firmware, now it's in the OS).
Then I realized you could call the method for Beep() in PowerShell!
[Console]::Beep(400,400)Any like many others I immediately googled around to find songs people have made. There are even a few custom C# functions to help write songs easier, but they all seemed pretty short hand or just a chain of the simple beep commands.
So I wrote my own script to allow you to write songs faster using 'standard' musical notation!
If you want to test it out, you can play the song "Still Alive" from Portal by copying it from the README.md!
When I get time, I plan to:
Add a param for key changes
Make output paste-able so you can embed songs into scripts easier
Let me know what y'all think and please share any songs you make today during your lunch time.. have fun! ;)
EDIT: For the lazy, copy and paste the following into PowerShell!
Function Play-Notes {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[string]$Notes,
[Parameter(Mandatory = $false)]
[int]$Tempo,
[Parameter(Mandatory = $false)]
[switch]$Output = $false
)
$NoteTypes = [pscustomobject]@{
# W = Whole, H = Half, Q = Quarter, E = Eighth, S = Sixteenth
'W'=1600;'W.'=2000;'H'=800;'H.'=1000;'Q'=400;'Q.'=600;'E'=200;'E.'=300;'S'=100;'S.'=150
}
$NoteIndex = [pscustomobject]@{
'C' = @(16.35,32.7,65.41,130.8,261.6,523.3,1047,2093,4186)
'C#' = @(17.32,34.65,69.3,138.6,277.2,554.4,1109,2217,4435)
'D' = @(18.35,36.71,73.42,146.8,293.7,587.3,1175,2349,4699)
'Eb' = @(19.45,38.89,77.78,155.6,311.1,622.3,1245,2489,4978)
'E' = @(20.6,41.2,82.41,164.8,329.6,659.3,1319,2637,5274)
'F' = @(21.83,43.65,87.31,174.6,349.2,698.5,1397,2794,5588)
'F#' = @(23.12,46.25,92.5,185,370,740,1480,2960,5920)
'G' = @(24.5,49,98,196,392,784,1568,3136,6272)
'G#' = @(25.96,51.91,103.8,207.7,415.3,830.6,1661,3322,6645)
'A' = @(27.5,55,110,220,440,880,1760,3520,7040)
'Bb' = @(29.14,58.27,116.5,233.1,466.2,932.3,1865,3729,7459)
'B' = @(30.87,61.74,123.5,246.9,493.9,987.8,1976,3951,7902)
'R' = '0'
}
foreach ($Note in ($Notes -split ',')){
$Note -match '(?<Pitch>[A-G][#|b]?|[R])(?<Octave>[0-8])?(?<NoteType>[Ww|Hh|Qq|Ee|Ss][\.]?)?' | Out-Null
$Pitch = $matches['Pitch']
if($matches['NoteType'] -eq $null){
if($Tempo){
[int]$Durration = 100/$Tempo*400
}else{
[int]$Durration = 400
}
}else{
if($Tempo){
[int]$Durration = 100/$Tempo*($NoteTypes.$($matches['NoteType']))
}else{
[int]$Durration = $NoteTypes.$($matches['NoteType'])
}
}
[int]$Frequency = switch ($matches['Octave']) {
0 {$NoteIndex.$Pitch} # Beep() does not support any frequencies lower than 38
1 {$NoteIndex.$Pitch | Where-Object {$_ -ge 32 -and $_ -le 62}} # using <38 for Rests
2 {$NoteIndex.$Pitch | Where-Object {$_ -ge 65 -and $_ -le 124}}
3 {$NoteIndex.$Pitch | Where-Object {$_ -ge 130 -and $_ -le 247}}
4 {$NoteIndex.$Pitch | Where-Object {$_ -ge 261 -and $_ -le 494}}
5 {$NoteIndex.$Pitch | Where-Object {$_ -ge 523 -and $_ -le 988}}
6 {$NoteIndex.$Pitch | Where-Object {$_ -ge 1047 -and $_ -le 1978}}
7 {$NoteIndex.$Pitch | Where-Object {$_ -ge 2093 -and $_ -le 3952}}
8 {$NoteIndex.$Pitch | Where-Object {$_ -ge 4186 -and $_ -le 7902}}
default {$NoteIndex.$Pitch | Where-Object {$_ -ge 523 -and $_ -le 988}}
}
if($Output){
($Pitch+$matches['Octave']+$matches['NoteType']+' - '+"${Durration}"+' - '+"${Frequency}")
}
if($Pitch -eq 'R'){
Start-Sleep -Milliseconds $Durration
}
else{
[console]::beep($Frequency,$Durration)
}
$Note = $null
$Pitch = $null
$Durration = $null
$Frequency = $null
}
$Tempo = $null
}
Play-Notes -Notes "R0H,G6E,F#6E,E6E,E6E,F#6H,R0H,R0Q,R0E,A5E,G6E,F#6E,E6E,E6E,F#6Q.,D6Q,E6E"
Play-Notes -Notes "A5H,R5E,R0Q.,A5E,E6Q,F#6E,G6Q.,E6E,C#6Q,D6Q.,E6Q,A5E,A5Q,F#6Q.,R0H"
Play-Notes -Notes "R0H,G6E,F#6E,E6E,E6E,F#6H,R0H,R0Q,R0E,A5E,G6E,F#6E,E6E,E6Q,F#6E,D6Q.,E6E"
Play-Notes -Notes "A5H,R5E,R0Q.,E6Q,F#6E,G6Q.,E6E,C#6Q.,D6E,E6Q,A5E,D6E,E6E"
Play-Notes -Notes "F6E,E6E,D6E,C6E,R0Q,A5E,Bb5E,C6Q,F6Q,E6E,D6E,D6E,C6E,D6E,C6E,C6Q,C6Q,A5E,Bb5E"
Play-Notes -Notes "C6Q,F6Q,G6E,F6E,E6E,D6E,D6E,E6E,F6Q,F6Q,G6E,A6E,Bb6E,Bb6E,A6Q,G6Q,F6E,G6E"
Play-Notes -Notes "A6E,A6E,G6Q,F6Q,D6E,C6E,D6E,F6E,F6E,E6Q,E6E,F#6E,F#6Q."
Play-Notes -Notes "A6E,A6E,G6Q,F6Q,D6E,C6E,D6E,F6E,F6E,E6Q,E6E,F#6E,F#6H"
Play-Notes -Notes "G6E,A6E,A6Q,R0Q,R0E,G6E,F#6E,F#6Q"
Play-Notes -Notes "G6E,A6E,A6Q,R0Q,R0E,G6E,F#6E,F#6Q"11
u/blownart Jul 06 '18
It's sad that this does not use the motherboard speaker...
5
u/wrktrway Jul 06 '18
I really really wish I could find that old article/blog post about it.
But it simply was just a piece of legacy firmware that was mandatory in most mobos that was actually taking up valuable space in a crowded world. Also when the mobo beep happened it actually slowed down processes on your computer! haha.
So it got replaced by a software function and only plays through speakers. They wouldn't have it at all if it wasn't for some ADA like regulations.
9
u/DrCubed Jul 06 '18
Video of it in action for the really lazy: Streamable, Direct link.
To make output pasteable, you could try encoding the parameters in Base64.
A fun challenge could be making a MIDI parser which would format the notes in the format for this script.
5
4
8
u/MrSenator Jul 06 '18
This is great! Awesome job, OP.
I took the liberty of attempting to transcribe a few ditties from some piano music. I know nothing about music so this actually helped me learn about musical timing, etc, a little bit better.
Here's my stuff:
Jurassic Park
play-notes -notes "d4e, c4e, d4h"
play-notes -notes "d4e, c4e, d4h"
play-notes -notes "d4e, c4e, d4h"
play-notes -notes "e4q, e4h, g4q, g4h"
play-notes -notes "f4e, d4e, e4q, c4e, a3h"
play-notes -notes "f4e, d4e, e4q"
play-notes -notes "a4e, d4e, g4q, f4e, f4q, e4e, e4h"
play-notes -notes "d4e, c4e, d4q, a3q, g3h"
play-notes -notes "d4e, c4e, d4q, a3q, g3h"
play-notes -notes "d4e, c4e, c4e, d4h, a3q, d3q, c4h"
play-notes -notes "d4e, c4e, d4q, a3q, g3h"
play-notes -notes "d4e, c4e, d4q, a3q, g3h"
play-notes -notes "d4e, c4e, c4e, d4h, a3q, d3q, c4w"
Pokemon (just the opening of the intro)
play-notes -notes "A5E, A5E, A5E, A5, A5, G5Q, E5E, C5"
play-notes -notes "R0, C5, A5, A5E, G5Q, F5E, G5"
play-notes -notes "R0, F5, B5b, B5b, B5be, A5, G5e, F"
play-notes -notes "F5, A5, A5, G5e, F5e, A5H"
play-notes -notes "A5e, A5, A5e, A5, A5e, G5, E5e, C5"
play-notes -notes "A5e, A5h, G5, F5e, G5h"
play-notes -notes "B5, B5e, B5e, B5, B5, A5e, G5, F5"
play-notes -notes "F5, A5e, A5, G5h, F5e, A5h"
Aerith's Theme
play-notes -notes "F3#q, A3q, D4w"
play-notes -notes "c4q, a3q, e3w"
play-notes -notes "f3#q, a3q, d4, c4, e4, d4, b3, c4q, a3w, e3w"
play-notes -notes "F3#q, A3q, D4w"
play-notes -notes "c4q, a3q, e3w"
play-notes -notes "d3q, e3q, d3q, R0q, f3q, e3q, d3q, e3q, d3w"
Final Fantasy VII Victory Fanfare
play-notes -notes "c5e, c5e, c5e, c5q, G4, A4, c5q, A4e, C5w"5
3
u/MrSenator Jul 07 '18
Adding Gerudo Valley:
play-notes -notes "C4e, F4e, G4e, A4q, C4e, F4e, G4e, A4h" play-notes -notes "D4e, F4e, G4e, A4q, D4e, F4e, G4e, A4h" play-notes -notes "b3e, E4e, F4e, G4q, B3e, E4e, F4e, G4h, F4e, G4e, F4e, F4h" play-notes -notes "C4e, F4e, G4e, A4q, C4e, F4e, G4e, A4h" play-notes -notes "D4e, F4e, G4e, A4q, D4e, F4e, G4e, A4h" play-notes -notes "b3e, E4e, F4e, G4q, B3e, E4e, F4e, G4h, a4e, b4e, a4e, G4h"
7
u/aaroneuph Jul 06 '18
As a music major who now makes his living posh-ing, take my upvote.
4
u/wrktrway Jul 06 '18
You are my target Audience XD
I played trumpet for 6 years but always loved writing/transposing music more.
9
u/Suihaki Jul 06 '18 edited Jul 06 '18
I copied the code and ran it and when I heard the song I flipped my shit. That's awesome.
EDIT: I'm currently trying to use this to create Flight of the Valkyries. It's coming along once I get the beats correct.
2
u/Suihaki Jul 06 '18
My coworkers are going to get so annoyed at how many times I play this today. Thanks!
2
u/wrktrway Jul 06 '18
This makes me so happy! Haha please share when you complete something! :)
7
u/Suihaki Jul 06 '18
BTW, this could TOTALLY be turned into a subreddit where people make songs and share lol
6
u/Suihaki Jul 06 '18
This is as much as I've been able to fit in between actually trying to work today. lol
Play-Notes -Notes "A5E,D6E,A5S,D6S,F6H,D6Q,F6E,D6S,F6S,A6H,F6Q,A6E,F6S,A6S,C7H,C6Q,F6E,C6S,F6S,A6H"
3
u/fourierswager Jul 06 '18
This is simultaneously the best and worst thing ever :)
3
u/wrktrway Jul 06 '18
I am very aware of the fact that there are literally hundreds of apps with better music writing software out there. XD This one is mine though!
3
u/fourierswager Jul 06 '18
No, no, I just meant that you could really annoy your co-workers with this. I didn't mean to imply that what you wrote wasn't up-to-snuff. It's really cool!
3
u/proudcanadianeh Jul 06 '18
As someone with no musical skills what so ever, could someone pleeeease do the notes for Never gonna give you up?
3
3
u/ka-splam Jul 08 '18
Do you know about the Nokia RTTTL (Ring Tone Text Transfer Language)? It's more or less what you've made; can't tell if you're implementing that or not.
Benefit of that version is, you can google "RTTTL tunes" and get thousands of other people's tunes from around the web.
(I made a PS player for it a while ago, basically the same approach as your code - regex match and beep - https://www.reddit.com/r/PowerShell/comments/5108xf/a_couple_of_tunes/d78dzui/ )
1
2
Jul 06 '18
Now I want to see a module that lets me do the final fantasy thing. Excellent work op!
3
u/wrktrway Jul 06 '18
I am working on it! Wills hare here when completed, but I have normal work to do for now, heh
3
Jul 06 '18
Can you explain how I should copy the song from the README ? I don't get it.
3
u/wrktrway Jul 06 '18
Download the repo some place
# Change dir cd C:\the\folder\the\script\lives\ <#PASTE SNIPPIT FROM README HERE#>2
Jul 06 '18
Alright can you describe how to do it in details because whoah rn I feel like I can't use a computer, but I really wanna test your thing it sounds v sweet
2
u/wrktrway Jul 06 '18
I made an edit, just copy and paste the code into powershell
3
Jul 06 '18
It's actually silent to me. It does seem like it's playing due to the delay and the lack of error, but it is indeed silent. P sure my speaker works too.
→ More replies (5)2
11
8
u/TheIncorrigible1 Jul 08 '18 edited Jul 08 '18
Made some improvements on your work. Namely, the typing and null setting you're doing doesn't actually accomplish anything. This also supports pipeline input. Creating a hashtable for instant lookups on octaves would probably be faster than parsing each note which could cause inconsistencies in songs. Further improvement suggestion would be creating types for the notes like:
static class Note { ... }Anyways, here's the code:
function Send-Note {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory, ValueFromPipeline)]
[string] $NoteList,
[Parameter()]
[int] $Tempo,
[Parameter()]
[switch] $Output,
[Parameter(DontShow)]
[hashtable] $NoteTypes = @{
'W' = 1600
'W.' = 2000
'H' = 1000
'H.' = 1500
'Q' = 400
'Q.' = 600
'E' = 200
'E.' = 300
'S' = 100
'S.' = 150
},
[Parameter(DontShow)]
[hashtable] $NoteIndex = @{
'C' = @(16.35,32.7,65.41,130.8,261.6,523.3,1047,2093,4186)
'C#' = @(17.32,34.65,69.3,138.6,277.2,554.4,1109,2217,4435)
'D' = @(18.35,36.71,73.42,146.8,293.7,587.3,1175,2349,4699)
'Eb' = @(19.45,38.89,77.78,155.6,311.1,622.3,1245,2489,4978)
'E' = @(20.6,41.2,82.41,164.8,329.6,659.3,1319,2637,5274)
'F' = @(21.83,43.65,87.31,174.6,349.2,698.5,1397,2794,5588)
'F#' = @(23.12,46.25,92.5,185,370,740,1480,2960,5920)
'G' = @(24.5,49,98,196,392,784,1568,3136,6272)
'G#' = @(25.96,51.91,103.8,207.7,415.3,830.6,1661,3322,6645)
'A' = @(27.5,55,110,220,440,880,1760,3520,7040)
'Bb' = @(29.14,58.27,116.5,233.1,466.2,932.3,1865,3729,7459)
'B' = @(30.87,61.74,123.5,246.9,493.9,987.8,1976,3951,7902)
'R' = '0'
}
)
process {
foreach ($Note in ($NoteList -split ',')) {
$null = $Note -match '(?<Pitch>[A-G][#|b]?|[R])(?<Octave>[0-8])?(?<NoteType>[w|h|q|e|s]\.?)?'
$Pitch = $matches['Pitch']
$Octave = $matches['Octave']
$NoteType = $matches['NoteType']
$Duration = if ($NoteType) {
if ($Tempo) {
100 / $Tempo * $NoteTypes[$NoteType]
} else {
$NoteTypes[$NoteType]
}
} else {
if ($Tempo) {
100 / $Tempo * 400
} else {
400
}
}
$Frequency = switch ($Octave) {
0 { $NoteIndex[$Pitch] } # Beep() does not support any frequencies lower than 38
1 { $NoteIndex[$Pitch].Where{$_ -ge 32 -and $_ -le 62} } # using <38 for Rests
2 { $NoteIndex[$Pitch].Where{$_ -ge 65 -and $_ -le 124} }
3 { $NoteIndex[$Pitch].Where{$_ -ge 130 -and $_ -le 247} }
4 { $NoteIndex[$Pitch].Where{$_ -ge 261 -and $_ -le 494} }
5 { $NoteIndex[$Pitch].Where{$_ -ge 523 -and $_ -le 988} }
6 { $NoteIndex[$Pitch].Where{$_ -ge 1047 -and $_ -le 1978} }
7 { $NoteIndex[$Pitch].Where{$_ -ge 2093 -and $_ -le 3952} }
8 { $NoteIndex[$Pitch].Where{$_ -ge 4186 -and $_ -le 7902} }
default { $NoteIndex[$Pitch].Where{$_ -ge 523 -and $_ -le 988} }
}
if ($Output.IsPresent) {
"$Pitch$Octave$NoteType - $Duration - $Frequency"
}
if ($Pitch -eq 'R') {
Start-Sleep -Milliseconds $Duration
} else {
[console]::beep($Frequency, $Duration)
}
}
}
}7
Jul 06 '18
If youre an idiot like me, this wont work if you have all sounds disabled in the Sounds menu.
This is top notch OP
5
u/nothingpersonalbro Jul 06 '18
Nice, eagerly awaiting someone to make the PowerShell version of Despacito now
3
u/Lightofmine Jul 06 '18 edited Jul 06 '18
This is awesome. I am going to play a song on my coworkers machine monday. THANK YOU!
Edit:
Is there something wrong with my notes? Does it have to be in 4/4 or can it play whatever?I cannot get it to play this sequence of notes at the end, but when I comment out the top two lines it will play.
Play-Notes -Notes "R0Q,B6E,A6Q,B6Q,G6Q,B6E,G6E,A6E,B6Q.R0Q"
Play-Notes -Notes "B6E,A6Q,B6Q,G6Q,R0E,G6E,A6Q,G6Q,R0Q,B6Q,A6Q,G6Q"
Play-Notes -Notes "E6E,G6E,A6E,D6E,G6E,A6E,C6E,G6E,A6E"Figured it out..Dolby Atmos for Headphones...Once you turn it off it plays at the correct tempo and plays all of the notes.
2
2
2
Nov 07 '18
The Star Spangled Banner
Play-Notes -Notes "C4E,A3E,F3Q,A3Q,C4Q,F4H,A4E,G4E,F4Q,A3Q,B3Q,C4H,C4E,C4E"
Play-Notes -Notes "A4Q.,G4E,F4Q,E4H,D4E,E4E,F4Q,F4Q,C4Q,A3Q,F3Q,C4E,A3E"
Play-Notes -Notes "F3Q,A3Q,C4Q,F4H,A4E,G4E,F4Q,A3Q,B3Q,C4H,C4E,C4E"
Play-Notes -Notes "A4Q.,G4E,F4Q,E4H,D4E,E4E,F4Q,F4Q,C4Q,A3Q,F3Q,A4E,A4E"
Play-Notes -Notes "A4Q,Bb4Q,C5Q,C5H,Bb4E,A4E,G4Q,A4Q,Bb4Q,Bb4H,Bb4Q"
Play-Notes -Notes "A4Q.,G4E,F4Q,E4H,D4E,E4E,F4Q,A3Q,B3Q,C4H,C4Q"
Play-Notes -Notes "F4Q,F4Q,F4E,E4E,D4Q,D4Q,D4Q,G4Q,Bb4E,A4E,G4E,F4E,F4Q,E4H,R0Q,C4E,C4E"
Play-Notes -Notes "F4Q.,G4E,A4E,Bb4E,C5H,F4E,G4E,A4Q.,Bb4E,G4Q,F4H"
1
1
u/TotesMessenger Jul 07 '18
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
[/r/u_azuredude] I wrote a PowerShell script that uses the Console class Beep() method and allows you to write songs in musical notion. Play-Notes.ps1
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
1
u/Evanthecat99_rip Dec 22 '25
I know this post is 8 years old... but daisy bell/bicycle built for two
1
u/codylilley Jul 06 '18
Ooh, somebody should make it play “Still Alive” https://youtu.be/FENROFZd9nE
2
Jul 06 '18
I may try to do it while I'm working this weekend. I have to build lesson plans for our lunch time learning lessons so I'll be deep in it anyway.
2
0
Jul 06 '18
[deleted]
2
u/wrktrway Jul 06 '18
I was going to use Invoke-Notes as the name, but decided it was too boring sounding. Play-Notes has more style to it, imo at least
0
32
u/wrktrway Jul 06 '18
Some suggested uses..
Having a melody play when your POSH profile loads
Pranking a coworker by playing a song they hate in Beeps on their machine
Having the Final Fantasy Victory Fanfare play at the end of a long cludgey script you run interactively