Ejemplo n.º 1
0
        public override int Initialize(IDictionary parameters)
        {
            base.Initialize(parameters);

            #region waveInOpen

            WAVEFORMATEX wfx = new WAVEFORMATEX()
            {
                nChannels       = (short)this.Channel,
                nSamplesPerSec  = this.SamplesPerSec,
                wBitsPerSample  = (short)this.BitsPerSample,
                nBlockAlign     = this.BlockAlign,
                nAvgBytesPerSec = this.BytesPerSec,
                cbSize          = 0,
                wFormatTag      = 1
            };
            this.free_pwfx     = PInvoke.StructureToPtr(wfx);
            this.waveInProcDlg = new waveIn.waveInProcDlg(this.waveInProc);
            int code = waveIn.waveInOpen(out this.hwi, waveIn.WAVE_MAPPER, this.free_pwfx, this.waveInProcDlg, 0, waveIn.WAVE_FORMAT_DIRECT | waveIn.CALLBACK_FUNCTION);
            if (code != MMSYSERR.MMSYSERR_NOERROR)
            {
                logger.ErrorFormat("waveInOpen失败, MMSYSERROR = {0}", code);
                return(DotNETCode.FAILED);
            }

            #endregion

            #region waveInPrepareHeader

            waveIn.wavehdr_tag wh = new waveIn.wavehdr_tag()
            {
                lpData         = this.free_pAudioData = Marshal.AllocHGlobal((int)(BlockAlign * SamplesPerSec)),
                dwBufferLength = (uint)(BlockAlign * SamplesPerSec),
                dwFlags        = 0x00000002
            };
            this.whSize   = Marshal.SizeOf(typeof(waveIn.wavehdr_tag));
            this.free_pwh = PInvoke.StructureToPtr(wh);
            code          = waveIn.waveInPrepareHeader(hwi, this.free_pwh, (uint)this.whSize);
            if (code != MMSYSERR.MMSYSERR_NOERROR)
            {
                logger.ErrorFormat("waveInPrepareHeader失败, MMSYSERROR = {0}", code);
                return(DotNETCode.FAILED);
            }

            #endregion

            #region waveInAddBuffer

            if ((code = waveIn.waveInAddBuffer(hwi, this.free_pwh, (uint)this.whSize)) != MMSYSERR.MMSYSERR_NOERROR)
            {
                logger.ErrorFormat("waveInAddBuffer失败, MMSYSERROR = {0}", code);
                return(DotNETCode.FAILED);
            }

            #endregion

            return(DotNETCode.SUCCESS);
        }
Ejemplo n.º 2
0
        private void waveInProc(IntPtr hwi, uint uMsg, uint dwInstance, uint dwParam1, uint dwParam2)
        {
            switch (uMsg)
            {
            case (uint)waveIn.uMsgEnum.WIM_OPEN:
                logger.InfoFormat("OPEN");
                break;

            case (uint)waveIn.uMsgEnum.WIM_DATA:
                if (!this.isRunning)
                {
                    break;
                }

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

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

                    this.NotifyDataReceived(buffer);
                }

                int code = waveIn.waveInAddBuffer(hwi, this.free_pwh, (uint)this.whSize);
                if (code != MMSYSERR.MMSYSERR_NOERROR)
                {
                    logger.ErrorFormat("waveInAddBuffer失败, MMSYSERROR = {0}", code);
                }

                break;

            case (uint)waveIn.uMsgEnum.WIM_CLOSE:
                logger.InfoFormat("CLOSE");
                break;

            default:
                throw new NotImplementedException();
            }
        }