r/PowerShell • • 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"
219 Upvotes

59 comments sorted by

View all comments

Show parent comments

3

u/[deleted] 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.

2

u/wrktrway Jul 06 '18

Do you have your speakers muted or volume up?

2

u/[deleted] Jul 06 '18

Volume rly up

2

u/wrktrway Jul 06 '18

What version of PowerShell do you have?

2

u/[deleted] Jul 06 '18

5.1.14393.1198. Also, W10 64bits.

2

u/Lee_Dailey [grin] Jul 06 '18

howdy babaduv,

what happens if you open the ISE, put [console]::Beep(1000, 300) in the bottom [console pane], and then hit enter to run it?

take care,
lee

2

u/[deleted] Jul 06 '18

Nothing :/

Is this normal doctor ?

1

u/Lee_Dailey [grin] Jul 06 '18

howdy babaduv,

no it really aint ... [frown] i have no freaking idea how that could happen, either. [sigh ...]

you may want to do some testing in another account on your system. that would let you know if it was account-specific or machine-specific.

how very, very odd! [grin]

take care,
lee

2

u/[deleted] Jul 06 '18

F

3

u/Lee_Dailey [grin] Jul 06 '18

howdy babaduv,

take a look at this thread ...

powershell - Turning off the cmd window beep sound - Super User
— https://superuser.com/questions/10575/turning-off-the-cmd-window-beep-sound

it appears that you can globally turn off the beep via net stop beep. apparently that otta go away on restarting the system, tho. it does imply there is a way to turn the whole thing OFF. you may need to see what non-default settings you have in your BIOS or sound settings in windows.

here's a more direct article ...

How to Disable CMD/PowerShell Beep Sounds in Windows - Error Fixer
— https://errorfixer.co/disable-cmd-powershell-beep-sounds/

another thread about the problem said that the "next win10 update" fixed the problem.

take care,
lee