Weekend Scripter: Create a Visual Piano with Windows PowerShell

Weekend Scripter: Create a Visual Piano with Windows PowerShell

  • Comments 4
  • Likes

Summary: Use Windows PowerShell to interact with the console display.

Honorary Scripting Guy, Sean Kearney, here—filling in for our good friend, Ed Wilson.

For some reason, I find a need to open today’s blog post like a soap opera—you know, “Last time for Sean Kearney on the Hey, Scripting Guys! Blog.”

Sigh, it feels that way…

Last time, we played with a hash table and keyboard input to create a simple toy to play with—a loud, off-key piano in Windows PowerShell. It is somewhat (but not much) worse than my singing voice.

Today we’re going to polish it off a bit. A little error trapping. And how about an actual keyboard on the screen to interact with our piano?

First, I’ll show you a simple keyboard that I spent all day drawing up (or maybe it was only my lunch hour):

[array]$keyboard=$Null

$keyboard+=""

$keyboard+=" _____________________________________________________________"

$keyboard+=" ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !"

$keyboard+=" ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !"

$keyboard+=" ! !___! !___! ! !___! !___! !___! ! !___! !___! !"

$keyboard+=" !  !  !  !  !  !  !  !  !  !  !"

$keyboard+=" !  !  !  !  !  !  !  !  !  !  !"

$keyboard+=" !_____!_____!_____!_____!_____!_____!_____!_____!_____!_____!"

$keyboard+=""

$keyboard+="  *** Be A True Rockstar and Use WindowsPowerShell *** "

This may not be a fancy keyboard, but it will work nicely. We are going to map the keyboard that we built yesterday to physical positions on the screen.

What am I trying to do?

I am going to make sure that every time I hit a note on the keyboard, a key is highlighted on the screen. So we’ll need to identify the row and column for each note.

The first part is easy. All sharps are on one row (upper), and the rest of the keys are on the lower row. I played with the following combinations to set the positions on the screen:

$Location=$Host.UI.RawUI.cursorposition

$Location.Y=4

$Location.X=10

$Host.UI.RawUI.cursorposition=$Location; WRITE-HOST “*”

By altering values of Y, I was able to determine my rows:

Upper   3

Lower   6

Next, the less tricky part was to figure out the first position for each set of keys. After playing with the previous set of cmdlets and altering the value of X, I was able to determine the starting point for each: the lower row starts at position 6 and the upper row starts at position 9.

From that point, it was a matter of some simple math to determine the remaining columns because each key is exactly 6 points apart.

So now for some fun. Because we need to apply the X and Y coordinates based on the key that is tapped on the piano keyboard, we have to extend our original hash table as follows by adding a column to hold the Y and X values respectively for each key to display:

[array]$piano=$NULL

$piano+=@{"Q"=290,6,6}

$piano+=@{"2"=310,3,9}

$piano+=@{"W"=330,6,12}

$piano+=@{"3"=345,3,15}

$piano+=@{"E"=360,6,18}

$piano+=@{"R"=390,6,24}         

$piano+=@{"5"=415,3,27}

$piano+=@{"T"=440,6,30}

$piano+=@{"6"=460,3,33}

$piano+=@{"Y"=480,6,36}

$piano+=@{"7"=505,3,39}

$piano+=@{"U"=530,6,42}

$piano+=@{"I"=580,6,48}

$piano+=@{"9"=605,3,51}

$piano+=@{"O"=630,6,54}

$piano+=@{"0"=670,3,57}

$piano+=@{"P"=710,6,60}

With the values of the potential X and Y positions mapped to each key, we can search like before. But now, it will return not only the note, but also the Y and X coordinates for each key.

We can use a simple piece of script like the following to obtain the current location, and then write some information to the screen:

$Location=$Host.UI.RawUI.cursorposition

$Location.Y=$keyboarddata[1]
$Location.X=$keyboarddata[2]

$Host.UI.RawUI.CursorPosition=$Location

WRITE-HOST “*”

To return the original cursor position and erase the information, we can simply reset the position:

# Insert a Command we might do to eat up time, like maybe our [console]::beep(500,200)
$Host.UI.RawUI.cursorposition=$Location
WRITE-HOST " "

So really, we’re going to have a loop that runs forever. Or we could insert a special key—maybe such as “ESC”ape to “Escape” the loop?

do {

# Wait until some fool hits the keyboard

# We're not going to show the key on the screen

# And yes, we'll include Shift, Control and all the others

 

$Key=$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

 

# Now flush it away so we don't get a pile of keys

# backing up in the queue (Flush? Backup? Ewww)

 

$Host.UI.RawUI.Flushinputbuffer()

 

# Grab the character from the $key Object and Match

# It Against the Array

 

$KeyboardData=$Piano.($Key.Character)

 

# Now IF we found something of value

# PLAY IT

 

If ($KeyboardData) {

 

$note=$KeyboardData[0]

$Location=$Host.UI.RawUI.cursorposition

$Location.Y=$keyboarddata[1]

$Location.X=$keyboarddata[2]

 

$Host.UI.RawUI.cursorposition=$Location

 

WRITE-HOST "*"

[console]::beep($note,150)

$Host.UI.RawUI.cursorposition=$Location

WRITE-HOST " "

 

}

 

# Key having fun until Somebody tries to ESCape

# Get it? Hit's the ESCape key? Bad Pun?

 

} until ( $key.VirtualKeyCode -eq 27 )

There you have it. A silly but fun project in Windows PowerShell that leverages hash tables and the console.

