Example #1
0
        public static SamplerChunk readStream(BinaryReader br)
        {
            var ns = new SamplerChunk()
            {
                manufacturer     = br.ReadInt32(),
                product          = br.ReadInt32(),
                period           = br.ReadInt32(),
                unityNote        = br.ReadInt32(),
                pitchFracton     = br.ReadInt32(),
                smtpeFormat      = br.ReadInt32(),
                smtpeOffset      = br.ReadInt32(),
                sampleLoopsCount = br.ReadInt32(),
                samplerData      = br.ReadInt32()
            };

            ns.loops = new SampleLoop[ns.sampleLoopsCount];
            for (int i = 0; i < ns.sampleLoopsCount; i++)
            {
                ns.loops[i] = SampleLoop.readStream(br);
            }
            return(ns);
        }
Example #2
0
        public static PCM16WAV readStream(BinaryReader br)
        {
            // Check for RIFF magic
            if (br.ReadUInt32() != 0x46464952u)
            {
                return(null);
            }
            int riff_chunck_size = br.ReadInt32();

            // Check for WAVE format
            if (br.ReadUInt32() != 0x45564157u)
            {
                return(null);
            }
            // Find + load format chunk
            var wavHAnch = br.BaseStream.Position;
            var fmtOfs   = findChunk(br, FORMAT); // Locate format chunk

            if (fmtOfs < 0)
            {
                return(null); // format chunk wasn't found. Abort.
            }
            br.BaseStream.Position = fmtOfs;
            // Format chunk (see variable names)
            var NewWave = new PCM16WAV()
            {
                chunkSize     = br.ReadInt32(),
                format        = br.ReadInt16(),
                channels      = br.ReadInt16(),
                sampleRate    = br.ReadInt32(),
                byteRate      = br.ReadInt32(),
                blockAlign    = br.ReadInt16(),
                bitsPerSample = br.ReadInt16()
            };

            // Find + load PCM16 buffer chunk
            br.BaseStream.Position = wavHAnch; // Seek back to section anchor (first section magic)
            var datOfs = findChunk(br, DATA);  // locate data chunk

            if (datOfs < 0)                    // no PCM buffer chunk, abort.
            {
                return(null);
            }
            var datSize = br.ReadInt32();                                            // section size.

            NewWave.sampleCount = datSize / NewWave.blockAlign;                      // calculate sample count (data length / block alignment)
            NewWave.buffer      = new short[NewWave.sampleCount * NewWave.channels]; // initialize PCM buffer array
            for (int i = 0; i < NewWave.sampleCount * NewWave.channels; i++)
            {
                NewWave.buffer[i] = br.ReadInt16(); // sprawl out samples into array
            }
            br.BaseStream.Position = wavHAnch;      // Seek back to section anchor (first section magic)
            // Load cue points (optional)
            var cueOfs = findChunk(br, CUE);        // Locate "cue " chunk

            if (cueOfs > 0)
            {
                var cueLength = br.ReadInt32();                // Reading just to align. (Section size)
                var cueCount  = br.ReadInt32();
                NewWave.cuePoints = new WAVCuePoint[cueCount]; // initialize array with length
                for (int i = 0; i < cueCount; i++)
                {
                    NewWave.cuePoints[i] = WAVCuePoint.readStream(br); // read individual points into array
                }
            }
            br.BaseStream.Position = wavHAnch;
            var smplOfs = findChunk(br, SMPL); //

            if (smplOfs > 0)
            {
                var smplSize = br.ReadInt32();
                NewWave.sampler = SamplerChunk.readStream(br);
            }
            return(NewWave);
        }