Making noises

Home  >>  Cool  >>  Making noises

Making noises

On January 11, 2007, Posted by , In Cool,PowerShell,Quiz, With 4 Comments

There are twothree ways I know of to make noises in PowerShell.

1. Simple and most boring: use the internal speaker’s beep.

write-host "`a`a`a"

2. Slightly better, use the .NET console beep. This plays A above middle C for one second.

[console]::beep(440,1000)

Both of the above are synchronous, ie, the script blocks until the sound has finished. Richy Rich is doing some interesting stuff with runspaces to do this asynchronously.

3. Lastly, you can play wav files. Sadly, only .wav files. No midi, mp3, mpeg4.. at least I couldn’t find anything in .net 2.

$file='C:\\WINDOWS\\Media\\Windows XP Startup.wav'
$sp = new-object Media.SoundPlayer ($file)
$sp.Load();
$sp.Play();
1..5 | % {
  echo "la la"
  sleep 1
}

This plays the wav asynchronously, so I feel should be used in a future version of psinvaders to get those authentic sounds.

So anyone know of a way to play midi files asynchronously in PowerShell with only a few lines of code?

4 Comments so far:

  1. gelyon says:

    The following uses Windows Media Player, it will play midi files and anything else WMP can play.

    function Play-Media( [String] $strMediaFilePath )
    {
    if( $strMediaFilePath )
    {
    $player = New-Object -ComObject “wmplayer.ocx”
    $media = $player.NewMedia($strMediaFilePath)
    [void] $player.CurrentPlaylist.AppendItem($media)
    $player.Controls.Play()
    }
    }

    Greg L

  2. adrian says:

    Thanks Greg – I suppose I should have thought about COM 🙂

  3. MoW says:

    I also have 2 blogentries about using media player, make playlists and playing music in PowerShell (Monad at that time).

    http://mow001.blogspot.com/2006/01/some-fun-with-monads-add-member-mp3.html

    http://mow001.blogspot.com/2005/12/monad-really-does-rock.html

    Enjoy

    Greetings /\/\o\/\/

  4. adrian says:

    Cheers /\/\o\/\/, I was wondering how to create objects with arbitrary properties like in http://mow001.blogspot.com/2006/01/some-fun-with-monads-add-member-mp3.html

    It does look a bit messy – I was hoping that an array of hashes would work – but this certainly solves some problems I was having.