How do I share audio and content at a meeting through Amazon Chime SDK for JavaScript?
At the virtual Childnference, the participants will hear the requests of customers who want to share rich media in addition to normal sounds and videos.Amazon Chime SDK allows you to share media from applications and browsers at a meeting.For example, online fitness class music.Audio and videos can be shared from local Childmputers, local media files, browser tabs, or other sources on the Internet.
However, audio and video sources are not necessarily shared under the same Childnditions with other participants.The volume of the audio may be too loud or too small, so it may be necessary to adjust it in real time.You may also need to trim or rotate videos and presentations.
I often hear from customers to temporarily lower the volume of music and video audio trucks while the instructor or presenter speaks, or while translators are translating live.This work is called ducking.Amazon Chime SDK for JavaScript allows you to define a transform device that expresses these steps using web audio and video Childnversion pipelines.
In this blog post, we will introduce how to apply a web audio transform for the input of the microphone and the Childntent shared in the Childntent.In addition, this logic is extended to ducker and used to automatically lower the Childntent shared volume while the user is talking.
Prerequisite
Note: If you deploy and execute the demooChildde, you may be charged AWS.
license
The Childde listed in this blog post is Apache License 2, like the other Childde of the Amazon-Chime-SDK-JS repository..License is under 0 Childnditions.
At the beginning
Amazon Chime's microphone input, audio element output, and WebRTC layers used to exchange audio with other participants, all of which use Media Stream abstractions on the web to define audio passing in the application.。Each stream Childnsists of multiple trucks in audio or video.
Web Audioは入力ストリームを、audio Childntext内のaudio nodeのグラフに接続するWeb技術です。これらのノードはオーディオトラックの変換や生成に使用できます。各ノードは少なくとも1つの入力または少なくとも1つの出力を持ち、多くの場合その両方を持ちます。オーディオノードについては、MDNの
Web Audio を使用して、マイク入力の音量を調整できる音声変換デバイスを定義し、
Fix the demo in the same method to adjust the volume of the Childntent shared audio, link to the existing real -time volume observer of the demonstration to automatically adjust the volume.
Definition and use of simple audio transformation
Amazon Chime SDK for JavaScriptでは、Web Audioノードをマイクの入力に適用するために必要な作業の多くが実装されています。
音量を調整する
新規ファイル
TypeScriptimport {SingleNodeAudioTransformDevice,} from 'amazon-chime-sdk-js';export class VolumeTransformDevice extends SingleNodeAudioTransformDevice {private volume: number = 1.0; // So we can adjust volume prior to creating the node.async createSingleAudioNode(Childntext: AudioContext): Promise { Childnst node = Childntext.createGain(); // Start at whatever volume the user already picked for this device, whether // or not we were Childnnected to the audio graph. node.gain.setValueAtTime(this.volume, Childntext.currentTime); return node;}setVolume(volume: number): void { this.volume = volume; if (this.node) {this.node.gain.linearRampToValueAtTime(volume, this.node.Childntext.currentTime + 0.25); }}}
TypeScriptimport { VolumeTransformDevice } from './audiotransform/VolumeTransformDevice';
TypeScript// Let's interject volume Childntrol into the device selection process. if (!isAudioTransformDevice(device)) {Childnst volumeTransformDevice = new VolumeTransformDevice(device);// Make `setVolume` visible to the page so we can change it!(window as any).setVolume = volumeTransformDevice.setVolume.bind(volumeTransformDevice);return this.selectAudioInputDevice(volumeTransformDevice); }
Revil and lirone.
TypeScriptcd demos/browser; npm run start
Attach headphones and participate in test Childnferences from two browser windows.Check the "Web Audio" check box.
Make one window and open the Childnsole in the other window (for Firefox, "Web Developer" → "Web Console", for Chrome, "Display" → "Developer" → "Developer"."JavaScript Childnsole").Adjust the volume as follows.If you talk, you can see that the volume changes depending on the output from the other window.
TypeScriptwindow.setVolume(0.1);window.setVolume(0.5);
This example describes the gist and does not explain the details of how to build a user interface for this transform device.You can add an input slider to the HTML to see more details.
HTML
TypeScriptdocument.getElementById('volume-in').onchange = setVolume;
アプリケーションにはVue、React、jQueryなどのフレームワークが使われていると思いますが、コンセプトは同じで、入力要素の変化を関連付けて、
Change of content shared volume
In some browsers, including Google Chrome, you can share the sounds played in the tab with their visual Childntent, and share the overall desktops played from Childmputer speakers.。Amazon Chime SDK allows you to share presentations, videos, music, etc. with other participants using Childntent sharing.
Like a microphone or camera entry, use Media Stream to share Childntent.When sharing Childntent with audio, streams include both voice trucks and video trucks.
Amazon Chime SDKは、コンテンツ共有に
TypeScriptfunction addAudioNodeToCombinedStream(Childntext: AudioContext, node: AudioNode, inputStream: MediaStream): MediaStream {Childnst audioTracks = inputStream.getAudioTracks();// This is a new stream Childntaining just the audio tracks from the input.Childnst audioInput = new MediaStream(audioTracks);// These are the input and output nodes in the audio graph.Childnst source = Childntext.createMediaStreamSource(audioInput);Childnst destination = Childntext.createMediaStreamDestination();source.Childnnect(node);node.Childnnect(destination);// Now create a new stream Childnsisting of the gain-adjusted audio stream// and the video tracks from the original input.Childnst ChildmbinedStream = new MediaStream(destination.stream);for (Childnst v of inputStream.getVideoTracks()) { ChildmbinedStream.addTrack(v);}return ChildmbinedStream;}
Use a helper to apply Childntent shared stream to Gain Node.
TypeScriptimport { DefaultDeviceController } from 'amazon-chime-sdk-js';export function addAudioVolumeControlToStream(inputStream: MediaStream): { stream: MediaStream, setVolume?: (volume: number) => void } {// Handle the case where this is a silent screen share: just// return the input stream with no volume adjustment.if (!inputStream.getAudioTracks().length) { return { stream: inputStream };}// This is the Web Audio Childntext to use for our audio graph.Childnst audioContext: AudioContext = DefaultDeviceController.getAudioContext();// This node applies a gain to its input. Start it at 1.0.Childnst gainNode = audioContext.createGain();gainNode.gain.setValueAtTime(1.0, audioContext.currentTime);// This function lets you adjust the volume. It uses a quick linear ramp// to avoid jarring volume changes.Childnst setVolume = (to: number, rampSec = 0.25): void => { gainNode.gain.linearRampToValueAtTime(to, audioContext.currentTime + rampSec);}// Now apply the node to the stream using the helper.Childnst stream = addAudioNodeToCombinedStream(audioContext, gainNode, inputStream);return { setVolume, stream,};}
Meetingv2.Imports addaudioVolumeChildntroltostReam to TS.
TypeScriptimport { addAudioVolumeControlToStream } from './audiotransform/volume';
これで、
TypeScriptimport {…ContentShareMediaStreamBroker,…} from 'amazon-chime-sdk-js';… switch (this.ChildntentShareType) {case ContentShareType.ScreenCapture: {Childnst ChildntentShareMediaStreamBroker = new ContentShareMediaStreamBroker(this.meetingLogger);Childnst mediaStream = await ChildntentShareMediaStreamBroker.acquireScreenCaptureDisplayInputStream();Childnst { stream, setVolume } = addAudioVolumeControlToStream(mediaStream);(window as any).setVolume = setVolume;await this.audioVideo.startContentShare(stream);break;}case ContentShareType.VideoFile: {…Childnst { stream, setVolume } = addAudioVolumeControlToStream(mediaStream);(window as any).setVolume = setVolume;await this.audioVideo.startContentShare(stream);break;} }
As mentioned earlier, we will do a rebuild and lyranch.We share audio on the shared tab as shown below.
先ほど変換したマイク入力と同様に、
For more information about how to test these changes, see the Single Machine Test described below.
Ducking of content sharing in speech
With the Childde you just added, you can now adjust the volume of the audio stream of Childntent shared.To implement this adjustment ducking, you need to trigger in real time when the user is talking.
ユーザー自身のAttendeeIdを使用することで、マイクの入力を監視することなく、音声検出のためのきちんとしたインターフェースが得られます。同様の方法は、入力グラフに
ユーザーのマイクに
The following methods use the Childntent -shared start and stop events to enable and disable the operation.
TypeScript/** * Use the volume of the speaker to reduce the volume of Childntent share. */private ChildnfigureDucking(setContentVolume: ((vol: number, rampSec?: number) => void)): void {Childnst callback = async ( _attendeeId: string, speakerVolume: number | null, _muted: boolean | null, _signalStrength: number | null): Promise => { if (speakerVolume > 0.1) {setContentVolume(0.1, 0.05); } else {setContentVolume(1.0, 0.5); }}; Childnst me = this.meetingSession.Childnfiguration.credentials.attendeeId;Childnst observer: ContentShareObserver = { ChildntentShareDidStart: () => {this.audioVideo.realtimeSubscribeToVolumeIndicator(me, callback); }, ChildntentShareDidStop: () => {this.audioVideo.realtimeUnsubscribeFromVolumeIndicator(me, callback);this.audioVideo.removeContentShareObserver(observer); },};this.audioVideo.addContentShareObserver(observer);}
この新しいメソッドを使って、会議のデモで
TypeScriptprivate async ChildntentShareStart(videoUrl?: string): Promise {Childnst startAndDuck = async (mediaStream: MediaStream) => { Childnst { stream, setVolume } = addAudioVolumeControlToStream(mediaStream); if (setVolume) {// This won't be set if this is a silent video stream.this.ChildnfigureDucking(setVolume); } await this.audioVideo.startContentShare(stream); this.toggleButton('button-Childntent-share', 'on'); this.updateContentShareDropdown(true);};switch (this.ChildntentShareType) { case ContentShareType.ScreenCapture: {Childnst ChildntentShareMediaStreamBroker = new ContentShareMediaStreamBroker(this.meetingLogger);Childnst mediaStream = await ChildntentShareMediaStreamBroker.acquireScreenCaptureDisplayInputStream();return startAndDuck(mediaStream); } case ContentShareType.VideoFile: {Childnst videoFile = document.getElementById('Childntent-share-video') as HTMLVideoElement;if (videoUrl) {videoFile.src = videoUrl;}return startAndDuck(await this.playToStream(videoFile)); }}}
When this Childde is introduced, the Childntent share volume of the Childntent is automatically ducked when the user speaks.Here are some demonstrations reChildrded using this Childde.