Esempio n. 1
0
        /// <summary>
        /// Save this Riff to waveform file.
        /// </summary>
        /// <param name="filePath">Target file.</param>
        /// <param name="riff">Riff data.</param>
        public static void SaveWaveFile(string filePath, Riff riff)
        {
            if (riff == null)
            {
                throw new ArgumentNullException("riff");
            }

            // Keep 4 bytes for rifftype, remove the other 8 bits riff head.
            riff.Size = riff.TotalSize - 8;

            FileStream filestream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            try
            {
                using (BinaryWriter bw = new BinaryWriter(filestream))
                {
                    filestream = null;
                    riff.SaveHead(bw);

                    if (riff.GetChunk(Riff.IdFormat) == null ||
                        riff.GetChunk(Riff.IdData) == null)
                    {
                        throw new InvalidDataException(
                            "Invalid chunks : Both Riff.IdFormat and Riff.IdData should be contained in chunks");
                    }

                    // Save IdFormat at first to keep consistence with original logical.
                    riff.GetChunk(Riff.IdFormat).Save(bw);

                    foreach (RiffChunk chunk in riff.Chunks)
                    {
                        if (chunk.Id != Riff.IdFormat)
                        {
                            riff.GetChunk(chunk.Id).Save(bw);
                        }
                    }
                }
            }
            finally
            {
                if (null != filestream)
                {
                    filestream.Dispose();
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Read waveform data from wave stream.
        /// </summary>
        /// <param name="stream">Wave stream.</param>
        /// <returns>Riff loaded.</returns>
        public static Riff ReadWave(Stream stream)
        {
            BinaryReader br = new BinaryReader(stream);
            Riff riff = new Riff();
            riff.LoadHead(br);

            while (br.BaseStream.Position != br.BaseStream.Length)
            {
                RiffChunk chunk = new RiffChunk();
                chunk.Load(br, Riff.IdUndefined);
                riff.Chunks.Add(chunk);

                if (br.BaseStream.Length - br.BaseStream.Position < 8)
                {
                    // Ignore chunk whose size is less than 8 bytes
                    break;
                }
            }

            return riff;
        }
Esempio n. 3
0
        /// <summary>
        /// Exactly do cut operation.
        /// </summary>
        /// <param name="samplePosition">Start sample position.</param>
        /// <param name="sampleNumber">Waveform sample length.</param>
        /// <returns>Riff of the cut piece.</returns>
        private Riff DoCut(int samplePosition, int sampleNumber)
        {
            if (this.Format.Channels != 1)
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "Does not support to cut part waveform in a multi-channel wave file instance.");
                throw new NotSupportedException(message);
            }

            Riff riff = new Riff();
            riff.RiffType = Riff.IdWave;

            RiffChunk fmt = this.Riff.GetChunk(Riff.IdFormat);
            riff.Chunks.Add(fmt);

            RiffChunk data = new RiffChunk();
            data.Id = Riff.IdData;

            int startbyte = samplePosition * this.Format.BlockAlign;
            int sizebyte = sampleNumber * this.Format.BlockAlign;

            data.SetData(new byte[sizebyte]);
            byte[] src = this.GetSoundData();
            int maxSizeToCopy = src.Length - startbyte;
            int sizeToCopy = sizebyte <= maxSizeToCopy ? sizebyte : maxSizeToCopy;
            Array.Copy(src, startbyte, data.GetData(), 0, sizeToCopy);
            riff.Chunks.Add(data);
            data.Size = sizebyte;

            riff.Size = fmt.TotalSize + data.TotalSize + 4;

            return riff;
        }
Esempio n. 4
0
 /// <summary>
 /// Initialize this instance.
 /// </summary>
 private void Initialze()
 {
     _riff = new Riff();
     RiffChunk dataChunk = new RiffChunk();
     dataChunk.Id = Riff.IdData;
     _riff.Chunks.Add(dataChunk);
 }
Esempio n. 5
0
        /// <summary>
        /// Read the sample count of a waveform file.
        /// </summary>
        /// <param name="filePath">Waveform file to read sample count.</param>
        /// <returns>Sample count of the waveform file. if -1 is returned.</returns>
        public static int ReadSampleCount(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            WaveFormat format;

            try
            {
                FileStream fs = new FileStream(filePath,
                    FileMode.Open, FileAccess.Read, FileShare.Read);
                try
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        fs = null;
                        Riff riff = new Riff();
                        riff.LoadHead(br);

                        RiffChunk fmt = new RiffChunk();
                        fmt.Load(br, Riff.IdFormat);

                        format = new WaveFormat();
                        format.Load(fmt.GetData());

                        int chunkId = 0;
                        int dataSize = 0;
                        do
                        {
                            chunkId = br.ReadInt32();
                            dataSize = br.ReadInt32();
                            if (dataSize < 0)
                            {
                                string message = string.Format(CultureInfo.InvariantCulture,
                                    "Invalid data size [{0}], which should not be negative integer.",
                                    dataSize);
                                throw new InvalidDataException(message);
                            }

                            long currPos = br.BaseStream.Position;
                            long newPos = br.BaseStream.Seek(dataSize, SeekOrigin.Current);
                            if (newPos != currPos + dataSize)
                            {
                                string message = string.Format(CultureInfo.InvariantCulture,
                                    "Invalid data size [{0}], which may be too large.",
                                    dataSize);
                                throw new InvalidDataException(message);
                            }
                        }
                        while (Riff.IdData != chunkId);

                        if (Riff.IdData != chunkId)
                        {
                            string message = string.Format(CultureInfo.InvariantCulture,
                                "Invalid waveform format for not data chunk found in file {0}",
                                filePath);
                            throw new InvalidDataException(message);
                        }

                        return dataSize / (format.BlockAlign * format.Channels);
                    }
                }
                finally
                {
                    if (null != fs)
                    {
                        fs.Dispose();
                    }
                }
            }
            catch (InvalidDataException ide)
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "Fail to read sample count of waveform file [{0}] for invalid data.",
                    filePath);
                throw new InvalidDataException(message, ide);
            }
            catch (EndOfStreamException ese)
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "Fail to read sample count of waveform file [{0}] for no enough data.",
                    filePath);
                throw new InvalidDataException(message, ese);
            }
        }
Esempio n. 6
0
        public static WaveFormat ReadFormat(string filePath)
        {
            WaveFormat format;

            try
            {
                using (FileStream fs =
                    new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (BinaryReader br = new BinaryReader(fs))
                {
                    Riff riff = new Riff();
                    riff.LoadHead(br);

                    RiffChunk fmt = new RiffChunk();
                    fmt.Load(br, Riff.IdFormat);

                    format = new WaveFormat();
                    format.Load(fmt.GetData());
                }
            }
            catch (InvalidDataException ide)
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "Fail to read format of waveform file [{0}] for invalid data.",
                    filePath);
                throw new InvalidDataException(message, ide);
            }

            return format;
        }