Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created February 7, 2020 22:21
Show Gist options
  • Save steviecoaster/eb72e60d78f7d08b82d55406ece86abe to your computer and use it in GitHub Desktop.
Save steviecoaster/eb72e60d78f7d08b82d55406ece86abe to your computer and use it in GitHub Desktop.
Happy Birthday, Mike Kanakos!!
function Resolve-Note {
[OutputType([void])]
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
param(
[Parameter(Position = 0, Mandatory, ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias('♪', '♫')]
[ValidateSet('C', 'C#', 'Db', 'D', 'D#', 'Eb', 'E', 'F', 'F#', 'Gb', 'G', 'G#', 'Ab', 'A', 'A#', 'Bb', 'B', 'R')]
[string]
$Note,
[Parameter(Position = 1, ValueFromPipelineByPropertyName)]
[ValidateRange(-4, 5)]
[int]
$Octave = 0,
[Parameter(Position = 2, ValueFromPipelineByPropertyName)]
[Alias('Length')]
[ValidateScript(
{
$_ -match '(64|32|16|8|4|2|1).*'
}
)]
[ArgumentCompleter(
{
param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)
return @('1','2','4','8','16','32','64') -match "^$WordToComplete"
}
)]
[string]
$NoteLength = '4',
[Parameter()]
[Alias('BPM')]
[ValidateRange(0, 300)]
[int]
$BeatsPerMinute = 120
)
begin {
$BaseNoteFrequencies = @{
'C' = 261.626
'C#' = 277.183
'Db' = 277.183
'D' = 293.665
'D#' = 311.127
'Eb' = 311.127
'E' = 329.628
'F' = 349.228
'F#' = 369.994
'Gb' = 369.994
'G' = 391.995
'G#' = 415.305
'Ab' = 415.305
'A' = 440
'A#' = 466.164
'Bb' = 466.164
'B' = 493.883
'R' = 0
}
$NoteDuration = @{
'64' = 0.25
'32' = 0.5
'16' = 1
'8' = 2
'4' = 4
'2' = 8
'1' = 16
}
}
process {
$Pitch = switch ($Octave) {
{$_ -lt 0} {
$BaseNoteFrequencies[$Note] / [Math]::Pow(2, - $Octave)
}
{$_ -gt 0} {
$BaseNoteFrequencies[$Note] * [Math]::Pow(2, $Octave)
}
0 {
$BaseNoteFrequencies[$Note]
}
}
if ($NoteLength -match '\.') {
$DotCount = ($NoteLength -replace '[^\.]').Length
$Multiplier = [Math]::Pow( 1.5, $DotCount )
}
else {
$Multiplier = 1
}
$DurationScale = $NoteDuration[$NoteLength -replace '\.']
$Duration = $DurationScale * $Multiplier * $BeatsPerMinute
if ($DurationScale -ge 1) {
$beep = "b$('e' * $DurationScale)p"
}
elseif ($DurationScale -eq 0.5) {
$beep = 'bp'
}
else {
$beep = 'b'
}
if ($PSCmdlet.ShouldProcess($("$Pitch Hz"), $beep)) {
if ($Pitch -ne 0) {
[Console]::Beep($Pitch, $Duration)
}
else {
Start-Sleep -Milliseconds $Duration
}
}
}
}
$json = '
[
{
"Note": "D",
"NoteLength": "8."
},
{
"Note": "D",
"NoteLength": "16"
},
{
"Note": "E",
"NoteLength": "4"
},
{
"Note": "D",
"NoteLength": "4"
},
{
"Note": "G",
"NoteLength": "4"
},
{
"Note": "F#",
"NoteLength": "2"
},
{
"Note": "D",
"NoteLength": "8."
},
{
"Note": "D",
"NoteLength": "16"
},
{
"Note": "E",
"NoteLength": "4"
},
{
"Note": "D",
"NoteLength": "4"
},
{
"Note": "A",
"NoteLength": "4"
},
{
"Note": "G",
"NoteLength": "2"
},
{
"Note": "D",
"NoteLength": "8."
},
{
"Note": "D",
"NoteLength": "16"
},
{
"Note": "D",
"NoteLength": "4",
"Octave": "1"
},
{
"Note": "B",
"NoteLength": "4"
},
{
"Note": "G",
"NoteLength": "4"
},
{
"Note": "F#",
"NoteLength": "4"
},
{
"Note": "E",
"NoteLength": "4"
},
{
"Note": "C",
"NoteLength": "8.",
"Octave": "1"
},
{
"Note": "C",
"NoteLength": "16",
"Octave":"1"
},
{
"Note": "B",
"NoteLength": "4"
},
{
"Note": "G",
"NoteLength": "4"
},
{
"Note": "A",
"NoteLength": "4"
},
{
"Note": "G",
"NoteLength": "2."
}
]
'
Write-Host "Happy Birthday Mike!!!" -ForegroundColor Green
($json | ConvertFrom-Json) | Resolve-Note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment