Example #1
0
        /// <summary>
        /// Write the structure and data to the .wav file.
        /// </summary>
        /// <param name="stream">The stream to write the data to.</param>
        /// <param name="waveHeaderStructure">The complete structure and sound data of the .wav file.</param>
        /// <returns>True if the write was succesfull; else false.</returns>
        public bool Write(System.IO.Stream stream, WaveStructure waveHeaderStructure)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            BinaryWriter binaryWriter = null;

            try
            {
                // Create a new binary reader from the file stream
                // set the starting position at the begining
                binaryWriter = new BinaryWriter(stream);
                binaryWriter.BaseStream.Seek(0, SeekOrigin.Begin);

                // Write each pice of binary data to the file.
                binaryWriter.Write(waveHeaderStructure.ChunckID);
                binaryWriter.Write(waveHeaderStructure.FileSize);
                binaryWriter.Write(waveHeaderStructure.RiffType);
                binaryWriter.Write(waveHeaderStructure.FmtID);
                binaryWriter.Write(waveHeaderStructure.FmtSize);
                binaryWriter.Write(waveHeaderStructure.FmtCode);
                binaryWriter.Write(waveHeaderStructure.Channels);
                binaryWriter.Write(waveHeaderStructure.SampleRate);
                binaryWriter.Write(waveHeaderStructure.FmtAverageByteRate);
                binaryWriter.Write(waveHeaderStructure.FmtBlockAlign);
                binaryWriter.Write(waveHeaderStructure.BitsPerSample);

                // If the format size is not '16' then write additional
                // data to the wave file.
                if (waveHeaderStructure.FmtSize != 16)
                {
                    binaryWriter.Write(waveHeaderStructure.FmtExtraSize);
                    binaryWriter.Write(waveHeaderStructure.FmtExtraData);
                }

                // Write the 'data' indicator and the actual data size value.
                binaryWriter.Write(waveHeaderStructure.DataID);
                binaryWriter.Write(waveHeaderStructure.DataSize);

                // From the data size value write the actual sound data.
                binaryWriter.Write(waveHeaderStructure.SoundData);

                // Close the streams
                binaryWriter.Flush();

                // The write operation completed.
                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// Read the structure and data of the .wav file.
        /// </summary>
        /// <param name="stream">The stream to read the data from.</param>
        /// <returns>The complete structure and sound data of the .wav file.</returns>
        public WaveStructure Read(System.IO.Stream stream)
        {
            WaveStructure wave         = new WaveStructure();
            BinaryReader  binaryReader = null;

            try
            {
                // Create a new binary reader from the file stream
                // set the starting position at the begining
                binaryReader = new BinaryReader(stream);
                binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);

                // Read each pice of binary data from the file.
                wave.ChunckID           = binaryReader.ReadInt32();
                wave.FileSize           = binaryReader.ReadInt32();
                wave.RiffType           = binaryReader.ReadInt32();
                wave.FmtID              = binaryReader.ReadInt32();
                wave.FmtSize            = binaryReader.ReadInt32();
                wave.FmtCode            = binaryReader.ReadInt16();
                wave.Channels           = binaryReader.ReadInt16();
                wave.SampleRate         = binaryReader.ReadInt32();
                wave.FmtAverageByteRate = binaryReader.ReadInt32();
                wave.FmtBlockAlign      = binaryReader.ReadInt16();
                wave.BitsPerSample      = binaryReader.ReadInt16();

                // If the format size is not '16' then read additional
                // data from the wave file.
                if (wave.FmtSize != 16)
                {
                    // Read any extra values
                    wave.FmtExtraSize = binaryReader.ReadInt16();
                    wave.FmtExtraData = binaryReader.ReadBytes(wave.FmtExtraSize);
                }

                // Read the 'data' indicator and the actual data size value.
                wave.DataID   = binaryReader.ReadInt32();
                wave.DataSize = binaryReader.ReadInt32();

                // From the data size value read the actual sound data.
                wave.SoundData = binaryReader.ReadBytes(wave.DataSize);

                // Return the wave structure and data.
                return(wave);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// Creates the default mono values of the wave structure.
        /// </summary>
        /// <param name="sampleRate">Sample rate typical values are 44100, 11025 etc.</param>
        /// <param name="soundData">The actual sound data the raw PCM data.</param>
        /// <returns>The wave structure for the current data.</returns>
        public static WaveStructure CreateDefaultMonoStructure(Int32 sampleRate, byte[] soundData)
        {
            WaveStructure wave = new WaveStructure();

            // Set all the default values.
            wave.ChunckID           = 1179011410;
            wave.FileSize           = (soundData.Length + 36);
            wave.RiffType           = 1163280727;
            wave.FmtID              = 544501094;
            wave.FmtSize            = 16;
            wave.FmtCode            = 1;
            wave.BitsPerSample      = 16;
            wave.Channels           = 1;
            wave.SampleRate         = sampleRate;
            wave.FmtAverageByteRate = (Int32)((sampleRate * 1) * (wave.BitsPerSample / 8));
            wave.FmtBlockAlign      = (Int16)(1 * (wave.BitsPerSample / 8));
            wave.DataID             = 1635017060;
            wave.DataSize           = soundData.Length;
            wave.SoundData          = soundData;

            // return the wave structure.
            return(wave);
        }
Example #4
0
        /// <summary>
        /// Read the structure and data of the .wav file.
        /// </summary>
        /// <param name="filePath">The full path and file name of the .wav file.</param>
        /// <returns>The complete structure and sound data of the .wav file.</returns>
        public WaveStructure Read(string filePath)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            WaveStructure wave         = new WaveStructure();
            FileStream    fileStream   = null;
            BinaryReader  binaryReader = null;

            try
            {
                // Create a new file stream for the file.
                fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

                // Create a new binary reader from the file stream
                // set the starting position at the begining
                binaryReader = new BinaryReader(fileStream);
                binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);

                // Read each pice of binary data from the file.
                wave.ChunckID           = binaryReader.ReadInt32();
                wave.FileSize           = binaryReader.ReadInt32();
                wave.RiffType           = binaryReader.ReadInt32();
                wave.FmtID              = binaryReader.ReadInt32();
                wave.FmtSize            = binaryReader.ReadInt32();
                wave.FmtCode            = binaryReader.ReadInt16();
                wave.Channels           = binaryReader.ReadInt16();
                wave.SampleRate         = binaryReader.ReadInt32();
                wave.FmtAverageByteRate = binaryReader.ReadInt32();
                wave.FmtBlockAlign      = binaryReader.ReadInt16();
                wave.BitsPerSample      = binaryReader.ReadInt16();

                // If the format size is not '16' then read additional
                // data from the wave file.
                if (wave.FmtSize != 16)
                {
                    // Read any extra values
                    wave.FmtExtraSize = binaryReader.ReadInt16();
                    wave.FmtExtraData = binaryReader.ReadBytes(wave.FmtExtraSize);
                }

                // Read the 'data' indicator and the actual data size value.
                wave.DataID   = binaryReader.ReadInt32();
                wave.DataSize = binaryReader.ReadInt32();

                // From the data size value read the actual sound data.
                wave.SoundData = binaryReader.ReadBytes(wave.DataSize);

                // Close the streams
                binaryReader.Close();
                fileStream.Close();

                // Return the wave structure and data.
                return(wave);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (binaryReader != null)
                {
                    binaryReader.Close();
                }

                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
        }
Example #5
0
        /// <summary>
        /// Write the structure and data to the .wav file.
        /// </summary>
        /// <param name="filePath">The full path and file name of the .wav file.</param>
        /// <param name="waveHeaderStructure">The complete structure and sound data of the .wav file.</param>
        /// <returns>True if the write was succesfull; else false.</returns>
        public bool Write(string filePath, WaveStructure waveHeaderStructure)
        {
            if (String.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            FileStream   fileStream   = null;
            BinaryWriter binaryWriter = null;

            try
            {
                // Create a new file stream for the file.
                fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Write);

                // Create a new binary reader from the file stream
                // set the starting position at the begining
                binaryWriter = new BinaryWriter(fileStream);
                binaryWriter.BaseStream.Seek(0, SeekOrigin.Begin);

                // Write each pice of binary data to the file.
                binaryWriter.Write(waveHeaderStructure.ChunckID);
                binaryWriter.Write(waveHeaderStructure.FileSize);
                binaryWriter.Write(waveHeaderStructure.RiffType);
                binaryWriter.Write(waveHeaderStructure.FmtID);
                binaryWriter.Write(waveHeaderStructure.FmtSize);
                binaryWriter.Write(waveHeaderStructure.FmtCode);
                binaryWriter.Write(waveHeaderStructure.Channels);
                binaryWriter.Write(waveHeaderStructure.SampleRate);
                binaryWriter.Write(waveHeaderStructure.FmtAverageByteRate);
                binaryWriter.Write(waveHeaderStructure.FmtBlockAlign);
                binaryWriter.Write(waveHeaderStructure.BitsPerSample);

                // If the format size is not '16' then write additional
                // data to the wave file.
                if (waveHeaderStructure.FmtSize != 16)
                {
                    binaryWriter.Write(waveHeaderStructure.FmtExtraSize);
                    binaryWriter.Write(waveHeaderStructure.FmtExtraData);
                }

                // Write the 'data' indicator and the actual data size value.
                binaryWriter.Write(waveHeaderStructure.DataID);
                binaryWriter.Write(waveHeaderStructure.DataSize);

                // From the data size value write the actual sound data.
                binaryWriter.Write(waveHeaderStructure.SoundData);

                // Close the streams
                binaryWriter.Flush();
                binaryWriter.Close();
                fileStream.Close();

                // The write operation completed.
                return(true);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (binaryWriter != null)
                {
                    binaryWriter.Close();
                }

                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
        }