forked from LinusU/node-loudness
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matthias Heyman
committed
Jun 15, 2022
1 parent
732b679
commit 13c526e
Showing
2 changed files
with
73 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,101 @@ | ||
import {execa} from 'execa'; | ||
import { execa } from "execa"; | ||
|
||
let defaultDeviceCache: string; | ||
let defaultArgs: string[]; | ||
|
||
async function amixer(...args: string[]) { | ||
const allArgs = []; | ||
allArgs.push(...await getDefaultArgs()); | ||
if (args && args.length > 0) { | ||
allArgs.push(...args); | ||
} | ||
|
||
console.info('exec amixer', allArgs); | ||
const result = await execa('amixer', allArgs); | ||
return result.stdout; | ||
const allArgs = []; | ||
allArgs.push(...(await getDefaultArgs())); | ||
if (args && args.length > 0) { | ||
allArgs.push(...args); | ||
} | ||
|
||
const result = await execa("amixer", allArgs); | ||
return result.stdout; | ||
} | ||
|
||
const reDefaultDevice = /simple mixer control '([a-z\d -]+)',\d+/i; | ||
|
||
function parseDefaultDevice(data: string) { | ||
const result = reDefaultDevice.exec(data); | ||
const result = reDefaultDevice.exec(data); | ||
|
||
if (result === null) { | ||
throw new Error('Alsa Mixer Error: failed to parse output'); | ||
} | ||
if (result === null) { | ||
throw new Error("Alsa Mixer Error: failed to parse output"); | ||
} | ||
|
||
return result[1]!; | ||
return result[1]!; | ||
} | ||
|
||
const reWhichPactl = /^\/.*\/pactl$/; | ||
|
||
async function systemHasPulseAudio() { | ||
try { | ||
const {stdout} = await execa('which', ['pactl']); | ||
if (reWhichPactl.test(stdout)) { | ||
return true; | ||
} | ||
} catch {} | ||
|
||
return false; | ||
try { | ||
const { stdout } = await execa("which", ["pactl"]); | ||
if (reWhichPactl.test(stdout)) { | ||
return true; | ||
} | ||
} catch {} | ||
|
||
return false; | ||
} | ||
|
||
async function getDefaultArgs() { | ||
if (!defaultArgs) { | ||
const hasPulse = await systemHasPulseAudio(); | ||
defaultArgs = hasPulse ? ['-D', 'pulse'] : []; | ||
} | ||
if (!defaultArgs) { | ||
const hasPulse = await systemHasPulseAudio(); | ||
defaultArgs = hasPulse ? ["-D", "pulse"] : []; | ||
} | ||
|
||
return defaultArgs; | ||
return defaultArgs; | ||
} | ||
|
||
async function getDefaultDevice() { | ||
if (defaultDeviceCache) { | ||
return defaultDeviceCache; | ||
} | ||
if (defaultDeviceCache) { | ||
return defaultDeviceCache; | ||
} | ||
|
||
const amixerResult = await amixer(); | ||
defaultDeviceCache = parseDefaultDevice(amixerResult); | ||
return defaultDeviceCache; | ||
const amixerResult = await amixer(); | ||
defaultDeviceCache = parseDefaultDevice(amixerResult); | ||
return defaultDeviceCache; | ||
} | ||
|
||
const reInfo | ||
= /[a-z][a-z ]*: playback [\d-]+ \[(\d+)%] (?:[[\d.-]+db] )?\[(on|off)]/i; | ||
const reInfo = | ||
/[a-z][a-z ]*: playback [\d-]+ \[(\d+)%] (?:[[\d.-]+db] )?\[(on|off)]/i; | ||
|
||
function parseInfo(data: string) { | ||
const result = reInfo.exec(data); | ||
const result = reInfo.exec(data); | ||
|
||
if (result === null) { | ||
throw new Error('Alsa Mixer Error: failed to parse output'); | ||
} | ||
if (result === null) { | ||
throw new Error("Alsa Mixer Error: failed to parse output"); | ||
} | ||
|
||
return {volume: Number.parseInt(result[1]!, 10), muted: result[2] === 'off'}; | ||
return { | ||
volume: Number.parseInt(result[1]!, 10), | ||
muted: result[2] === "off", | ||
}; | ||
} | ||
|
||
async function getInfo() { | ||
const device = await getDefaultDevice(); | ||
const amixerOutput = await amixer('get', device); | ||
return parseInfo(amixerOutput); | ||
const device = await getDefaultDevice(); | ||
const amixerOutput = await amixer("get", device); | ||
return parseInfo(amixerOutput); | ||
} | ||
|
||
export async function getVolume() { | ||
const info = await getInfo(); | ||
return info.volume; | ||
const info = await getInfo(); | ||
return info.volume; | ||
} | ||
|
||
export async function setVolume(value: number) { | ||
const device = await getDefaultDevice(); | ||
await amixer('set', device, `${value}%`); | ||
const device = await getDefaultDevice(); | ||
await amixer("set", device, `${value}%`); | ||
} | ||
|
||
export async function getMuted() { | ||
const info = await getInfo(); | ||
return info.muted; | ||
const info = await getInfo(); | ||
return info.muted; | ||
} | ||
|
||
export async function setMuted(value: boolean) { | ||
const device = await getDefaultDevice(); | ||
await amixer('set', device, value ? 'mute' : 'unmute'); | ||
const device = await getDefaultDevice(); | ||
await amixer("set", device, value ? "mute" : "unmute"); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,41 @@ | ||
import {join, dirname} from 'node:path'; | ||
import {fileURLToPath} from 'node:url'; | ||
import {execa} from 'execa'; | ||
import { join, dirname } from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
import { execa } from "execa"; | ||
|
||
const executablePath = join( | ||
dirname(fileURLToPath(import.meta.url)), | ||
'adjust_get_current_system_volume_vista_plus.exe', | ||
dirname(fileURLToPath(import.meta.url)), | ||
"adjust_get_current_system_volume_vista_plus.exe" | ||
); | ||
|
||
console.log('EXE path:', executablePath); | ||
|
||
async function runProgram(...args: string[]) { | ||
const result = await execa(executablePath, args); | ||
return result.stdout; | ||
const result = await execa(executablePath, args); | ||
return result.stdout; | ||
} | ||
|
||
async function getVolumeInfo() { | ||
const data = await runProgram(); | ||
const args = data.split(' '); | ||
const data = await runProgram(); | ||
const args = data.split(" "); | ||
|
||
return { | ||
volume: Number.parseInt(args[0]!, 10), | ||
muted: Boolean(Number.parseInt(args[1]!, 10)), | ||
}; | ||
return { | ||
volume: Number.parseInt(args[0]!, 10), | ||
muted: Boolean(Number.parseInt(args[1]!, 10)), | ||
}; | ||
} | ||
|
||
export async function getVolume() { | ||
const volumeInfo = await getVolumeInfo(); | ||
return volumeInfo.volume; | ||
const volumeInfo = await getVolumeInfo(); | ||
return volumeInfo.volume; | ||
} | ||
|
||
export async function setVolume(value: number) { | ||
await runProgram(String(value)); | ||
await runProgram(String(value)); | ||
} | ||
|
||
export async function getMuted() { | ||
const volumeInfo = await getVolumeInfo(); | ||
return volumeInfo.muted; | ||
const volumeInfo = await getVolumeInfo(); | ||
return volumeInfo.muted; | ||
} | ||
|
||
export async function setMuted(value: boolean) { | ||
await runProgram(value ? 'mute' : 'unmute'); | ||
await runProgram(value ? "mute" : "unmute"); | ||
} |