コード例 #1
0
        public static void Create(string sourcePath, string outputFile, uint samplesPerSecond = 0, short bitsPerSample = 0, short channels = 0)
        {
            if (File.Exists(outputFile))
            {
                File.Delete(outputFile);
            }

            byte[]         data        = File.ReadAllBytes(sourcePath);
            WaveFileHeader file_header = ReadHeader(sourcePath);

            if (samplesPerSecond == 0)
            {
                samplesPerSecond = file_header.SamplesPerSecond;
            }
            if (bitsPerSample == 0)
            {
                bitsPerSample = file_header.BitsPerSample;
            }
            if (channels == 0)
            {
                channels = file_header.Channels;
            }

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

            WriteHeader(outputFile, header);
            WriteData(outputFile, header.DATAPos, data);

            data = null;
        }
コード例 #2
0
        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((short)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();
        }
コード例 #3
0
        public static void Create(byte[] data, string dst, WaveFileHeader h)
        {
            if (File.Exists(dst))
            {
                File.Delete(dst);
            }
            WaveFileHeader header = CreateNewWaveFileHeader(h.SamplesPerSecond, h.BitsPerSample, h.Channels, (uint)(data.Length), 44 + data.Length);

            WriteHeader(dst, header);
            WriteData(dst, header.DATAPos, data);
        }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
        private static WaveFileHeader ReadHeader(string fileName)
        {
            WaveFileHeader header = new WaveFileHeader();

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

                if (fs.CanRead)
                {
                    header = ReadHeader(fs);
                }

                fs.Close();
            }

            return(header);
        }
コード例 #6
0
        public static WaveFileHeader ReadHeader(Stream input, bool dataRead = true)
        {
            WaveFileHeader header = new WaveFileHeader();

            if (input.CanRead)
            {
                header.RIFF       = input.ReadString(4).ToCharArray();
                header.RiffSize   = input.ReadValueU32();
                header.RiffFormat = input.ReadString(4).ToCharArray();

                header.FMT     = input.ReadString(4).ToCharArray();
                header.FMTSize = input.ReadValueU32();
                header.FMTPos  = input.Position;
                //header.AudioFormat = (short)rd.ReadInt16();
                header.AudioFormat      = (WavFormatId)input.ReadValueS16();
                header.Channels         = input.ReadValueS16();
                header.SamplesPerSecond = input.ReadValueU32();
                header.BytesPerSecond   = input.ReadValueU32();
                header.BlockAlign       = input.ReadValueS16();
                header.BitsPerSample    = input.ReadValueS16();

                if (!dataRead)
                {
                    return(header);
                }

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

                header.DATA     = input.ReadString(4).ToCharArray();
                header.DATASize = input.ReadValueU32();
                header.DATAPos  = (int)input.Position;

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

                header.Payload = input.ReadBytes((int)header.DATASize);
            }
            return(header);
        }
コード例 #7
0
        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      = WavFormatId.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);
        }