FindChunk() public method

public FindChunk ( string name, int startFromChunk ) : int
name string
startFromChunk int
return int
コード例 #1
0
        // GetWavinfo
        private static wavinfo_t GetWavInfo(string name, byte[] wav)
        {
            wavinfo_t info = new wavinfo_t();

            if (wav == null)
            {
                return(info);
            }

            // debug
            //using (FileStream fs = new FileStream(Path.GetFileName(name), FileMode.Create, FileAccess.Write, FileShare.Read))
            //{
            //    fs.Write(wav, 0, wav.Length);
            //}
            WavHelper helper = new WavHelper(wav);

            int offset = 0;

            // find "RIFF" chunk
            int riff = helper.FindChunk("RIFF", offset);

            if (riff == -1)
            {
                Con.Print("Missing RIFF chunk\n");
                return(info);
            }

            string wave = Encoding.ASCII.GetString(wav, offset + 8, 4);

            if (wave != "WAVE")
            {
                Con.Print("RIFF chunk is not WAVE\n");
                return(info);
            }

            // get "fmt " chunk
            offset += 12; //iff_data = data_p + 12;

            int fmt = helper.FindChunk("fmt ", offset);

            if (fmt == -1)
            {
                Con.Print("Missing fmt chunk\n");
                return(info);
            }

            int format = helper.GetLittleShort(fmt + 8);

            if (format != 1)
            {
                Con.Print("Microsoft PCM format only\n");
                return(info);
            }

            info.channels = helper.GetLittleShort(fmt + 10);
            info.rate     = helper.GetLittleLong(fmt + 12);
            info.width    = helper.GetLittleShort(fmt + 16 + 4 + 2) / 8;

            // get cue chunk
            int cue = helper.FindChunk("cue ", offset);

            if (cue != -1)
            {
                info.loopstart = helper.GetLittleLong(cue + 32);

                // if the next chunk is a LIST chunk, look for a cue length marker
                int list = helper.FindChunk("LIST", cue);
                if (list != -1)
                {
                    string mark = Encoding.ASCII.GetString(wav, list + 28, 4);
                    if (mark == "mark")
                    {
                        // this is not a proper parse, but it works with cooledit...
                        int i = helper.GetLittleLong(list + 24);   // samples in loop
                        info.samples = info.loopstart + i;
                    }
                }
            }
            else
            {
                info.loopstart = -1;
            }

            // find data chunk
            int data = helper.FindChunk("data", offset);

            if (data == -1)
            {
                Con.Print("Missing data chunk\n");
                return(info);
            }

            int samples = helper.GetLittleLong(data + 4) / info.width;

            if (info.samples > 0)
            {
                if (samples < info.samples)
                {
                    Sys.Error("Sound {0} has a bad loop length", name);
                }
            }
            else
            {
                info.samples = samples;
            }

            info.dataofs = data + 8;

            return(info);
        }
コード例 #2
0
ファイル: SoundMem.cs プロジェクト: Memorix101/SharpQuake
        // GetWavinfo
        static wavinfo_t GetWavInfo(string name, byte[] wav)
        {
            wavinfo_t info = new wavinfo_t();

            if (wav == null)
                return info;

            // debug
            //using (FileStream fs = new FileStream(Path.GetFileName(name), FileMode.Create, FileAccess.Write, FileShare.Read))
            //{
            //    fs.Write(wav, 0, wav.Length);
            //}
            WavHelper helper = new WavHelper(wav);

            int offset = 0;

            // find "RIFF" chunk
            int riff = helper.FindChunk("RIFF", offset);
            if (riff == -1)
            {
                Con.Print("Missing RIFF chunk\n");
                return info;
            }

            string wave = Encoding.ASCII.GetString(wav, offset + 8, 4);
            if (wave != "WAVE")
            {
                Con.Print("RIFF chunk is not WAVE\n");
                return info;
            }

            // get "fmt " chunk
            offset += 12; //iff_data = data_p + 12;

            int fmt = helper.FindChunk("fmt ", offset);
            if (fmt == -1)
            {
                Con.Print("Missing fmt chunk\n");
                return info;
            }

            int format = helper.GetLittleShort(fmt + 8);
            if (format != 1)
            {
                Con.Print("Microsoft PCM format only\n");
                return info;
            }

            info.channels = helper.GetLittleShort(fmt + 10);
            info.rate = helper.GetLittleLong(fmt + 12);
            info.width = helper.GetLittleShort(fmt + 16 + 4 + 2) / 8;

            // get cue chunk
            int cue = helper.FindChunk("cue ", offset);
            if (cue != -1)
            {
                info.loopstart = helper.GetLittleLong(cue + 32);

                // if the next chunk is a LIST chunk, look for a cue length marker
                int list = helper.FindChunk("LIST", cue);
                if (list != -1)
                {
                    string mark = Encoding.ASCII.GetString(wav, list + 28, 4);
                    if (mark == "mark")
                    {
                        // this is not a proper parse, but it works with cooledit...
                        int i = helper.GetLittleLong(list + 24); // samples in loop
                        info.samples = info.loopstart + i;
                    }
                }
            }
            else
                info.loopstart = -1;

            // find data chunk
            int data = helper.FindChunk("data", offset);
            if (data == -1)
            {
                Con.Print("Missing data chunk\n");
                return info;
            }

            int samples = helper.GetLittleLong(data + 4) / info.width;
            if (info.samples > 0)
            {
                if (samples < info.samples)
                    Sys.Error("Sound {0} has a bad loop length", name);
            }
            else
                info.samples = samples;

            info.dataofs = data + 8;

            return info;
        }