Objective
To apply an audio filter or effect on an inbound video stream/subscriber.
Applies To
- Vonage Video API
- Javascript SDK
- Subscriber Audio
Procedure
Audio and Video are normally provided as a single MediaStream to a <VIDEO> element in your HTML page. These will need to be separated in order to apply audio effects or filters. In this example, we are going to use the StereoPanner object to allow subscribers to pan individual streams to the left or right. There are many other features in the Web Audio API that could be used in this way.
Below is the basic process. Please find an example Panner class in the additional information below.
- Get a reference to your video element
- Get the MediaStream object from the video element
- Get the AudioTrack from the MediaStream
- Clone the AudioTrack and insert it back into the MediaStream
- Disable the original AudioTrack in the MediaStream
- Create an AudioSource from the MediaStream
- Create a StereoPanner object
- Connect the AudioSource to the Panner
- Connect the Panner to the Destination
- Set a pan value from -1 -> 1 in the Panner to pan audio left to right
Additional Information
Here is an example Panner class that can be used to pan audio on an inbound video stream.
class Panner {
constructor(element){
// This is the VIDEO element you are receiving the stream into
this.element = element
// Create an AudioContext
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
// The "stream" is the audio and video from the element
let stream = element.captureStream();
// The origAudioTrack is the original audio from the stream.
let origAudioTrack = stream.getAudioTracks()[0]; // There should only be 1
let newAudioTrack = origAudioTrack.clone(); // Then we create a clone of the audio track
// Then we add the new audio track back into the original stream and disable the old one
stream.addTrack(newAudioTrack);
origAudioTrack.enabled = false;
// We create an audio source from the stream and a new StereoPanner
let source = audioContext.createMediaStreamSource(stream);
this.panner = audioContext.createStereoPanner();
// We set a value for the panner. Default is 0 (zero) for centered
this.panner.pan.value = 1; // --> sound panned to the right
// Finally we connect the panner to the output destination and the source to the panner
// source --> panner --> destination
this.panner.connect(audioContext.destination);
source.connect(this.panner);
}
// Calling set(-1) will pan left and set(1) will pan right
set (value){
this.panner.pan.value = Number(value).toFixed(1);
}
}
To use this code you need to create a new Panner object passing your video element to the constructor. You then need to call Panner.set() with a value of -1 to 1 to pan from left to right.
Articles in this section
- Why isn't the Video Inspector displaying user information and events?
- Has the Video Session Monitoring Callback data changed?
- Vonage Scalable Video Simulcast
- How can I find the creation or expiration time of a token?
- What does OT.checkSystemRequirements() check?
- How to Get HD/FHD Resolution Recordings and Live Streaming in Vonage Video API Sessions
- How can I implement a ringing or calling application with Vonage Video API?
- How can I get information about multiple sessionIds in one Insights GraphQL query?
- What codecs are supported on the Vonage Video API platform?
- Can I set the Audio Output device when using the Vonage Video API in Javascript