Web Audio API merging audio in single channel doesn't fully work - audio

I have a video and a WebRTC audio stream and want to use Web Audio API to send the audio from the video to the Left channel, while the WebRTC to the right channel. So basically I'm doing:
video = document.getElementsByTagName("video")[0]
video.src = "http://link/to/my/video"
video.load()
audioContext = new AudioContext()
videoSourceL = audioContext.createMediaElementSource(video)
#create merger with 2 inputs, left (0) and right (1)
merger = audioContext.createChannelMerger(2)
merger.connect(audioContext.destination)
#now strange work around for WebRTC
audio = new Audio();
audio.muted = true
audio.srcObject = remoteStream
audioStreamR = audioContext.createMediaStreamSource(remoteStream)
# connect remote audio stream channel 0 to input 1 (right)
audioStreamR.connect(merger, 0, 1)
#connect video source channel 0 to input 0 (left)
videoSourceL.connect(merger, 0, 0)
The problem I have is that although the remote audio does go to the right channel (And is not audible in the Left), the audio from the video is also still slightly present in the right channel. So basically I have audio bleeding. The weird thing is that if I redirect both the remote stream and the video to the same channel, then the other channel has absolute silence.
Whereas if I had used an oscillator in place of the video audio, I would have a perfect separation. Any idea what I'm doing wrong?
EDIT: I also tried from the OS audio settings to turn off the left channel, and the audio bleeding to the right channel stopped (also tried this on a colleagues machine), so is this maybe a hardware/configuration
issue?

Was a hardware issue after all, effect is not there with good headphones.

Related

DiscordJS audio playback with multiple streams

I'm trying to create a discord bot using discord.js that is able to both play music and sound effects.
The bot correctly plays music, but when trying to play a sound effect during music playback, the sound effect never starts/ends.
I have the following code for playing sound effects.
function play(guildID, serverQueue, resource){
const newAudioPlayer = createAudioPlayer();
const oldAudioPlayer = serverQueue.audioPlayer;
const connection = getVoiceConnection(guildID)
oldAudioPlayer.pause();
connection.subscribe(newAudioPlayer);
newAudioPlayer.on(AudioPlayerStatus.Idle, () => {
console.log("Done playing");
connection.subscribe(oldAudioPlayer);
oldAudioPlayer.unpause();
newAudioPlayer.stop();
}).on('error', err => {
console.log("Something went wrong when trying to play sound effect");
console.log(err);
connection.subscribe(oldAudioPlayer);
oldAudioPlayer.unpause();
newAudioPlayer.stop();
})
newAudioPlayer.play(resource);
console.log("Trying to play");
}
I realize that a voice connection can only subscribe to one audioplayer at a time and that an audioplayer can only play one resource at a time. This is why, I create a new temporary audio player called "newAudioPlayer" that will play the short sound effect. I then pause the old audio player and subscribe the connection to the new audio player.
The sound effect is correctly played as long as the oldAudioPlayer has not been used to play a resource before. As soon as the oldAudioPlayer has been used to play a resource, the newAudioPlayer never starts playing the resource. I have checked all the different AudioPlayerStates for the newAudioPlayer, but none of them get triggered.
serverQueue.audioPlayer is initialized when a voice channel is joined, and is always set.
The program does print "Trying to play" but no audio can be heard.
Apparently this is a known issue with discord.js:
https://github.com/discordjs/discord.js/issues/7232
The workaround is to play the local file as a stream if the file for the other audioPlayer is also reading a stream.

Screen Recording with both headphone & system audio

I am trying to build a web-application with the functionality of screen-recording with system audio + headphone-mic audio being captured in the saved video.
I have been thoroughly googling on a solution for this, however my findings show multiple browser solutions where the above works so long as headphones are NOT connected, meaning the microphone input is coming from the system rather than headset.
In the case that you connect headphones, all of these solutions capture the screen without video-audio, and the microphone audio from my headset. So to re-clarify on this, it should have recorded video-audio from the video being played whilst recording, and the headset-mic audio also.
This is thoroughly available in native applications, however I am searching for a way to do this on a browser.
If there are no solutions for this currently that anybody knows of, some insight on the limitations around developing this would also really help, thank you.
Your browser manages the media input being received in the selected tab/window
To receive media input, you need to ensure you have the checkbox Share Audio in the image below checked. However this will only record media-audio being played in your headphones, when it comes to receiving microphone audio, the opposite must be done i.e the checkbox should be unchecked, or merge the microphone audio separately on saving the recorded video
https://slack-files.com/T1JA07M6W-F0297CM7F32-89e7407216
create two const, one retrieving on-screen video, other retrieving audio media:
const DISPLAY_STREAM = await navigator.mediaDevices.getDisplayMedia({video: {cursor: "motion"}, audio: {'echoCancellation': true}}); // retrieving screen-media
const VOICE_STREAM = await navigator.mediaDevices.getUserMedia({ audio: {'echoCancellation': true}, video: false }); // retrieving microphone-media
Use AudioContext to retrieve audio sources from getUserMedia() and getDisplayMedia() separately:
const AUDIO_CONTEXT = new AudioContext();
const MEDIA_AUDIO = AUDIO_CONTEXT.createMediaStreamSource(DISPLAY_STREAM); // passing source of on-screen audio
const MIC_AUDIO = AUDIO_CONTEXT.createMediaStreamSource(VOICE_STREAM); // passing source of microphone audio
Use the method below to create a new audio source which will be used as as the merger or merged version of audio, then passing audios into the merger:
const AUDIO_MERGER = AUDIO_CONTEXT.createMediaStreamDestination(); // audio merger
MEDIA_AUDIO.connect(AUDIO_MERGER); // passing media-audio to merger
MIC_AUDIO.connect(AUDIO_MERGER); // passing microphone-audio to merger
Finally, connect the merged-audio and video together into one array to form a track, and pass it to the MediaStreamer:
const TRACKS = [...DISPLAY_STREAM.getVideoTracks(), ...AUDIO_MERGER.stream.getTracks()] // connecting on-screen video with merged-audio
stream = new MediaStream(TRACKS);

