LoginSignup
3
2

Macでいつものサウンド入力/出力設定に切り替える

Last updated at Posted at 2021-10-07

説明

  • 最近は何かとオンラインミーティングが多いので、音声出力/音声入力の設定変更も多いです。
  • そこでサウンド入力出力デバイスを切り替えるアプリケーションをつくり、その起動にショートカットキーを設定します。
  • 完成イメージ: わかりにくいですが、ショートカットキー(ここではshift + control + s)により実行したあとに、デバイスが切り替わっています

Install switchaudio-osx

  • サウンドデバイスの設定に使います
brew install switchaudio-osx

出力可能なデバイス一覧を表示する

  • 使用したい出力デバイスのnameを使います
 SwitchAudioSource -f json -t output -a | jq
{
  "name": "HyperX QuadCast S",
  "type": "output",
  "id": "46",
  "uid": "AppleUSBAudioEngine:Kingston:HyperX QuadCast S:4100:1"
}
{
  "name": "外部ヘッドフォン",
  "type": "output",
  "id": "76",
  "uid": "BuiltInHeadphoneOutputDevice"
}
{
  "name": "MacBook Proのスピーカー",
  "type": "output",
  "id": "69",
  "uid": "BuiltInSpeakerDevice"
}

入力可能なデバイス一覧を表示する

  • 使用したい入力デバイスのnameを使います
 SwitchAudioSource -f json -t input -a | jq
{
  "name": "HyperX QuadCast S",
  "type": "input",
  "id": "51",
  "uid": "AppleUSBAudioEngine:Kingston:HyperX QuadCast S:4100:2"
}
{
  "name": "外部マイク",
  "type": "input",
  "id": "87",
  "uid": "BuiltInHeadphoneInputDevice"
}
{
  "name": "MacBook Proのマイク",
  "type": "input",
  "id": "82",
  "uid": "BuiltInMicrophoneDevice"
}

Macのスクリプトエディタでコードを書いてアプリケーションとして保存する

# https://apple.stackexchange.com/questions/190319/display-notification-works-from-applescript-editor-but-not-when-exported-as-ap
tell application "Finder" to activate

# System Settingsアプリを起動し、Soundの設定を開く
tell application "System Settings"
	activate
	reveal pane "Sound"
end tell

# Krispを起動する
# do shell script "~/Applications/krisp.app/Contents/MacOS/krisp"

# 入力デバイスを指定する
do shell script "
/opt/homebrew/bin/SwitchAudioSource -f json -t input -s 'Yeti Nano' ||
/opt/homebrew/bin/SwitchAudioSource -f json -t input -s 'External Headphones' ||
/opt/homebrew/bin/SwitchAudioSource -f json -t input -s 'YukiAirPodsPro' ||
/opt/homebrew/bin/SwitchAudioSource -f json -t input -s 'YukiAirPods' ||
/opt/homebrew/bin/SwitchAudioSource -f json -t input -s 'MacBook Pro Microphone'
"

# 入力音量を75%に設定する
set volume input volume 80

# 出力デバイスを指定する
do shell script "
/opt/homebrew/bin/SwitchAudioSource -f json -t output -s 'External Headphones' || 
/opt/homebrew/bin/SwitchAudioSource -f json -t output -s 'YukiAirPodsPro' || 
/opt/homebrew/bin/SwitchAudioSource -f json -t output -s 'YukiAirPods' || 
/opt/homebrew/bin/SwitchAudioSource -f json -t output -s 'MacBook Pro Speakers'
"

# 現在の出力デバイスを取得する
set currentOutputDevice to do shell script "/opt/homebrew/bin/SwitchAudioSource -c -t output"

# 出力音量を設定する
if currentOutputDevice = "External Headphones" then
	set volume output volume 70
else if currentOutputDevice = "YukiAirPodsPro" then
	set volume output volume 70
else if currentOutputDevice = "MacBook Pro Speakers" then
	set volume output volume 100
else
	set volume output volume 75
end if

# 現在の入力デバイスを取得する
set currentInputDevice to do shell script "/opt/homebrew/bin/SwitchAudioSource -c -t input"

# 入出力デバイスを表示する
display notification "Input : " & currentInputDevice & "
Output : " & currentOutputDevice with title "reset-sound.app" sound name "Glass"

# フィードバックサウンドを再生する
do shell script "/usr/bin/afplay /System/Library/Sounds/Glass.aiff"

  • ついでに音量を設定しています
  • 名前は適当に、、ファイルフォーマットはアプリケーションを選択します。

実行

  • MacのSpotlight検索から実行したり、
  • Alfredを使っているならショートカットキーを設定すると便利です
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2