Change Audio Format in C# - c#

I want to change an audio file format into another. Specifically, I want to convert any audio file into 6khz, 16 bit, mono, PCM wav format.
How can I cope with this problem.
Thanks again in advance.

You can also do this using the open source C# audio library NAudio. Have a look at the NAudioDemo project for an example of passing WAV files through the ACM codecs installed on your machine to convert to another format. If your input file is not WAV or MP3, you will first need something that converts it to WAV though.

I would use the BASS Library. It has many possibilities to do format conversions using the built in encoding/decoding capabilities. It also has a .NET wrapper availabe.

I'm not entirely sure whether you'll be able to do this as well as you may like.
To start with refer to the windows API for dealing with RIFF files (that's the file group for WAV files.)
You'll need to read the headers, extract the data and uncompress it to get the raw data format. I beleive that the header data will tell you what codec was used for compression.
You'll need to perform some processing on the raw data. Conversion to mono and 16bit may not be a problem, but I'm not too sure about changes to the sampling rate.
You can then recompress using your specified codec.

Related

What is the output format of microphone to save it in Wav

I'm recording audio from the microphone in unity and saving it as an AudioClip. The thing is I need to know in which format (either ogg or uncompressed) it is saved so I can convert it to Wav. I would also need help to convert it to wav.
You may try both cases with SoX library but it's hard to put it inside unity project
If your going to after saving your audio just convert to wav it won't matter much which one you pick. You could in theory lose some quality with the compressed version. You will want to use a program like Audacity to convert it to wav.

Converting WAV to M4A using c# NAudio

I want to convert a WAV file into a M4A file.
I could not find any reference or any example of how to achieve that.
Naudio should do the trick i assume, but i havent figured out how to do this yet.
I am writing a WebAPI2 project, and i need to return an m4a audio file to the user upon request. Been using a PushStreamContent in order to provide the user with the file, but using an MP3 as the conversion target, lead to many difficulties with browser support and Seek/Pause/Stop support.
Thanks in advance,
Nokky.
M4A files simply contain AAC encoded audio. Have a look at the Media Foundation Encoder demo in the NAudio WPF demo application to see an example of using MediaFoundationEncoder to create AAC files. It will let you encode as aac if you have an appropriate Media Foundation Codec on your machine (you should have with Win 7 and above, although note that it doesn't do low bitrates).

How to play non-PCM file or convert it to PCM on the fly ?

The following code works with some wav files, but with others I get, "InvalidOperationException was unhandled. Message=Sound API only supports playing PCM wave files."
var webClient = new WebClient();
webClient.DownloadFile(url, fileName);
var fileSound = new SoundPlayer(fileName);
fileSound.PlaySync();
Is there a way to programmatically check if a wav file is "bad" (not a PCM wave file) and then convert it as necessary?
What is odd is that the code works in the legacy Delphi app - all of the wav files play just fine. Here's the Delphi code:
filename := GetEnvironmentVariable('TEMP')+'\archieAndDingbat.wav';
URLDownloadToFile(nil, PChar(url), PChar(filename), 0, nil);
PlaySound(filename);
I looked at the properties of the two files in Explorer, and I see that there is, indeed, a difference. For the file that does play, its audio format is PCM; the one that won't play is CCITT u-Law.
So...I either need a way to convert from CCITT u-Law to PCM on the fly after downloading these files (they are download from an url and then played locally) OR perhaps a different way of playing these files than PlaySync() ...
Look at audiolab library from mitov. It works great
So, do you want to PLAY the file or CONVERT it ? What is the primary goal ? Do you play it as a prove you can convert it, or do you convert it because you don't know how to play not-converted file ?
http://www.catb.org/esr/faqs/smart-questions.html#goal
Your question's title claims "convert" but the body claims "Play"
This answer is about playing files.
You also may try to use FFDShow codecs directly without DirectX intermediate.
http://en.wikipedia.org/wiki/Libavcodec and http://libav.org/ and http://ffmpeg.org/ (they recently had a schism)
Googling for "FFDShow dotnet", "libav dotnet", "ffmpeg dotnet" shows a bunch of libraries to use it, such as
https://github.com/ermau/libav.net
Controlling ffdshow from .Net
Solid FFmpeg wrapper for C#/.NET
There is also BASS library. It is targeted as sound playback during gaming, so it probably has less range of formats and not much for re-coding. Still many music players are built on top of it. Some says it is the most simple API to use. So it worth considering. http://www.un4seen.com/
http://MediaInfo.sf.net is a library (native win32/win64 DLL) allowing to check most multimedia formats content.
I don't know if using tis C or C++ APis is easy from C# side.
The way to do it is to use newkie's code at: http://www.codeproject.com/Articles/175030/PlaySound-A-Better-Way-to-Play-Wav-Files-in-C?msg=4366037#xx4366037xx
In my case, at least, I had to change all of the lowercase x's to uppercase x's, though, to get it to work.

Convert Isolated Storage /RAW Format into WMV on Client side

Currently we're trying to create a project which has access via web cam to capture
the Video and Audio and store it on Cache.
We have been able to successfully capture the Video into the "Isolated Storage" or "Raw" File format. The next step is to create a silverlight friendly format like WMV version 8 or 9 which can be used for Replay.
Asof now we have been able to successfully get a project from Link
to capture the video and encode it into AVI format for replay mode. Unfortunately we've not been able to get a client version code snippet or API that allows encoding into WMV format directly.
Any pointers in the direction would be most appreciated.
TIA
For the moment there is no easy solution for encoding video/audio raw format from SL, and I think SL5 does not have that capabilities as well.
The only solution I found when I had that question too, was from StreamCoders's SilverSuite product, but it was too pricey for my needs. You might have a look there:
http://www.streamcoders.com/products/silversuite.html
Another solution would be to upload the raw data to a server and perform the encoding there, problem is the size of the raw data file are so big that it make it even not suitable.
I tried to zip the raw file, for a 45 second video/audio I had ~ 320 MB file size and ~ 210 after zipping. That was still way too high for my need.
So far I've not see much hope encoding raw file from the client side apart from the StreamCoders products.
Good luck
We have been developing the same application as a Pure Silverlight Browser solution and moved towards an out of browser solution where -
We capture the RAW Isolated Storage Format and convert it into AVI using AVIDLL available from the following urls -
Silverlight 4 More on Capturing Videos from Webcams
Silverlight 4 Yet More on Capturing Videos from Webcams
And later convert the AVI video (RAW->AVI) which is still large into a WMV file using FFMpeg.exe
Statistics on SIZE:
RAW atleast 700MB for a 1 min recording
AVI atleast 600MB after conversion from RAW
WMV atleast 500KB after conversion from AVI
Statistics on TIMELINE:
RAW to AVI conversion takes about 1min for the 700MB conversion
AVI to WMV conversion takes another 1min for the 600MB conversion
Comparing this with the RAW file upload to the server and converting it would be quite a time consuming process considering that the RAW file size is large.
Any optimizations which could help change the time taken to convert or perform a direct conversion to WMV could make the solution better.
Cheers !

Audio output from Silverlight

I'm looking to develop a Silverlight application which will take a stream of data (not an audio stream as such) from a web server.
The data stream would then be manipulated to give audio of a certain format (G.711 a-Law for example) which would then be converted into PCM so that additional effects can be applied (such as boosting the volume).
I'm OK up to this point. I've got my data, converted the G.711 into PCM but my problem is being able to output this PCM audio to the sound card.
I basing a solution on some C# code intended for a .Net application but in Silverlight there is a problem with trying to take a copy of a delegate (function pointer) which will be the topic of a separate question once I've produced a simple code sample.
So, the question is... How can I output the PCM audio that I have held in a data structure (currently an array) in my Silverlight to the user? (Please don't say write the byte values to a text box)
If it were a MP3 or WMA file I would play it using a MediaElement but I don't want to have to make it into a file as this would put a crimp on applying dynamic effects to the audio.
I've seen a few posts from people saying low level audio support is poor/non-existant in Silverlight so I'm open to any suggestions/ideas people may have.
The simple answer is that there is no support for PCM playback from Silverlight in version 2. So unless you want to write a fully managed PCM to MP3 converter you are stuck. Even then I'm not sure you could get the MediaElement to play from isolated storage.
Is there any chance you could use a web service to perform the conversion?
See also this question:
Where's the sound API in Silverlight? Or, how do I write a music app to run in the browser?
Update: Silverlight 3 supports your custom audio sources. However, it won't let you intercept samples to perform effects on WMA or MP3, presumably for DRM reasons, so you would still potentially need to write your own decoder.
Short answer is use a MediaElement + a MediaStreamSource
Check out these:
http://blogs.msdn.com/gillesk/archive/2009/03/23/playing-back-wave-files-in-silverlight.aspx
http://code.msdn.microsoft.com/wavmss/Release/ProjectReleases.aspx?ReleaseId=2417
Basically, write a decoder in managed code to convert G.711 a-Law to PCM, then do whatever modifications you want to the raw values, then pass those into a MediaStreamSource.
Looks like Silverlight 3 supports direct PCM output now, or will when released. I don't see anything in the docs about the raw AV pipeline yet.
Mark Heath's answer is correct - only certain formats are supported - mp3 and certain flavours of WMA (unfortunately not WMA lossless which would be 'closer' to PCM).
To play PCM data in Silverlight, you could do the following:
* Convert the PCM into mp3 data, and store it in memory.
* Play the mp3 data using the technique presented at ManagedMediaHelpers. The idea here involves a class called Mp3MediaStreamSource (derived from System.Windows.Media.MediaStreamSource) that provides mp3 chunks to a MediaElement to play. The chunks will need to be in a stream, but of course a memory stream will do.
I initially thought you might be able to provide PCM chunks via MediaStreamSource, but this does not work. It's a real shame as it would solve your problem (and the one I was facing - making a Speex audio file player) really easily!

Categories