npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

win-audio-manager

v0.0.2

Published

A Node.js library for managing Windows audio devices

Readme

windows-audio-manager


🎵 Windows Audio Manager

A simple and lightweight Node.js library for managing audio devices on Windows.

🔊 Easily list, set, and get default playback devices programmatically!


📜 Features

List all audio devices (speakers, headphones, etc.)
Set a default playback device (switch between speakers, headphones, monitors)
Get the current default playback device
Handles errors safely and efficiently


🔧 Installation

1️⃣ Install the PowerShell Module

This library relies on the AudioDeviceCmdlets PowerShell module created by frgnca.
Before using this library, install the required module by running:

Install-Module -Name AudioDeviceCmdlets -Scope CurrentUser

If you are prompted about an untrusted repository, confirm with:

Y

💡 Credits: This library builds upon the amazing work of frgnca, who developed the AudioDeviceCmdlets module.
🔗 GitHub: AudioDeviceCmdlets


2️⃣ Install This Library

Now, install windows-audio-manager using npm:

npm install windows-audio-manager

🛠️ Usage

📌 1. Import the Library

const {
    isModuleInstalled,
    listAudioDevices,
    setAudioDevice,
    getDefaultPlaybackDevice
} = require("windows-audio-manager");

📌 2. Check if the PowerShell Module is Installed

Before using the library, ensure that AudioDeviceCmdlets is installed:

Using then/catch:

isModuleInstalled().then(installed => {
    console.log(installed ? "✅ Module is installed" : "❌ Module is NOT installed. Please install it.");
}).catch(error => console.error("❌ Error:", error.message));

Using async/await:

(async () => {
    try {
        const installed = await isModuleInstalled();
        console.log(installed ? "✅ Module is installed" : "❌ Module is NOT installed. Please install it.");
    } catch (error) {
        console.error("❌ Error:", error.message);
    }
})();

📌 3. List All Available Audio Devices

Using then/catch:

listAudioDevices().then(devices => {
    console.log("🔊 Available Audio Devices:");
    devices.forEach((device, index) => {
        console.log(`${index}: ${device.Name} (ID: ${device.ID})`);
    });
}).catch(error => console.error("❌ Error:", error.message));

Using async/await:

(async () => {
    try {
        const devices = await listAudioDevices();
        console.log("🔊 Available Audio Devices:");
        devices.forEach((device, index) => {
            console.log(`${index}: ${device.Name} (ID: ${device.ID})`);
        });
    } catch (error) {
        console.error("❌ Error:", error.message);
    }
})();

🔹 Example Output:

🔊 Available Audio Devices:
0: Speakers (Realtek) (ID: {0.0.0.00000000})
1: Headphones (ID: {1.0.0.00000001})
2: External Monitor Speakers (ID: {2.0.0.00000002})

📌 4. Get the Current Default Playback Device

Using then/catch:

getDefaultPlaybackDevice().then(device => {
    console.log(`🎧 Current Default Playback Device: ${device.Name} (ID: ${device.ID})`);
}).catch(error => console.error("❌ Error:", error.message));

Using async/await:

(async () => {
    try {
        const device = await getDefaultPlaybackDevice();
        console.log(`🎧 Current Default Playback Device: ${device.Name} (ID: ${device.ID})`);
    } catch (error) {
        console.error("❌ Error:", error.message);
    }
})();

🔹 Example Output:

🎧 Current Default Playback Device: Headphones (ID: {1.0.0.00000001})

📌 5. Set a New Default Playback Device

Using then/catch:

✅ By Index:

listAudioDevices().then(devices => {
    const selectedIndex = 1; // Change this index as needed
    const selectedDevice = devices[selectedIndex];

    console.log(`🔄 Switching to: ${selectedDevice.Name} (ID: ${selectedDevice.ID})`);

    setAudioDevice(selectedDevice.Index).then(index => {
        console.log(`✅ Successfully set device: ${devices.find(d => d.Index === index).Name}`);
    }).catch(error => {
        console.error("❌ Error:", error.message);
    });
});

✅ By ID:

listAudioDevices().then(devices => {
    const selectedIndex = 1; // Change this index as needed
    const selectedDevice = devices[selectedIndex];

    console.log(`🔄 Switching to: ${selectedDevice.Name} (ID: ${selectedDevice.ID})`);

    setAudioDeviceById(selectedDevice.ID).then(id => {
        console.log(`✅ Successfully set device by ID: ${devices.find(d => d.ID === id).Name}`);
    }).catch(error => {
        console.error("❌ Error:", error.message);
    });
});

Using async/await:

✅ By Index:

(async () => {
    try {
        const devices = await listAudioDevices();
        const selectedIndex = 1; // Change this index as needed
        const selectedDevice = devices[selectedIndex];

        console.log(`🔄 Switching to: ${selectedDevice.Name} (ID: ${selectedDevice.ID})`);

        const deviceIndex = await setAudioDevice(selectedDevice.Index);
        console.log(`✅ Successfully set device: ${devices.find(d => d.Index === deviceIndex).Name}`);
    } catch (error) {
        console.error("❌ Error:", error.message);
    }
})();

✅ By ID:

(async () => {
    try {
        const devices = await listAudioDevices();
        const selectedIndex = 1; // Change this index as needed
        const selectedDevice = devices[selectedIndex];

        console.log(`🔄 Switching to: ${selectedDevice.Name} (ID: ${selectedDevice.ID})`);

        const deviceId = await setAudioDeviceById(selectedDevice.ID);
        console.log(`✅ Successfully set device by ID: ${devices.find(d => d.ID === deviceId).Name}`);
    } catch (error) {
        console.error("❌ Error:", error.message);
    }
})();

🛠️ Error Handling

This library throws errors for failed operations. Always use .catch() or try...catch to handle exceptions.

Example:

(async () => {
    try {
        await setAudioDevice(9999); // Invalid index
    } catch (error) {
        console.error("❌ Error:", error.message);
    }
})();

🔹 Example Error Output:

❌ Error: Error setting default audio device: PowerShell execution error: Invalid index

📜 API Reference

🔍 isModuleInstalled()

Checks if the required PowerShell module is installed.
Returns: Promise<boolean>
🔹 Usage:

const installed = await isModuleInstalled();

🔊 listAudioDevices()

Lists all available audio devices.
Returns: Promise<Array<{ Index: number, Name: string, ID: string }>>
🔹 Usage:

const devices = await listAudioDevices();

🎧 getDefaultPlaybackDevice()

Gets the current default playback device.
Returns: Promise<{ Index: number, Name: string, ID: string }>
🔹 Usage:

const device = await getDefaultPlaybackDevice();

🔄 setAudioDevice(index)

Sets the default playback device to the given index.
Returns: Promise<number> (resolves with the index of the new default device)
🔹 Usage:

const index = await setAudioDevice(1);

CLI-Interface

🔗 CLI-INTERFACE: CLI-INTERFACE

🚀 Enjoy seamless audio device management with windows-audio-manager! 🎧🔥