Coder Social home page Coder Social logo

arciiix / easy-volume Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 1.0 309 KB

The easiest way to get and control your system's volume is finally here

Home Page: https://www.npmjs.com/package/easy-volume?activeTab=readme

License: MIT License

TypeScript 53.00% C++ 45.09% JavaScript 1.91%
control electron loudness speaker volume

easy-volume's Introduction

easy-volume

npm

Controlling system volume level in Node.js has never been easier!

This cross-platform, dependency-free library lets you control and get the status of volume in your system. No matter what OS or what framework (it works everywhere - as well as on regular Node.js and Electron - where they were tested), you don't have to worry about the compatibility and potential errors - this library will handle it all.

Note that this library solves the Electron production-build specific problem with asar-packed files - other libraries similar to this, which have to call executable file or native module, don't work on Electron - easy-volume, on the other hand, was designed (and tested) to work with Electron like a charm!

Usage

import { setVolume, getVolume, setMute, getMute } from "easy-volume";

// Set volume - value from 0 to 100%
await setVolume(20);

// Get volume - value from 0 to 100%
const volume: number = await getVolume();
console.log(volume); // 20

// Set mute - true is muted and false is unmuted
await setMute(true);

// Get current mute status - true if system audio is muted, otherwise false
const isMuted: boolean = await getMute();
console.log(isMuted); // true

API

getVolume(): Promise<number>

  • Get current system volume
  • Returns: System volume, from 0 to 100 [%]

setVolume(targetValue: number) => Promise<void>

  • Change system volume to target value
  • Param targetValue: Target volume, from 0 to 100

getMute(): Promise<boolean>

  • Get current mute status (whether the system audio is muted or not)
  • Returns: Whether the system audio is muted, i.e. true == muted, false == unmuted

setMute(isMuted: boolean) => Promise<void>

  • Either mute or unmute system audio
  • Param isMuted: Whether to mute or unmute the system audio

toggleMute() => Promise<void>

  • Toggle mute state
  • Returns: Current (new) mute state (true == muted, false == unmuted)

Compatibility

This library should be compatible with every of the most popular OS:

  • Windows (uses native C++ CLI tool I wrote by myself (see src/platforms/windows/main.cpp))
  • macOS (uses AppleScript (osascript))
  • Linux (uses Advanced Linux Sound Architecture (ALSA, amixer) - installed by default on most Linux distros)

Tested on:

  • Windows 11 22H2
  • macOS Ventura 13.4
  • Ubuntu 22.04.2 LTS

Test

You can test the library on your setup by running this command:

npm test

If any of the test fails or you're using another setup which isn't implemented here, feel free to create an issue or a pull request.

Feel free to give some ideas for future features by creating issues on the GitHub repository.

Building

To build the library, just run simple

npm run build

IMPORTANT: After building, be sure to copy src/platforms/windows/volume.exe into dist/platforms/windows/volume.exe

Creating own implementations

If you're using a different setup and/or want to create your own implementation of the library, feel free to make a pull request.

  1. In src/platforms, create a new directory with the name of your target platform.
  2. In your newly created directory, create index.ts file which exports an object of type PlatformImplementation (see its declaration in src/types.ts).
  3. Create your custom implementation. Note: if you're calling some native modules or any files, make sure to surround your path with the toElectronPath function from src/utils/toElectronPath.ts.
  4. In the src/index.ts file, add a special case to the switch statement with your platform, following the patterns in the file.
  5. Before creating the pull request, please test the library (see above for running tests).

Made with ❤️ by Artur Nowak

easy-volume's People

Contributors

arciiix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

xhl592576605

easy-volume's Issues

ERROR in ./node_modules/easy-volume/dist/execCommand.js Module not found: Error: Can't resolve 'child_process'

Hi,

I am trying to use this module in an angular application running in electron.
I run npm install easy-volume and later import it in my TS file with import { getVolume } from "easy-volume";

But after trying to build, I get these errors ->

Error: Module not found: Error: Can't resolve 'child_process' in '/Users/path-to-app/node_modules/easy-volume/dist'
Error: Module not found: Error: Can't resolve 'os' in '/Users/path-to-app/node_modules/easy-volume/dist'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
	- add a fallback 'resolve.fallback: { "os": require.resolve("os-browserify/browser") }'
	- install 'os-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.fallback: { "os": false }

Error: Module not found: Error: Can't resolve 'path' in '/Users/abhinavram.ananth/LinkDesktop/RLDC-app/call-popout-app/node_modules/easy-volume/dist/platforms/windows'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
	- add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
	- install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.fallback: { "path": false }

I tried adding those resolve lines to my package.json but I guess I am not adding them using proper syntax.

Anyway to solve these issues?
Thanks

High CPU usage by Antimalware when getting repeatedly

I am getting volume repeatedly 10 times per second and Antimalware Service Executable is using lot of my CPU
I think it's because the volume.exe is run every time
There is any solution for reduce this CPU usage ?
It could be good to have a functionnality that run the volume.exe with specific param quand the volume.exe is checking every X times and print the new volume/muted status when it changes, like that the file is run one time by the lib and the lib handle stdout of the process
Sorry for my bad english, I am French :/
Thanks

image

Not working on Windows

Hi !
I tried to use this lib on Windows 10 x64 with last updates, but I got an error in code, I found that error with volume.exe
I tried to compile on my computer but same error
image
"Unable to run the code because libstdc++-6.dll was not found. Reinstalling the program may correct this problem."

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.