Quantcast
Channel: NAudio
Viewing all 5831 articles
Browse latest View live

New Post: Converting M4A to Wav/PCM

$
0
0

MP3 is not the same as MP4 so the Mp3FileReader can't play it. The next NAudio includes MediaFoundationReader which will be able to decode M4A. There is no public release yet, but if you want to build the latest from Source Code (click the tab above) you can use it.

(by the way, there is no need for pcmStream or baStream above. For MP3s, just use mp3Reader)

Mark


New Post: How to get Preferred Format List for any audio capture device?

$
0
0

Thank you for your reply. I am going through the article that you have suggested.

But actually I was creating WasapiCapture instance for my audio capture device and I faced above issues. But I tried creating WaveIn instance, configuring it with the required WaveFormat (16 kbps, 44 kHz, 2 channels), capturing with this instance and is working fine. I am also able to record received audio samples using above mentioned codec.

Thank you so much for your help.

New Post: How to get Preferred Format List for any audio capture device?

$
0
0

yes, but you will still be recording in PCM. For WMA you'd need to encode afterwards

New Post: Converting M4A to Wav/PCM

$
0
0

Thanks I'll try

I thought MediaFoundationReader was only working on Windows 8 ? Does it works with Windows 7 ?

New Post: Converting M4A to Wav/PCM

$
0
0

No, it will work on Windows 7 too. The Media Foundation API was actually introduced with Windows Vista.

Created Issue: Conversion to Float is wrong [16380]

$
0
0
This code is from SimpleCompressorStream there is the followig code
left = BitConverter.ToInt16(buffer, start) / 32768.0;

that is wrong because max short is 32767 while min short is -32768

the resulting code will span ranges from[-1 to 0.99999999999999999]

Created Issue: Envelope Follower and other very usefull DSP class are private [16381]

$
0
0
I need to compute RMS and other attributes of audio data (I am not doing any playback) and i found a lot of very useful classes in NAudio are private classes.

Commented Issue: Conversion to Float is wrong [16380]

$
0
0
This code is from SimpleCompressorStream there is the followig code
left = BitConverter.ToInt16(buffer, start) / 32768.0;

that is wrong because max short is 32767 while min short is -32768

the resulting code will span ranges from[-1 to 0.99999999999999999]
Comments: Why do you think that is wrong? And what would you do instead? I don't want to divide by 32767 as then you have values that go over 1.0, which may get clipped unneccessarily depending on what goes on later in your pipeline. And I certainly don't want an input sample of 0 to convert to anything other than 0.0, or it will introduce some dc offset.

New Post: NAudio as audio analysis library

$
0
0

I am trying to build some audio analysis tool using naudio. I find it too much playback oriented and I am having quite a lot of troubles.

I also believe it should be refactored better so that all core classes (audio stream definition, providers) can be really portable and then have all other things (ASIO, WM and so on) as other nuget packages.

 

I do not understand why winforms controls are in the library itself. I find the source code confusing and a lot of dependencies that are not needed. Is htere any plan to move to something like most of the other nuget packaes are doing?

 

I would also like some help I have some very trivial code that should be computing the db peak and RMS of a track.

The results are really wrong.

 

I am using directly the iff file reader since i do not have access to the SampleCount property via the audiofilereader.

 

Any help .. pretty sure I am doing something wrong

 

void Compute() 
{var audioFile = @"C:\Users\colombod\Downloads\Trees V2.aif";double[] audioMono;double[] audioLeft;double[] audioRight;int window;using (var stream = new NAudio.Wave.AiffFileReader(audioFile))
            {
                Console.WriteLine("Channels {0} Rate{1} Bit{2}",stream.WaveFormat.Channels, stream.WaveFormat.SampleRate, stream.WaveFormat.BitsPerSample);var bytesPerSample = (stream.WaveFormat.BitsPerSample / 8);
                 window = (20 *stream.WaveFormat.SampleRate)/1000;

                Console.WriteLine("Buffer size required {0}",window);var bufferSize = (int)(window* bytesPerSample * stream.WaveFormat.Channels);var dstSize = stream.SampleCount;

                audioMono = newdouble[dstSize];
                audioLeft = newdouble[dstSize];
                audioRight = newdouble[dstSize];var buffer = newbyte[bufferSize];var dst = 0;var read = 0;while ( ( read = stream.Read(buffer, 0, bufferSize) )> 0)
                {var sampleCount = (read / bytesPerSample) / stream.WaveFormat.Channels;for (int i = 0; i < sampleCount; i++)
                    {var sum = 0.0;for (int j = 0; j < stream.WaveFormat.Channels; j++)
                        {var off = (j*bytesPerSample);var sSample = BitConverter.ToInt16(buffer, i + off);var sample = sSample / ( sSample >= 0 ? 32767.0 : 32768.0);if(j == 0)
                            {
                                audioLeft[i + dst] = sample;
                            }else
                            {
                                audioRight[i + dst] = sample;
                            }
                            sum += sample;
                        }
                        audioMono[i + dst] = sum / stream.WaveFormat.Channels;
                    }

                    dst += sampleCount;

                }

            }


            var rms = audioMono.Aggregate(0.0, (current, f) => current + (f*f));

            rms = rms / audioMono.Length;

            rms = Math.Sqrt(rms);
            var decibel = 20 * Math.Log10(rms);var peak =  20 * Math.Log10(audioMono.Max());var rmsl = audioLeft.Aggregate(0.0, (current, f) => current + (f * f));

            rmsl = rmsl / audioLeft.Length;

            rmsl = Math.Sqrt(rmsl);
            var decibell = 20 * Math.Log10(rmsl);var peakl = 20 * Math.Log10(audioLeft.Max());var rmsr = audioRight.Aggregate(0.0, (current, f) => current + (f * f));

            rmsr = rmsr / audioRight.Length;

            rmsr = Math.Sqrt(rmsr);
            var decibelr = 20 * Math.Log10(rmsr);var peakr = 20 * Math.Log10(audioRight.Max());


        }

