This project has moved. For the latest updates, please go here.

Format audio PCM 32 Bits

Oct 19, 2012 at 6:46 PM

Hi,

I think the library NAudio does not support 32-bit PCM audio, so here is my modest contribution by adding a class Pcm32BitsToSampleProvider and a small change in the class SampleProviderConverters.
Sorry for the quality of my english;-)

 

/* Complement Format PCM
 * 
 * ajout dans SampleProviderConverters.cs
 * 
 * dans ConvertWaveProviderIntoSampleProvider
 * 
 * else if (waveProvider.WaveFormat.BitsPerSample == 32)
 * {
 *      sampleProvider = new Pcm32BitToSampleProvider(waveProvider);
 * }
 * 
 */
namespace NAudio.Wave.SampleProviders
{
    /// <summary>
    /// Converts an IWaveProvider containing 32 bit PCM to an
    /// ISampleProvider
    /// </summary>
    public class Pcm32BitToSampleProvider : SampleProviderConverterBase
    {
        /// <summary>
        /// Initialises a new instance of Pcm32BitToSampleProvider
        /// </summary>
        /// <param name="source">Source Wave Provider</param>
        public Pcm32BitToSampleProvider(IWaveProvider source)
            : base(source)
        {

        }

        /// <summary>
        /// Reads floating point samples from this sample provider
        /// </summary>
        /// <param name="buffer">sample buffer</param>
        /// <param name="offset">offset within sample buffer to write to</param>
        /// <param name="count">number of samples required</param>
        /// <returns>number of samples provided</returns>
        public override int Read(float[] buffer, int offset, int count)
        {
            int sourceBytesRequired = count * 4;
            EnsureSourceBuffer(sourceBytesRequired);
            int bytesRead = source.Read(sourceBuffer, 0, sourceBytesRequired);
            int outIndex = offset;
            for (int n = 0; n < bytesRead; n += 4)
            {
                buffer[outIndex++] = (((sbyte)sourceBuffer[n+3] << 24 |
					      sourceBuffer[n + 2] << 16) |
					      (sourceBuffer[n + 1] << 8) |
					      sourceBuffer[n]) / 2147483648f;
            }
            return bytesRead / 4;
        }
    }
}

And modify SampleProviderConverters

 

...

}
else if (waveProvider.WaveFormat.BitsPerSample == 32)
{
	sampleProvider = new Pcm32BitToSampleProvider(waveProvider);
}
else
{
          throw new InvalidOperationException("Unsupported operation");
}

...

Best Regards, Manu.N

Coordinator
Oct 21, 2012 at 4:11 PM

thanks, this is checked in