Jump to content

Fast Sound Input/Output Selector [Updated 14 Nov 2022]


Recommended Posts

There are probably quite a few sound device selector script filters out there, but the ones I've come across are mostly written in applescript. I rewrote my own in C++ Swift and it runs much faster, so I'm sharing it here in case anyone's interested. 

 

Dependencies

  1. switchaudio-osx
  2. [optional] terminal-notifier

 

Optional user configuration:

  1. Set 'Custom Notifier' to `'terminal-notifier`
    • This replaces the alfred notification banner with one that looks like it's from System Preferences
    • If entering `terminal-notifier` doesn't work, try entering its path (find using `which terminal-notifier` in Terminal
  2. Add device names to a blocklist to hide them from results

 

Changelog

v2.0.0 (completely overhauled!)

  • New: `switchaudio-osx` no longer required
  • New: Significantly faster loading speeds
  • New: Icons now indicate whether a device is selected
  • New: Makes use of Alfred 5 user configuration panel to simplify setup

v1.1.1

  • Fixed: Cannot parse switchaudio-osx JSON in some cases 

v1.1.0

  • Added: User-configurable device blocklist (see feature request)
  • Fixed: Issue with user-set path for switchaudio-osx

v1.0.2

  • Minor performance optimisations

v1.0.1

  • Added: In most cases, users no longer need to specify the path of switchaudio-osx (see relevant comment)
  • Fixed: Missing icon images in terminal-notifier notification

 

Download | Source

I'm very much a beginner at c++, so any criticism / guidance would be much appreciated.

 

Screenshot

128549282_Screenshot2022-11-14at12_16_18PM.thumb.png.6cd286e64c6a6010052f375194297939.png

 

Edited by Mingwei
Update to v2.0.0
Link to comment
10 minutes ago, Mingwei said:

Set `CMDPATH` to the path of `switchaudio-osx`

 

This could be made optional (and then they all would be!) by setting PATH to /opt/homebrew/bin:/usr/local/bin:${PATH} in your code. It would cover the majority of installs, especially if done via Homebrew which you already recommend. Then the variable could be used for the user to set an extra PATH entry if needed.

Link to comment
4 minutes ago, vitor said:

 

This could be made optional (and then they all would be!) by setting PATH to /opt/homebrew/bin:/usr/local/bin:${PATH} in your code. It would cover the majority of installs, especially if done via Homebrew which you already recommend. Then the variable could be used for the user to set an extra PATH entry if needed.

Thanks for the tip! Will update it

Link to comment
  • Mingwei changed the title to Fast Sound Input/Output Selector [Updated 17 Nov]
  • Mingwei changed the title to Fast Sound Input/Output Selector [Updated 29 Nov]

This looks super useful, I just found myself needing a workflow like this and thought of your recent post and installed.

I do have switchaudio installed (today, v1.1.0 for Monterey) and this workflow "disappears" a split second after the Loading prompt appear. Debugging shows an empty error without much context - any thoughts?

 

See screenshot of the erroring Script Filter:

image.png.ab55136f84027b785417dad3c2cc52d0.png

 

I tried looking into the sound_filter cpp source but I have no clue what I'm doing and can't get anything to compile.

Link to comment

@xgo Error code 2 in the log means the script filter couldn't parse the json coming from switchaudio-osx. I think I've identified the problem, but could I see the output you receive from switchaudio-osx to be sure? Run this in your terminal and paste the output here: 

switchaudiosource -af json

 

If you have some time, do you mind also downloading v1.0.2 of the workflow and trying that to see if it works? I made some changes in the new version unrelated to the new blocklist feature that could be causing the problem. 

Edited by Mingwei
Link to comment

@Mingwei thanks for the quick turnaround. Can confirm that 1.0.2 works as expected. If I trump it back with latest (w/ blocklist feature) I get back into the problems.

 

Here's the output from my -af call - it's probably a decent test case since I have so many things connected to my work laptop (monitor w/ speakers, USB speakers, Jabra headset)

{"name": "USB2.0 Device", "type": "input", "id": "95", "uid": "AppleUSBAudioEngine:Generic:USB2.0 Device:20170726905959:2"}
{"name": "ThinkPad Thunderbolt 3 Dock USB Audio", "type": "input", "id": "86", "uid": "AppleUSBAudioEngine:Lenovo:ThinkPad Thunderbolt 3 Dock USB Audio:000000000000:1"}
{"name": "Jabra Link 370", "type": "input", "id": "111", "uid": "AppleUSBAudioEngine:Unknown Manufacturer:Jabra Link 370:70BF9247FC7A:2"}
{"name": "Logitech BRIO", "type": "input", "id": "103", "uid": "AppleUSBAudioEngine:Unknown Manufacturer:Logitech BRIO:A34C02B9:4"}
{"name": "MacBook Pro Microphone", "type": "input", "id": "81", "uid": "BuiltInMicrophoneDevice"}
{"name": "PHL 346B1C", "type": "output", "id": "115", "uid": "AppleGFXHDAEngineOutputDP:f803:0:{0C41-093E-000070EB}"}
{"name": "USB2.0 Device", "type": "output", "id": "99", "uid": "AppleUSBAudioEngine:Generic:USB2.0 Device:20170726905959:1"}
{"name": "ThinkPad Thunderbolt 3 Dock USB Audio", "type": "output", "id": "90", "uid": "AppleUSBAudioEngine:Lenovo:ThinkPad Thunderbolt 3 Dock USB Audio:000000000000:2"}
{"name": "Jabra Link 370", "type": "output", "id": "107", "uid": "AppleUSBAudioEngine:Unknown Manufacturer:Jabra Link 370:70BF9247FC7A:1"}
{"name": "MacBook Pro Speakers", "type": "output", "id": "74", "uid": "BuiltInSpeakerDevice"}

 

