m1dugh / native-sound-mixer

MIT License
25 stars 13 forks source link

Native Sound Mixer

Introduction

Native cross-platform sound mixer

This node.js project is a sound mixer for node desktop apps providing control over volume for each render/capture device (I/O devices) and for each audio session in an audio device separately.

The native c++ code is provided in cppsrc/ and compiled using node-addon-api

postinstall scripts will automatically build bin files

Install

This is a Node js package available through npm registry.

prerequisites

Linux

Windows

Install

> npm install native-sound-mixer

or using yarn :

> yarn add native-sound-mixer

Features


DOCUMENTATION

Summary :

  1. SoundMixer: factory, default export

  2. Device: Represents a physical/virtual device with channels and volume controls

  3. AudioSession: Represents an app-linked audio channel with volume controls

  4. Data Structures

1) SoundMixer

const device: Device | undefined = SoundMixer.getDefaultDevice(DeviceType.RENDER);


### 2) Device

```Typescript
class Device {
    private constructor(); // Device instantiation is disallowed
    public volume: VolumeScalar;
    public mute: boolean;
    public readonly name: string;
    public readonly type: DeviceType;
    public readonly sessions: AudioSession[];
}
// import ...

let device: Device;
// set device to any valid Device object.

const sessions: AudioSession[] = device.sessions;

// retrieving the mute flag const mute: boolean = device.mute;

// toggling mute device.mute = !mute;


 - ### device volume
gets and sets the [`volume scalar`](#volumescalar) for the device. 
```TypeScript
// import ...

// retrieving the volume 
const volume: VolumeScalar = device.volume;

// adding 10% to volume
device.volume += .1;
// import ...

// retrieving the volume 
const balance: VolumeBalance = device.balance;

// sets right VolumeScalar to 1 and left VolumeScalar to .5
// by default, left and right are equal to the VolumeScalar of the device
device.balance = {right: 1, left: .5};

3) AudioSession

// class declaration
class AudioSession {
    private constructor(); // AudioSession instantiation is disallowed

    public volume: VolumeScalar;
    public mute: boolean;
    public readonly name: string;
    public readonly appName: string;
}
// import ...

let session: AudioSession;
// set session to a valid session object
const mute: boolean = session.mute;
// toggling mute 
session.mute = !mute;
// import ...

let session: AudioSession;
// set session to a valid session object
const volume: VolumeScalar = session.volume;
// adding 10% to volume
session.volume += .1;
// import ...

// retrieving the volume
let session: AudioSession;
const balance: VolumeBalance = session.balance;

// sets right VolumeScalar to 1 and left VolumeScalar to .5
// by default, left and right are equal to the VolumeScalar of the session
session.balance = {right: 1, left: .5};
// import ...

// retrieving the state
let session: AudioSession;
const state: AudioSessionState = session.state;

if (state === AudioSessionState.ACTIVE) {
    // do something...
}

4) Data Structures

AudioSessionState.INACTIVE; // session is incative but valid AudioSessionState.ACTIVE; // session is active AudioSessionState.EXPIRED; // session no longer exists or is no longer available

 - ### DeviceType
an enumeration representing the type of the device. Possible values are : 

```TypeScript
import {DeviceType} from "native-sound-mixer";

DeviceType.RENDER; // device type is output
DeviceType.CAPTURE; // device type is input
DeviceType.ALL; // device type is both input and output

Contributing

See CONTRIBUTING.md


License

This project is under MIT license