コード例 #1
0
ファイル: Form1.cs プロジェクト: hasham12/RentAcar
 private void Form1_Load(object sender, EventArgs e)
 {
     sound = new DirectSoundHelper();
     sound.OnBufferFulfill += new EventHandler(SendVoiceBuffer);
 }
コード例 #2
0
        public override bool Open(WaveFormat af)
        {
            this.windowHandle = GetDesktopWindow();

            this.audioFormat = af;

            #region 创建DirectSound对象

            int dsErr = DirectSoundHelper.DirectSoundCreate8(IntPtr.Zero, out this.pds8, IntPtr.Zero);
            if (dsErr != DSERR.DS_OK)
            {
                logger.ErrorFormat("DirectSoundCreate8失败, DSERR = {0}", dsErr);
                return(false);
            }

            this.ds8 = Marshal.GetObjectForIUnknown(this.pds8) as IDirectSound8;

            dsErr = this.ds8.SetCooperativeLevel(this.windowHandle, DSSCL.DSSCL_NORMAL);
            if (dsErr != DSERR.DS_OK)
            {
                logger.ErrorFormat("SetCooperativeLevel失败, DSERR = {0}", dsErr);
                return(false);
            }

            #endregion

            #region 创建音频缓冲区对象

            this.wfx = new WAVEFORMATEX()
            {
                nChannels       = (short)this.audioFormat.Channel,
                nSamplesPerSec  = this.audioFormat.SamplesPerSec,
                wBitsPerSample  = (short)this.audioFormat.BitsPerSample,
                nBlockAlign     = (short)this.audioFormat.BlockAlign,
                nAvgBytesPerSec = this.audioFormat.AvgBytesPerSec,
                cbSize          = 0,
                wFormatTag      = DirectSoundHelper.WAVE_FORMAT_PCM
            };

            this.pwfx_free = Utils.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   = audioFormat.AvgBytesPerSec,
                dwReserved      = 0
            };
            this.buffer_size    = audioFormat.AvgBytesPerSec;
            this.min_free_space = this.wfx.nBlockAlign;

            IntPtr pdsb;
            dsErr = this.ds8.CreateSoundBuffer(ref this.dsbd, out pdsb, IntPtr.Zero);
            if (dsErr != DSERR.DS_OK)
            {
                logger.ErrorFormat("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;

            #endregion

            return(true);
        }