Commented Issue: Conversion to Float is wrong [16380]

$
0
0
This code is from SimpleCompressorStream there is the followig code
left = BitConverter.ToInt16(buffer, start) / 32768.0;

that is wrong because max short is 32767 while min short is -32768

the resulting code will span ranges from[-1 to 0.99999999999999999]
Comments: usually i would have done things like this var sSample = BitConverter.ToInt16(buffer, i + off); var sample = sampleCount == 0 ? 0.0 : sSample / ( sSample > 0 ? 32767.0 : 32768.0); I think that is wrong since you won't be able to cover[-1,1] interval with your conversion, you are introducing a kind of limiting with your code. What do you think? I sent you a little request for help last night, have you seen my code snippet??

New Post: Converting M4A to Wav/PCM

$
0
0

Ok get it alpha2 ! Works fine with wav output.

What is best practice to output into a MemoryStream ? The WaveFileWriter.CreateWaveFile only takes a string (filename) à as parameter. I see code with loop/read but there might be a more straigh forward function ?

 

using (var reader = new MediaFoundationReader(file))using (var resampler = new MediaFoundationResampler(reader, 16000))using (var memoryStream = new MemoryStream()) {
  WaveFileWriter.CreateWaveFile(new MemoryStream(), resampler);
}

 

Commented Issue: Conversion to Float is wrong [16380]

$
0
0
This code is from SimpleCompressorStream there is the followig code
left = BitConverter.ToInt16(buffer, start) / 32768.0;

that is wrong because max short is 32767 while min short is -32768

the resulting code will span ranges from[-1 to 0.99999999999999999]
Comments: I would not want to do that as you are distorting the signal by amplifying the positive half by a different amount to the bottom half, and therefore introducing some additional frequencies into the sound.

New Post: NAudio as audio analysis library

$
0
0

It looks like your offsets are wrong. Samples are interleaved left, right etc. Try incrementing off by bytesPerSample every time you read a sample (off needs to be declared outside the top loop);

var sSample = BitConverter.ToInt16(buffer, off);
off+= bytesPerSample;

New Post: Convert 32 wav to 16 wav

$
0
0

Hello,

I use NAudio to capture the loopback sound from my computer and I receive 32 bit sound according to the WaveFormat. Is there a way I can convert the received bytes from 32 bit to 16 bit? Or maybe to make WasapiLoopbackCapture generate 16 bit sound?

Here is what I tried:

privatevoid btnStart_Click(object sender, EventArgs e)
{
    _waveIn = new WasapiLoopbackCapture();

    _waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(_waveIn_DataAvailable);

    _waveIn.StartRecording();
}

void _waveIn_DataAvailable(object sender, WaveInEventArgs e)
{

    byte[] newArray16Bit = newbyte[e.BytesRecorded / 2];
    short two;
    int value;
    for (int i = 0, j = 0; i < e.BytesRecorded; i = i + 4, j = j + 2)
    {
        value = (BitConverter.ToInt32(e.Buffer, i));
        two = (short)(value % short.MaxValue);

        newArray16Bit[j] = (byte)(two & 0xFF);
        newArray16Bit[j + 1] = (byte)((two >> 8) & 0xFF);
    }

    //do something with the newArray16Bit 
}

I receive a sound which is similar with what my computer is actually playing but it sounds like a am listening to a radio station with a very poor signal (the sound is not clean - you can nearly understand what the sound is about).

Does anybody know how can I clean up the sound?

Thanks!

New Post: Convert 32 wav to 16 wav

$
0
0

I just changed the type of the 'two' variable from int to float and calculated it by multiplying the 'value' with short.MaxValue.

So I replace:

two = (short)(value % short.MaxValue);

with:

two = (short)(value * short.MaxValue);
Now the noise is gone but the entire sound is lower than it should be. Any ideas?

 

