Esempio n. 1
0
        private void readPaktChunk(BinaryReader source)
        {
            long nbPackets = StreamUtils.DecodeBEInt64(source.ReadBytes(8));
            long nbFrames  = StreamUtils.DecodeBEInt64(source.ReadBytes(8));

            //duration = nbFrames * channelsPerFrame * bitsPerChannel / 8;
            duration = nbFrames * 1000d / sampleRate;
        }
        public void StreamUtils_LongConverters()
        {
            ulong longValue = 0x45976248;

            byte[] byteValue = BitConverter.GetBytes(longValue);
            Assert.AreEqual(longValue, StreamUtils.DecodeUInt64(byteValue));

            byteValue = StreamUtils.EncodeBEUInt64(longValue);
            Assert.AreEqual((long)longValue, StreamUtils.DecodeBEInt64(byteValue));
        }
Esempio n. 3
0
        // WARNING : EXPERIMENTAL / UNTESTED DUE TO THE LACK OF METADATA-RICH SAMPLE FILES
        private void readStringsChunk(BinaryReader source)
        {
            uint nbEntries = StreamUtils.DecodeBEUInt32(source.ReadBytes(4));

            Dictionary <uint, long> stringIds = new Dictionary <uint, long>();

            for (int i = 0; i < nbEntries; i++)
            {
                uint stringId     = StreamUtils.DecodeBEUInt32(source.ReadBytes(4));
                long stringOffset = StreamUtils.DecodeBEInt64(source.ReadBytes(8));
                stringIds.Add(stringId, stringOffset);
            }
            long initialPos = source.BaseStream.Position;

            string stringValue;

            foreach (uint id in stringIds.Keys)
            {
                source.BaseStream.Seek(initialPos + stringIds[id], SeekOrigin.Begin);
                stringValue = StreamUtils.ReadNullTerminatedString(source, Encoding.UTF8);
                SetMetaField("str-" + id, stringValue, true);
            }
        }
Esempio n. 4
0
        protected override bool read(BinaryReader source, ReadTagParams readTagParams)
        {
            ResetData();
            source.BaseStream.Seek(0, SeekOrigin.Begin);

            bool result         = readFileHeader(source);
            long cursorPos      = source.BaseStream.Position;
            long audioChunkSize = 0;

            // Iterate through chunks
            while (cursorPos < source.BaseStream.Length)
            {
                string chunkType = Utils.Latin1Encoding.GetString(source.ReadBytes(4));
                long   chunkSize = StreamUtils.DecodeBEInt64(source.ReadBytes(8));

                if (readTagParams.PrepareForWriting)
                {
                    structureHelper.AddZone(cursorPos, chunkSize + 12, chunkType);
                }

                switch (chunkType)
                {
                case CHUNK_AUDIO_DESC:
                    readAudioDescriptionChunk(source);
                    break;

                case CHUNK_CHANNEL_LAYOUT:
                    readChannelLayoutChunk(source);
                    break;

                case CHUNK_COOKIE:
                case CHUNK_UMID:
                    if (readTagParams.PrepareForWriting || readTagParams.ReadAllMetaFrames)
                    {
                        readStringChunk(source, chunkType, chunkSize);
                    }
                    break;

                case CHUNK_STRINGS:
                    if (readTagParams.PrepareForWriting || readTagParams.ReadAllMetaFrames)
                    {
                        readStringsChunk(source);
                    }
                    break;

                case CHUNK_INFO:
                    readInfoChunk(source, readTagParams.PrepareForWriting || readTagParams.ReadAllMetaFrames);
                    break;

                case CHUNK_AUDIO:
                    AudioDataOffset = cursorPos;
                    audioChunkSize  = chunkSize;
                    AudioDataSize   = chunkSize + 12;
                    if (secondsPerByte > 0)
                    {
                        duration = chunkSize * secondsPerByte * 1000;
                    }
                    break;

                case CHUNK_PACKET_TABLE:
                    if (0 == secondsPerByte)
                    {
                        readPaktChunk(source);
                    }
                    break;
                }
                source.BaseStream.Seek(cursorPos + chunkSize + 12, SeekOrigin.Begin);
                cursorPos = source.BaseStream.Position;
            }
            bitrate = audioChunkSize * 8d / duration;
            if (null == channelsArrangement)
            {
                if (1 == channelsPerFrame)
                {
                    channelsArrangement = MONO;
                }
                else if (2 == channelsPerFrame)
                {
                    channelsArrangement = STEREO;
                }
                else
                {
                    channelsArrangement = new ChannelsArrangement((int)channelsPerFrame, "Custom layout (" + channelsPerFrame + " channels)");
                }
            }

            return(result);
        }
        public void StreamUtils_Exceptions()
        {
            Assert.IsFalse(StreamUtils.ArrEqualsArr(new byte[1], new byte[2]));
            Assert.IsFalse(StreamUtils.StringEqualsArr(".", new char[2]));

            try
            {
                StreamUtils.DecodeBEUInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeUInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEInt16(new byte[1]);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeBEInt24(new byte[2]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEUInt24(new byte[2]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.EncodeBEUInt24(0x01FFFFFF);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeBEUInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeUInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeInt32(new byte[3]);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeUInt64(new byte[7]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeBEInt64(new byte[7]);
                Assert.Fail();
            }
            catch { }


            try
            {
                StreamUtils.DecodeSynchSafeInt(new byte[6]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.DecodeSynchSafeInt32(new byte[6]);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.EncodeSynchSafeInt(1, 0);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.EncodeSynchSafeInt(1, 6);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.ReadBits(new BinaryReader(new MemoryStream()), 0, 0);
                Assert.Fail();
            }
            catch { }

            try
            {
                StreamUtils.ReadBits(new BinaryReader(new MemoryStream()), 0, 33);
                Assert.Fail();
            }
            catch { }
        }