Beispiel #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);
        }
Beispiel #2
0
        private bool CreateCaptureBuffer()
        {
            uint dsErr = DSERR.DS_OK;

            #region 创建默认音频流格式

            this.wfx = new tWAVEFORMATEX()
            {
                nChannels       = LibConsts.Channels,
                nSamplesPerSec  = LibConsts.SamplesPerSec,
                wBitsPerSample  = LibConsts.BitsPerSample,
                nBlockAlign     = LibConsts.BlockAlign,
                nAvgBytesPerSec = LibConsts.Bps,
                cbSize          = 0,
                wFormatTag      = LibNatives.WAVE_FORMAT_PCM
            };

            this.pwfx_free = LibUtils.StructureToPtr(this.wfx);

            this.dsbd = new _DSCBUFFERDESC()
            {
                dwFlags       = 0,
                dwSize        = Marshal.SizeOf(typeof(_DSCBUFFERDESC)),
                dwReserved    = 0,
                dwFXCount     = 0,
                dwBufferBytes = LibConsts.BufferSize,
                lpwfxFormat   = this.pwfx_free,
                lpDSCFXDesc   = IntPtr.Zero
            };

            #endregion

            IntPtr pdscb;
            Guid   iid_dscb8;
            dsErr = this.dsc8.CreateCaptureBuffer(ref this.dsbd, out pdscb, IntPtr.Zero); //TestInvoke2(this.free_bufferDesc, out ppDSCBuff);
            if (dsErr == DSERR.DS_OK)
            {
                // 获取IDirectSoundCaptureBuffer8接口实例
                iid_dscb8 = new Guid(IID.IID_IDirectSoundCaptureBuffer8);
                Marshal.QueryInterface(pdscb, ref iid_dscb8, out this.pdscb8);
                Marshal.Release(pdscb);
                this.dscb8 = Marshal.GetObjectForIUnknown(this.pdscb8) as IDirectSoundCaptureBuffer8;
            }
            else
            {
                LibUtils.PrintLog("CreateCaptureBuffer失败, DSERROR = {0}", dsErr);
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        private bool CreateSecondaryBuffer()
        {
            uint dsErr = DSERR.DS_OK;

            #region 创建默认音频流格式

            this.wfx = new tWAVEFORMATEX()
            {
                nChannels       = LibConsts.Channels,
                nSamplesPerSec  = LibConsts.SamplesPerSec,
                wBitsPerSample  = LibConsts.BitsPerSample,
                nBlockAlign     = LibConsts.BlockAlign,
                nAvgBytesPerSec = LibConsts.Bps,
                cbSize          = 0,
                wFormatTag      = LibNatives.WAVE_FORMAT_PCM
            };

            this.pwfx_free = LibUtils.StructureToPtr(this.wfx);

            this.dsbd = new _DSBUFFERDESC()
            {
                dwSize          = Marshal.SizeOf(typeof(_DSBUFFERDESC)),
                dwFlags         = DSBCAPS.DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS.DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS.DSBCAPS_GLOBALFOCUS | DSBCAPS.DSBCAPS_CTRLVOLUME,
                lpwfxFormat     = this.pwfx_free,
                guid3DAlgorithm = new _GUID(),
                dwBufferBytes   = LibConsts.PLAY_BUFF_SIZE,
                dwReserved      = 0
            };

            #endregion

            IntPtr pdsb;
            dsErr = this.ds8.CreateSoundBuffer(ref this.dsbd, out pdsb, IntPtr.Zero);
            if (dsErr != DSERR.DS_OK)
            {
                LibUtils.PrintLog("CreateSoundBuffer失败, DSERR = {0}", dsErr);
                return(false);
            }

            Guid iid_dsb8 = new Guid(IID.IID_IDirectSoundBuffer8);
            Marshal.QueryInterface(pdsb, ref iid_dsb8, out this.pdsb8);
            Marshal.Release(pdsb);
            this.dsb8 = Marshal.GetObjectForIUnknown(this.pdsb8) as IDirectSoundBuffer8;

            return(true);
        }