Raj
Raj

Reputation: 59

Detect Audio Output on Mac?

Is there a way to detect if audio is being output from the Mac system? Whether it's via headphone jack, usb-c, or bluetooth, is there a process which runs if and only if audio is playing?

I've made a unix script which plays a 15hz sine waveform, the idea is to execute the script every 580 seconds if and only if no audio is playing. If audio is playing, the script won't execute.

I have really good speakers but the drawback is they enter a 'standby' power saver mode every 10 minutes (600 seconds). When I'm at work I don't really care about whether it runs in the background or not* but when I'm at home I tend to miss notifications when speakers enter standby, so the aim of the script is to play the 3 second waveform every 580 seconds if and only if no audio is playing.

Upvotes: 1

Views: 1806

Answers (2)

user3439894
user3439894

Reputation: 7555

You do not necessarily need to use AppleScript to accomplish the goal, although you certainly can if you want.

I'd opt to use a Launch Agent, as in the following example XML plist code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.my.play.if.no.audio.is.playing</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>-c</string>
        <string>[[ $(pmset -g | grep ' sleep') =~ coreaudiod ]] || '/path/to/shell_script'</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartInterval</key>
    <integer>580</integer>
</dict>
</plist>

Change '/path/to/shell_script' in the XML plist code to the actual fully qualified pathname of your shell script.

Then save is as, e.g., com.my.play.if.no.audio.is.playing.plist in ~/Library/LaunchAgents/ and load it from Terminal using, e.g.:

cd ~/Library/LaunchAgents/
launchctl load com.my.play.if.no.audio.is.playing.plist

Then every 580 seconds your shell script will execute if there is no audio playing.


Notes:

As coded, it assumes the executable bit is set on your shell script.

If you are going to use Launch Agents and Launch Daemons, I highly recommend you read the manual pages for launchctl, launchd.plist and launchd.

You can read the manual page for command in Terminal by typing man command, then press enter, or for easier reading, just type command and then right-click on it and select: Open man Page

To stop your Launch Agent, in Terminal:

cd ~/Library/LaunchAgents/
launchctl unload com.my.play.if.no.audio.is.playing.plist

You could also instead, include the test condition in your shell script, e.g.:

#!/bin/bash

if [[ ! $(pmset -g | grep ' sleep') =~ coreaudiod ]]; then

    # Code to generate your sine waveform goes here.

fi

Then, the command used in the example XML plist code would be:

    <key>ProgramArguments</key>
    <array>
        <string>/path/to/shell_script</string>
    </array> 


If you really want to use AppleScript, and assuming a Stay Open application, then:

Example AppleScript code:

on run
    --  # Add any AppleScript code you what run when the
    --  # Stay Open AppleScript application is opened.
    
end run

on idle
    
    do shell script ¬
        "[[ $(pmset -g | grep ' sleep') =~ coreaudiod ]] || '/path/to/shell_script'"
    
    return 580
    
end idle

on quit
    continue quit
end quit

Notes:

When saving the example AppleScript code in Script Editor , set File Format: [Application] and [√] Stay open after run handler.

If your add the test condition to your shell script, as shown further above, then your do shell script command would be:

do shell script "'/path/to/shell_script'"

I tend to stay away from using this type of AppleScript application, as I find that over time they can/may become resource intensive in that they continue consume more RAM. YMMV.

Upvotes: 1

puhbeige
puhbeige

Reputation: 145

I'm not sure if this is super related to what you want but maybe try out Soundflower ? It's super useful for re-routing audio in mac.

Upvotes: 1

Related Questions