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

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?

Related

Play and record 16-bit PCM (raw) audio bytes in WPF

Are there any standard Microsoft components that will allow the following from a WPF application:
Record (default) PC microphone input to 16-bit PCM audio byte array chunks, and
Play 16-bit PCM audio byte array chunks on the (default) PC speaker
I have tried to investigate whether I could somehow use the SoundPlayer, MediaElement or the MediaPlayer components, but I don't really think any of them fit the bill. If I'm wrong, please provide a bit of input on how to go about this.
I can use the great NAudio library by Mark Heath to accomplish it, but if at all possible I would like to avoid taking on a dependency outside of the .NET Framework.
Thanks in advance.
I'm afraid there's nothing built into the .NET framework to do this, which is why libraries like NAudio exist.

How to connect to Windows 7 audio

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.

Streaming Audio from video server c#

I have a video server with an IP:192.168.1.XX
It has 3 possible formats JPEG, MPEG-4 or H.264
The video server is broadcasting a video(with audio) on real time
I have no problems streaming the video with AFORGE lib
but i also need to stream the audio
the video server has several protocols: HTTP,RTSP,RTP,RTCP
according to the user's manual RTSP is the protocol I should use to get MPEG-4(Audio and video), but I haven't found anything to stream by RTSP on C# so I'm trying to stream audio and video separate
the ports are:
RTSP: 554
RTP(Video): 5556
RTP(Audio):5558
RTCP(Video): 5557
RTCP(Audio): 5559
Does any body know how RTP works or how can I get the sound from the video server?
I would learn gstreamer. I am assuming that you are using windows since you are doing this in C#. It has a fairly stable windows port with a nice .net wrapper. If you aren't using Windows, then gstreamer is most certainly your best bet.
In gstreamer you would most likely use a pipeline like:
your video src -> x264enc or ffenc_mpv4 -> rtph264pay or rtpmp4vpay -> udpsink
your audio src -> ffenc_aac or preferably a lower latency codec like mULaw -> rtppay -> udpsink
and so on. It is very easy to use. They even have a nice rtpbin for your to use if you want to actually manage an rtp session.
More information can be found here:
http://gstreamer.freedesktop.org/
Here is a nice sample of how to do rtp:
http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-good-plugins/html/gst-plugins-good-plugins-gstrtpbin.html
I have done this sort of thing with the direct show filters but it is much more involved. You usually have to manually handle the rtp payloading and the transport--not to mention deal with COM--whereas GStreamer provides those mechanisms for you out of the box.
You can use https://net7mma.codeplex.com/
It is a C# Media Server and it will get you each RtpPacket and from there you can get them to a Decoder of your choice among other things all without bogging down the source stream.

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..

Silverlight 4 - encoding PCM data from the microphone

I've written a basic SL4 application to capture audio data from the microphone using CaptureSource. The trouble is, it's raw PCM output - which means huge and uncompressed.
Given that I need this application to run purely within a SL4 environment, how can I compress the PCM audio data into something that can be delivered to a remote server more easily?
Essentially I need a solution that I can also deploy/include in a Windows Phone Series 7 application as well as one that will work in the browser environment - so managed code solutions only, I think?
In conversation, people have suggested Speex and WMA for instance, but I haven't found any libraries or examples that work without requiring reference to DLL's that won't work in a SL4 project.
Just a small addition to Jason's post:
There is another port of Speex to .Net and Silverlight 4 called NSpeex.
Please see the WavFileHelper class in Silverlight 4 Rough Notes: Camera and Microphone Support on Mike Taulty's blog (a bit lower than the middle of the page, but the full article is worthwhile) in which he compresses the PCM file to WAV.
Here's another example of when writing to WAV you can change values such as Mono/Stereo, which will directly change the size of the WAV file: Audio recorder Silverlight 4 sample. And one more that gives more details about writing to WAV: Creating Sound using MediaStreamSource in Silverlight 3 Beta
Take a look at this. It looks like he has ported the Speex encoder to C# for the exact problem you are trying to solve. It is available here. Speex is designed for speech and should perform better than wma, mp3, or other audio codecs that are designed to handle music if you are just encoding speech, which I assume since you are grabbing from the mic.
This article http://alvas.net/alvas.audio,articles.aspx#how-to-save-audio-to-mp3-on-silverlight about save audio on client. To send audio data to a server you can use WebClient, for example.
You can do encoding thru the server, by send all stream to WCF service and do your encoding thru Microsoft Expression Encoding SDK API.
Please, see this url that i have asked before:
http://forums.silverlight.net/forums/t/181141.aspx
Regards

Categories