Created
August 7, 2022 09:37
-
-
Save stefanwatt/13a40a3de8518eb7db7b5ac1ade6265e to your computer and use it in GitHub Desktop.
script to alternate between 2 audio devices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
import childProcess from 'node:child_process'; | |
import { promisify } from 'util'; | |
const allowedSinks = ['PRO X Wireless Gaming Headset Analog Stereo', 'SoundCore 2'] | |
const exec = promisify(childProcess.exec); | |
const switchInputToSink= async (input,sink) =>{ | |
await exec(`ponymix -t sink-input -d ${input} move ${sink}`) | |
} | |
const parseInputs = (inputString)=>{ | |
if (!inputString) return; | |
const inputStrings = inputString.split(/\n(?=(sink-input))/g).filter(elem => elem!=="sink-input") | |
const inputs = inputStrings.map(inputString =>{ | |
const lines = inputString.split('\n') | |
const id = lines[0].split(' ')[1].split(":")[0] | |
return id | |
}) | |
return inputs | |
} | |
const switchDefaultSink = async ()=>{ | |
const currentDefaultId = (await exec('ponymix defaults')).stdout[5] | |
const newSinkId = sinks.find(sink => sink.id !== currentDefaultId )?.id | |
if (!newSinkId) return | |
await exec(`ponymix set-default -d ${newSinkId}`) | |
return newSinkId | |
} | |
//get all audio outputs | |
const listSinksCmd = await exec('ponymix -t sink list') | |
const sinkStrings = listSinksCmd.stdout.split(/\n(?=(sink))/g).filter(elem => elem!=="sink") | |
const sinks = sinkStrings | |
.map((sinkString) => { | |
const [idLine,nameLine,volumeLine] = sinkString.split("\n") | |
const id = idLine[5] | |
const name = nameLine.trim() | |
return { id, name, volume:volumeLine } | |
}) | |
.filter(sink => allowedSinks.includes(sink.name)) | |
//get all running apps (inputs)... | |
const listInputsCmd = await exec('ponymix list -t sink-input') | |
const inputs = parseInputs(listInputsCmd?.stdout) | |
const newSinkId = await switchDefaultSink() | |
if ( newSinkId ){ | |
//...and switch them to the new sink (audio output) | |
inputs?.forEach(input => switchInputToSink(input, newSinkId)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment