0

I'm using this WaveReader class in my code. I'm getting this error:

ERROR: Ensure that samples are integers (e.g. not floating-point numbers)

if (format.wFormatTag != 1) // 1 = PCM 2 = Float
        throw new ApplicationException("Format tag " + format.wFormatTag + " is not supported!");

All I'm trying to is convert WAV file to FLAC so I can feed it to GoogleSpeechAPI. I can do the first step, record WAV files. I am stuck on the second step: convert WAV file to FLAC. I can do the 3rd step: convert FLAC to text using GoogleSpeech API.

For the second step, where I'm getting stuck, here is my code:

public void WAV_to_FLAC_converter()
    {
        string inputFile = "inputFile.wav";
        //string outputFile = Path.Combine("flac", Path.ChangeExtension(input, ".flac"));
        string outputFile = "outputFile.flac";

        if (!File.Exists(inputFile))
            throw new ApplicationException("Input file " + inputFile + " cannot be found!");
        var stream = File.OpenRead(@"C:\inputFile.wav");
        WavReader wav = new WavReader(stream);

        using (var flacStream = File.Create(outputFile))
        {
            FlacWriter flac = new FlacWriter(flacStream, wav.BitDepth, wav.Channels, wav.SampleRate);
            // Buffer for 1 second's worth of audio data
            byte[] buffer = new byte[wav.Bitrate / 8];
            int bytesRead;//**I GET THE ABOVE ERROR HERE.**
            do
            {
                bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length);
                flac.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);
            flac.Dispose();
            flac = null;
        }
    }

Apparently there is something wrong with the input wav file I am giving the function. I think it says the stream variable that I created is in float-point instead of integer. But what am I supposed to do? I didn't mess with the WAV file. It's just a WAV file. How can I change a WAV file from float-point to Integer?? I don't know how to fix this.

Awesome_girl
  • 484
  • 3
  • 9
  • 30

1 Answers1

3

I've tested your code with a random wave file and it worked perfectly. Then I downloaded a stereo 32-bit float data wave sample from here and I got the same error as you:

ERROR: Ensure that samples are integers (e.g. not floating-point numbers)

Then I debugged the code and following exception was thrown

// Ensure that samples are 16 or 24-bit
if (format.wBitsPerSample != 16 && format.wBitsPerSample != 24)
    throw new ApplicationException(format.wBitsPerSample + " bits per sample is not supported by FLAC!");

I'm afraid the WavReader class simply does not support 32 bit float wave samples, nor does the FlacWriter.

UPDATE: I got your project working now. You have to rename your libFlac.dll to LibFlac.dll in your debug folder. There should be no more problems loading the library. What I got then was a PInvokeStackImabalance exception. If you get it too, you could follow the instructions from the post here or simple turn throwing of this type of exception off under Debug->Exceptions->Managed Debugging Assistans->PInvokeStackImbalance.

Community
  • 1
  • 1
stefankmitph
  • 3,236
  • 2
  • 18
  • 22
  • I renamed the DLL file under bin/Debug to LibFlac.dll. I still get DLLNotFoundException unhandeled. I made sure that 'PInvokeStackImbalance' was set to 'thrown', but i don't get that exception – Awesome_girl Mar 05 '15 at 15:12
  • in your main method implement `Console.WriteLine(Environment.CurrentDirectory);`... in this directory copy the dll (and rename it) – stefankmitph Mar 05 '15 at 15:18
  • yupp that's was the same directory I added LibFlac.dll. I downloaded another LibFlac_dynamic.dll from the web. i opened this new libFlac_dynamic.dll in dependency walker, it has no errors. but the previous LibFlac.dll I was using had errors in dependancyWalker. so I renamed LibFlac_dynamic.dll to LibFlac.dll and put it in current directory and ran. I get a new error: BadImageFormatException was unhandeled. An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) – Awesome_girl Mar 05 '15 at 15:33
  • Thanks Stefan! that error is gone, I tried both 32bit version dll and 64 bit version dll. I did that before but didn't work. Because you told me to rename it, it worked. oddly though the 32 bit version worked instead of 64 even though my laptop is 64-bit.. ^_^ yay. moving on to new error! – Awesome_girl Mar 05 '15 at 15:42
  • as i pointed out somewhere above: make sure throwing of exception `PInvokeStackImbalance` is DISABLED. – stefankmitph Mar 05 '15 at 16:02
  • 1
    OMG!! IT'S FIXED! You are terribly awesome, thanks for sticking with me through this pain staking journey! – Awesome_girl Mar 05 '15 at 16:06
  • I cannot vote it up, sadly, my reputation in StackOverFlow is 1. I'm even banned from answering questions... – Awesome_girl Mar 05 '15 at 16:19
  • there's a check with my answer (below the up- and downvote buttons). by checking it, you accept my answer and the check button will be green (http://meta.stackexchange.com/questions/106319/in-stack-overflow-how-do-i-put-the-big-green-check-to-the-answer-which-i-like-th) – stefankmitph Mar 05 '15 at 16:21