#---8<---gadget-piano.ps1---Jean-JMST-Belgium---8<--- #=9/2006===================================Version 1==========# #=========== "human hearable" string music notes ============# # # To use : # # -music paremeter is a string containing music notes separated # by a space. # # A simplified abc notation is used. # A music note begins with the keyboard's ocatve used (1, 2 or 3) # followed by a character music note (from C to B) + eventually # a # character indicating an half tone higher (sharp), so : # # 2F# is music note F sharp played on the second piano # keyboard's octave. # # Values allowed for notes : # C,C#,D,D#,E,F,F#,G,G#,A,A# and B # preceded by 1,2 or 3 # # A silence is marked with a Z. # # To multiply a note duration, ie $black*2 : 3C2 # To divide a note duration, ie $black/4 : 3C/4 # # -black is the duration in millisecond a note is played # default=200 # # -tempo is the duration between notes in milliseconds # default=100 # # To test : # # .\gadget-piano # °plays a little music # .\gadget-piano "2A" 5000 # °plays a A note 5 seconds ... to tune your Stradivarius # .\gadget-piano (("1F# Z ")*5) 300 # °plays a F sharp and a silence 5 times # .\gadget-piano "1A 1B 1C 3A 3B 3C" 150 50 # °plays a sequence of notes, each one played 150 ms with # an interval of 50 ms # # NOTE : # # You must have a pc internal speaker present and enabled # to hear something :-) # #========Tested on PowerShell 1 RC2===========================# param( [string]$music='2C 2D 2E 2G 2F 2F 2A 2G 2G 3C 2B 3C 2G 2E 2C', [int]$black=200, [int]$tempo=100 ) filter play-sound{ [console]::Beep( [math]::Pow([math]::Pow(2,1/12),$_)*440, $black*$ratio ) } $script:OFS='' $notes=('C','C#','D','D#','E','F','F#','G','G#','A','A#','B') $sounds=-21..14 $keyboards=(($sounds[0..11]),($sounds[12..23]),($sounds[24..35])) $h=@{} for($i=1;$i -lt 4;$i++) {0..11|%{$h.Add("$i"+$notes[$_],$keyboards[$i-1][$_])}} [regex]$r='(\d)?([A-GZ])(#)?(/)?(\d)?(\-)?' $music.Split(' ',[StringSplitOptions]::RemoveEmptyEntries)|% ` { if($_ -match $r){ $ratio=1 if($matches[4] -and $matches[5]) {$ratio=invoke-expression ('1'+[string]$matches[4..5])} elseif($matches[5]) {$ratio=invoke-expression ('1*'+[string]$matches[5])} if($matches[2] -eq 'Z'){Start-Sleep -m ($black*$ratio)} else{ $note=$h[[string]($matches[1..3])] $note|play-sound } if(!$matches[6]){Start-Sleep -m $tempo} } } #---8<---gadget-piano.ps1---Jean-JMST-Belgium---8<---