diff --git a/src/index.js b/src/index.js index 4c413cd..b47607a 100755 --- a/src/index.js +++ b/src/index.js @@ -27,6 +27,12 @@ const o = yargs.options({ describe: "Verbosity level, add more -v for more verbosity", type: "count", }, + pulsehost: { + alias: "u", + default: "127.0.0.1", + describe: "Pulse server host", + type: "string", + }, mpdport: { alias: "p", default: 6600, @@ -60,77 +66,12 @@ function isNight() { function getBrightness() { return isNight() ? 60 : 100; } -const volume = - (Math.min( - parseInt( - spawnSync( - `pactl list | grep -E "Name: .*${AUDIO_NAME}$|Volume" | grep "Name:" -A1 | tail -1 | cut -d% -f1 | cut -d/ -f2 | tr -d " "`, - { shell: true }, - ).stdout, - ), - 30, - ) / - 30) * - 100; -async function setMutedMic() { - const muted = !spawnSync( - `pacmd dump | grep ^set-source-mute.*${MIC_NAME}.*yes`, - { shell: true }, - ).status; - const b = muted ? getBrightness() : 100; - return dbus.staticMatrix( - ...(muted - ? brightness(colors.BASE_COLOR, b).rgb() - : brightness(colors.ALERT_COLOR, b).rgb()), - 6, - 22, - ); -} - -async function setAudio(matrix) { - const keys = [21]; - const muted = !spawnSync( - `pacmd dump | grep ^set-sink-mute.*${AUDIO_NAME}.*yes`, - { shell: true }, - ).status; - if (muted) { - keys.forEach(x => - dbus.setKey(matrix, 0, x, ...colors.SAFE_COLOR.rgb()), - ); - } else if (volume === 0) { - keys.forEach(x => dbus.setKey(matrix, 0, x, ...colors.BLUE.rgb())); - } else { - const volume = parseInt( - spawnSync( - `pactl list | grep -E "Name: .*${AUDIO_NAME}$|Volume" | grep "Name:" -A1 | tail -1 | cut -d% -f1 | cut -d/ -f2 | tr -d " "`, - { shell: true }, - ).stdout, - ); - const c = brightness( - volume < DANGEROUS_VOLUME - ? colors.BASE_COLOR.rgb() - : colors.ALERT_COLOR.rgb(), - (volume / 50) * 100, - ); - keys.forEach(x => dbus.setKey(matrix, 0, x, ...c.rgb())); - } -} - -async function run() { - const interfaces = await dbus.connect(); - - const matrix = await setMutedMic(); - setAudio(matrix); - setPlaying(matrix); - await interfaces.brightness.setBrightness(100); - await interfaces.chroma.setKeyRow(matrix); - dbus.disconnect(); -} async function daemon() { - const matrix = dbus.staticMatrix(...colors.BASE_COLOR.rgb(), 6, 22); + let matrix = dbus.staticMatrix(...colors.BASE_COLOR.rgb(), 6, 22); const setKey = (...args) => { dbus.setKey(matrix, ...args); + return matrix; }; const interfaces = await dbus.connect(); const draw = async () => await interfaces.chroma.setKeyRow(matrix); @@ -140,7 +81,13 @@ async function daemon() { setKey, draw, )(); - const p = pulse.connect({ out: AUDIO_NAME, mic: MIC_NAME, setKey, draw }); + const p = pulse.connect({ + host: o.pulsehost, + out: AUDIO_NAME, + mic: MIC_NAME, + setKey, + draw, + }); process.on("SIGINT", () => { mpdDisconnect(); diff --git a/src/mpd.js b/src/mpd.js index b824280..96397a3 100644 --- a/src/mpd.js +++ b/src/mpd.js @@ -2,7 +2,7 @@ const { EventEmitter } = require("events"); const net = require("net"); const { spawnSync } = require("child_process"); const { colors } = require("./colors"); -const { debug, log, info, error } = require("./logger")("mpd"); +const { debug, log, info, warn, error } = require("./logger")("mpd"); class Client extends EventEmitter { socket; @@ -84,7 +84,7 @@ module.exports = ({ host, port }, setKey, draw) => { return () => { try { c.close(); - log("Closing mpd"); + warn("Closing mpd"); } catch (e) { error(e); } diff --git a/src/pulse.js b/src/pulse.js index a404a6c..723191d 100644 --- a/src/pulse.js +++ b/src/pulse.js @@ -1,6 +1,7 @@ const PAClient = require("paclient"); const { colors, brightness } = require("./colors"); const { readFileSync } = require("fs"); +const { debug, log, info: loginfo, warn, error } = require("./logger")("pulse"); const DANGEROUS_VOLUME = 0.4; function getFnFromType(type) { var fn; @@ -41,6 +42,8 @@ const setColor = (muted, volume, setKey) => { module.exports = { connect({ host = "127.0.0.1", + out, + mic, cookiePath = `${process.env.HOME}/.config/pulse/cookie`, setKey = (...args) => { console.log("setKey", ...args); @@ -52,8 +55,10 @@ module.exports = { readFileSync(cookiePath, "binary"), "binary", ); - pa.connect({ host: "127.0.0.1", cookie }); + loginfo("Connecting to pulse at ", host); + pa.connect({ host, cookie }); pa.on("ready", () => { + loginfo("Connected to pulse"); pa.subscribe(["sink", "source"]); }) .on("change", (type, index) => { @@ -61,14 +66,15 @@ module.exports = { if (err) { return; } - if (info.name.includes("analog-surround-51")) { + if (info.name.includes(out)) { const { muted, baseVolume, channelVolumes } = info; const vol = Math.max(...channelVolumes) / baseVolume; setColor(muted, vol, setKey); draw(); } - if (info.name.includes("USB_PnP_Sound_Device")) { + if (info.name.includes(mic)) { const { muted } = info; + loginfo("Setting mic to muted = ", muted); const c = muted ? colors.BASE_COLOR : colors.ALERT_COLOR; @@ -88,6 +94,7 @@ module.exports = { }); return { disconnect() { + warn("Disconnecting from pulse"); pa.end(); }, };