public WaveWriter(Stream Output, WaveFormat Format) : base(Output, Format) { if ( !OutStream.CanSeek ) { throw new ArgumentException("The stream must supports seeking if AudioDataSize is not supported", "Output"); } OutStream.Seek(WaveHeaderSize+8, SeekOrigin.Current); }
/// <summary> /// Create a Mp3Writer with specific MP3 format /// </summary> /// <param name="output">Stream that will hold the MP3 resulting data</param> /// <param name="inputDataFormat">PCM format of input data</param> /// <param name="mp3Config">Desired MP3 config</param> /// <param name="isStreamOwner"> </param> public Mp3Writer(Stream output, WaveFormat inputDataFormat, BE_CONFIG mp3Config, bool isStreamOwner) : base(output, inputDataFormat) { IsStreamOwner = isStreamOwner; try { _mMp3Config = mp3Config; uint lameResult = Lame_encDll.beInitStream(_mMp3Config, ref _mInputSamples, ref _mOutBufferSize, ref _mHLameStream); if ( lameResult != Lame_encDll.BE_ERR_SUCCESSFUL) { throw new ApplicationException(string.Format("Lame_encDll.beInitStream failed with the error code {0}", lameResult)); } _mInBuffer = new byte[_mInputSamples*2]; //Input buffer is expected as short[] _mOutBuffer = new byte[_mOutBufferSize]; } catch { CloseStream(); throw; } }
/// <summary> /// Create a Mp3Writer with specific MP3 format /// </summary> /// <param name="Output">Stream that will hold the MP3 resulting data</param> /// <param name="InputDataFormat">PCM format of input data</param> /// <param name="Mp3Config">Desired MP3 config</param> public Mp3Writer(Stream Output, WaveFormat InputDataFormat, BE_CONFIG Mp3Config, bool isStreamOwner) : base(Output, InputDataFormat) { IsStreamOwner = isStreamOwner; try { m_Mp3Config = Mp3Config; uint LameResult = Lame_encDll.beInitStream(m_Mp3Config, ref m_InputSamples, ref m_OutBufferSize, ref m_hLameStream); if ( LameResult != Lame_encDll.BE_ERR_SUCCESSFUL) { throw new ApplicationException(string.Format("Lame_encDll.beInitStream failed with the error code {0}", LameResult)); } m_InBuffer = new byte[m_InputSamples*2]; //Input buffer is expected as short[] m_OutBuffer = new byte[m_OutBufferSize]; } catch { CloseStream(); throw; } }
/// <summary> /// Create a Mp3Writer with the default MP3 format /// </summary> /// <param name="output">Stream that will hold the MP3 resulting data</param> /// <param name="inputDataFormat">PCM format of input data</param> /// <param name="isStreamOwner"> </param> public Mp3Writer(Stream output, WaveFormat inputDataFormat, bool isStreamOwner) : this(output, inputDataFormat, new BE_CONFIG(inputDataFormat), isStreamOwner) { }
public Mp3WriterConfig(WaveFormat InFormat) : this(InFormat, new BE_CONFIG(InFormat)) { }
public AudioWriter(Stream Output, WaveFormat InputDataFormat) : base(Output, System.Text.Encoding.ASCII) { m_InputDataFormat = InputDataFormat; }
private void ReadHeader() { BinaryReader Reader = new BinaryReader(m_Stream); if (ReadChunk(Reader) != "RIFF") throw new Exception("Invalid file format"); Reader.ReadInt32(); // File length minus first 8 bytes of RIFF description, we don't use it if (ReadChunk(Reader) != "WAVE") throw new Exception("Invalid file format"); if (ReadChunk(Reader) != "fmt ") throw new Exception("Invalid file format"); int FormatLength = Reader.ReadInt32(); if ( FormatLength < 16) // bad format chunk length throw new Exception("Invalid file format"); m_Format = new WaveFormat(22050, 16, 2); // initialize to any format m_Format.wFormatTag = Reader.ReadInt16(); m_Format.nChannels = Reader.ReadInt16(); m_Format.nSamplesPerSec = Reader.ReadInt32(); m_Format.nAvgBytesPerSec = Reader.ReadInt32(); m_Format.nBlockAlign = Reader.ReadInt16(); m_Format.wBitsPerSample = Reader.ReadInt16(); if ( FormatLength > 16) { m_Stream.Position += (FormatLength-16); } // assume the data chunk is aligned while(m_Stream.Position < m_Stream.Length && ReadChunk(Reader) != "data") ; if (m_Stream.Position >= m_Stream.Length) throw new Exception("Invalid file format"); m_Length = Reader.ReadInt32(); m_DataPos = m_Stream.Position; Position = 0; }
public WaveWriter(Stream Output, WaveFormat Format, uint AudioDataSize) : base(Output, Format) { m_AudioDataSize = AudioDataSize; WriteWaveHeader(); }
public BE_CONFIG(WaveFormat format) : this(format, 128, LAME_QUALITY_PRESET.LQP_NORMAL_QUALITY) { }
public BE_CONFIG(WaveFormat format, uint MpeBitRate, LAME_QUALITY_PRESET qualityPreset) { this.dwConfig = BE_CONFIG_LAME; this.format = new Format(format, MpeBitRate, qualityPreset); }
/// <summary> /// Create a Mp3Writer with the default MP3 format /// </summary> /// <param name="Output">Stream that will hold the MP3 resulting data</param> /// <param name="InputDataFormat">PCM format of input data</param> public Mp3Writer(Stream Output, WaveFormat InputDataFormat, bool isStreamOwner) : this(Output, InputDataFormat, new BE_CONFIG(InputDataFormat), isStreamOwner) { }
public Mp3WriterConfig(WaveFormat InFormat, BE_CONFIG beconfig) : base(InFormat) { m_BeConfig = beconfig; }
public Format(WaveFormat format, uint MpeBitRate, LAME_QUALITY_PRESET qualityPreset) { lhv1 = new LHV1(format, MpeBitRate, qualityPreset); }
public ushort nQuality; // Quality Setting, HIGH BYTE should be NOT LOW byte, otherwhise quality=5 // FUTURE USE, SET TO 0, align strucutre to 331 bytes //[ MarshalAs( UnmanagedType.ByValArray, SizeConst=255-4*4-2 )] //public byte[] btReserved;//[255-4*sizeof(DWORD) - sizeof( WORD )]; public LHV1(WaveFormat format, uint MpeBitRate, LAME_QUALITY_PRESET qualityPreset) { if (format.wFormatTag != (short)WaveFormats.Pcm) { throw new ArgumentOutOfRangeException("format", "Only PCM format supported"); } if (format.wBitsPerSample != 16) { throw new ArgumentOutOfRangeException("format", "Only 16 bits samples supported"); } dwStructVersion = 1; dwStructSize = (uint)Marshal.SizeOf(typeof(BE_CONFIG)); switch (format.nSamplesPerSec) { case 16000: case 22050: case 24000: dwMpegVersion = MPEG2; break; case 32000: case 44100: case 48000: dwMpegVersion = MPEG1; break; default: throw new ArgumentOutOfRangeException("format", "Unsupported sample rate"); } dwSampleRate = (uint)format.nSamplesPerSec; // INPUT FREQUENCY dwReSampleRate = 0; // DON'T RESAMPLE switch (format.nChannels) { case 1: nMode = MpegMode.MONO; break; case 2: nMode = MpegMode.STEREO; break; default: throw new ArgumentOutOfRangeException("format", "Invalid number of channels"); } switch (MpeBitRate) { case 32: case 40: case 48: case 56: case 64: case 80: case 96: case 112: case 128: case 160: //Allowed bit rates in MPEG1 and MPEG2 break; case 192: case 224: case 256: case 320: //Allowed only in MPEG1 if (dwMpegVersion != MPEG1) { throw new ArgumentOutOfRangeException("MpsBitRate", "Bit rate not compatible with input format"); } break; case 8: case 16: case 24: case 144: //Allowed only in MPEG2 if (dwMpegVersion != MPEG2) { throw new ArgumentOutOfRangeException("MpsBitRate", "Bit rate not compatible with input format"); } break; default: throw new ArgumentOutOfRangeException("MpsBitRate", "Unsupported bit rate"); } dwBitrate = MpeBitRate; // MINIMUM BIT RATE nPreset = qualityPreset; // QUALITY PRESET SETTING dwPsyModel = 0; // USE DEFAULT PSYCHOACOUSTIC MODEL dwEmphasis = 0; // NO EMPHASIS TURNED ON bOriginal = 1; // SET ORIGINAL FLAG bWriteVBRHeader = 0; bNoRes = 1; // No Bit resorvoir bCopyright = 0; bCRC = 0; bEnableVBR = 0; bPrivate = 0; bStrictIso = 0; dwMaxBitrate = 0; dwVbrAbr_bps = 0; nQuality = 0; nVbrMethod = VBRMETHOD.VBR_METHOD_NONE; nVBRQuality = 0; }
private void ReadHeader() { var reader = new BinaryReader(_mStream); if (ReadChunk(reader) != "RIFF") throw new Exception("Invalid file format"); reader.ReadInt32(); // File length minus first 8 bytes of RIFF description, we don't use it if (ReadChunk(reader) != "WAVE") throw new Exception("Invalid file format"); if (ReadChunk(reader) != "fmt ") throw new Exception("Invalid file format"); int formatLength = reader.ReadInt32(); if ( formatLength < 16) // bad format chunk length throw new Exception("Invalid file format"); _mFormat = new WaveFormat(22050, 16, 2) { wFormatTag = reader.ReadInt16(), nChannels = reader.ReadInt16(), nSamplesPerSec = reader.ReadInt32(), nAvgBytesPerSec = reader.ReadInt32(), nBlockAlign = reader.ReadInt16(), wBitsPerSample = reader.ReadInt16() }; // initialize to any format if ( formatLength > 16) { _mStream.Position += (formatLength-16); } // assume the data chunk is aligned while(_mStream.Position < _mStream.Length && ReadChunk(reader) != "data") { } if (_mStream.Position >= _mStream.Length) throw new Exception("Invalid file format"); _mLength = reader.ReadInt32(); _mDataPos = _mStream.Position; Position = 0; }
public ushort nQuality; // Quality Setting, HIGH BYTE should be NOT LOW byte, otherwhise quality=5 #region Constructors // FUTURE USE, SET TO 0, align strucutre to 331 bytes //[ MarshalAs( UnmanagedType.ByValArray, SizeConst=255-4*4-2 )] //public byte[] btReserved;//[255-4*sizeof(DWORD) - sizeof( WORD )]; public LHV1(WaveFormat format, uint MpeBitRate, LAME_QUALITY_PRESET qualityPreset) { if ( format.wFormatTag != (short)WaveFormats.Pcm ) { throw new ArgumentOutOfRangeException("format", "Only PCM format supported"); } if ( format.wBitsPerSample != 16) { throw new ArgumentOutOfRangeException("format", "Only 16 bits samples supported"); } dwStructVersion = 1; dwStructSize = (uint)Marshal.SizeOf(typeof(BE_CONFIG)); switch (format.nSamplesPerSec) { case 16000 : case 22050 : case 24000 : dwMpegVersion = MPEG2; break; case 32000 : case 44100 : case 48000 : dwMpegVersion = MPEG1; break; default : throw new ArgumentOutOfRangeException("format", "Unsupported sample rate"); } dwSampleRate = (uint)format.nSamplesPerSec; // INPUT FREQUENCY dwReSampleRate = 0; // DON'T RESAMPLE switch (format.nChannels) { case 1 : nMode = MpegMode.MONO; break; case 2 : nMode = MpegMode.STEREO; break; default: throw new ArgumentOutOfRangeException("format", "Invalid number of channels"); } switch (MpeBitRate) { case 32 : case 40 : case 48 : case 56 : case 64 : case 80 : case 96 : case 112 : case 128 : case 160 : //Allowed bit rates in MPEG1 and MPEG2 break; case 192 : case 224 : case 256 : case 320 : //Allowed only in MPEG1 if (dwMpegVersion != MPEG1) { throw new ArgumentOutOfRangeException("MpsBitRate", "Bit rate not compatible with input format"); } break; case 8 : case 16 : case 24 : case 144 : //Allowed only in MPEG2 if (dwMpegVersion != MPEG2) { throw new ArgumentOutOfRangeException("MpsBitRate", "Bit rate not compatible with input format"); } break; default : throw new ArgumentOutOfRangeException("MpsBitRate", "Unsupported bit rate"); } dwBitrate = MpeBitRate; // MINIMUM BIT RATE nPreset = qualityPreset; // QUALITY PRESET SETTING dwPsyModel = 0; // USE DEFAULT PSYCHOACOUSTIC MODEL dwEmphasis = 0; // NO EMPHASIS TURNED ON bOriginal = 1; // SET ORIGINAL FLAG bWriteVBRHeader = 0; bNoRes = 1; // No Bit resorvoir bCopyright = 0; bCRC = 0; bEnableVBR = 0; bPrivate = 0; bStrictIso = 0; dwMaxBitrate = 0; dwVbrAbr_bps = 0; nQuality = 0; nVbrMethod = VBRMETHOD.VBR_METHOD_NONE; nVBRQuality = 0; }