Bart Simons

Bart Simons


Thoughts, stories and ideas.

Bart Simons
Author

Share


Tags


.net .net core Apache C# CentOS LAMP NET Framework Pretty URLs Windows Server WireGuard WireGuard.io access log add analysis android api at the same time authentication authorization automate automation azure azurerm backup bash basics batch bootstrap build capture cheat sheet chromium chroot class cli click to close code snippet command line commands compile compiling compression containers control controller controlling convert cpu usage create credentials csv csvparser curl data dd deployment desktop detect devices disable diskpart dism distributed diy docker dom changes dotnet core drivers ease of access encryption example export file transfer files fix folders generalize getting started ghost ghost.org gui guide gunicorn gzip html html tables icewarp igd imagex import inotify install installation interactive ios iphone itunes java javascript jquery json kiosk kotlin linux live load data loading screen lock screen loopback audio lxc lxd lxml macos manage manually message messages minio mirrored mod_rewrite monitor monitoring mutationobserver mysql nexmo nginx no oobe node node.js nodejs not installing notification notifications object storage on desktop one command openssl owncloud parallels parallels tools parse perfect philips hue play port forwarding portainer.io powershell processing ps-spotify python quick raspberry pi record rip ripping rsync rtmp save save data sbapplication scraping script scripting scriptingbridge scripts security send server service sharedpreferences sms songs sonos spotify spotify api spotlight ssh stack streaming streamlink studio sudo swarm swift sync sysprep system audio systemd tables terminal tracking tutorial twilio ubiquiti ubuntu ubuntu 18.04 ui code unifi unlock unsplash source upnp uptime usb tethering wallpapers wasapi website websites webview windows windows 10 without itunes without oobe workaround xaml

Recording system audio through WASAPI with PowerShell

If you are planning to create certain audio-related automation tasks in PowerShell, you might come to the conclusion that there are not many ready to use cmdlets available. Luckily, there are some smart ways to work around that. Since PowerShell is capable to load and work with .NET assemblies, I decided to dive deeper into a project called CSCore, which claims to be an advanced audio library written in pure C# capable of playing and recording, encoding and decoding, and much more.

WASAPI stands for Windows Audio Session API and is one of the many audio APIs available for Windows. One of the features of WASAPI is the ability to record system audio as if it was a loopback audio device like in Linux. Luckily, CSCore has bindings available for the loopback recording functionality of WASAPI. Neat!

I was in need of a loopback audio recording solution for PowerShell, hence this post's title. In order to work around some minor problems I came through when directly adding the CSCore compiled DLL as a type in PowerShell, a wrapper was needed for me to create a more elegant way of calling methods. Check out this example:

Add-Type -Path PSCore.dll

$Recording = [PSCore.LoopbackRecorder]
$Recording::StartRecording("C:\Users\Bart Simons\Desktop\testing.wav")

When you are done and want to stop your recording, use this to stop your recording and dispose all used objects.

$Recording::StopRecording()

You can find the source code for my assembly at https://github.com/bmsimons/PSCore. If you need a pre-compiled assembly: I have included that as well in the debug folder.

You can record in .wma, .aac, .mp3 and .wav. For all formats except wave files, you can specify the bitrate, here's an example of how you can record an mp3 file in 320kbps:

$Recording::StartRecording("C:\Users\Bart Simons\Desktop\testing.mp3", 320000)

Happy system audio recording with PowerShell!

Bart Simons
Author

Bart Simons

View Comments