Web Audio Api destination maxChannelCount always is 2 on Linux

I have a problem with Web Audio API and an usb audio interface on Linux;
I wrote some audio player code on Web Audio API.
Everything is alright when I connect my 7.1 USB Audio Interface (TASCAM 16x08 - there are 8 output channels) and start my APP on Windows machine. context.destination.maxChannelCount equals 8 and I can select the channel to output the sound.
But when I do the same on Linux machine context.destination.maxChannelCount always is 2 (stereo).
I tried to:
create virtual audio multichannel device = same result - always only 2 maxChannelCount;
setting alsa, pulseaudio, jack audio connection kit and more...
The result is the same: in my code context.destination.maxChannelCount is always is 2
but the operating systems settings dialog detects 8 channels.
This is some code to be clear:
var context = new (window.AudioContext || window.webkitAudioContext)();
var audio = new Audio();
var source = context.createMediaElementSource(audio);
source.connect(context.destination);
audio.src = 'audio.mp3';
audio.play();
console.log(context.destination.maxChannelCount); //output on win: 2
on linux: 8
What can be the problem?
I found solution here https://ubuntuforums.org/archive/index.php/t-1072792.html
solved it by editing /etc/pulse/daemon.conf.
; default-sample-channels
= 2 uncomment the line and add more channels.
What browser are you running? Browser is responsible for giving you those available outputs, if they're not there (in all available browsers) then i think you're out of luck. I have done some things with Web Audio and multiple outputs and even on the same OS i got different results from different browsers.

vlc parameters to set audio stream from microphone

I have C# project where stream from ip-camera recorded to the file, I use libvlc.
This is part of code with vlc parameters:
string VlcArguments = #":sout=#transcode{acodec=mpga,deinterlace}:standard{access=file,mux=mp4,dst="C:\Users\I\Desktop\Output.mp4"}";
var media = factory.CreateMedia<IMedia>(rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov, VlcArguments);
var player = factory.CreatePlayer<IPlayer>();
player.Open(media);
filename is the path of the result file.
It works fine, but I need to record sound from a microphone Microphone (High Definition Audio Device).
What I need to change to achieve that?
UPD
It should look something like this
var media = factory.CreateMedia<IMedia>("dshow:// dshow-vdev=rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov dshow-adev=Microphone (High Definition Audio Device)", VlcArguments)
But it doesn't work (
UPD2
So, I think I found the answer
https://forum.videolan.org/viewtopic.php?f=14&t=124229&p=425550&hilit=camera+microphone+dshow#p425550
Unfortunately this will not work

Windows Phone 8.1 play audio data stream through speaker?

I receive over network PCM audio data stream and this part works fine so I am ending up with
DataReader incomming = args.GetDataReader();
byte[] RcvBuffer = new byte[incomming.UnconsumedBufferLength];
incomming.ReadBytes(RcvBuffer);
I have all audio data in buffer.
How I can play this through telephone Speaker ? Can you point me in some direction ?
Thanks
There're many ways to do that.
You can prepend the WAVE header to your data, and use MediaElement for playback, see the documentation for SetSource method.
If however by “telephone speaker” you mean the earphone, then it is only possible if you are creating a VoIP app.
It took a while but I sorted it, maybe someone else will need help in the future.
First Problem - since I just started app development for Windows Phone I have chosen Blank App (Windows Phone) instead Blank App (Windows Phone Silverlight) and I did not have access to many features that are available in Silverlight projects, so my suggestions for beginners: understand what each project is for.
Like Soonts said there are many ways to do this, this is one that I used.
I simplified this code and retyped this so there can be some typos.
using Microsoft.Xna.Framework.Audio;
using System.IO;
1) Create Stream to load your incoming data:
MemoryStream stream = new MemoryStream();
2) Load data from buffer to stream:
stream.Write(RcvBuffer, 0, RcvBuffer.Length);
3) I am using SoundEfect to play this through Loud-Speaker. Sample rate that I use is 8 kHz
SoundEffect sound;
sound = new SoundEffect(stream.toArray(), 8000, AudioChannels.Mono)
sound.Play();

Resources