Hello everyone, I've spent too much trying to add ...
# sdk-web
p
Hello everyone, I've spent too much trying to add input gain adjustment feature with no luck. I've also tried creating a custom processor. I don't think this is a hard problem to solve in general, it seems particularly hard to implement with Livekit. Does anyone have an implementation example? At this point we're considering going the Google meet route where the user is not allowed to adjust their volume during the meeting.
The only solution I have working now is to unpublish and publish the track. If someone has a better way of doing it let me know. This example allows you to update device and gain by unpublishing the track and publishing a new one
Copy code
// state
let localTrackPublication = null;

// event listener
room.localParticipant.on(ParticipantEvent.LocalTrackPublished, (publication) => {
  if (publication.kind === Track.Kind.Audio && publication.track && publication.isLocal) {
    localTrackPublication = publication;
  }
});

// method
async updateDeviceAndGain(audioSettings) {
  if (localTrackPublication) {
    room.localParticipant.unpublishTrack(localTrackPublication.track);
  }
  
  const localTrack = await createLocalAudioTrack({
    deviceId: audioSettings.audioDeviceId,
    echoCancellation: true,
    noiseSuppression: true,
    autoGainControl: false,
  });
  
  const audioContext = new (window.AudioContext || (window as any).webkitAudioContext)();
  const mediaStreamAudioSourceNode = audioContext.createMediaStreamSource(localTrack.mediaStream);
  const medialStreamDestinationNode = audioContext.createMediaStreamDestination();
  const gainNode = audioContext.createGain();
  gainNode.gain.value = audioSettings.micRecordingLevel / 100;
  mediaStreamAudioSourceNode.connect(gainNode);
  gainNode.connect(medialStreamDestinationNode);
  
  const track = new LocalAudioTrack(medialStreamDestinationNode.stream.getTracks()[0]);
  
  await room.localParticipant.publishTrack(track, {
    name: 'microphone',
    source: Track.Source.Microphone,
  });
}
h
Hey! I see that the tracks are always created using the participant's
audioContext
property. Also, there is the participant's
setAudioContext()
method (that is marked as internal). Maybe you will be able to use the room's options
webAudioMix
to set your audio context to be used from the beginning. An alternative would be to call
localParticipant.setAudioContext(yourAudioContext)
before the audio tracks are created, or anytime you need to update it afterwards.
I'm not sure if this will help. In the product that I work on we are planning on adding this feature in a few weeks, and the things that I mentioned are what I would try to use.
p
And what would you store in your state to be able to update your input device id and gain?
h
Currently we have an
useEffect
that calls
currentAudioTrack.setDeviceId(selectedAudioDeviceId)
to change the device ID, and we check what is the current device id of the current audio track when we need to read it. Now, to store / update the gain, I think I would see if I can change it dynamically on the audio context, or if I would need to create a new audio context and call the
localParticipant.setAudioContext()
. I still have to learn about the audio context api
p
I'll try this out and let you know
So far not working for me, I tried storing the
gainNode
in the state and reusing it, and I tried creating a new
audioContext
and setting it again using
setAudioContext
. I wonder if there is a way to get the
audioContext
from the room that was set initially? currentAudioTrack.setDeviceId is great, it worked. However, I cannot use it if I am republishing the track. Let me know if you implement in the future or if you have other ideas
h
I looked briefly at livekit's code and I see that it have this code to use the audioContext when receiving a remote audio track, but it does not have something similar for the local audio track.
So, when I actually implement this, I would try creating a custom track processor and use an audio context to control that