How to connect to Windows 7 audio - c#

I want to programmatically be able to connect to my computer's audio output (the exact same thing I'd hear if I plugged my headphones into the side of my laptop) and collect the audio.
So far, I haven't really been able to find anything that does this, other than Core Audio APIs, and even then it seems better built for simply locating sound files and encoding/unencoding then.
I've also taken a look at NAudio, however this seems better suited, again, for sound files. The one bit of streaming it is able to do, is only uses a URL as the source.
Can anyone point me to a library or interface within the .Net framework that will allow me to simply connect to my computer's audio out function and collect a stream of data?

In NAudio, you can capture the audio being played on your system with WasapiLoopbackCapture.

Related

How can I save a webcam-recorded mp4 with audio in Visual C#?

I'm two days into researching this so I thought I'd post here. It looks like video recording software is usually written in C++ but my project requires C#. It's a WPF application and I just need it to save an mp4 locally. I don't need to upload to a server, I don't need to let the user pick a file location, I don't even need it to display the webcam feed as it's recording. All I need it to do is start/stop recording audio and video from a webcam/microphone and save it in a location that's determined by the code. I understand this may be broken down into steps like connecting to a webcam, connecting to a microphone, synchronizing them, encoding a video file, and saving it on the hard drive.
Are there any .NET classes that can help me with this? If not, can anyone recommend some libraries/frameworks/etc. that handle this sort of thing? I appreciate any help regarding how to do this. Thank you.

Grab Audio Sessions as they appear in the windows audio mixer (C# or C++)

I'm trying to figure out how to grab the individual audio streams as they appear in the audio mixer to reroute them to an aggregate audio device. I'm specifically looking to keep them as discreet streams for the purposes of the program I'm making (If they're muxed down to a 2-channel mix, that defeats the purposes of what I'm trying to achieve.)
E.X.: (As I've just made this account, I apparently am not able to post images, so here's a link to the image)
windows audio mixer
In this, I'm hoping to grab "System Sounds" and "Stream Client Bootstrapper" as discreet audio streams to route elsewhere, while maintaining their original destination as well (essentially copying the audio going to the original audio device to another simultaneously).
I'm looking to do this in either C# or C++. I've perused the audio APIs that microsoft has published, and while some things look to be close to what I'm trying to do, nothing has hit the nail on the head. I appreciate any help. Thanks.
The sessions can be enumerated using IAudioSessionManager2::GetSessionEnumerator and friends (sample C++ code is here and there). Standard Windows volume mixer application is using this API as well.
The API however has no access to data streams, you won't have either (you certainly don't have data whether they are downmixed or not). Neither you can reroute streams to another device. Applications are not allowed to interfere that deep. The best you can do is to create your own device, interactively select it as default output device and then accept data from applications playing audio through this device.

How can I play H.264 RTSP video in Windows 8 Metro C# XAML app?

I have a device that provides an H.264 video stream from a URL like:
rtsp://192.168.0.10:554/videoservice
Since this is live video I don't need to be able to control it (pause, rewind, etc), just play. Is this supported by MediaElement or another standard class, do I need something like Smooth Streaming Client SDK or is this a lot more complicated than I thought?
Update:
I downloaded Microsoft's Player Framework but this doesn't play the stream either. I can't find anything in the examples about RTSP.
Update:
I used Wireshark to compare the packets that VLC Media Player (which works) sends with MediaElement and Player Framework and neither of them seemed to use RTSP protocol. Instead they're sending WPAD packets to a different address, even though I've provided the IP address of the source. Why is this necessary? Is there any way of turning this behaviour off?
See the post here. You need to wrap your data in MPEG-4 Part 14 containers and then pass them into MediaElement.SetSource. It appears you can't do this just using the documented API. Here is the important information from the link:
We don't support RTP but rather the MPEG-4 Part 14 container format.
You will need to write our own source to be able to parse the data a
pass it directly to the Microsoft decoder. At this time we do not have
any samples on how to write a custom source and have it loaded from
your Metro style app. Unfortunately it is not possible to simply use
the documentation to figure out how to do this. I have been talking
with Stan and we are trying to figure out how and when we can make
this information available. As soon as this information is available
I will announce it on my blog http://blogs.msdn.com/mediasdkstuff/.
Here is a list of supported video formats.
There is also an example here of how to extend the media class which might have an example of how to do something similar to what you are asking.
There is a similar example here.

Real time audio playback from mic. c#

I am looking to create an application that will allow me to record from my mic and playback the recording through other pc's. At this point however I would just like it to play back on my own computer so I can get it working.
I have been looking at NAudio for the past few hours and it seems like it may be able to help me achieve this goal.
I am just wondering if anyone else has had any experience with this and if it is at all possible?
Thanks,
Stuart
There is an example project on codeproject doing this:
http://www.codeproject.com/KB/cs/Streaming_wave_audio.aspx
I don't know how low the latency is.
As a codec I'd recommend Speex(at least for speech). It's free, open source and offers low latency and low bandwidth.
Bass Audio Library is another solid option worth looking into.
It is possible to do, but you are unlikely to get low latency with WaveIn/WaveOut (possibly better results with WASAPI). You could use the BufferedWaveProvider (in the latest source code) to store up the audio being recorded from the microphone and supplying the output to soundcard.
NAudio is great as a starting point for audio capture and playback but as Mark pointed out the latency might be an issue.
If you take the next step and want to sent the audio data across the network you will need a codec to compress the data as PCM or WAV are uncompressed and for voice you only need a small part of the bandwidth needed for WAV.
As you are working with C# there is a C# port of Speex available, called NSpeex, which might be worth having a look at..

C# resample audio from 8khz to 44.1/48khz

I have encountered a bug in DirectShow .NET where I create a secondary buffer with a sample rate of 8khz, and upon playback, the sound plays back at approx. 8.1khz instead.
Googling this, I discovered that I might be forced to upsample the 8khz audio myself to 48khz or 44.1khz depending on the soundcard in the PC.
Is there any C# library or generic algorithm I could use for this?
Thanks!!
Roey
For Alvas.Audio see code below
byte[] data48khz = AudioCompressionManager.Convert(format8khz, format48khz, data8khz, false);
You could always port Secret Rabbit Code to C#?
Or how about using the Audio Compression Manager directly via platform invoke?
Your issues of "sounds being played back too fast" may be soundcard specific. Not sure which OS you are on, but I believe Windows natively upsamples all audio streams to either 44 or 48khz before directing the samples to the soundcard (so it can properly mix it with all the other streams). So I'm not sure if you upsample with your own code if you'll get any improvements.
Have you tried using a different PC (running a different OS) or plugging in some USB headphones to see if this issue is consistent?

Categories