public void Hide(Stream messageStream, Stream keyStream) { byte[] waveBuffer = new byte[bytesPerSample]; byte message, bit, waveByte; int messageBuffer, keyByte; while ((messageBuffer = messageStream.ReadByte()) >= 0) { //read one byte of the message message = (byte)messageBuffer; //for each bit in message for (int bitIndex = 0; bitIndex < 8; bitIndex++) { //read a byte from the key keyByte = GetKeyValue(keyStream); //skip samples for (int n = 0; n < keyByte - 1; n++) { sourceStream.Copy(waveBuffer, 0, waveBuffer.Length, destinationStream); } //read one sample from the wave stream sourceStream.Read(waveBuffer, 0, waveBuffer.Length); waveByte = waveBuffer[bytesPerSample - 1]; bit = (byte)(((message & (byte)(1 << bitIndex)) > 0) ? 1 : 0); //change the last bit of the sample if ((bit == 1) && ((waveByte % 2) == 0)) { waveByte += 1; } else if ((bit == 0) && ((waveByte % 2) == 1)) { waveByte -= 1; } waveBuffer[bytesPerSample - 1] = waveByte; //write the result to destinationStream destinationStream.Write(waveBuffer, 0, bytesPerSample); } } waveBuffer = new byte[sourceStream.Length - sourceStream.Position]; sourceStream.Read(waveBuffer, 0, waveBuffer.Length); destinationStream.Write(waveBuffer, 0, waveBuffer.Length); }
/// <summary> /// Converts Wav to MP3. /// </summary> /// <param name="inputFile">The input file.</param> /// <param name="outputPath">The output path.</param> /// <returns> /// Path of changed file /// </returns> public static string ConvertWavToMp3(string inputFile, string outputPath) { var reader = new WaveStream(inputFile); try { var config = new BE_CONFIG(reader.Format, (uint)(Math.Abs(BitRate - 0.0) < 0.1 ? DefaultBitRate : BitRate)); var writer = new Mp3Writer(new FileStream(outputPath, FileMode.Create), reader.Format, config); try { var buffer = new byte[writer.OptimalBufferSize]; int read; while ((read = reader.Read(buffer, 0, buffer.Length)) > 0) { writer.Write(buffer, 0, read); } } finally { writer.Close(); } } finally { reader.Close(); } return(outputPath); }
internal byte[] ConvertWavToMp3(Stream stream) { var inStr = new WaveStream(stream); try { var convertedStream = new MemoryStream(); var writer = new Mp3Writer(convertedStream, inStr.Format); try { var buff = new byte[writer.OptimalBufferSize]; int read; while ((read = inStr.Read(buff, 0, buff.Length)) > 0) { writer.Write(buff, 0, read); } convertedStream.Seek(0, SeekOrigin.Begin); return(convertedStream.ToArray()); } catch (Exception e) { LoggerWrapper.LogTo(LoggerName.Errors).ErrorException( "AudioConverter.ConvertWavToMp3 inner вылетело исключение {0}", e); } finally { writer.Close(); } } catch (Exception e) { LoggerWrapper.LogTo(LoggerName.Errors).ErrorException( "AudioConverter.ConvertWavToMp3 outer вылетело исключение {0}", e); } finally { inStr.Close(); } return(null); }
/// <summary> /// Converts Wav to MP3. /// </summary> /// <param name="inputFile">The input file.</param> /// <param name="outputPath">The output path.</param> /// <returns> /// Path of changed file /// </returns> public static string ConvertWavToMp3(string inputFile, string outputPath) { var reader = new WaveStream(inputFile); try { var config = new BE_CONFIG(reader.Format, (uint)(Math.Abs(BitRate - 0.0) < 0.1 ? DefaultBitRate : BitRate)); var writer = new Mp3Writer(new FileStream(outputPath, FileMode.Create), reader.Format, config); try { var buffer = new byte[writer.OptimalBufferSize]; int read; while ((read = reader.Read(buffer, 0, buffer.Length)) > 0) { writer.Write(buffer, 0, read); } } finally { writer.Close(); } } finally { reader.Close(); } return outputPath; }