Wednesday, January 14, 2009

Change audio devices via shell script

I searched for a long time for a way to activate Soundflower as part of a script, but I couldn't find any way to do so, short of opening system preferences and using UI scripting to control the settings.

There had to be a better way.

I found SoundSource, but I couldn't find any way to activate it from applescript, even with UI scripting. I asked the developers about scriptability, and after a few emails back and forth, they decided to release the source, which I used to build audiodevice.

Audiodevice
Source

This the basic functionality of SoundSource with a quick and dirty command line interface slapped on. Type "audiodevice help" for usage information. Since I got my Airport express, I haven't had much use for my Soundflower script, but I still regularly use it in conjunction with Proxi to activate my bluetooth headset from the keyboard.

46 comments:

Dex†er said...

Hey! How do you use this file? I'm really a noob on this stuff and I just want an easy way to change my audio output without going to all the trouble on the system settings. I don't really know if this is that kind of stuff but would be very happy if somebody could help me. I'm running a Mac OS X ver 10.5.8 Intel Core 2.

Please mail me at andreasdigrazia@hotmail.com
or leave a comment please!

Thanks!

Best regards

whosawhatsis? said...

If you're a noob, you may just want to use SoundSource. It puts a menu in the right side of your menu bar that lets you select audio devices. This is fast enough access for most purposes. This program is intended to make it slightly faster for those who are already using a launcher, or for those who need to include the functionality in a larger script.

ze'ev said...

AWESOME! Just what I was looking for!
Thank you!

Zettt said...

Could you help me, please? This isn't working for me anymore. I've installed it yesterday and it worked just fine, but an hour ago (right after I was testing another audiodevice shell thing http://www.macosxhints.com/article.php?story=20050614171126634) it didn't work anymore.
Do you know what I broke?

whosawhatsis? said...

I assume you're talking about the SwitchAudioSource program. I don't know how that works, but I would guess that if you set everything to built-in audio and reboot, it should work as normal. If the prefpane works, Audiodevice should work too.

Zettt said...

Simply opening the Sound PrefPane did the trick. Thanks a lot!

Unknown said...

Hi,

Really great work. We put your command shell in all our macs in our school lab and then we can use ARD to change the device used by the students macs.

We have a request more, is there a way to implement the possibility to choose the volume for one port ? By example : audiodevice output "Micronas USB Headset" volume 50

Thanks in advance !

Christian

whosawhatsis? said...

I never did figure out how to do that. As explained, the code that actually does the work in this program isn't mine, I just wrote a little interface code.

Unknown said...

I've taken your command-line utility, wrapped it in a shell script to switch back and forth between two inputs and then call it from a php file. This lets me integrate into my home automation application and switch audio inputs from my ipad. You can read more about it here: http://forum.micasaverde.com/index.php?topic=6137.0

Januz said...

Hi,

thanks for the script! Sadly, in my case it doesn´t work as it should. I have to output devices:

Internal Speakers
USB Sound Device

I can select the internal speakers with

audiodevice output "Internal Speakers"

but the same for the USB device fails with the error "device not found!". When I don´t use quotation marks, I don´t get the error but the source isn´t changed...

Would be nice if you could help me.

Thanks,

Januz

Peacegrove said...

Awesome work!
I'm using you program in a Alfred extension. http://dl.dropbox.com/u/514866/uploaded/Change_audio_device.zip

Fabian said...

Trying to use this on Lion, but the terminal simply ignores the command...? Could you help?

whosawhatsis? said...

What command are you trying? Does the list command work? Are you getting any output?

Jeff Couturier said...

Great work on this. I use it with a little Applescript to mute Spotify ads: http://jeffcouturier.com/2012/04/mute-spotify-ads-in-osx-lion/

Thom Patterson said...

I was having trouble switching to my "USB Sound Device" for output. Turns out there were a bunch of spaces (8) trailing the name. When I get the list of output devices (./audiodevice output list) I realized when I was highlighting "USB Sound Device" that there was extra whitespace at the end, the others didn't have it. I got it to work using the following command:

./audiodevice output "USB Sound Device "

Thom Patterson said...

Looks like blogger mashed all my spaces down to 1, suffice it to say in my previous post there are 8 spaces before the closing quote.

Dave said...

Hey,
I have a quick question. Just getting into playing with bash, and I was wondering if I could "install" this utility, so I don't have to navigate to the directory and use "./audiodevice" to get it to run.

Basically I want to just be able to type "audiodevice output 'Dave's Room'" at the prompt and have it run. Currently if I do this, it says "-bash: audioadvice: command not found"

I've tried Googling how to install a shell script, but just not had any luck finding what I'm after yet.

Oh, and I'm running OS X 10.8.3 :)

Thanks! :)

