/// <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) : base(output, System.Text.Encoding.ASCII) { try { m_InputDataFormat = inputDataFormat; m_Mp3Config = mp3Config; uint lameResult = LameEnc.beInitStream(m_Mp3Config, ref m_InputSamples, ref m_OutBufferSize, ref m_hLameStream); if (lameResult != LameEnc.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 { base.Close(); throw; } }
static public void startrecoding() { try { WinMM.memStream = new MemoryStream(); WinMM.waveStream = new MemoryStream(); WinMM.bytesread = 0; WinMM.waveformat = new WaveFormat(44100, 16, 2); BE_CONFIG _mp3config = new BE_CONFIG(waveformat); WinMM._mp3writer = new Mp3Writer(memStream, waveformat, _mp3config); WinMM.recording = true; WinMM._trd = new Thread(WinMM.wavtomp3); WinMM._trd.IsBackground = true; WinMM._trd.Start(); WinMM.ThrowExceptionForError(WinMM.waveInOpen(out WinMM.phwi, 0, WinMM.waveformat, WinMM.callbackm, IntPtr.Zero, WinMM.WaveOpenFlags.Function)); int buffsize = WinMM.waveformat.AverageBytesPerSecond / 100; for (int i = 0; i < WinMM.buffers.Length; i++) { WinMM.buffers[i] = new WinMM.WaveHdr(); WinMM.buffers[i].lpData = Marshal.AllocHGlobal(buffsize); WinMM.buffers[i].dwBufferLength = buffsize; WinMM.buffers[i].dwUser = IntPtr.Zero; WinMM.buffers[i].dwFlags = WinMM.WHDR.None; WinMM.buffers[i].dwLoops = 0; WinMM.buffers[i].lpNext = IntPtr.Zero; WinMM.buffers[i].reserved = IntPtr.Zero; WinMM.unmanagedHeaders[i] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WinMM.WaveHdr))); Marshal.StructureToPtr(WinMM.buffers[i], WinMM.unmanagedHeaders[i], false); WinMM.ThrowExceptionForError(WinMM.waveInPrepareHeader(WinMM.phwi, WinMM.unmanagedHeaders[i], Marshal.SizeOf(typeof(WinMM.WaveHdr)))); WinMM.ThrowExceptionForError(WinMM.waveInAddBuffer(WinMM.phwi, WinMM.unmanagedHeaders[i], Marshal.SizeOf(typeof(WinMM.WaveHdr)))); } WinMM.ThrowExceptionForError(WinMM.waveInStart(WinMM.phwi)); } catch (Exception e) { throw e; } }
public void recordingloop() { uint packetLength; uint numFramesAvailable; uint flags; ulong junk1; ulong junk2; byte[] pData; uint AUDCLNT_BUFFERFLAGS_SILENT = 2; uint bytespersample = (uint)(waveformat.nChannels * waveformat.wBitsPerSample / 8); WaveFormat _wf = new WaveFormat((int)waveformat.nSamplesPerSec, (int)waveformat.wBitsPerSample, (int)waveformat.nChannels); BE_CONFIG _mp3config = new BE_CONFIG(_wf); Mp3Writer _mp3writer = new Mp3Writer(memStream, _wf, _mp3config); int retVal = iAudioClient.Start(); if (retVal != 0) { throw new Exception("IAudioClient.Start()"); } keepgoing = true; while (keepgoing) { Thread.Sleep((int)(hnsActualDuration / 5000)); retVal = iAudioCaptureClient.GetNextPacketSize(out packetLength); if (retVal != 0) { throw new Exception("iAudioCaptureClient.GetNextPacketSize()"); } while (packetLength != 0) { IntPtr dataPtr = IntPtr.Zero; retVal = iAudioCaptureClient.GetBuffer(out dataPtr, out numFramesAvailable, out flags, out junk1, out junk2); if (retVal != 0) { throw new Exception("iAudioCaptureClient.GetBuffer()"); } pData = new byte[bytespersample * numFramesAvailable]; if ((flags & AUDCLNT_BUFFERFLAGS_SILENT) != 0) { for (int i = 0; i < pData.Length; i++) { pData[i] = 0; } } else { Marshal.Copy(dataPtr, pData, 0, (int)(bytespersample * numFramesAvailable)); } retVal = iAudioCaptureClient.ReleaseBuffer(numFramesAvailable); if (retVal != 0) { throw new Exception("iAudioCaptureClient.ReleaseBuffer()"); } lock (synclock) { _mp3writer.Write(pData, 0, pData.Length); } if (notify != null) { notify(); } retVal = iAudioCaptureClient.GetNextPacketSize(out packetLength); if (retVal != 0) { throw new Exception("iAudioCaptureClient.GetNextPacketSize()"); } } } _mp3writer.Close(); memStream.Close(); memStream.Dispose(); if (iAudioClient != null) { retVal = iAudioClient.Stop(); if (retVal != 0) { throw new Exception("iAudioClient.Stop()"); } } }
internal static extern uint beInitStream(BE_CONFIG pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream);
/// <summary> /// This function is the first to call before starting an encoding stream. /// </summary> /// <param name="pbeConfig">Encoder settings</param> /// <param name="dwSamples">Receives the number of samples (not bytes, each sample is a SHORT) to send to each beEncodeChunk() on return.</param> /// <param name="dwBufferSize">Receives the minimun number of bytes that must have the output(result) buffer</param> /// <param name="phbeStream">Receives the stream handle on return</param> /// <returns>On success: BE_ERR_SUCCESSFUL</returns> public static uint beInitStream(BE_CONFIG pbeConfig, ref uint dwSamples, ref uint dwBufferSize, ref uint phbeStream) { return(is32bit ? Lame86.beInitStream(pbeConfig, ref dwSamples, ref dwBufferSize, ref phbeStream) : Lame64.beInitStream(pbeConfig, ref dwSamples, ref dwBufferSize, ref phbeStream)); }