purple-rainbow-1246
07/28/2025, 12:18 PMpurple-rainbow-1246
07/28/2025, 9:09 PM// 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,
});
}high-flower-44013
07/29/2025, 6:05 PMaudioContext 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.high-flower-44013
07/29/2025, 6:06 PMpurple-rainbow-1246
07/29/2025, 6:09 PMhigh-flower-44013
07/29/2025, 6:19 PMuseEffect 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 apipurple-rainbow-1246
07/29/2025, 6:21 PMpurple-rainbow-1246
07/29/2025, 7:12 PMgainNode 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 ideashigh-flower-44013
07/29/2025, 8:03 PMhigh-flower-44013
07/29/2025, 8:06 PM