Hi friends at resonance-audio,
Thank you so much for offering such an interesting and useful library! :-)
I've spent the past few days working on this, and I've gotten the following to work ๐๐ช:
export function createDraggableDiv(socketId, stream, participantUsername) {
const video = createVideo(socketId, stream, participantUsername);
const div = getWrapper(socketId, participantUsername);
document.getElementById('main').appendChild(div);
div.append(video);
const soundSource = scene.createSource();
soundSources[socketId] = soundSource;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TODO: Remove this temporary section about audioElement.
const audioElement = document.createElement('audio');
const audioSrc = audioSources.pop();
audioElement.src = audioSrc;
audioElement.crossOrigin = 'anonymous';
audioElement.load();
audioElement.play();
audioElement.loop = true;
div.append(audioElement);
const mediaElementAudioSourceNode = audioContext.createMediaElementSource(audioElement);
mediaElementAudioSourceNode.connect(soundSource.input);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TODO: Uncomment this section.
// const mediaStreamAudioSourceNode = audioContext.createMediaStreamSource(video.srcObject);
// mediaStreamAudioSourceNode.connect(soundSource.input);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
When I drag the divs around on my screen, they call soundSource.setPosition(x, y, z);
(not shown here), and I hear what I expected to hear in my headphones: the position of the dragged element on the screen determines where in 3D space in my ears I hear the sound. This is great! ๐
However, as you can see, what I actually am trying to build instead is a video chat platform that allows spatial audio.
I don't want to be using the wav files in my audioSources
array, which I was just doing temporarily to check whether Resonance was working.
What I'm really trying to do is use a mediaStreamAudioSourceNode
that comes from my video.srcObject
(from the RTCPeerConnection
).
How can I use a MediaStream source instead?
I really appreciate your help! :-)
P.S. For context, I want to build a video chat app that:
- is easy to start just by visiting a website, like Google Meet (I built this already)
- lets the user drag other participants' videos into other orders/arrangements, like Zoom
- uses spatial audio, where each user's choices of how to arrange other participants' videos does not affect others' spatial audio. E.g. On a call with 9 participants:
- Mike (or anyone) can choose to show himself his own selfie view or hide it, and it won't affect the audio that he or anyone else hears.
- Mike can choose to hide all but 3 of the other participants and arranges those visible 3 in one horizontal row, in which case the audio he hears will be 4 channels: front-left, front-center, front-right, and then a catch-all channel for all invisible videos, which maybe I'll orient as coming from behind Mike.
- Simultaneously, and without affecting Mike's experience or anyone else's, Carol could choose to show all 8 other participants, and she could arrange them in whatever line or grid she wants, and the audio from each will sound to her as if it's coming from the appropriate part of her screen.