whosawhatsis? said...

Use the command "echo $PATH" to get a colon-separated list of the folders you can install the script in to make it available from anywhere, without the "./" in front.

Unknown said...

How do disable headphones on mac. I have something stuck in my headphone jack and the internal speakers only chime on startup . After that it is disabled. Is there any script to disable the input. thx

Unknown said...

Just clarifying my earlier update. I have an end of a headphone stuck in the computer. Hence there is no audio output from the computer coz it thinks there is a headphone attached. I can only use the bluetooth output. However when the computer starts up , the chime is sounded, but then the speaker icon is disabled after the os comes up. I was wondering if there is any script to disable the headphone . Thx

whosawhatsis? said...

The OS only has an option for "Built-in Audio", which goes to the headphone jack if there is something plugged into it, and to the speakers if there isn't (the startup chime plays before the part of the system that controls this is available). As far as I can tell, there is no way to select between Headphones and built-in speakers in software.

ABWC said...

This is outstanding work. Thank you !!

Unknown said...

Fantastic work. Have you considered posting the source to GitHub, to make it easy to browse and examine?

NJ said...

Great job on this. It really came in handy for me. I was wondering if you know a way to mirror your Mac desktop screen to an AirPlay Display using a similar method with scripting. I know you can use AppleScript to set the device but I was wondering if there is a script that I can just pass in the AirPlay Display name (Apple TV) and it would automatically mirror my desktop screen to that device. I haven't been able to find anything on that in Apple's Developer Docs.

Adam Christianson said...

I am trying to use this to select an AirPlay audio output source and it seems to work, but only if I use the name of the last AirPlay I had selected in the System Preferences pane. For example, if I have "Internal Speakrs and two AirPlay destinations "AirPlay 1" and "AirPlay 2" and "AirPlay 1" is currently selected. I can run:

audiodevice output "Internal Speakers"

and then

audiodevice output "AirPlay 1"

and it will work great. But if I try running:

audiodevice output "AirPlay 2"

it will not work until I have manually selected that output source in the System Preferences pane. Then after that I can run:

audiodevice output "AirPlay 2"

And it will switch, but then:

audiodevice output "AirPlay 1"

no longer works.

hope this makes sense

joeyw said...

you can always just hold alt when you press the menubar item, you don't have to go into soundPrefs.

doodller said...

Good stuff! But I have an asking.

Audiodevice is Universal Binary,

Can I use Audiodevice on Mac OS X 10.4 PPC?

Thanks :)

eLDeus said...

Hi, can you tell if I have two output sources which have the same name, one of them is mono and the other one is stereo, I need to switch to stereo, but 'audiodevice' switches only to the first one (mono), how can I tell him to switch to the second device in my list of ouput devices?:
MacBook-Pro-15-eLDeus:~ eldeus$ /Users/eldeus/Audiodevice/audiodevice output list
Internal Speakers
BSA301
BSA301
MacBook-Pro-15-eLDeus:~ eldeus$

Thank you!

Unknown said...

Seems I'm missing something with how audiodevice handles names.

If i do
audiodevice output Internal\ Speakers
All is well.
But
audiodevice output Mike\'s\ AirPods
or
audiodevice output Mike's\ Airpods
results in some sort of drop into a secondary prompt and nothing changes.

Unknown said...

Just started getting the error "Illegal instruction: 4" after updating to macOS Sierra 10.12.4.

Unknown said...

Works great in MacOS Mojave 10.14.1.

If anyone has issues with the command, wrap your device name in quotes. That worked for me. Example:

audiodevice input "MacBook Pro Microphone"

audiovirtual said...

It doesnt seem to find my airplay outputs - responds with "device not found!". I can only select "Internal Speakers". If I manually select an airplay output, it will show up in the output list. when I switch back to internal, the airplay output disappears from the output list.

