Skip to main content

MicrophoneAudioProvider

Class

The Audio Track Provider of the audio from microphone.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

Since Lens Scripting v145

Index

Example

// @input Asset.AudioTrackAsset audioTrack
// @input int sampleRate = 44100

var control = script.audioTrack.control;
if (control.isOfType(MicrophoneAudioProvider.getTypeName())) {
control.start();
}

control.sampleRate = script.sampleRate;
var audioFrame = new Float32Array(control.maxFrameSize);

script.createEvent("UpdateEvent").bind(function (eventData) {
var audioFrameShape = control.getAudioFrame(audioFrame);
if (audioFrameShape.x == 0) {
return;
}
// do something with data
})

Extends

Properties

maxFrameSize

readonly maxFrameSize: number

The maximum frame size of the audio track asset.

Since

Lens Scripting Version 145

Inherited from

AudioTrackProvider.maxFrameSize


sampleRate

sampleRate: number

Sample rate (samples per second) of the audio track asset.

Since

Lens Scripting Version 145

Inherited from

AudioTrackProvider.sampleRate

Methods

ensureLoaded()

ensureLoaded(): Promise<void>

Returns a Promise that resolves once this Provider has finished loading its resource.

Returns

Promise<void>

Remarks

If the Provider is already loaded when ensureLoaded() is called, the returned Promise resolves immediately. Otherwise it resolves once loading completes.

Completion is determined by the Provider's load status (see Provider.getLoadStatus). Some Providers only become loaded once supporting runtime data is available — for example, a Provider whose resource depends on tracking will not report loaded until the relevant tracking data has arrived — so the returned Promise stays pending until that data is produced.

This method is useful when Lens script needs to await a Provider before reading its data — for example, waiting for a remote-asset Provider to finish downloading before sampling it.

Since

Lens Scripting Version 370

Example

Fetch a Texture at runtime via RemoteMediaModule and wait for its Provider to finish loading before using it:

const internetModule = require("LensStudio:InternetModule");
const remoteMediaModule = require("LensStudio:RemoteMediaModule");

function loadImageTexture(url) {
const resource = internetModule.makeResourceFromUrl(url);
return new Promise((resolve, reject) => {
remoteMediaModule.loadResourceAsImageTexture(resource, resolve, reject);
});
}

const texture = await loadImageTexture("https://example.com/image.png");

// Wait until the fetched Texture's Provider has finished loading before using it.
await texture.control.ensureLoaded();
print("Texture ready, loadStatus: " + texture.control.getLoadStatus());

Inherited from

AudioTrackProvider.ensureLoaded


getAudioFrame()

getAudioFrame(audioFrame): vec3

Exposes user data

Writes current frame audio data to the passed in Float32Array and returns its shape. The length of the array can't be more than maxFrameSize.

Parameters

ParameterType
audioFrameFloat32Array

Returns

vec3

Since

Lens Scripting Version 147


getAudioFramePCM16()

getAudioFramePCM16(audioFrame): vec3

Exposes user data

Retrieves the current audio frame, converts it to PCM16 (Pulse-Code Modulation) format, and writes the raw audio samples into the provided audioFrame as an Int16Array. The length of the array can’t be more than maxFrameSize.

Parameters

ParameterType
audioFrameInt16Array

Returns

vec3

Since

Lens Scripting Version 145


getLoadStatus()

getLoadStatus(): LoadStatus

Returns

LoadStatus

Inherited from

AudioTrackProvider.getLoadStatus


getTypeName()

static getTypeName(): TypeName<MicrophoneAudioProvider>

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<MicrophoneAudioProvider>

Since

Lens Scripting Version 145

Overrides

AudioTrackProvider.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

AudioTrackProvider.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object matches or derives from the passed in type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

AudioTrackProvider.isOfType


isSame()

isSame(other): boolean

Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

AudioTrackProvider.isSame


start()

start(): void

Start processing audio from microphone. Useful to avoid redundant processing.

Returns

void

Since

Lens Scripting Version 145


stop()

stop(): void

Stop processing audio from microphone.

Returns

void

Since

Lens Scripting Version 145