Search code examples
c#asp.net-mvc-4naudio

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


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);
}

Solution

  • 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