Skip to content

Instantly share code, notes, and snippets.

@pjobson
Last active July 15, 2022 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjobson/986e6d602920280a28e73951d9a7a5dc to your computer and use it in GitHub Desktop.
Save pjobson/986e6d602920280a28e73951d9a7a5dc to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// Forces the audio card and profile in Linux
// tested on Mint, should work on other distros which use pulse audio.
//
// Requires pulseaudio and node installed in your path.
// apt install pulseaudio-utils pulseaudio
// apt install nodejs # or install it some other way
//
// For some reason my MEDIA PC would randomly change the output to dummy or internal
// and would disable HDMI.
// Sometimes it would also change the card order for no particular reason.
// This looks at the existing cards for output:hdmi-stereo-extra1
// and sets the card prfile to the correct card number.
//
// I read various Github articles on this, nothing really helped.
//
// If you want to use this, you'll need to figure out your audio output
// and change AudioOutputName to whatever it is.
//
// To find your output use this command.
// pactl list cards | less
// First you'll want to look at the "Card #", for example my 0th card is my GP107GL High Definition Audio Controller
// continue down and look at the "Profiles:", they'll show available: yes OR available: no.
// You can test each available one with:
// pactl set-card-profile ## output:PROFILE_NAME
//
// Mine for example on this boot would be:
// pactl set-card-profile 0 output:hdmi-stereo-extra1
const AudioOutputName = 'hdmi-stereo-extra1';
const init = async () => {
const cp = require('child_process');
const stdout = cp.execSync('pactl list cards');
let cardNum;
stdout.toString().split("\n").forEach(line => {
const card = line.match(/Card #(\d+)/);
cardNum = (card) ? card[1] : cardNum;
if (new RegExp(AudioOutputName).test(line)) {
const cmd = `pactl set-card-profile ${cardNum} output:${AudioOutputName}`;
console.log(`${AudioOutputName} is on Card #${cardNum}`);
console.log('Setting Output:', cmd);
cp.execSync(cmd);
process.exit(0);
}
});
};
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment