I have an SVG that produces audio based on keypresses, and I want to record the audio coming from it in my react app.
In the svg I have a group of audio elements like this:
<svg id="keyboard_svg">
*other svg stuff*
<g id="keyboard">
<audio src="https://arweave.net/<hash>/fileName1.flac"></audio
<audio src="https://arweave.net/<hash>/fileName2.flac"></audio>
<audio src="https://arweave.net/<hash>/fileName3.flac"></audio>
<audio src="https://arweave.net/<hash>/fileName4.flac"></audio>
</g>
</svg>
in my react app I get access to the audio elements like this:
const svg = document.getElementById("keyboard_svg")
const sound = svg.getElementById("Electric Piano")
so sound is the group of audio elements. I can iterate through them like this:
for(let i=0; i<sound.children.length; i++) {
//do something with sound.children[i]
}
the SVG is of a keyboard that users can use their keyboard to play. my goal is to record the audio from these elements in javascript. My understanding is that I can create a MediaStream object, add MediaStreamTracks to it, and then pass the MediaStream in the MediaRecorder constructor. But maybe Im misunderstanding how it works.
What I've tried:
let stream = new MediaStream()
for(let i=0; i<sound.children.length; i++) {
stream.addTrack(sound.children[i])
}
this doesnt work because audio elements are not of type MediaStreamTrack.
let audio = new Audio()
for(let i=0; i<sound.children.length; i++) {
let source = document.createElement("source")
source.src = sound.children[i].src
audio.appendChild(source)
}
so audio is gives me this:
<audio>
<source src="https://arweave.net/<hash>/fileName1.flac">
<source src="https://arweave.net/<hash>/fileName2.flac">
<source src="https://arweave.net/<hash>/fileName3.flac">
<source src="https://arweave.net/<hash>/fileName4.flac">
</audio>
then I tried:
let stream = audio.captureStream()
but if I pass that into a MediaRecorder, i get:
Failed to construct 'MediaRecorder': parameter 1 is not of type 'MediaStream'
So thats where im stuck at basically. Im aware of these other functions
AudioContext.createMediaElementSource()
AudioContext.createMediaStreamSource()
AudioContext.createMediaStreamTrackSource()
MediaStreamTrackGenerator()
it sounds like theyre what I need but I cant figure out how to use them. Ive tried a bunch of other things but what I listed above is what I think makes most sense, but still doesnt work.
Any help would be great!