Ejemplo n.º 1
0
        public static RiffWaveChunk FromPcmAudio(PcmAudio pcmAudio)
        {
            IEnumerable <double>[] blocks = pcmAudio.Blocks.ToArray();

            RiffFormatChunk formatChunk = new RiffFormatChunk((ushort)pcmAudio.Channels.Count(), (ushort)(blocks.Length / pcmAudio.Length), 16);

            byte[] data     = new byte[formatChunk.BlockSize * blocks.Length];
            int    position = 0;

            for (int blockIndex = 0; blockIndex < blocks.Length; blockIndex++)
            {
                double[] samples = blocks[blockIndex].ToArray();

                for (int sampleIndex = 0; sampleIndex < samples.Length; sampleIndex++)
                {
                    if (samples[sampleIndex] < -1 || samples[sampleIndex] >= +1)
                    {
                        throw new InvalidDataException(string.Format("Sample at index {0} was out of range ({1}).", sampleIndex, samples[sampleIndex]));
                    }

                    foreach (byte part in BitConverter.GetBytes((short)(samples[sampleIndex] * 0x8000)))
                    {
                        data[position++] = part;
                    }
                }
            }

            RiffDataChunk dataChunk = new RiffDataChunk(data);

            return(new RiffWaveChunk(formatChunk, dataChunk));
        }
Ejemplo n.º 2
0
        public RiffWaveChunk(RiffFormatChunk formatChunk, RiffDataChunk dataChunk)
            : base("RIFF", 4 + 8 + formatChunk.Size + 8 + dataChunk.Size + dataChunk.Size % 2)
        {
            if (formatChunk == null)
            {
                throw new ArgumentNullException("formatChunk");
            }
            if (dataChunk == null)
            {
                throw new ArgumentNullException("dataChunk");
            }

            this.riffID      = "WAVE";
            this.formatChunk = formatChunk;
            this.dataChunk   = dataChunk;
        }
Ejemplo n.º 3
0
        public RiffWaveChunk(BinaryReader reader)
            : base(reader)
        {
            if (ID != "RIFF")
            {
                throw new InvalidDataException(string.Format("Wrong chunk ID '{0}', should be 'RIFF'.", ID));
            }

            this.riffID = Encoding.ASCII.GetString(reader.ReadBytes(4));

            if (riffID != "WAVE")
            {
                throw new InvalidDataException(string.Format("Wrong RIFF ID '{0}', should be 'WAVE'.", riffID));
            }

            this.formatChunk = new RiffFormatChunk(reader);
            this.dataChunk   = new RiffDataChunk(reader);

            if (Size != 4 + 8 + formatChunk.Size + 8 + dataChunk.Size + dataChunk.Size % 2)
            {
                throw new InvalidDataException(string.Format("Incorrect chunk size '{0}', should be '{1}'.", Size, 4 + 8 + formatChunk.Size + 8 + dataChunk.Size + dataChunk.Size % 2));
            }
        }