Exemplo n.º 1
0
            public static wavData Read(BinaryReader reader)
            {
                wavData rv = new wavData();

                rv.read(reader);
                return(rv);
            }
Exemplo n.º 2
0
        //reads the wav placed at the sourcePath
        private void ReadWav()
        {
            try
            {
                wavdata      = new wavData();
                wavdata.data = new List <short>();

                //read Wav
                using (FileStream fs = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        try
                        {
                            wavdata.riffID     = br.ReadBytes(4);
                            wavdata.size       = br.ReadUInt32();
                            wavdata.wavID      = br.ReadBytes(4);
                            wavdata.fmtID      = br.ReadBytes(4);
                            wavdata.fmtSize    = br.ReadUInt32();
                            wavdata.format     = br.ReadUInt16();
                            wavdata.channels   = br.ReadUInt16();
                            wavdata.sampleRate = br.ReadUInt32();
                            wavdata.bytePerSec = br.ReadUInt32();
                            wavdata.blockSize  = br.ReadUInt16();
                            wavdata.bit        = br.ReadUInt16();
                            wavdata.dataID     = br.ReadBytes(4);
                            wavdata.dataSize   = br.ReadUInt32();

                            for (int i = 0; (i < wavdata.dataSize / wavdata.blockSize); i += wavdata.channels)
                            {
                                wavdata.data.Add((short)br.ReadUInt16());
                                if (wavdata.channels == 2)
                                {
                                    br.ReadUInt16();                    //Skip any second channel
                                }
                                if (fs.Position == fs.Length)
                                {
                                    break;                          //stop when end of file
                                }
                            }
                            wavdata.channels = 1; //set channels on 1 incase it was 2
                        }
                        finally
                        {
                            if (br != null)
                            {
                                br.Close();
                            }
                            if (fs != null)
                            {
                                fs.Close();
                            }
                        }
                    }
            }
            catch (Exception)
            {
                throw new Exception("trouble reading WAV file! Perhaps your wav file is corrupt.");
            }
        }
Exemplo n.º 3
0
 private void clear()
 {
     header   = null;
     format   = null;
     data     = null;
     FileName = null;
     Name     = null;
 }
Exemplo n.º 4
0
 public wavData(wavData copy)
 {
     samples = new byte[copy.samples.Length];
     for (int k = 0; k < copy.samples.Length; k++)
     {
         samples[k] = copy.samples[k];
     }
 }
Exemplo n.º 5
0
 public Wav(Wav copy)
 {
     header       = new wavHeader(copy.header);
     format       = new wavFormat(copy.format);
     data         = new wavData(copy.data);
     OrigDuration = copy.OrigDuration;
     FileName     = copy.FileName;
     Name         = copy.Name;
 }
        // internally used methodes
        /// <summary>
        ///     update all WAV data
        /// </summary>
        private void ReadWav()
        {
            try
            {
                wavdata      = new wavData();
                wavdata.data = new List <short>(); //just to be sure idk maybe the pointer ain't forgotten...

                //read Wav
                using (FileStream fs = new FileStream(wavPath, FileMode.Open, FileAccess.Read))
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        try
                        {
                            wavdata.riffID     = br.ReadBytes(4);
                            wavdata.size       = br.ReadUInt32();
                            wavdata.wavID      = br.ReadBytes(4);
                            wavdata.fmtID      = br.ReadBytes(4);
                            wavdata.fmtSize    = br.ReadUInt32();
                            wavdata.format     = br.ReadUInt16();
                            wavdata.channels   = br.ReadUInt16(); //important!
                            wavdata.sampleRate = br.ReadUInt32();
                            wavdata.bytePerSec = br.ReadUInt32();
                            wavdata.blockSize  = br.ReadUInt16();//important!
                            wavdata.bit        = br.ReadUInt16();
                            wavdata.dataID     = br.ReadBytes(4);
                            wavdata.dataSize   = br.ReadUInt32();//important!

                            for (int i = 0; (i < wavdata.dataSize / wavdata.blockSize); i += wavdata.channels)
                            {
                                wavdata.data.Add((short)br.ReadUInt16());
                                if (wavdata.channels == 2)
                                {
                                    br.ReadUInt16();                   //throw away 2nd channel
                                }
                            }
                        }
                        finally
                        {
                            if (br != null)
                            {
                                br.Close();
                            }
                            if (fs != null)
                            {
                                fs.Close();
                            }
                        }
                    }
            }
            catch (Exception)
            {
                throw new Exception("trouble reading WAV file! Perhaps your wav file is corrupt.");
            }
        }
Exemplo n.º 7
0
 public void Cat(Wav inputFile)
 {
     if (header == null)
     {
         header = new wavHeader(inputFile.header);
         format = new wavFormat(inputFile.format);
         data   = new wavData(inputFile.data);
     }
     else
     {
         data.Cat(inputFile);
     }
 }
Exemplo n.º 8
0
            public void Write(BinaryWriter writer, wavFormat format, wavData data)
            {
                calcLen(format, data);
                writer.Write(sGroupID.ToCharArray());
                writer.Write(dwFileLength);
                writer.Write(sRiffType.ToCharArray());

                // Write the format chunk
                format.Write(writer);

                // Write the data chunk
                data.Write(writer);
            }
Exemplo n.º 9
0
        public void Load(string filePath)
        {
            clear();
            FileName = filePath;
            Name     = Path.GetFileName(FileName);
            using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader reader = new BinaryReader(fileStream))
                {
                    // Read the header
                    header = wavHeader.Read(reader);

                    // Read the format chunk
                    format = wavFormat.Read(reader);

                    // Read the data chunk
                    data         = wavData.Read(reader);
                    OrigDuration = Duration;

                    reader.Close();
                }
                fileStream.Close();
            }
        }
Exemplo n.º 10
0
 private void calcLen(wavFormat format, wavData data)
 {
     dwFileLength = (uint)sRiffType.Length + format.getLength() + data.getLength();
 }