VISE-ATEM_Midi_AudioDemo.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID ed462c11-229b-4fc2-bbd0-a0118760e9a7
 
.AUTHOR Ian Morrish
  
.TAGS
    VISE, MIDI, ATEM, Audio
 
.PROJECTURI https://ianmorrish.wordpress.com
 
.EXTERNALSCRIPTDEPENDENCIES equires ATEMLib.dll and PeteBrown.PowerShellMidi.dll
 
.RELEASENOTES This script only runs on Windows 10 and ISE 64bit.Blackmagic ATEM Software Control must also be instaled (v6.4 or grater)
Video demo can be seen here https://youtu.be/rIW_97YYxlA
#>


<#
 
.DESCRIPTION
 "Windows PowerShell script to control Blackmagic ATEM audio mixer using a MIDI device"
 
#>
 

#ATEM setup

add-type -path 'C:\Users\imorrish\OneDrive\PowerShell\ATEM\SwitcherLib.dll'
$Global:atem = New-Object SwitcherLib.Switcher("192.168.1.8")
$atem.Connect()
$audio = $atem.GetAudioInputs()

#MIDI setup
# Device ID is for Behringer CMD MM-1

Import-Module 'C:\Users\imorrish\OneDrive\PowerShell\Midi\PeteBrown.PowerShellMidi.dll'
$deviceId = "\\?\SWD#MMDEVAPI#MIDII_B5FB4C9F.P_0001#{504be32c-ccf6-4d2c-b73f-6f8b3747e22b}"
[PeteBrown.PowerShellMidi.MidiInputPort]$inputPort = Get-MidiInputPort -id $deviceId
$inputPort.TranslateZeroVelocityNoteOnMessage = $true;
$noteOnSourceId = "NoteOnMessageReceivedID"
$controlChangeSourceId = "ControlChangeMessageReceivedID"
Unregister-Event -SourceIdentifier $noteOnSourceId -ErrorAction SilentlyContinue
Unregister-Event -SourceIdentifier $controlChangeSourceId -ErrorAction SilentlyContinue

function logScale($level){
    if($level -ne 0){
        20 * [System.Math]::Log($level)
        }
    else{
    -66
    }

}

#offset applied to inputs so we can control 8 (or more) inputs from single controler.
$global:g=4 

$HandleNoteOn = 
{
    switch ($event.sourceEventArgs.Note) {
            16{$global:g=0;write-host "Control inputs 1-4";break}
            17{$global:g=4;write-host "Control inputs 5&6";break}
            48{$audio[0+$g].InputGain=-66;break}
            49{$audio[1+$g].InputGain=-66;break}
            50{$audio[2].InputGain=-66;break}
            51{$audio[3].InputGain=-66;break}
            19{if($audio[0+$g].MixOption -eq "bmdSwitcherAudioMixOptionOff"){$audio[0+$g].MixOption="On";} else {$audio[0+$g].MixOption="Off"};write-host "1 $($audio[0+$g].MixOption)";;break}
            23{if($audio[1+$g].MixOption -eq "bmdSwitcherAudioMixOptionOff"){$audio[1+$g].MixOption="On";} else {$audio[1+$g].MixOption="Off"};write-host "2 $($audio[1+$g].MixOption)";;break}
            27{if($audio[2].MixOption -eq "bmdSwitcherAudioMixOptionOff"){$audio[2].MixOption="On";} else {$audio[2].MixOption="Off"};write-host "3 $($audio[2].MixOption)";;break}
            31{if($audio[3].MixOption -eq "bmdSwitcherAudioMixOptionOff"){$audio[3].MixOption="On";} else {$audio[3].MixOption="Off"};write-host "4 $($audio[3].MixOption)";;break}
            20{$audio[0+$g].MixOption="FollowVideo";break}
            24{$audio[1+$g].MixOption="FollowVideo";break}
            28{$audio[2].MixOption="FollowVideo";break}
            32{$audio[3].MixOption="FollowVideo";break}
        }
}

$HandleControlChange = 
{
    switch ($event.sourceEventArgs.Controller)
    {
        48{$audio[0+$g].InputGain=logScale($event.sourceEventArgs.Value/127) }
        49{$audio[1+$g].InputGain=logScale($event.sourceEventArgs.Value/127) }
        50{$audio[2].InputGain=logScale($event.sourceEventArgs.Value/127) }
        51{$audio[3].InputGain=logScale($event.sourceEventArgs.Value/127) }
    }
}

#Start event receivers
$job1 = Register-ObjectEvent -InputObject $inputPort -EventName NoteOnMessageReceived -SourceIdentifier $noteOnSourceId -Action $HandleNoteOn
$job2 = Register-ObjectEvent -InputObject $inputPort -EventName ControlChangeMessageReceived -SourceIdentifier $controlChangeSourceId -Action $HandleControlChange

write-Host "Started"