Question

I am beginner c#. I would like to make program converting from .wav file to .raw file. I found some source and I would like to use it. but something happened in the code. the error is related to ObjectDisposedException.

could you give me a some code or idea for me? the whole code is under

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    namespace WaveTestRead
    {
       class WaveReader
       { 
    FileInfo m_fInfo;
    FileStream m_fStream;
    BinaryReader m_binReader;

    // RIFF chunk
    byte[] chunkID;
    UInt32 chunkSize;
    byte[] format;

    // fmt subchunk
    byte[] fmtChunkID;
    UInt32 fmtChunkSize;
    UInt16 audioFormat;
    UInt16 numChannels;
    UInt32 sampleRate;
    UInt32 byteRate;
    UInt16 blockAssign;
    UInt16 BitsPerSample;

    // data subchunk
    byte[] dataChunkID;
    UInt32 dataChunkSize;
    byte[] data8L;              // 8-bit left channel
    byte[] data8R;              // 8-bit right channel
    Int16[] data16L;           // 16-bit left channel
    Int16[] data16R;           // 16-bit right channel
    int numSamples;

    public WaveReader()
    {

    }

    public bool Open(String filename)
    {
        string str;
        m_fInfo = new FileInfo(filename);
        m_fStream = m_fInfo.OpenRead();
        m_binReader = new BinaryReader(m_fStream);

        chunkID = new byte[4];
        format = new byte[4];

        chunkID = m_binReader.ReadBytes(4);
        chunkSize = m_binReader.ReadUInt32();
        format = m_binReader.ReadBytes(4);

        str = System.Text.ASCIIEncoding.ASCII.GetString(chunkID, 0, 4);
        if (str != "RIFF")
            return false;

        str = System.Text.ASCIIEncoding.ASCII.GetString(format, 0, 4);
        if (str != "WAVE")
            return false;

        if (ReadFmt() == false)
            return false;
        if (ReadData() == false)
            return false;

        m_fStream.Close();

        return true;
    }

    private bool ReadFmt()
    {
        fmtChunkID = new byte[4];
        fmtChunkID = m_binReader.ReadBytes(4);

        string str = System.Text.ASCIIEncoding.ASCII.GetString(fmtChunkID, 0, 4);
        if (str != "fmt ")
            return false;

        fmtChunkSize = m_binReader.ReadUInt32();
        audioFormat = m_binReader.ReadUInt16();
        numChannels = m_binReader.ReadUInt16();
        sampleRate = m_binReader.ReadUInt32();
        byteRate = m_binReader.ReadUInt32();
        blockAssign = m_binReader.ReadUInt16();
        BitsPerSample = m_binReader.ReadUInt16();

        return true;
    }
   static void Main(string[] args)
    {
        p.Open("wavetest.wav");
        bool a = p.ReadFmt();
        p.ReadData();
    }
 }
 }
Was it helpful?

Solution 2

The problem likely exists in Main. The Open method closes the file at the end, and the ReadFmt call in Main reads from m_binReader. Either don't call ReadFmt from your main method after the Open call, or change Open to not close the file when it's done (and make the close explicit).

It looks like Open is doing all the work for you anyway (by calling ReadFmt and ReadData). You don't need to do it again in Main; just access the data from p.

OTHER TIPS

The code posted is not very well written.

Anyway you can try with these quick changes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WaveTestRead
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (var waveReader = new WaveReader())
            {
                if (!waveReader.Open("wavetest.wav"))
                {
                    Console.WriteLine("Failed to read file.");
                    return;
                }

                if (!waveReader.ReadFmt())
                {
                    Console.WriteLine("Failed to read fmt.");
                    return;
                }

                // this method is not defined...
                //waveReader.ReadData();
            }

        }
    }

    class WaveReader : IDisposable
    {
        FileInfo m_fInfo;
        FileStream m_fStream;
        BinaryReader m_binReader;

        // RIFF chunk
        byte[] chunkID;
        UInt32 chunkSize;
        byte[] format;

        // fmt subchunk
        byte[] fmtChunkID;
        UInt32 fmtChunkSize;
        UInt16 audioFormat;
        UInt16 numChannels;
        UInt32 sampleRate;
        UInt32 byteRate;
        UInt16 blockAssign;
        UInt16 BitsPerSample;

        // data subchunk
        byte[] dataChunkID;
        UInt32 dataChunkSize;
        byte[] data8L;              // 8-bit left channel
        byte[] data8R;              // 8-bit right channel
        Int16[] data16L;           // 16-bit left channel
        Int16[] data16R;           // 16-bit right channel
        int numSamples;

        public WaveReader()
        {

        }

        public bool Open(String filename)
        {
            string str;
            m_fInfo = new FileInfo(filename);
            m_fStream = m_fInfo.OpenRead();
            m_binReader = new BinaryReader(m_fStream);

            chunkID = new byte[4];
            format = new byte[4];

            chunkID = m_binReader.ReadBytes(4);
            chunkSize = m_binReader.ReadUInt32();
            format = m_binReader.ReadBytes(4);

            str = System.Text.ASCIIEncoding.ASCII.GetString(chunkID, 0, 4);
            if (str != "RIFF")
                return false;

            str = System.Text.ASCIIEncoding.ASCII.GetString(format, 0, 4);
            if (str != "WAVE")
                return false;

            //if (ReadFmt() == false)
            //    return false;
            //if (ReadData() == false)
            //    return false;

            return true;
        }

        public bool ReadFmt()
        {
            fmtChunkID = new byte[4];
            fmtChunkID = m_binReader.ReadBytes(4);

            string str = System.Text.ASCIIEncoding.ASCII.GetString(fmtChunkID, 0, 4);
            if (str != "fmt ")
                return false;

            fmtChunkSize = m_binReader.ReadUInt32();
            audioFormat = m_binReader.ReadUInt16();
            numChannels = m_binReader.ReadUInt16();
            sampleRate = m_binReader.ReadUInt32();
            byteRate = m_binReader.ReadUInt32();
            blockAssign = m_binReader.ReadUInt16();
            BitsPerSample = m_binReader.ReadUInt16();

            return true;
        }

        public void Dispose()
        {
            if (m_fStream != null)
                m_fStream.Dispose();
        }
    }
}

Basically I have created a class WaveReader with your code and removed the internal call to ReadFmt. Then in the Main method I have checked the return code and in case of false I write to the console.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top