Пример #1
0
        public int Initialize()
        {
            #region waveInOpen

            WAVEFORMATEX wfx = new WAVEFORMATEX()
            {
                nChannels       = Channels,
                nSamplesPerSec  = SamplesPerSec,
                wBitsPerSample  = BitsPerSample,
                nBlockAlign     = BlockAlign,
                nAvgBytesPerSec = (uint)(BlockAlign * SamplesPerSec),
                cbSize          = 0,
                wFormatTag      = 1
            };
            this.free_pwfx     = LibUtils.StructureToPtr(wfx);
            this.waveInProcDlg = new waveNatives.waveInProcDlg(this.waveInProc);
            int retValue = waveNatives.waveInOpen(out this.hwi, waveNatives.WAVE_MAPPER, this.free_pwfx, this.waveInProcDlg, 0, waveNatives.WAVE_FORMAT_DIRECT | waveNatives.CALLBACK_FUNCTION);
            if (retValue != MMSYSERR.MMSYSERR_NOERROR)
            {
                LibUtils.PrintLog("waveInOpen失败, MMSYSERROR = {0}", retValue);
                return(retValue);
            }

            #endregion

            #region waveInPrepareHeader

            wavehdr_tag wh = new wavehdr_tag()
            {
                lpData         = this.free_pAudioData = Marshal.AllocHGlobal((int)(BlockAlign * SamplesPerSec)),
                dwBufferLength = (BlockAlign * SamplesPerSec),
                dwFlags        = 0x00000002
            };
            this.whSize   = Marshal.SizeOf(typeof(wavehdr_tag));
            this.free_pwh = LibUtils.StructureToPtr(wh);
            retValue      = waveNatives.waveInPrepareHeader(hwi, this.free_pwh, (uint)this.whSize);
            if (retValue != MMSYSERR.MMSYSERR_NOERROR)
            {
                LibUtils.PrintLog("waveInPrepareHeader失败, MMSYSERROR = {0}", retValue);
                return(retValue);
            }

            #endregion

            #region waveInAddBuffer

            retValue = waveNatives.waveInAddBuffer(hwi, this.free_pwh, (uint)this.whSize);
            if (retValue != MMSYSERR.MMSYSERR_NOERROR)
            {
                LibUtils.PrintLog("waveInAddBuffer失败, MMSYSERROR = {0}", retValue);
                return(retValue);
            }

            #endregion

            return(MMSYSERR.MMSYSERR_NOERROR);
        }
Пример #2
0
        private void waveInProc(IntPtr hwi, uint uMsg, uint dwInstance, uint dwParam1, uint dwParam2)
        {
            switch (uMsg)
            {
            case (uint)waveNatives.uMsgEnum.WIM_OPEN:
                LibUtils.PrintLog("OPEN");
                break;

            case (uint)waveNatives.uMsgEnum.WIM_DATA:
                if (this.isStop)
                {
                    break;
                }

                wavehdr_tag hdr = (wavehdr_tag)Marshal.PtrToStructure(this.free_pwh, typeof(wavehdr_tag));

                // 处理音频数据
                {
                    byte[] buffer = new byte[hdr.dwBytesRecorded];
                    Marshal.Copy(hdr.lpData, buffer, 0, buffer.Length);

                    if (this.AudioCaptured != null)
                    {
                        this.AudioCaptured(buffer);
                    }
                }

                int retValue = waveNatives.waveInAddBuffer(hwi, this.free_pwh, (uint)this.whSize);
                if (retValue != MMSYSERR.MMSYSERR_NOERROR)
                {
                    LibUtils.PrintLog("waveInAddBuffer失败, MMSYSERROR = {0}", retValue);
                }

                break;

            case (uint)waveNatives.uMsgEnum.WIM_CLOSE:
                LibUtils.PrintLog("CLOSE");
                break;
            }
        }