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

Unable to load wave file from embedded resource...

Apr 19, 2015 at 7:20 PM
Hi!

I am trying to embed the wave files into the DLL by embedding them.

Everything works fine and I get the UnmanagedMemoryStream. But then it all goes bad.

I was trying to follow your example for fire and forget game sounds. This is version 137325 of my tries:
        public CachedSound(UnmanagedMemoryStream audioFileStream)
        {
            using (var audioFileReader = new WaveFileReader(audioFileStream))
            {
                WaveFormat = audioFileReader.WaveFormat;

                var wholeFile = new List<float>((int)(audioFileReader.Length / 4));
                var readBuffer = new byte[4];
                int samplesRead;

                while ((samplesRead = audioFileReader.Read(readBuffer, 0, readBuffer.Length)) > 0)
                {
                    wholeFile.Add(BitConverter.ToSingle(readBuffer, 0));
                }

                AudioData = wholeFile.ToArray();
            }
        }
The ISampleProvider uses float arrays, but the WaveFileReader can only read byte[]. So what I need is a function that can read an UnmanagedMemoryStream and put the correct sample data into a float[] for the ISampleProvider to read it and supply it to the mixer.

Again, this above is my latest version, which as you can see is already very desperate as it reads float by float. Thats because I tried to see if the problem is with endianess, but it isnt.

The float array I get out of this has lots of NaNs, and if I go the other endian its the same. So the data is corrupt the way I read it.

I also tried lots of other ways to read the data (including RawWaveStream), and the exact same wave files work perfectly fine if I use this below routine (from the blog) and place the file into the directory:
        public CachedSound(string audioFileName)
        {
            using (var audioFileReader = new AudioFileReader(audioFileName))
            {
                WaveFormat = audioFileReader.WaveFormat;
                var wholeFile = new List<float>((int)(audioFileReader.Length / 4));
                var readBuffer = new float[audioFileReader.WaveFormat.SampleRate * audioFileReader.WaveFormat.Channels];
                int samplesRead;

                while ((samplesRead = audioFileReader.Read(readBuffer, 0, readBuffer.Length)) > 0)
                {
                    wholeFile.AddRange(readBuffer.Take(samplesRead));
                }

                AudioData = wholeFile.ToArray();
            }
        }
So why can I read the file from the harddisk but not embed it into the DLL?

Any ideas welcome... ;)

Thx!
May 1, 2015 at 3:34 PM
Try WaveFileReader.toSampleProvider