“Must be already floating point” when converting an audio file into wav file

我的梦境 提交于 2019-12-08 00:13:41

问题


Here I have a code for covering a audio file into wav format for better quality and reducing file size. Here I am using naudio file compression source code and I got an exception when I try to convert that file.

Must be already floating point

public string ConvertToWAV(string tempFilePath, string tempFileName, string audioType)
{
    //Try to transform the file, if it fails use the original file            
    FileInfo fileInfo = new FileInfo(tempFilePath + tempFileName);
    byte[] fileData = new byte[fileInfo.Length];
    fileData = File.ReadAllBytes(tempFilePath + tempFileName);
    ISampleProvider sampleProvider;
    try
    {
        if (audioType.ToLower().Contains("wav"))
        {
            try
            {
                using (MemoryStream wav = new MemoryStream(fileData))
                {
                    WaveStream stream = new WaveFileReader(wav);

                    WaveFormat target = new WaveFormat();

                    var s = new RawSourceWaveStream(new MemoryStream(), new WaveFormat(8000, 16, 1));
                    var c = new WaveFormatConversionStream(WaveFormat.CreateALawFormat(8000, 1), s);

                    sampleProvider = new WaveToSampleProvider(c);

                    WaveFileWriter.CreateWaveFile16(tempFilePath + tempFileName, sampleProvider);

                    wav.Close();
                }
            }
            catch (Exception ex)
            {
                //We couldn't convert the file, continue with the original file.                        
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return Convert.ToBase64String(fileData);
}

回答1:


There are a couple problems with the code and the concept in general.

First, you're ignoring the WaveFormat of the input file. I'm guessing that you're assuming it's 8K, 16-bit, 1 channel based on the line where you create var s, but this is not a guarantee.

Second, you don't need a MemoryStream or RawSourceWaveStream. WaveFileReader is a WaveStream, and is suitable for any "next-stage" NAudio wave processor.

Third (and this is most likely your exception): The NAudio Wave processors and converters don't like A-Law (or u-Law) in the WaveFormat. A-Law (and u-Law) is technically not PCM data. As such, they're not the "wave" data that NAudio likes to play with.

Ok, with all that said, here are some suggestions. There are very particular A-Law and u-Law encoders in the NAudio.Codecs namespace. Oddly enough, they're named ALawEncoder and MuLawEncoder. These things are not stream-compatible, so we want to make them compatible.

I've added a class at the end here that does just that: Creates an IWaveProvider that actually spits out a stream A-Law or u-Law. Here's the test code that makes use of the new class. The test code does the following:

  1. Reads an input file using MediaFoundationReader (I like this one)
  2. Converts whatever the input format is into 16-bit PCM (while keeping the channel count) using MediaFoundationResampler. Note that this means that your input file does not have to have same format as the A-law output, so it'll convert pretty much anything.
  3. Feeds that new 16-bit PCM stream to the custom "ALaw-to-IWaveProvider" converter class.
  4. Writes the IWaveProvider-compatible A-Law output to a wave file.

I use the MediaFoundation classes here because they don't seem to be as particular about wave formats as the ACM-based ones.

    static void ConversionTest( string _outfilename, string _infilename )
    {
        try
        {
            using( var reader = new MediaFoundationReader(_infilename) )
            {
                // Create a wave format for 16-bit pcm at 8000 samples per second.
                int channels = reader.WaveFormat.Channels;
                int rate = 8000;
                int rawsize = 2;
                int blockalign = rawsize * channels; // this is the size of one sample.
                int bytespersecond = rate * blockalign;
                var midformat =
                    WaveFormat.CreateCustomFormat( WaveFormatEncoding.Pcm,
                                                   rate,
                                                   channels,
                                                   bytespersecond,
                                                   blockalign,
                                                   rawsize * 8 );

                // And a conversion stream to turn input into 16-bit PCM.
                var midstream = new MediaFoundationResampler(reader, midformat);
                //var midstream = new WaveFormatConversionStream(midformat, reader);

                // The output stream is our custom stream.
                var outstream = new PcmToALawConversionStream(midstream);


                WaveFileWriter.CreateWaveFile(_outfilename, outstream);
            }
        }
        catch( Exception _ex )
        {
        }
    }

And here's the class that converts 16-bit PCM into A-Law or u-Law. At the end are specializations for A-Law or u-Law:

    /// <summary>
    /// Encodes 16-bit PCM input into A- or u-Law, presenting the output
    /// as an IWaveProvider.
    /// </summary>
    public class PcmToG711ConversionStream : IWaveProvider
    {
        /// <summary>Gets the local a-law or u-law format.</summary>
        public WaveFormat WaveFormat { get { return waveFormat; } }

        /// <summary>Returns <paramref name="count"/> encoded bytes.</summary>
        /// <remarks>
        /// Note that <paramref name="count"/> is raw bytes.  It doesn't consider
        /// channel counts, etc.
        /// </remarks>
        /// <param name="buffer">The output buffer.</param>
        /// <param name="offset">The starting position in the output buffer.</param>
        /// <param name="count">The number of bytes to read.</param>
        /// <returns>The total number of bytes encoded into <paramref name="buffer"/>.</returns>
        public int Read(byte[] buffer, int offset, int count)
        {
            // We'll need a source buffer, twice the size of 'count'.
            int shortcount = count*2;
            byte [] rawsource = new byte [shortcount];
            int sourcecount = Provider.Read(rawsource, 0, shortcount);
            int bytecount = sourcecount / 2;
            for( int index = 0; index < bytecount; ++index )
            {
                short source = BitConverter.ToInt16(rawsource, index*2);
                buffer[offset+index] = Encode(source);
            }
            return bytecount;
        }


        /// <summary>
        /// Initializes and A-Law or u-Law "WaveStream".  The source stream
        /// must be 16-bit PCM!
        /// </summary>
        /// <param name="_encoding">ALaw or MuLaw only.</param>
        /// <param name="_sourcestream">The input PCM stream.</param>
        public PcmToG711ConversionStream( WaveFormatEncoding _encoding,
                                          IWaveProvider _provider )
        {
            Provider = _provider;
            WaveFormat sourceformat = Provider.WaveFormat;
            if( (sourceformat.Encoding != WaveFormatEncoding.Pcm) &&
                (sourceformat.BitsPerSample != 16) )
            {
                throw new NotSupportedException("Input must be 16-bit PCM.  Try using a conversion stream.");
            }

            if( _encoding == WaveFormatEncoding.ALaw )
            {
                Encode = this.EncodeALaw;
                waveFormat = WaveFormat.CreateALawFormat( _provider.WaveFormat.SampleRate,
                                                          _provider.WaveFormat.Channels) ;

            }
            else if( _encoding == WaveFormatEncoding.MuLaw )
            {
                Encode = this.EncodeMuLaw;
                waveFormat = WaveFormat.CreateMuLawFormat( _provider.WaveFormat.SampleRate,
                                                           _provider.WaveFormat.Channels) ;
            }
            else
            {
                throw new NotSupportedException("Encoding must be A-Law or u-Law");
            }
        }


        /// <summary>The a-law or u-law encoder delegate.</summary>
        EncodeHandler Encode;
        /// <summary>a-law or u-law wave format.</summary>
        WaveFormat waveFormat;
        /// <summary>The input stream.</summary>
        IWaveProvider Provider;

        /// <summary>A-Law or u-Law encoder delegate.</summary>
        /// <param name="_sample">The 16-bit PCM sample to encode.</param>
        /// <returns>The encoded value.</returns>
        delegate byte EncodeHandler( short _sample );

        byte EncodeALaw( short _sample )
        {
            return ALawEncoder.LinearToALawSample(_sample);
        }
        byte EncodeMuLaw( short _sample )
        {
            return MuLawEncoder.LinearToMuLawSample(_sample);
        }
    }


    public class PcmToALawConversionStream : PcmToG711ConversionStream
    {
        public PcmToALawConversionStream( IWaveProvider _provider )
          : base(WaveFormatEncoding.ALaw, _provider)
        {
        }
    }

    public class PcmToMuLawConversionStream : PcmToG711ConversionStream
    {
        public PcmToMuLawConversionStream( IWaveProvider _provider )
          : base(WaveFormatEncoding.MuLaw, _provider)
        {
        }
    }
}



回答2:


at last i got a solution for this issue ie, need to add kind of additional feature named Media Foundation for the better working in Windows Server 2012.

Use the Add Roles and Features wizard from the Server Manager. Skip through to Features and select Media Foundation



来源:https://stackoverflow.com/questions/36934672/must-be-already-floating-point-when-converting-an-audio-file-into-wav-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!