Skip to main content

Audio Input State

By default, the Anam client starts capturing input audio from the user’s microphone when a session starts and stops capturing when the session ends. For certain use cases, you may wish to control the input audio state programmatically.

The InputAudioState Type

The InputAudioState interface contains two properties:
interface InputAudioState {
  isMuted: boolean;
  permissionState: AudioPermissionState;
}

enum AudioPermissionState {
  PENDING = 'pending',
  GRANTED = 'granted',
  DENIED = 'denied',
  NOT_REQUESTED = 'not_requested',
}

Getting the Current State

To check the current input audio state:
import { InputAudioState, AudioPermissionState } from '@anam-ai/js-sdk';

const audioState: InputAudioState = anamClient.getInputAudioState();
// { isMuted: false, permissionState: 'granted' }

// Check if microphone permission was granted
if (audioState.permissionState === AudioPermissionState.DENIED) {
  console.log('Microphone access was denied');
}

Mute Audio Input

To mute the input audio:
const audioState: InputAudioState = anamClient.muteInputAudio();
// { isMuted: true, permissionState: 'granted' }
If you mute the input audio before starting a stream, the session will start with microphone input disabled.

Unmute Audio Input

To unmute the input audio:
const audioState: InputAudioState = anamClient.unmuteInputAudio();
// { isMuted: false, permissionState: 'granted' }

Changing Audio Input Device

To switch to a different microphone during an active session:
// Get available audio input devices
const devices = await navigator.mediaDevices.enumerateDevices();
const audioInputs = devices.filter(device => device.kind === 'audioinput');

// Switch to a specific device by its ID
await anamClient.changeAudioInputDevice(audioInputs[1].deviceId);
You must be actively streaming to change the audio input device. This method throws an error if called before streamToVideoElement().

Configuration Options

Disabling Audio Input

To completely disable microphone input (useful for text-only interactions or when using custom input streams):
import { createClient } from '@anam-ai/js-sdk';

const anamClient = createClient(sessionToken, {
  disableInputAudio: true,
});
When disableInputAudio is true:
  • The SDK will not request microphone permissions
  • muteInputAudio() and unmuteInputAudio() will have no effect
  • Any user-provided audio stream will be ignored

Specifying an Audio Device

To use a specific microphone at initialization:
const anamClient = createClient(sessionToken, {
  audioDeviceId: 'specific-device-id',
});

Custom Input Streams

If you wish to control the microphone input audio capture yourself, you can pass your own MediaStream object when starting a stream:
// Get your own media stream
const userProvidedMediaStream = await navigator.mediaDevices.getUserMedia({
  audio: true,
  video: false,
});

anamClient.streamToVideoElement(
  'video-element-id',
  userProvidedMediaStream
);
The userProvidedMediaStream object must be an instance of MediaStream and the user input audio should be the first audio track returned from the MediaStream.getAudioTracks() method.
This is the default behavior if you are using navigator.mediaDevices.getUserMedia().