static public void wavtomp3() { try { while (WinMM.recording) { WinMM.syncthreads.WaitOne(); WinMM.buffers[WinMM.currentbuffer] = (WaveHdr)Marshal.PtrToStructure(WinMM.unmanagedHeaders[WinMM.currentbuffer], typeof(WaveHdr)); byte[] bytes = new byte[WinMM.buffers[WinMM.currentbuffer].dwBytesRecorded]; Marshal.Copy(WinMM.buffers[WinMM.currentbuffer].lpData, bytes, 0, WinMM.buffers[WinMM.currentbuffer].dwBytesRecorded); lock (WinMM.reclock) { WinMM.waveStream.Write(bytes, 0, bytes.Length); if (WinMM.waveStream.Length > maxstreamlength) { lock (WinMM.synclock) { int bytestotransfer = 0; MemoryStream tmpMemStream = new MemoryStream(); WinMM.waveStream.Position = WinMM.wavepointer; bytestotransfer = (int)(WinMM.waveStream.Length - WinMM.waveStream.Position); byte[] buffer2 = new byte[bytestotransfer]; WinMM.waveStream.Read(buffer2, 0, bytestotransfer); tmpMemStream.Write(buffer2, 0, bytestotransfer); WinMM.wavepointer = 0; WinMM.waveStream.Dispose(); WinMM.waveStream = null; WinMM.waveStream = tmpMemStream; } } } WinMM.ThrowExceptionForError(WinMM.waveInAddBuffer(WinMM.phwi, WinMM.unmanagedHeaders[WinMM.currentbuffer], Marshal.SizeOf(typeof(WinMM.WaveHdr)))); WinMM.currentbuffer = (WinMM.currentbuffer + 1) % WinMM.buffers.Length; if (WinMM.waveStream.Length > WinMM.wavepointer) { int toread = (int)(WinMM.waveStream.Length - WinMM.wavepointer); byte[] buffer = new byte[toread]; lock (WinMM.reclock) { long prevpos = WinMM.waveStream.Position; WinMM.waveStream.Position = WinMM.wavepointer; WinMM.waveStream.Read(buffer, 0, toread); WinMM.waveStream.Position = prevpos; WinMM.wavepointer += toread; } lock (WinMM.synclock) { WinMM._mp3writer.Write(buffer, 0, buffer.Length); } } Thread.Sleep(10); } } catch (Exception e) { throw e; } }
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; } }