By the way, Github release says 1.1.0 but Alfred lists as 1.0.3 - might be worth aligning those!

 

Lastly, switchaudio doesn't seem to support airplay outputs, but there's been a few discussions on how to enable it, even on another Alfred workflow. I personally would not use this, but might be worth keeping in a backlog

https://github.com/deweller/switchaudio-osx/issues/9

Link to comment
  • Mingwei changed the title to Fast Sound Input/Output Selector [Updated 13 Dec]
  • 3 months later...
  • Mingwei changed the title to Fast Sound Input/Output Selector [Updated 14 Nov 2022]
  • 1 month later...

Hello @Mingwei,

When trying to execute the workflow I get an exception.

After selecting "Switch Sound Devices" I get the following in the debug console:

[11:20:10.398] Switch Sound Devices[Script Filter] Queuing argument '(null)'
[11:20:10.412] Switch Sound Devices[Script Filter] Script with argv '(null)' finished
[11:20:10.420] ERROR: Switch Sound Devices[Script Filter] Code 134: dyld[14716]: Symbol not found: (_$s10Foundation11JSONEncoderC6encodeyAA4DataVxKSERzlFTj)
  Referenced from: '/Users/david/Library/CloudStorage/GoogleDrive/My Drive/setup/config/Alfred.alfredpreferences/workflows/user.workflow.6C1A49F3-FDC5-4A82-9C8D-464F5C8CF805/sound-filter'
  Expected in: '/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation'

 

I did followed the awgo wiki to solve the permission issue, and it worked.

I didn't set any configuration.

I don't have the following installed:

  • terminal-notifier
  • switchaudio-osx

I'm using:

Monterey / Alfred 5 / Switch.Sound.Devices 2.0.0

 

Thanks!

Link to comment

Thank you so much for this! This is just what I need. I have some Soundcore headphones that I like, but the multipoint functionality can be temperamental.

 

I did however see the following message, so followed these instructions to manually open "sound-filter" when then worked.

 

Quote

"sound-filter" can't be opened because Apple cannot check it for malicious software.

 

 

2 suggestions:

 

1) Disabling notifications entirely.

 

2) I'd love to maybe change these icons so they work more universally across Alfred themes - they're a little hard to see:

image.thumb.png.d32b528ea395b0a3d67b791394b9774d.png

Edited by alfredpanda
Link to comment

@davidraviv That error message means there's probably some sort of issue with how frameworks are linked to the compiled binary. It's strange because I can't reproduce the error on my end. I'll play around with it and see if I can figure it out, though this might take a while as I'm busy with other stuff right now, and I'll probably need you to help me test it out. 

 

@alfredpanda very valid suggestions.

  • I personally like the notifications, but you can easily disable them on your end by deleting the Post Notification action in the workflow (and deleting all lines after the first line in the Run Script action if you have terminal-notifier installed, but I'm guessing you don't). 
  • Realised I made icons that only really work in dark mode after releasing it haha, I've been meaning to do something about that for a while but haven't had the time. In the meantime you can change the icons by opening the workflow folder and replacing all icon images with identically named files of your choosing. I got those icons from SF Symbols so you could do the same to create light mode compatible versions. 
Link to comment
On 12/24/2022 at 9:44 AM, Mingwei said:

@davidraviv That error message means there's probably some sort of issue with how frameworks are linked to the compiled binary. It's strange because I can't reproduce the error on my end. I'll play around with it and see if I can figure it out, though this might take a while as I'm busy with other stuff right now, and I'll probably need you to help me test it out. 

 

@alfredpanda very valid suggestions.

  • I personally like the notifications, but you can easily disable them on your end by deleting the Post Notification action in the workflow (and deleting all lines after the first line in the Run Script action if you have terminal-notifier installed, but I'm guessing you don't). 
  • Realised I made icons that only really work in dark mode after releasing it haha, I've been meaning to do something about that for a while but haven't had the time. In the meantime you can change the icons by opening the workflow folder and replacing all icon images with identically named files of your choosing. I got those icons from SF Symbols so you could do the same to create light mode compatible versions. 

Thanks @Mingwei for investing your time into it! Please let me know how can I help.

Edited by davidraviv
Link to comment
  • 5 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...