Skip to main content
Version: Next

ConstantSourceNode

The ConstantSourceNode is an AudioScheduledSourceNode which represents an audio source, that outputs a single constant value. The offset parameter controls this value. Although the node is called "constant" its offset value can be automated to change over time, which makes it a powerful tool for controlling multiple other AudioParam values in an audio graph. Just like AudioScheduledSourceNode, it can be started only once.

Constructor

constructor(context: BaseAudioContext, options?: ConstantSourceOptions)

ConstantSourceOptions

ParameterTypeDefault
offset
Optional
number1.0Initial value for offset
info

You can also create a ConstantSourceNode via BaseAudioContext.createConstantSource(), that creates node with default values.

Example

import React, { useRef } from 'react';
import { Text } from 'react-native';
import {
AudioContext,
OscillatorNode,
GainNode,
ConstantSourceNode
} from 'react-native-audio-api';

function App() {
const audioContextRef = useRef<AudioContext | null>(null);
if (!audioContextRef.current) {
audioContextRef.current = new AudioContext();
}
const audioContext = audioContextRef.current;

const oscillator1 = audioContext.createOscillator();
const oscillator2 = audioContext.createOscillator();
const gainNode1 = audioContext.createGain();
const gainNode2 = audioContext.createGain();
const constantSource = audioContext.createConstantSource();

oscillator1.frequency.value = 440;
oscillator2.frequency.value = 392;
constantSource.offset.value = 0.5;

oscillator1.connect(gainNode1);
gainNode1.connect(audioContext.destination);

oscillator2.connect(gainNode2);
gainNode2.connect(audioContext.destination);

// We connect the constant source to the gain nodes gain AudioParams
// to control both of them at the same time
constantSource.connect(gainNode1.gain);
constantSource.connect(gainNode2.gain);

oscillator1.start(audioContext.currentTime);
oscillator2.start(audioContext.currentTime);
constantSource.start(audioContext.currentTime);
}

Properties

Inherits all properties from AudioScheduledSourceNode.

AudioNodeproperties
NameTypeDefault valueDescription
offset
Read only
AudioParam1.0a-rate AudioParam representing the value which the node constantly outputs.

Methods

Inherits all methods from AudioScheduledSourceNode.