Exemple #1
0
        public WAVHeader ReadWAVFile()
        {
            WAVHeader wavHeader = new WAVHeader();

            using (FileStream fs = File.Open(this.Filename, FileMode.Open))
            {
                BinaryReader binaryReader = new BinaryReader(fs);
                wavHeader.ChunkId       = this.FormatValue(binaryReader.ReadInt32());
                wavHeader.ChunkSize     = binaryReader.ReadUInt32();
                wavHeader.Format        = this.FormatValue(binaryReader.ReadInt32());
                wavHeader.Subchunk1Id   = this.FormatValue(binaryReader.ReadInt32());
                wavHeader.Subchunk1Size = binaryReader.ReadInt32();
                wavHeader.AudioFormat   = binaryReader.ReadInt16();
                wavHeader.NumChannels   = binaryReader.ReadInt16();
                wavHeader.SampleRate    = binaryReader.ReadInt32();
                wavHeader.ByteRate      = binaryReader.ReadInt32() * 8;
                wavHeader.BlockAlign    = binaryReader.ReadInt16();
                wavHeader.BitPerSample  = binaryReader.ReadInt16();
                wavHeader.Subchunk2Id   = this.FormatValue(binaryReader.ReadInt32());
                wavHeader.Subchunk2Size = binaryReader.ReadInt32();
                byte[]  readDataBytes = isEncoded ? binaryReader.ReadBytes(wavHeader.Subchunk2Size * 4) : binaryReader.ReadBytes(wavHeader.Subchunk2Size);;
                WAVData wavData       = new WAVData(readDataBytes, wavHeader.Subchunk2Size);
                wavHeader.WavData = wavData;
            }
            return(wavHeader);
        }
        /**
         * Loads specified file
         */
        private void BtnLoadWAV_Click(object sender, RoutedEventArgs e)
        {
            var fileContent = string.Empty;
            var filePath    = string.Empty;

            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "c:\\";
                openFileDialog.Filter           = "wav files (*.wav)|*.wav";
                openFileDialog.FilterIndex      = 2;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    filePath  = openFileDialog.FileName;
                    wavHeader = new WAVReader(filePath).ReadWAVFile();
                    AddToList(wavHeader);
                    DFT     dft    = new DFT(wavHeader.WavData.ChannelData);
                    Point[] points = dft.FourierData(wavHeader.SampleRate);
                    dft.FourierData(wavHeader.SampleRate);
                    List <double> dataFourier = new List <double>();
                    this.LoadChartData(points);
                }
            }
        }
 /**
  * Makes an array of 4 bytes from ciphered float saves into a file
  */
 private void BtnCipher_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         WAVHeader cipheredFile = wavHeader;
         Cipher    cipher       = new Cipher(cipheredFile.WavData.OriginalData);
         float[]   encoded      = cipher.getCipheredData();
         cipheredFile.WavData.DataToSave = cipheredFile.WavData.Normalize(encoded);
         WAVWriter wavWriter = new WAVWriter();
         wavWriter.WriteWAVFile(cipheredFile);
         System.Windows.MessageBox.Show("Plik zostal zaszyfrowany i zapisany do pliku.");
     }
     catch (Exception ex)
     {
         System.Windows.MessageBox.Show("Najpierw trzeba wczytac plik.");
     }
 }
        /// <summary>
        /// Adds wav header items to first tab for MainWindow.xaml
        /// </summary>
        private void AddToList(WAVHeader header)
        {
            List <ListFormat> list = new List <ListFormat>();

            list.Add(new ListFormat("ChunkID", header.ChunkId));
            list.Add(new ListFormat("ChunkSize", header.ChunkSize));
            list.Add(new ListFormat("Format", header.Format));
            list.Add(new ListFormat("Subchunk1ID", header.Subchunk1Id));
            list.Add(new ListFormat("Subchunk1Size", header.Subchunk1Size));
            list.Add(new ListFormat("AudioFormat", header.AudioFormat));
            list.Add(new ListFormat("Number of channels", header.NumChannels));
            list.Add(new ListFormat("Sample Rate", header.SampleRate));
            list.Add(new ListFormat("Byte Rate", header.ByteRate));
            list.Add(new ListFormat("Block Align", header.BlockAlign));
            list.Add(new ListFormat("Bits per Sample", header.BitPerSample));
            list.Add(new ListFormat("Subchunk2ID", header.Subchunk2Id));
            list.Add(new ListFormat("Subchunk2Size", header.Subchunk2Size));

            listTemplate.ItemsSource = list;
        }
Exemple #5
0
 public void WriteWAVFile(WAVHeader header)
 {
     using (FileStream fs = File.Open(this.filename, FileMode.OpenOrCreate))
     {
         BinaryWriter binaryWriter = new BinaryWriter(fs);
         binaryWriter.Write(this.FormatValue(header.ChunkId));
         binaryWriter.Write(header.ChunkSize);
         binaryWriter.Write(this.FormatValue(header.Format));
         binaryWriter.Write(this.FormatValue(header.Subchunk1Id));
         binaryWriter.Write(header.Subchunk1Size);
         binaryWriter.Write((Int16)header.AudioFormat);
         binaryWriter.Write((Int16)header.NumChannels);
         binaryWriter.Write(header.SampleRate);
         binaryWriter.Write(header.ByteRate / 8);
         binaryWriter.Write((Int16)header.BlockAlign);
         binaryWriter.Write((Int16)header.BitPerSample);
         binaryWriter.Write(this.FormatValue(header.Subchunk2Id));
         binaryWriter.Write(header.Subchunk2Size);
         for (int i = 0; i < header.WavData.DataToSave.Length; i++)
         {
             binaryWriter.Write(header.WavData.DataToSave[i]);
         }
     }
 }