Thanks!


New Post: Convert 32 wav to 16 wav

$
0
0

yes, it is likely that your 32 bit incoming data is float not int. Multiplying by short.MaxValue is correct. It may just be that your recorded signal level is low.

New Post: AcmNotPossible calling acmStreamOpen

$
0
0

Hello community,

With the following piece of code:

         LoopStream inputStream;
            if (fileName.EndsWith(".mp3" ))
            {
                var mp3Reader = new Mp3FileReader(fileName);
                inputStream = new LoopStream(mp3Reader);
            }

 A friend of mine is receiving the error " AcmNotPossible calling acmStreamOpen"

 The stack trace looks something like this:

   at NAudio.Wave.AcmMp3FrameDecompressor..ctor(WaveFormat sourceFormat)   at NAudio.Wave.Mp3FileReader.CreateAcmFrameDecompressor(WaveFormat mp3Format)   at NAudio.Wave.Mp3FileReader..ctor(Stream inputStream, FrameDecompressorBuilder frameDecompressorBuilder)   at NAudio.Wave.Mp3FileReader..ctor(Stream inputStream)   at NAudio.Wave.Mp3FileReader..ctor(String mp3FileName)   at Dream.World.AssetManager.CreateInputStream(String fileName)

Does anyone have any ideas? 

Commented Issue: Playing Short Audio Files [16377]

$
0
0
I'm writing a game and I'd like to use NAudio to add sound to it. I followed a few examples and was rather pleased with NAudio's API and performance.

I ran into the following issue: NAudio will not play short audio files. I've used WaveChannel32.Length to get the following numbers.
Files with length 755712 or less will not play.
Files with length 5935104 do play.
I'm not sure where the exact limit is. There are no exceptions thrown. I was hoping to use NAudio to play files of about 1 second long. Is anyone else having this issue? Is there a fix? Do I need to find a different library?
Comments: Here's the NAudio code I'm calling. Initializing: WaveMixerStream32 mixer = new WaveMixerStream32(); mixer.AutoStop = false; IWavePlayer waveOutDevice = new WaveOut(); waveOutDevice.Init(mixer); waveOutDevice.Play(); WaveChannel32 sample = CreateInputStream(filename); Loading functions: (From tutorial, really :b) private static WaveChannel32 CreateInputStream(string fileName) { if (!File.Exists(fileName)) { throw new InvalidOperationException("Cannot find audio file: " + fileName); } WaveChannel32 inputStream; if (fileName.EndsWith(".mp3")) { WaveStream mp3Reader = new Mp3FileReader(fileName); inputStream = new WaveChannel32(mp3Reader); } else if (fileName.EndsWith(".wav")) { WaveStream mp3Reader = new WaveFileReader(fileName); inputStream = new WaveChannel32(mp3Reader); } else { throw new InvalidOperationException("Unsupported extension"); } return inputStream; } Play sound: mixer.AddInputStream(sample); Stop sound: mixer.RemoveInputStream(sample); I don't have an idea how to make an IWaveProvider that adds trailing silence. But instead I've added trailing silence in my audio files and tried out stuff that way. I found some rather curious things. If I add 30 seconds of trailing silence to a 1 second effect sound, it won't play it. If I add 10 seconds of silence before the 1 second effect and 30 seconds after, it won't play it. If I put three 1 second effects in sequence, separated by 5 seconds and add 30 seconds after the last one, I will hear only the last 2 effects in the sequence. It seems like silence and short sounds aren't enough to be taken serious by the library. It feels like the first effect in the sequence is just lost trying to convince the system that there's actual sound in the file. Does anyone have a good idea? I can work around this issue by making a single sound file of all my effects and adding the first effect twice. It's ugly, but it should work.

New Post: AcmNotPossible calling acmStreamOpen

$
0
0

It means you don't have an ACM codec installed that can decompress MP3. What operating system are you running on?

Commented Issue: Playing Short Audio Files [16377]

$
0
0
I'm writing a game and I'd like to use NAudio to add sound to it. I followed a few examples and was rather pleased with NAudio's API and performance.

I ran into the following issue: NAudio will not play short audio files. I've used WaveChannel32.Length to get the following numbers.
Files with length 755712 or less will not play.
Files with length 5935104 do play.
I'm not sure where the exact limit is. There are no exceptions thrown. I was hoping to use NAudio to play files of about 1 second long. Is anyone else having this issue? Is there a fix? Do I need to find a different library?
Comments: there should be no problem with short sounds. I've made a drum machine example in the WPF demo that plays loads of short sounds. I can't see a problem with your code although WaveMixerStream32 is an ancient class that really I'd like to take out of NAudio - it was written for one very specific application a long time ago. If you can, I'd recommend using a similar approach to the WPF demo app. Use AudioFileReader to get your samples as an ISampleProvider, and the MixingSampleProvider to mix them together.
Viewing all 5831 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>