Sascha Lamprecht said...

I leeched the Sources, removed the Compile-Errors, and here is a X64 Version. Catalina safe. :) The command runs from 10.10 on. have fun.

https://www.file-upload.net/download-13712395/audiodeviceX64.zip.html

Knut said...

When I download

https://www.file-upload.net/download-13712395/audiodeviceX64.zip.html

it contains Flash Player.....

iGerard said...

Sascha,

Do you have this on GitHub?

Unknown said...

@whosawhatsis What under what license do you release the source code for audiodevice?

whosawhatsis? said...

Most of the code is from Soundsource, which Rogue Amoeba released under a BSD license. You can consider Audiodevice to be under the same license.

Unknown said...

Re: License, perfect. Thank you!

I have updated the source to compile on Catalina (with many deprecation warnings). Do you have the source on GitHub? I would be happy to contribute a pull request or send you the patch. (I had to remove/replace Get/SetSysBeepVolume().)

Unknown said...

@whosawhatsis It appears that the Soundflower license _was_ GPL, and they changed it to an MIT License in 2014: https://github.com/mattingalls/Soundflower/blob/master/License.txt

Would you care to elaborate which BSD license you would use, or consider using MIT?
https://en.wikipedia.org/wiki/BSD_licenses

(Want to use this at work, so need to be clear on license. No plan to publish your software or derivatives, binary or code.)

whosawhatsis? said...

Soundflower is a different project. This program can interface with it by selecting it as a source/target, but AFAIK, there is no code in common.

To be honest, I haven't touched this code since I wrote this post. I received the Soundsource source code as a tgz file in an email, and the only discussion of licenses was the following message:

Hey it works, not bad.

Think it should print usage when run with no args myself.

Doing things based off of Device Name can also be a little hit or miss. If you plug in 2 or more of the same device it falls over. But for your uses it's probably fine.

If you want to release it to the public, you can consider SoundSource BSD licensed, http://www.opensource.org/licenses/bsd-license.html, which basically is just a NO WARRENTY disclaimer.




- Quentin D. Carnicelli

Software Engineer

Rogue Amoeba Software, LLC


The link goes to the 2-clause license, so I guess it's that version.

I haven't even bothered to keep it compiling for current OSes, since it's not part of my workflow anymore. If you'd like to create a repo and take over maintaining it, you're more than welcome to.

Unknown said...

Thanks for your generosity and for the clarification on licensing info again. I will reach out again here if we do anything with the source.
Kind regards,
-Bill

Knut said...

When I try to run audiodevice on macOS 10.15.3, it refuses to do so since it is not signed. Has this been fixed? Is there a new version?

Unknown said...

@Knut The prebuilt audiodevice is only 32-bit, so it will not run anyway on macOS Catalina, which does not support 32-bit apps.

If you have Xcode installed, you can compile a Debug version from the source provided above with only a few modifications (comment out the lines related to the long-gone Get/SetSysBeepVolume() functions).

Hypothetically, if somebody gave you an unsigned 64-bit version, you could remove the com.apple.quarantine flag (google the xattr command) or find another way to disable Gatekeeper to allow the app to run. (e.g. Click "Allow" in the Security Pref pane after you try to run it once.)

Knut said...

@Bill F. I already have a 64 bit version, which I think I downloaded from this thread. And when I followed your advice, I was able to run it. Thanks.

Zanthoxylum said...

Thanks. Works great. I use it to toggle Soundflower and AU lab Equalizer.

Rukesh Prasad said...

Very Nice Blog!
Choose your smart choice sound recording device at Spy Store. Trend Audio Devices Online Shopping with free shipping at the best price (9999332499, 9999332099). Buy and Sell Spy Hidden Voice Recorder Device for office, home, vehicle, and more places. Free Demo wih free shipping, and one month replacement policy.
Voice Recorder Device in Kolkata
Sound Recorder Device in Chennai
Audio Device Device in Delhi
Audio Device Shop in India
Shop Online Voice Recorder
Spy Hidden Voice Recorder Device
Audio Device in Mumbai
Audio Device in Ahemadabad
Audio Device in Lucknow
Audio Device in Patna
Audio Device in Goa