示例#1
0
        /// <summary>
        ///     WriteHeader
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="header"></param>
        /// <param name="dataSize"></param>
        public static void WriteHeader(string fileName, WaveFileHeader header)
        {
            //Datei öffnen
            var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            var wr = new BinaryWriter(fs, Encoding.UTF8);

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

            //Chunk 2
            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(header.BlockAlign));
            wr.Write(Int16ToBytes(header.BitsPerSample));

            //Chunk 3
            wr.Write(header.DATA);
            wr.Write(Int32ToBytes((int)header.DATASize));

            //Datei schliessen
            wr.Close();
            fs.Close();
        }
示例#2
0
        /// <summary>
        /// Read
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static WaveFileHeader Read(string fileName)
        {
            //Header lesen
            WaveFileHeader header = ReadHeader(fileName);

            //Fertig
            return(header);
        }
示例#3
0
        /// <summary>
        /// PlayFile (Wave Files)
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool PlayFile(string fileName, string waveOutDeviceName)
        {
            lock (Locker)
            {
                try
                {
                    //WaveFile auslesen
                    WaveFileHeader header = WaveFile.Read(fileName);

                    //Wenn Daten vorhanden
                    if (header.Payload.Length > 0)
                    {
                        //Wenn geöffnet
                        if (Open(waveOutDeviceName, (int)header.SamplesPerSecond, (int)header.BitsPerSample, (int)header.Channels, 8))
                        {
                            int index = GetNextFreeWaveOutHeaderIndex();
                            if (index != -1)
                            {
                                //Bytes Teilweise in Ausgabebuffer abspielen
                                this.IsStarted = true;
                                return(PlayBytes(header.Payload));
                            }
                            else
                            {
                                //Kein freier Ausgabebuffer vorhanden
                                AutoResetEventDataPlayed.Set();
                                return(false);
                            }
                        }
                        else
                        {
                            //Nicht geöffnet
                            AutoResetEventDataPlayed.Set();
                            return(false);
                        }
                    }
                    else
                    {
                        //Fehlerhafte Datei
                        AutoResetEventDataPlayed.Set();
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(String.Format("PlayFile | {0}", ex.Message));
                    AutoResetEventDataPlayed.Set();
                    return(false);
                }
            }
        }
示例#4
0
        /// <summary>
        /// WriteNew
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        public static void Create(string fileName, uint samplesPerSecond, short bitsPerSample, short channels, Byte[] data)
        {
            //Bestehende Datei löschen
            if (System.IO.File.Exists(fileName))
            {
                System.IO.File.Delete(fileName);
            }

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

            //Header schreiben
            WriteHeader(fileName, header);
            //Daten schreiben
            WriteData(fileName, header.DATAPos, data);
        }
示例#5
0
        /// <summary>
        /// AppendData
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        /// <param name="forceWriting"></param>
        public static void AppendData(string fileName, Byte[] data, bool forceWriting)
        {
            //Header auslesen
            WaveFileHeader header = ReadHeader(fileName);

            //Wenn Daten vorhanden
            if (header.DATASize > 0 || forceWriting)
            {
                //Daten anfügen
                WriteData(fileName, (int)(header.DATAPos + header.DATASize), data);

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

                //Header überschreiben
                WriteHeader(fileName, header);
            }
        }
示例#6
0
        /// <summary>
        /// CreateWaveFileHeader
        /// </summary>
        /// <param name="SamplesPerSecond"></param>
        /// <param name="BitsPerSample"></param>
        /// <param name="Channels"></param>
        /// <param name="dataSize"></param>
        /// <returns></returns>
        private static WaveFileHeader CreateNewWaveFileHeader(uint SamplesPerSecond, short BitsPerSample, short Channels, uint dataSize, long fileSize)
        {
            //Header erstellen
            WaveFileHeader Header = new WaveFileHeader();

            //Werte setzen
            Array.Copy("RIFF".ToArray <Char>(), Header.RIFF, 4);
            Header.RiffSize = (uint)(fileSize - 8);
            Array.Copy("WAVE".ToArray <Char>(), Header.RiffFormat, 4);
            Array.Copy("fmt ".ToArray <Char>(), 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("data".ToArray <Char>(), Header.DATA, 4);
            Header.DATASize = dataSize;

            //Fertig
            return(Header);
        }
示例#7
0
        /// <summary>
        ///     ReadHeader
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static WaveFileHeader ReadHeader(string fileName)
        {
            //Ergebnis
            var header = new WaveFileHeader();

            //Wenn die Datei existiert
            if (File.Exists(fileName))
            {
                //Datei öffnen
                var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                var rd = new BinaryReader(fs, Encoding.UTF8);

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

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

                    //Zu Beginn von Chunk3 gehen
                    fs.Seek(header.FMTPos + header.FMTSize, SeekOrigin.Begin);

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

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

                    //Payload einlesen
                    if (header.DATASize <= fs.Length - header.DATAPos)
                    {
                        header.Payload = rd.ReadBytes((int)header.DATASize);
                    }
                }

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

            //Fertig
            return(header);
        }