예제 #1
0
파일: WaveFile.cs 프로젝트: zhrren/PCM2WAV
        public static void Create(string fileName, uint samplesPerSecond, short bitsPerSample, short channels, Byte[] data)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            WaveFileHeader header = CreateNewWaveFileHeader(samplesPerSecond, bitsPerSample, channels, (uint)(data.Length), 44 + data.Length);

            WriteHeader(fileName, header);
            WriteData(fileName, header.DATAPos, data);
        }
예제 #2
0
파일: WaveFile.cs 프로젝트: zhrren/PCM2WAV
        public static void AppendData(string fileName, Byte[] data)
        {
            WaveFileHeader header = ReadHeader(fileName);

            if (header.DATASize > 0)
            {
                WriteData(fileName, (int)(header.DATAPos + header.DATASize), data);

                header.DATASize += (uint)data.Length;
                header.RiffSize += (uint)data.Length;

                WriteHeader(fileName, header);
            }
        }
예제 #3
0
파일: WaveFile.cs 프로젝트: zhrren/PCM2WAV
        private static WaveFileHeader ReadHeader(string fileName)
        {
            WaveFileHeader header = new WaveFileHeader();

            if (File.Exists(fileName))
            {
                FileStream   fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader rd = new BinaryReader(fs, Encoding.UTF8);

                if (fs.CanRead)
                {
                    header.RIFF       = rd.ReadChars(4);
                    header.RiffSize   = (uint)rd.ReadInt32();
                    header.RiffFormat = rd.ReadChars(4);

                    header.FMT              = rd.ReadChars(4);
                    header.FMTSize          = (uint)rd.ReadInt32();
                    header.FMTPos           = fs.Position;
                    header.AudioFormat      = (short)rd.ReadInt16();
                    header.Channels         = (short)rd.ReadInt16();
                    header.SamplesPerSecond = (uint)rd.ReadInt32();
                    header.BytesPerSecond   = (uint)rd.ReadInt32();
                    header.BlockAlign       = (short)rd.ReadInt16();
                    header.BitsPerSample    = (short)rd.ReadInt16();

                    fs.Seek(header.FMTPos + header.FMTSize, SeekOrigin.Begin);

                    header.DATA     = rd.ReadChars(4);
                    header.DATASize = (uint)rd.ReadInt32();
                    header.DATAPos  = (int)fs.Position;

                    if (new String(header.DATA).ToUpper() != "DATA")
                    {
                        uint DataChunkSize = header.DATASize + 8;
                        fs.Seek(DataChunkSize, SeekOrigin.Current);
                        header.DATASize = (uint)(fs.Length - header.DATAPos - DataChunkSize);
                    }

                    header.Payload = rd.ReadBytes((int)header.DATASize);
                }

                rd.Close();
                fs.Close();
            }

            return(header);
        }
예제 #4
0
파일: WaveFile.cs 프로젝트: zhrren/PCM2WAV
        private static WaveFileHeader CreateNewWaveFileHeader(uint SamplesPerSecond, short BitsPerSample, short Channels, uint dataSize, long fileSize)
        {
            WaveFileHeader Header = new WaveFileHeader();

            Array.Copy(new char[] { 'R', 'I', 'F', 'F' }, Header.RIFF, 4);
            Header.RiffSize = (uint)(fileSize - 8);
            Array.Copy(new char[] { 'W', 'A', 'V', 'E' }, Header.RiffFormat, 4);
            Array.Copy(new char[] { 'f', 'm', 't', ' ' }, Header.FMT, 4);
            Header.FMTSize          = 16;
            Header.AudioFormat      = WAVE_FORMAT_PCM;
            Header.Channels         = (short)Channels;
            Header.SamplesPerSecond = (uint)SamplesPerSecond;
            Header.BitsPerSample    = (short)BitsPerSample;
            Header.BlockAlign       = (short)((BitsPerSample * Channels) >> 3);
            Header.BytesPerSecond   = (uint)(Header.BlockAlign * Header.SamplesPerSecond);
            Array.Copy(new char[] { 'd', 'a', 't', 'a' }, Header.DATA, 4);
            Header.DATASize = dataSize;

            return(Header);
        }
예제 #5
0
파일: WaveFile.cs 프로젝트: zhrren/PCM2WAV
        private static void WriteHeader(string fileName, WaveFileHeader header)
        {
            FileStream   fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            BinaryWriter wr = new BinaryWriter(fs, Encoding.UTF8);

            wr.Write(header.RIFF);
            wr.Write(Int32ToBytes((int)header.RiffSize));
            wr.Write(header.RiffFormat);

            wr.Write(header.FMT);
            wr.Write(Int32ToBytes((int)header.FMTSize));
            wr.Write(Int16ToBytes(header.AudioFormat));
            wr.Write(Int16ToBytes(header.Channels));
            wr.Write(Int32ToBytes((int)header.SamplesPerSecond));
            wr.Write(Int32ToBytes((int)header.BytesPerSecond));
            wr.Write(Int16ToBytes((short)header.BlockAlign));
            wr.Write(Int16ToBytes((short)header.BitsPerSample));

            wr.Write(header.DATA);
            wr.Write(Int32ToBytes((int)header.DATASize));

            wr.Close();
            fs.Close();
        }
예제 #6
0
파일: WaveFile.cs 프로젝트: zhrren/PCM2WAV
        public static WaveFileHeader Read(string fileName)
        {
            WaveFileHeader header = ReadHeader(fileName);

            return(header);
        }