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()"); } } }