For some fun trivia, I did actually try to launch multiple beeps through different Windows PowerShell consoles to try to generate chords. No good. Doesn’t work. Although I would love to figure out later how to generate MIDI tones via Windows PowerShell. That could be more interesting.

If you don’t feel like doing a lot of typing, download it here from the TechNet Gallery: A Windows PowerShell Piano.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Sean Kearney,
Honorary Scripting Guy and Windows PowerShell MVP
 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I love it!!!!

    ... Added an octave shifter and made your $keyboard a text block instead of an array and put a cheatsheet in the keyboard layout...

    # Define our lovely and oh so CLASSIC

    # looking ASCII Keyboard!

    $keyboard=@"

      _____________________________________________________________

      !   !   ! !   !   !   !   ! !   ! !   !   !   !   ! !   !   !

      !   ! 2 ! ! 3 !   !   ! 5 ! ! 6 ! ! 7 !   !   ! 9 ! ! 0 !   !

      !   !___! !___!   !   !___! !___! !___!   !   !___! !___!   !

      !     !     !     !     !     !     !     !     !     !     !

      !  q  !  w  !  e  !  r  !  t  !  y  !  u  !  i  !  o  !  p  !

      !_____!_____!_____!_____!_____!_____!_____!_____!_____!_____!

      Up octave = 'a'  Down octave = 'z'

    "@

    # Clear the screen and display

    CLEAR-HOST

    $keyboard

    $Host.UI.RawUI.WindowTitle="Windows PowerShell Rockin' Piano!"

    # Define Keyboard keys and Tone for Notes

    [array]$piano=$NULL

    $piano+=@{"Q"=290,6,6,"q"}

    $piano+=@{"2"=310,3,9,"2"}

    $piano+=@{"W"=330,6,12,"w"}

    $piano+=@{"3"=345,3,15,"3"}

    $piano+=@{"E"=360,6,18,"e"}

    $piano+=@{"R"=390,6,24,"r"}

    $piano+=@{"5"=415,3,27,"s"}

    $piano+=@{"T"=440,6,30,"t"}

    $piano+=@{"6"=460,3,33,"6"}

    $piano+=@{"Y"=480,6,36,"y"}

    $piano+=@{"7"=505,3,39,"7"}

    $piano+=@{"U"=530,6,42,"u"}

    $piano+=@{"I"=580,6,48,"i"}

    $piano+=@{"9"=605,3,51,"9"}

    $piano+=@{"O"=630,6,54,"o"}

    $piano+=@{"0"=670,3,57,"0"}

    $piano+=@{"P"=710,6,60,"p"}

    $octave = 1

    # Now for the Fun part, playing!

    do {

    # Wait until some fool hits the keyboard

    # We're not going to show the key on the screen

    # And yes, we'll include Shift, Control and all the others

    $key=$host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")

    # Now flush it away so we don't get a pile of keys

    # backing up in the queue (Flush? Backup? Ewww)

    $host.ui.rawUi.Flushinputbuffer()

    #Octave shifter

    if($key.character -eq 'a'){$octave *= 2}

    if($key.character -eq 'z'){$octave *= .5}

    if($octave -lt .25){$octave = .25}

    if($octave -gt 16){$octave = 16}

    # Grab the character from the $key Object and Match

    # It Against the Array

    $KeyboardData=$Piano.($Key.Character)

    # Now IF we found something of value

    # PLAY IT

    If ($KeyboardData) {

    $note=$KeyboardData[0]

    $location=$host.ui.rawui.cursorposition

    $location.Y=$keyboarddata[1]

    $location.X=$keyboarddata[2]

    $host.ui.rawui.cursorposition=$location

    WRITE-HOST "*"

    [console]::beep($note*$octave,150)

    $host.ui.rawui.cursorposition=$location

    WRITE-HOST $keyboarddata[3]

    }

    # Key having fun until Somebody tries to ESCape

    # Get it? Hit's the ESCape key? Bad Pun?

    } until ( $key.VirtualKeyCode -eq 27 )

    clear

  • Sorry, it's a here-string... not a text block.

  • Corrected your 'off=key' -- Sorry... when I changed octaves, everything was wrong... (had to fix it.)

    [array]$piano=$NULL

    $piano+=@{"Q"=261.626,6,6,"q"}

    $piano+=@{"2"=277.183,3,9,"2"}

    $piano+=@{"W"=293.994,6,12,"w"}

    $piano+=@{"3"=311.127,3,15,"3"}

    $piano+=@{"E"=329.628,6,18,"e"}

    $piano+=@{"R"=349.994,6,24,"r"}

    $piano+=@{"5"=369.995,3,27,"s"}

    $piano+=@{"T"=391.995,6,30,"t"}

    $piano+=@{"6"=415.305,3,33,"6"}

    $piano+=@{"Y"=440,6,36,"y"}

    $piano+=@{"7"=466.164,3,39,"7"}

    $piano+=@{"U"=493.883,6,42,"u"}

    $piano+=@{"I"=523.251,6,48,"i"}

    $piano+=@{"9"=554.365,3,51,"9"}

    $piano+=@{"O"=587.330,6,54,"o"}

    $piano+=@{"0"=622.254,3,57,"0"}

    $piano+=@{"P"=659.255,6,60,"p"}

  • Udemy Course The OMG Piano course is unlike anything you’ve ever seen. I’ve condensed years of piano training into a step-by-step system that shows you exactly how to start playing, by ear, your favorite songs on the piano in a matter of days. This course, gives you the skills to sound like a polished piano player without having to spend years studying the piano. Go To here for more info:>> https://www.udemy.com/learn-to-play-the-piano-using-pop-rb-and-gospel-chords/