Skip to content

add audio-device extension - #144

Merged
fe9lix merged 11 commits into
raycast:mainfrom
benvp:main
Jan 14, 2022
Merged

fe9lix merged 11 commits into
raycast:mainfrom
benvp:main

Conversation

@benvp

@benvp benvp commented Oct 20, 2021 •

Copy link
Copy Markdown
Contributor

Description

Adds a new extension for switching audio devices (input/output) via raycast using the macos-audio-devices utility.

Type of change

  • New extension

Screencast

https://capture.dropbox.com/E6pbSuaX61nhziEM

Checklist

@PitNikola PitNikola added the new extension Label for PRs with new extensions label Oct 20, 2021
@PitNikola

Copy link
Copy Markdown
Member

Is this link suppose to contain a video? It's just a still image for me (checked in multiple browsers)

Also from quick look I noticed that your command names don't follow Title Case naming convention. Could you please take a look at our guidelines document: https://developers.raycast.com/basics/prepare-an-extension-for-store.

@PitNikola PitNikola self-assigned this Oct 20, 2021
@benvp

benvp commented Oct 21, 2021

Copy link
Copy Markdown
Contributor Author

Yeah, it has been a gif. Somehow one would need to hit Download for it do be played. I guess that's due to the file size. Anyway, I converted it into a video and updated the link. Also fixed the casing issue.

@pernielsentikaer pernielsentikaer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome extension @benvp 💪

I'm not sure if the problem is occurring for me because I'm on Monterey, but I can't get it to set any other Audio Output Device than my Bluetooth headset (but that works well with the change from default speakers to Bluetooth)

QhSKu7kimfh0vOfo.mp4

The only error I see in the debug log is when changing to "Background Music" - the two other choices don't do anything.

Stack trace

Timestamp               Ty Process[PID:TID]
2021-10-22 21:26:26.050 Db Raycast[11539:2381c6] [com.raycast.extensions.audio-device:set-output-device] Error: Command failed: /usr/local/Cellar/switchaudio-osx/1.1.0/SwitchAudioSource -t output -u (null)
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `/usr/local/Cellar/switchaudio-osx/1.1.0/SwitchAudioSource -t output -u (null)'

    at ChildProcess.exithandler (node:child_process:397:12)
    at ChildProcess.emit (node:events:390:28)
    at maybeClose (node:internal/child_process:1064:16)
    at Socket.<anonymous> (node:internal/child_process:450:11)
    at Socket.emit (node:events:390:28)
    at Pipe.<anonymous> (node:net:672:12) {
  killed: false,
  code: 2,
  signal: null,
  cmd: '/usr/local/Cellar/switchaudio-osx/1.1.0/SwitchAudioSource -t output -u (null)',
  stdout: '',
  stderr: "/bin/sh: -c: line 0: syntax error near unexpected token `('\134n" +
    "/bin/sh: -c: line 0: `/usr/local/Cellar/switchaudio-osx/1.1.0/SwitchAudioSource -t output -u (null)'\134n"
}

@pernielsentikaer pernielsentikaer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a few more corrections

