Example #1
0
        public IEnumerable <double> OutputDouble()
        {
            switch (BitsPerSample)
            {
            case 8:
                return(Output8BitsDouble());

            case 16:
                return(Output16BitsDouble());

            default:
                Sanity.BeBetter(true, $"Unexpected bits per sample {BitsPerSample}.");
                return(new List <double>());
            }
        }
Example #2
0
        private void ParseChunk(bool isDeep, int offset)
        {
            if (offset + 8 < bytes.Length)
            {
                Sanity.Requires(!isDeep, $"Audio length mismatch in 0x{offset.ToString("x")}.");
                return;
            }
            string name         = Encoding.ASCII.GetString(bytes, offset, 4);
            int    length       = BitConverter.ToInt32(bytes, offset + 4);
            Chunk  currentChunk = new Chunk()
            {
                Name = name, Length = length, Offset = offset
            };

            switch (name)
            {
            case fmt_:
                ChunkFmt_ = currentChunk;
                break;

            case data:
                ChunkData = currentChunk;
                break;

            case list:
                ChunkList = currentChunk;
                break;

            case fact:
                ChunkFact = currentChunk;
                break;

            default:
                Chunks.Add(currentChunk);
                break;
            }
            if (offset + 8 + length == bytes.Length)
            {
                return;
            }
            if (offset + 8 + length > bytes.Length)
            {
                Sanity.Requires(!isDeep, $"Audio length mismatch in {name}.");
                return;
            }
            ParseChunk(isDeep, offset + 8 + length);
        }
Example #3
0
        private void PostProcess()
        {
            Sanity.Requires(!string.IsNullOrWhiteSpace(ChunkFmt_.Name), "Missing format chunk.");
            Sanity.Requires(!string.IsNullOrWhiteSpace(ChunkData.Name), "Missing data chunk.");

            int offset = ChunkFmt_.Offset + 8;

            AudioTypeId = BitConverter.ToInt16(bytes, offset);
            Sanity.Requires(AudioTypeDict.ContainsKey(AudioTypeId), "Unsupported audio type.");
            AudioTypeString = AudioTypeDict[AudioTypeId];
            Channel         = BitConverter.ToInt16(bytes, offset + 2);
            SampleRate      = BitConverter.ToInt32(bytes, offset + 4);
            ByteRate        = BitConverter.ToInt32(bytes, offset + 8);
            BlockAlign      = BitConverter.ToInt16(bytes, offset + 12);
            BitsPerSample   = BitConverter.ToInt16(bytes, offset + 14);
            AudioLength     = 1.0 * ChunkData.Length / ByteRate;
        }
Example #4
0
        private void ParseRiff(byte[] bytes, bool isDeep)
        {
            this.bytes = bytes;
            Sanity.Requires(bytes.Length >= 44, "File is less than 44 bytes.");
            Sanity.Requires(RIFF == Encoding.ASCII.GetString(bytes, 0, 4), $"File is not a {RIFF} file.");
            int length = BitConverter.ToInt32(bytes, 4);

            if (isDeep)
            {
                Sanity.Requires(bytes.Length == length + 8, "Audio length mismatch in RIFF");
            }
            Sanity.Requires(WAVE == Encoding.ASCII.GetString(bytes, 8, 4));
            ChunkRiff = new Chunk()
            {
                Name = "RIFF", Length = length, Offset = 0
            };
            ParseChunk(isDeep, 12);
        }