1.0.14 • Published 11 months ago

@mickit/input v1.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@mickit/input

📚 Overview

@mickit/input is a simple and lightweight library for capturing audio input from the microphone. It provides a simple API to listen to audio chunks, detect silence, and adjust the gain automatically.

The Audio Chunk is encoded is a simple .

  • Audio Chunk: Int16Array (PCM 16-bit signed integer)

🚀 Installation

To install MicInputManager, use npm or yarn:

npm install @mickit/input

or

yarn add @mickit/input

🛠️ Setup and Initialization

Basic Usage Example

import { MicInputManager, MicInputManagerEvent } from '@mickit/input';

const micManager = new MicInputManager(250, true);

// Start listening to microphone
micManager.listen();

// Event listeners
micManager.on(MicInputManagerEvent.LISTENING, () => {
  console.log('Microphone is listening');
});

micManager.on(MicInputManagerEvent.STOPPED, () => {
  console.log('Microphone has stopped');
});

micManager.on(MicInputManagerEvent.MIC_MUTED, () => {
  console.log('Microphone muted');
});

micManager.on(MicInputManagerEvent.CHUNK, (event) => {
  // event.data is a Int16Array
  console.log('Audio chunk:', event.data);
});

🎧 API Reference

Constructor

constructor(silenceDurationMs?: number, debug?: boolean)
  • sliceDurationMs: Duration of audio chunks in milliseconds (default: 250).
  • debug: Enable debug logs (default: false).

Methods

listen()

Starts listening to the microphone input.

micManager.listen();

stop()

Stops the microphone input.

micManager.stop();

on(event: MicInputManagerEvent, callback: MicKitEventCallback)

Adds an event listener.

micManager.on(MicInputManagerEvent.LISTENING, () => console.log('Listening'));

setMaxGain(maxGain: number)

Sets the maximum gain value.

micManager.setMaxGain(2.0);

setMinGain(minGain: number)

Sets the minimum gain value.

micManager.setMinGain(0.1);

setGain(gain: number)

Manually sets the gain. (if auto gain is enabled this will be changed automatically) If you want to set the gain manually, you should disable auto gain.

micManager.setGain(1.0);

setAutoGain(enabled: boolean)

Enables or disables automatic gain adjustment.

micManager.setAutoGain(true);

setDefaultGain(gain: number)

Sets the default gain value.

micManager.setDefaultGain(1.0);

setSelectedMic(deviceId: string)

Sets the microphone device to use.

micManager.setSelectedMic('default');

getAvailableMicrophones()

Fetches available microphone devices. Returns a promise with an array of MicrophoneDevice.

const mics = await micManager.getAvailableMicrophones();
console.log(mics);

setProcessorThresholds(thresholds: ProcessorThresholds)

Sets the processor thresholds.

micManager.setProcessorThresholds({
  silentThreshold: 0.01,
  tooQuietThreshold: 0.1,
  tooLoudThreshold: 0.9,
  peakThreshold: 0.9,
  clippingPercentage: 0.1, // 10%
});

📊 Events

EventDescriptionhasRMS
LISTENINGMicrophone has started listening.false
STOPPEDMicrophone has stopped.false
MIC_MUTEDMicrophone was muted.false
MIC_UNMUTEDMicrophone was unmuted.false
MIC_NOT_FOUNDNo microphone was found.false
MIC_PERMISSION_DENIEDMicrophone access was denied.false
MIC_DISCONNECTEDCurrently used microphone was disconnected.false
MIC_LIST_CHANGEDList of available microphones changed.false
STREAM_ENDEDAudio stream has ended.false
CHUNKAudio chunk data is available.true
TOO_LOUDAudio input is too loud.true
TOO_QUIETAudio input is too quiet.true
SILIENTSilence detected.true
PEAKSAudio peaks detected.true
CLIPPINGAudio clipping detected.true
AUDIO_PROCESSING_ERRORError during audio processing.false

RMS: Root Mean Square Some events have RMS data available in the event object. It can be accessed using event.rms. The RMS value is a number between 0 and 1. It represents the volume level of the audio input.

🐞 Debugging

Enable debug mode

const micManager = new MicInputManager(250, true);

Debug logs will appear in the console prefixed with [DEBUG].

Future Improvements

  • Other sample rates (Currently fixed at 16000 Hz)
  • Other Audio encodings (Currently fixed at PCM 16-bit signed integer)
  • VAD (Voice Activity Detection)
  • Audio Processing (Noise Reduction, Echo Cancellation, etc.)
  • Noise Level Detection

📄 License

This project is licensed under the MIT License.

🤝 Contributing

Contributions are welcome! Please open an issue or submit a pull request.

1.0.14

11 months ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago