public static void StreamThroughWaveFile(string sFileName, IReadWaveData objCallback)
 {
     using (FileStream fs = new FileStream(sFileName, FileMode.Open, FileAccess.Read))
         using (BufferedStream bs = new BufferedStream(fs, nBufferSize))
             using (BinaryReader r = new BinaryReader(bs))
             {
                 StreamThroughWaveFile(r, objCallback);
             }
 }
 public static void StreamThroughWaveFile(string sFileName,IReadWaveData objCallback)
 {
     using (FileStream fs = new FileStream(sFileName,FileMode.Open,FileAccess.Read))
     using (BufferedStream bs = new BufferedStream(fs, nBufferSize))
     using (BinaryReader r = new BinaryReader(bs))
     {
         StreamThroughWaveFile(r, objCallback);
     }
 }
    public static void StreamThroughWaveFile(BinaryReader r, IReadWaveData objCallback)
    {
        bool bRaw = objCallback.PreferRawSample();
        CWavStreamReadHeaders winfo = CWavStreamReadHeaders.ReadWavStreamReadHeaders(r);

        if (winfo.nAudioFormat != 1)
        {
            throw new WavCutException("Only audio format 1 is supported");
        }
        if (winfo.nSampleRate != 44100)
        {
            throw new WavCutException("expect samplerate==44100");
        }
        if (winfo.nBitsPerSample != 16)
        {
            throw new WavCutException("expect nBitsPerSample==16");
        }
        if (winfo.nChannels != 2)
        {
            throw new WavCutException("expect nChannels==2");
        }

        uint nDataSize = 0;

        while (true)
        {
            // Go through chunks. We are looking for "data" chunk
            string sDatatag = new string(r.ReadChars(4));
            nDataSize = r.ReadUInt32();
            if (nDataSize > int.MaxValue)
            {
                throw new WavCutException("File too large.");
            }

            if (sDatatag == "data") // found the data section
            {
                break;
            }
            else // something else, continue looping
            {
                r.BaseStream.Seek(nDataSize, SeekOrigin.Current);
            }
        }

        uint      nLengthOfDataChunk = (uint)(nDataSize / (winfo.nChannels * (winfo.nBitsPerSample / 8)));
        int       nSample            = 0;
        const int nReadsize          = 1024;
        bool      bComplete          = false;

        // loop through wave until we reach the expected number of samples.
        // we should not read until eof because there might be a later riff chunk.
        while (!bComplete)
        {
            byte[] rawdata = r.ReadBytes((int)nReadsize);
            if (rawdata == null || rawdata.Length == 0)
            {
                break;
            }

            for (int j = 0; j < rawdata.Length; j += 4)
            {
                if (bRaw)
                {
                    objCallback.ReceiveRawSample(rawdata[j], rawdata[j + 1], rawdata[j + 2], rawdata[j + 3]);
                }
                else
                {
                    short  sha1 = rawdata[j + 0]; // intel byte order
                    short  sha2 = (short)(((short)rawdata[j + 1]) << 8);
                    short  sha  = (short)(sha1 + sha2);
                    double sa   = sha / ((double)short.MaxValue);
                    short  shb1 = rawdata[j + 2]; // intel byte order
                    short  shb2 = (short)(((short)rawdata[j + 3]) << 8);
                    short  shb  = (short)(sha1 + sha2);
                    double sb   = shb / ((double)short.MaxValue);
                    if (sa != sb)
                    {
                        throw new WavCutException("different here");
                    }

                    objCallback.ReceiveProcessedSample(sa, sb);
                }

                nSample++;
                if (nSample >= nLengthOfDataChunk)
                {
                    bComplete = true;
                    break;
                }
            }
        }
        if (nSample != nLengthOfDataChunk)
        {
            Console.WriteLine("warning: eof reached earlier than expected. Expected " + nLengthOfDataChunk + " got " + nSample);
        }

        objCallback.OnFinished();
    }
    public static void StreamThroughWaveFile(BinaryReader r,IReadWaveData objCallback)
    {
        bool bRaw = objCallback.PreferRawSample();
        CWavStreamReadHeaders winfo = CWavStreamReadHeaders.ReadWavStreamReadHeaders(r);

        if (winfo.nAudioFormat != 1)
            throw new WavCutException("Only audio format 1 is supported");
        if (winfo.nSampleRate != 44100) throw new WavCutException("expect samplerate==44100");
        if (winfo.nBitsPerSample != 16) throw new WavCutException("expect nBitsPerSample==16");
        if (winfo.nChannels != 2) throw new WavCutException("expect nChannels==2");

        uint nDataSize = 0;
        while (true)
        {
            // Go through chunks. We are looking for "data" chunk
            string sDatatag = new string(r.ReadChars(4));
            nDataSize = r.ReadUInt32();
            if (nDataSize > int.MaxValue)
                throw new WavCutException("File too large.");

            if (sDatatag == "data") // found the data section
            {
                break;
            }
            else // something else, continue looping
            {
                r.BaseStream.Seek(nDataSize,SeekOrigin.Current);
            }
        }

        uint nLengthOfDataChunk = (uint)(nDataSize / (winfo.nChannels*(winfo.nBitsPerSample/8)));
        int nSample = 0;
        const int nReadsize = 1024;
        bool bComplete = false;

        // loop through wave until we reach the expected number of samples.
        // we should not read until eof because there might be a later riff chunk.
        while (!bComplete)
        {
            byte[] rawdata = r.ReadBytes((int)nReadsize);
            if (rawdata == null || rawdata.Length == 0)
                break;

            for (int j = 0; j < rawdata.Length; j += 4)
            {
                if (bRaw)
                {
                    objCallback.ReceiveRawSample(rawdata[j],rawdata[j + 1],rawdata[j + 2],rawdata[j + 3]);
                }
                else
                {
                    short sha1 = rawdata[j + 0]; // intel byte order
                    short sha2 = (short)(((short)rawdata[j + 1]) << 8);
                    short sha = (short)(sha1 + sha2);
                    double sa = sha / ((double)short.MaxValue);
                    short shb1 = rawdata[j + 2]; // intel byte order
                    short shb2 = (short)(((short)rawdata[j + 3]) << 8);
                    short shb = (short)(sha1 + sha2);
                    double sb = shb / ((double)short.MaxValue);
                    if (sa != sb)
                        throw new WavCutException("different here");

                    objCallback.ReceiveProcessedSample(sa,sb);
                }

                nSample++;
                if (nSample >= nLengthOfDataChunk)
                {
                    bComplete = true;
                    break;
                }
            }
        }
        if (nSample != nLengthOfDataChunk)
            Console.WriteLine("warning: eof reached earlier than expected. Expected "+nLengthOfDataChunk+" got "+nSample);

        objCallback.OnFinished();
    }