private static MemoryStream Hide(string s, WaveStream sourceStream)
        {
            int messageLength = s.Length;

            byte[] length = BitConverter.GetBytes(messageLength);

            byte[] messageByte = Encoding.UTF8.GetBytes(s);
            byte[] merge       = new byte[length.Length + messageByte.Length];
            Buffer.BlockCopy(length, 0, merge, 0, length.Length);
            Buffer.BlockCopy(messageByte, 0, merge, length.Length, messageByte.Length);
            Stream messageStream  = new MemoryStream(merge);
            int    bytesPerSample = 2;

            byte[] waveBuffer = new byte[bytesPerSample];
            byte   message, bit, waveByte;
            int    messageBuffer;

            while ((messageBuffer = messageStream.ReadByte()) >= 0)
            {
                message = (byte)messageBuffer;

                for (int bitIndex = 0; bitIndex < 8; bitIndex++)
                {
                    sourceStream.Read(waveBuffer, 0, waveBuffer.Length);
                    waveByte = waveBuffer[bytesPerSample - 1];

                    bit = (byte)(((message & (byte)(1 << bitIndex)) > 0) ? 1 : 0);

                    if ((bit == 1) && ((waveByte % 2) == 0))
                    {
                        waveByte += 1;
                    }
                    else if ((bit == 0) && ((waveByte % 2) == 1))
                    {
                        waveByte -= 1;
                    }

                    waveBuffer[bytesPerSample - 1] = waveByte;

                    destinationStream.Write(waveBuffer, 0, bytesPerSample);
                }
            }
            waveBuffer = new byte[sourceStream.Length - sourceStream.Position];
            sourceStream.Read(waveBuffer, 0, waveBuffer.Length);
            destinationStream.Write(waveBuffer, 0, waveBuffer.Length);
            destinationStream = WaveStream.CreateStream(destinationStream, sourceStream.Format);
            return(destinationStream);
        }
예제 #2
0
 /// <summary>Stop recording, add a header to the sound data</summary>
 private void Stop()
 {
     waveRecorder.Dispose();
     recordedStream = WaveStream.CreateStream(recordedData, format);
     Close();
 }
예제 #3
0
 private void AskForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     Stop();
     if (_isSaving == true)
     {
         byte[] waveBuffer = new byte[16];
         _streamWave = WaveStream.CreateStream(_streamMemory, _waveFormat);
         waveBuffer  = new byte[_streamWave.Length - _streamWave.Position];
         _streamWave.Read(waveBuffer, 0, waveBuffer.Length);
         //if ("" != "")
         //    _streamFile = new FileStream("" + "\\" + _sampleFilename, FileMode.Create);
         //else
         _streamFile = new FileStream(_sampleFilename, FileMode.Create);
         _streamFile.Write(waveBuffer, 0, waveBuffer.Length);
         _isSaving = false;
     }
     if (_streamOut != null)
     {
         try
         {
             _streamOut.Close();
         }
         catch { }
         finally
         {
             _streamOut = null;
         }
     }
     if (_streamWave != null)
     {
         try
         {
             _streamWave.Close();
         }
         catch { }
         finally
         {
             _streamWave = null;
         }
     }
     if (_streamFile != null)
     {
         try
         {
             _streamFile.Close();
         }
         catch { }
         finally
         {
             _streamFile = null;
         }
     }
     if (_streamMemory != null)
     {
         try
         {
             _streamMemory.Close();
         }
         catch { }
         finally
         {
             _streamMemory = null;
         }
     }
 }
예제 #4
0
 private void DataArrived(IntPtr data, int size)
 {
     if (_isSaving == true)
     {
         byte[] recBuffer = new byte[size];
         System.Runtime.InteropServices.Marshal.Copy(data, recBuffer, 0, size);
         _streamMemory.Write(recBuffer, 0, recBuffer.Length);
     }
     if (_recorderBuffer == null || _recorderBuffer.Length != size)
     {
         _recorderBuffer = new byte[size];
     }
     if (_recorderBuffer != null)
     {
         System.Runtime.InteropServices.Marshal.Copy(data, _recorderBuffer, 0, size);
         if (_isPlayer == true)
         {
             _streamOut.Write(_recorderBuffer, 0, _recorderBuffer.Length);
         }
         _audioFrame.Process(ref _recorderBuffer);
         if (_audioFrame.IsEventActive == true)
         {
             if (_isSaving == false && false == true)
             {
                 _sampleFilename    = DateTime.Now.ToString("yyyyMMddHHmmss") + ".wav";
                 _timeLastDetection = DateTime.Now;
                 _isSaving          = true;
             }
             else
             {
                 _timeLastDetection = DateTime.Now;
             }
             Invoke(new MethodInvoker(AmplitudeEvent));
         }
         if (_isSaving == true && DateTime.Now.Subtract(_timeLastDetection).Seconds > 3)
         {
             byte[] waveBuffer = new byte[16];
             _streamWave = WaveStream.CreateStream(_streamMemory, _waveFormat);
             waveBuffer  = new byte[_streamWave.Length - _streamWave.Position];
             _streamWave.Read(waveBuffer, 0, waveBuffer.Length);
             //if ("" != "")
             //    _streamFile = new FileStream("" + "\\" + _sampleFilename, FileMode.Create);
             //else
             _streamFile = new FileStream(_sampleFilename, FileMode.Create);
             _streamFile.Write(waveBuffer, 0, waveBuffer.Length);
             if (_streamWave != null)
             {
                 _streamWave.Close();
             }
             if (_streamFile != null)
             {
                 _streamFile.Close();
             }
             _streamMemory = new MemoryStream();
             _isSaving     = false;
             Invoke(new MethodInvoker(FileSavedEvent));
         }
         _audioFrame.RenderTimeDomainLeft(ref pictureBoxTimeDomainLeft);
         _audioFrame.RenderTimeDomainRight(ref pictureBoxTimeDomainRight);
     }
 }