Beispiel #1
0
        public RIFFDataChunk ReadDataChunk()
        {
            RIFFDataChunk data = new RIFFDataChunk();

            data.chunkID = FOURCC.data;
            data.dwChunkSize = fReader.ReadUInt32();
            //ReadUInt32 is the most important function here.

            //Once we've read in the ChunkSize, 
            //we're at the start of the actual data.
            data.lFilePosition = fReader.BaseStream.Position;

            //If the fact chunk exists, we don't have to calculate 
            //the number of samples ourselves.
            if (null != fFact)
                data.dwNumSamples = fFact.dwNumSamples;
            else
                data.dwNumSamples = data.dwChunkSize /
                  (fFormat.dwBitsPerSample / 8 * fFormat.wChannels);
            //The above could be written as data.dwChunkSize / format.wBlockAlign, 
            //but I want to emphasize
            //what the frames look like.

            data.dwMinLength = (data.dwChunkSize / fFormat.dwAvgBytesPerSec) / 60;
            data.dSecLength = ((double)data.dwChunkSize /
                              (double)fFormat.dwAvgBytesPerSec) -
                              (double)data.dwMinLength * 60;

            if (fFormat.dwBitsPerSample == 8)
                data.byteArray = fReader.ReadBytes((int)data.dwNumSamples);

            return data;
        }
Beispiel #2
0
        public WaveFile(string filename)
        {
            fFileName = filename;
            RIFFStreamReader fReader = new RIFFStreamReader(filename);
            fBinaryReader = fReader.BinaryReader;
            fHeader = fReader.ReadHeader();

            // Read the chunks from the file
            uint chunkName = 0;
            while (fReader.Position < (long) fHeader.dwFileLength)
            {
                chunkName = fReader.ReadFourCC();

                switch (chunkName)
                {
                    case FOURCC.fmt:
                        fFormat = fReader.ReadFormatChunk();
                        if (fReader.Position + fFormat.dwChunkSize == fHeader.dwFileLength)
                            return;
                    break;

                    case FOURCC.fact:

                        fFact = fReader.ReadFactChunk();
                        if (fReader.Position + fFact.dwChunkSize == fHeader.dwFileLength)
                            return;
                    break;

                    case FOURCC.data:
                        fData = fReader.ReadDataChunk();
                        if (fReader.Position + fData.dwChunkSize == fHeader.dwFileLength)
                            return;
                        break;

                    default:
                        // Skip over unsupported chunks.
                        fReader.AdvanceToNext();
                        break;
                }
            }
        }