Comment thread extensions/audio-device/src/helpers.tsx Outdated
function SetAudioDeviceAction({ device }: SetAudioDeviceActionProps) {
return (
<ActionPanel.Item
title="Make active"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title="Make active"
title="Select"

Select is not necessary the one you have to choose, but it needs to follow the naming convention like "Set Active" "Make Active" "Select"

Comment thread extensions/audio-device/package.json Outdated
"title": "Set Output Device",
"subtitle": "Audio Device",
"description": "Sets the active audio output device.",
"keywords": [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"keywords": [

Unnecessary keywords, It's already indexed by title and subtitle

Comment thread extensions/audio-device/package.json Outdated
"subtitle": "Audio Device",
"description": "Sets the active audio output device.",
"keywords": [
"audio",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"audio",

Comment thread extensions/audio-device/package.json Outdated
"description": "Sets the active audio output device.",
"keywords": [
"audio",
"device",

ghost Oct 22, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"device",

Comment thread extensions/audio-device/package.json Outdated
"keywords": [
"audio",
"device",
"output"

ghost Oct 22, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"output"

Comment thread extensions/audio-device/package.json Outdated
"description": "Sets the active audio input device.",
"keywords": [
"audio",
"device",

ghost Oct 22, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"device",

Comment thread extensions/audio-device/package.json Outdated
"keywords": [
"audio",
"device",
"input"

ghost Oct 22, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"input"

Comment thread extensions/audio-device/package.json Outdated
"audio",
"device",
"input"
],

ghost Oct 22, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
],

Comment thread extensions/audio-device/package.json Outdated
"name": "switchAudioSourcePath",
"title": "SwitchAudioSource (switchaudio-osx) path",
"description": "Your local path to SwitchAudioSource.",
"link": "https://github.com/deweller/switchaudio-osx",

ghost Oct 22, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"link": "https://github.com/deweller/switchaudio-osx",

There is no link attribute

ghost Oct 24, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This structure has basically been copied from the "Tower" extension. Will remove it.

Comment thread extensions/audio-device/package.json Outdated
"link": "https://github.com/deweller/switchaudio-osx",
"type": "textfield",
"default": "/opt/homebrew/bin/SwitchAudioSource",
"value": "/opt/homebrew/bin/SwitchAudioSource",

ghost Oct 22, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"value": "/opt/homebrew/bin/SwitchAudioSource",

@benvp

ghost commented Oct 24, 2021

Copy link
Copy Markdown
Contributor Author

@pernielsentikaer Do you mind running SwitchAudioSource -t output -f json and paste the output? I'm wondering why the uid field of the device is null.

@pernielsentikaer

ghost commented Oct 24, 2021

Copy link
Copy Markdown
Contributor

Hi @benvp 👋

I added -a to the command, otherwise I just got an error, here is the output for you:

❯ SwitchAudioSource -a -t output -f json
{"name": "Background Music", "type": "output", "id": "48", "uid": "(null)"}
{"name": "Background Music (UI Sounds)", "type": "output", "id": "53", "uid": "BGMDevice_UISounds"}
{"name": "MacBook Pro Speakers", "type": "output", "id": "57", "uid": "BuiltInSpeakerDevice"}

@benvp

ghost commented Oct 25, 2021

Copy link
Copy Markdown
Contributor Author

@pernielsentikaer Could you try that again? Seems like the uid might not the best field to rely on. I changed it to use the device id instead.

ghost left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That worked a lot better @benvp 💪 Confirming that changing from a Bluetooth headset and back to build-in speakers works as intended.

I tried to rewrite your useAudioDevices function since there is some empty state flicker going on rarely

export function useAudioDevices(type: "input" | "output") {
  const [isLoading, setIsLoading] = useState(false);
  const [audioDevices, setAudioDevices] = useState<UseAudioDevicesResponse["data"]>();

  useEffect(() => {
    const fetchDevices = async () => {
      setIsLoading(true);
      try {
        const allDevicesResult = await execp(`${switchAudioPath()} -a -t ${type} -f json`);
        const currentDeviceResult = await execp(`${switchAudioPath()} -c -t ${type} -f json`);
        const devices = parseSwitchAudioOutput(allDevicesResult.stdout);
        const current = parseSwitchAudioOutput(currentDeviceResult.stdout)[0];

        setAudioDevices({
          devices,
          current,
        });
      } catch {
        await showToast(ToastStyle.Failure, `Error!`, `There was an error fetching the audio devices.`);
      } finally {
        setIsLoading(false);
      }
    };

    fetchDevices();
  }, [type]);

  return {
    isLoading,
    data: audioDevices,
  };
}

I'll check if its caused by a known bug and get back to you

@PitNikola

ghost commented Oct 26, 2021

Copy link
Copy Markdown
Member

@benvp, we'll need to hold on with merging this PR. We want to discuss internally what to do with extensions that require installation of some third party CLIs. My worry that for such simple use cases like this (changing output), it might be quite a bummer for users when they see that installation of some CLI is required. Some users might not even have brew installed. I'll get back to you once we get to a conclusion, sorry for the wait.

Btw, in the meantime, if you find a way to use something like AppleScript for it, it could be a huge UX win.

@benvp

ghost commented Oct 27, 2021

Copy link
Copy Markdown
Contributor Author

@PitNikola I understand. AppleScript could somewhat work (by automating clicks to system preferences etc) but I don't think that it provides a nice UX as it could also break between macOS Releases and could be different throughout macOS versions.

@waynehoover

ghost commented Oct 29, 2021

Copy link
Copy Markdown

As a user I would love to see this merged, I don't think it's too much to ask that certain dependencies be installed, as long as it's documented somewhere. Even the emoji extension downloads dependencies on startup.

That being said, here is an example of applescript controlling the sound input: https://github.com/raycast/script-commands/blob/master/commands/system/audio/audio-output-switch.template.applescript

I don't think there is a way to get the list of all available input sources through apple script though, that's the key and why this dependency is important.

@pernielsentikaer

ghost commented Oct 29, 2021

Copy link
Copy Markdown
Contributor

@waynehoover you can at any time just import it yourself if you want have it

@PitNikola

ghost commented Nov 2, 2021

Copy link
Copy Markdown
Member

@benvp, we had an internal discussion about that and came to agreement that there must be a better way to handle dependencies like that. Do you think it would be possible to download a compiled version of the CLI if it's missing and then use it under the hood when running the extension? You have nodejs at your disposal for it. You should be able to identify the machine's architecture to download the correct CLI. What do you think?

Our main goal with the extension store is to make it easy to install and run all these "little productivity apps". If we end up with a lot of extensions that ask you to install additional CLIs, it will significantly degrade experience of the Store and Raycast in general. I hope you can understand our intention. Thanks!

@pernielsentikaer

ghost commented Nov 3, 2021

Copy link
Copy Markdown
Contributor

I'm not sure it does what you want out of the box, but could https://www.npmjs.com/package/macos-audio-devices solve it somehow?

@benvp

ghost commented Nov 10, 2021

Copy link
Copy Markdown
Contributor Author

@pernielsentikaer This looks promising. Quickly tested that package locally and seems to work fine. Let me see if I make the time to update the code to use this package in the next couple of days.

@PitNikola

ghost commented Nov 10, 2021

Copy link
Copy Markdown
Member

https://github.com/karaggeorge/macos-audio-devices will require Swift runtime libraries so it might be even more hassle than brew to install them for users who don't have Xcode installed...

@pernielsentikaer

ghost commented Nov 10, 2021

Copy link
Copy Markdown
Contributor

@PitNikola But it's only when you have High Sierra or lower. From 10.14 Mojave (Released in 2018) it's build-in out of the box and then we support the last 4 versions.

Raycast itself requires macOS 10.14+ so I think we're good to go as I read it?

@PitNikola

ghost commented Nov 10, 2021

Copy link
Copy Markdown
Member

Oh, ok, I misread the doc. If it works without any additional installations, then great 👍

@pernielsentikaer

ghost commented Nov 17, 2021

Copy link
Copy Markdown
Contributor

Hi @benvp 👋

Do you need any help regarding this, just checking if there is something you need help with 😄

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @benvp 🔥

Thanks for the update, it looks very nice with the subtitle 🚀 I have been a long journey, but it's worth the waiting time – what a lovely extension that we can ship 🚢

I'm going to accept it and then will my colleague do a final review before it's finally merged.

@raycastbot

ghost commented Jan 14, 2022

Copy link
Copy Markdown
Collaborator

@benvp

ghost commented Jan 14, 2022

Copy link
Copy Markdown
Contributor Author

@fe9lix I'm curious, what made you change your mind regarding the included binary vs a downloaded one? Thanks for merging, though. 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new extension Label for PRs with new extensions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants