Combining two audio files in C# - c#

There are 2 audio ( mp3 or wav) files. The first file has the voice of me, the second one has the voice of my friend. Each of them has 10 seconds duration.
I want to combine them to get one file that also should have 10 seconds duration.
What are the tools or utilities I should use to solve this issue?
UPDATE: I used lame.exe, but it concatenated them and created the file of 20 seconds duration.

This might do the trick.
My WAVFile class supports 8- and 16-bit audio, mono or stereo. One of
its special features is a method that will mix WAV audio files
together, so that the audio from each source WAV file will be heard
simultaneously.
Note: In the above paragraph, My refers to the author of the class, not to myself.

Check out NAudio tutorial: Mixing multiple wave files together in real time.

Related

Convert big mp3 files to wav timing issue

I am working with some large mp3 sound files. Ranging from a few hours to 15+ hours.
I need to convert these mp3 files into wav.
Due to the size of the files it is not possible to load a whole file and then convert it.
My solution so far is using NAudio and NAudio.Lame
I have modified: Mp3FileReader and Mp3FrameDecompressor
to my needs. And this works, I can load in 100 mb chucks at a time and convert it.
Due to my implementation I can load a Mp3 fils where each frame in the same mp3 file varies slightly (1 frame is mono and another is stereo in the same file).
This is all good.
My problem is the timing between the original mp3 and the generated wav.
An example is a 9 hours 34 minutes and 38 seconds mp3 file, generated to a wav file is only 9 hours 21 minutes and 15 seconds long.
That is a lose of 13 minutes and 23 seconds, 3.7% shorter!. But I can not find anything which is lost in the audio. Everything seems to be there, it is just, shorter. Like the audio is played 3.7% faster than the mp3. It is a problem for me, because I need to be able to make a mapping between the wav and mp3 at a later date, and I would greatly prefer the mapping is 1 to 1.
I have tried to import the Mp3 file into Audacity with the same result, the imported audio in Audacity is again 13 minutes shorter than the original mp3 file.
This leads me to think the problem is not my code alone, is there an explanation to this behavior?
I have passed the MP3 files through mp3val and the problem appears to be, not NAudio and NAudio.Lame, but the MP3 file itself.
The MP3 file contain errors!
The fix is then to make sure the MP3 files does not contain errors.
MP3 files contains a sequence of MP3 frames.
when you have converted the MP3 to WAV, your wav player is reading the correct audio length.
Your original MP3 length was misinterpreted by your player
Some players don't read the MP3 audio length correctly which is generally due to trimmed frames

NAudio playing multiple sounds together gives exception "All mixer inputs must have the same WaveFormat"

I am trying to play multiple sound files at the same time using NAudio MixingSampleProvider's AddMixerInput method. I am using the AudioPlaybackEngine found at this page: https://gist.github.com/markheath/8783999
However, when I tried to play different files, I get an exception while playing some sounds: "All mixer inputs must have the same WaveFormat" It is probably because the sound files I have do not have the same sample rate (I see some files have 7046 as sample rate, instead of 44100).
Is there a way to modify the CachedSound class so that the sample rates are all converted to 44100 when reading the files?
Thanks!
You cant mix audio streams together without converting them to the same sample rate. In NAudio there are three ways of accessing a resampler:
WaveFormatConversionStream - this uses the ACM resampler component under the hood. Note that you may have issues resampling IEEE float with this one.
MediaFoundationResampler - this uses MediaFoundation. It allows you to adjust the resampling quality.
WdlResamplingSampleProvider - this is brand new in the latest NAudio, and offers fully managed resampling.
As well as matching sample rates, you also need matching channel counts. NAudio has various ways to turn mono into stereo and vice versa.

Rendering a wav file from wav files in c#

I got some short .wav files and after i get sequence how it should be played in time I need to "render" a one wav file which will contain that .wav files. I know I can append it but the problem is that some wav files should be played at one time, and also it need to be in rhythm (exact spaces).
How?
You will need to use some libraries like ffmpeg or SoX to do this.
Reference: http://ffmpeg.org, http://sox.sourceforge.net

Merge two mp3 audio files into one

I have two audio files that I need to merge on top of each other. Each mp3 is one side of a conversation, so in theory when I merge these two files together I should hear a complete conversation. Has anybody ever accomplished this in .Net?
I've seen examples of people concatenating audio files together, but I repeat, I don't want to do that. I want to merge/mix two audio tracks so the are on the same audio file.
Any help would be appreciated.
I am looking at the NAudio library. Actual code answers would be very helpful as well.
This should get you started with nAudio.
Convert the mp3's to wav32*
Mix the 2 wav (check this code)
Convert the mixed wav to mp3*
*the samples/discussion at nAudio shows how to convert between formats
I ended up using Sox to do the merge.
http://sox.sourceforge.net/
sox.exe -m fileone.mp3 filetwo.mp3 output.mp3
Sox doesn't have support for mp3s so you need to download this version which has those references compiled into the executable. Additionally you can search for the dll and the latest should pick them up.
http://www.codeproject.com/KB/aspnet/Compiling_SOX_with_Lame.aspx
Use a Process class to call this from .Net.

WAV file auto repeat in C#

I have a 10 second sound effect wave file. What I would like to do is take that file and repeat it n number of times and then save the longer WAV file to disk. This way I can create a much longer background effect rather than auto-repeat on the media player which is a bit stuttered between repeats. I am trying to do this in C#.
That's reasonably easy to do, given the WAV file format - assuming it's uncompressed audio (most WAV files are - or at least, they were last time I had anything to do with them).
There may well be audio APIs you can use to do this without getting stuck into the binary format, but I suspect they'd take as long to learn as just doing it yourself as you're not doing anything particularly complicated.
If you only need to do this with a small number of files, you might as well do it by hand with Audacity.
If you're using .Net 2.0 or higher then you can use the System.Media.SoundPlayer class and specifically its PlayLooping method to achieve what you want with no stuttering. This is preferable to creating a new wav file - it means less disk space for the file and less memory needed to render the sound. In general you should always use buffered techniques like this for audio playback if the sound will be looped or is longer than a few seconds.
You can do this easily in C# using the NAudio .NET audio library. You would need to use the WaveFileReader and WaveFileWriter classes. You can also use it to create a custom WaveStream that will loop your audio as many times as you want without the need to create a long WAV file.

Categories