Skip to content

set_Melody

James Brundage edited this page Oct 23, 2026 · 1 revision

set_Melody

Sets the Melody

Sets the Melody of a Wave.

View Source
<#
.SYNOPSIS
    Sets the Melody
.DESCRIPTION
    Sets the Melody of a Wave.
#>
param()

return $this | 
    Add-Member NoteProperty '#Melody' $Args -Force -PassThru
filter unroll { $_ }
$allArgs = $args | unroll

$currentWave = $this
if (-not $currentWave) { $currentWave = wave }

$currentMelody = @(
    foreach ($arg in $allArgs) {
        if ($arg.pstypenames -notcontains 'Note') {
            break
        }
        $arg
    }
)

$remaining =
    if ($allArgs.Length -gt $currentMelody.Length) {
        $allArgs[$($currentMelody.Length)..$($allArgs.Length - 1)]
    }
    else {
        @()
    }

$bpm = 
    if ($currentWave.BPM -is [TimeSpan]) { $currentWave.BPM }
    else { [TimeSpan]::FromSeconds(60/128/4) }            

if ($remaining -as [double[]]) {   
    $newMelody = [PSCustomObject]([Ordered]@{PSTypeName='Note'} + [Ordered]@{
        Frequency = $remaining -as [double[]]
        Duration = $bpm
    })

    $currentWave | 
        Add-Member NoteProperty '#Melody' @(
            if ($currentMelody) {
                $currentMelody
            }
            $newMelody
        ) -Force -PassThru
    return $currentWave
}

$newMelody = 
    @(foreach ($arg in $remaining) {
        if ($arg -is [Collections.IDictionary]) {
            [PSCustomObject]([Ordered]@{PSTypeName='Note'} + $arg)
        }
        elseif ($arg -as [Collections.IDictionary[]]) {
            foreach ($dictionary in $arg) {
                [PSCustomObject]([Ordered]@{PSTypeName='Note'} + $dictionary)
            }
        }
        elseif ($arg -as [double[]]) {
            
            [PSCustomObject]([Ordered]@{PSTypeName='Note'} + [Ordered]@{
                Frequency = $arg -as [double[]]
                Duration = $bpm
            })                        
        }
        else {
            $arg
        }
    })

$currentWave | 
    Add-Member NoteProperty '#Melody' @(
        if ($currentMelody) {
            $currentMelody
        }            
        $newMelody
    ) -Force -PassThru

Clone this wiki locally