毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> net技术 >> 正文

C#中如何使用DirectSound来获取麦克音量 第2页

更新时间:2012-9-4:  来源:毕业论文

            if (state == State.Recording || state == State.PreRecording)
            {
                secondBuffer.Add(CaptureData);
            }          
        }
        private void CreateCaptureBuffer()
        {
            CaptureBufferDescription bufferdescription = new CaptureBufferDescription();
            if (null != mNotify)
            {
                mNotify.Dispose();
                mNotify = null;
            }
            if (null != mRecBuffer)
            {
                mRecBuffer.Dispose();
                mRecBuffer = null;
            }
            // set notification's size: 1 second
            mNotifySize = (1024 > mWavFormat.AverageBytesPerSecond / 8) ? 1024 : (mWavFormat.AverageBytesPerSecond / 8);
            mNotifySize -= mNotifySize % mWavFormat.BlockAlign;
            // set size of buffer
            mBufferSize = mNotifySize * cNotifyNum;

            bufferdescription.BufferBytes = mBufferSize;
            bufferdescription.Format = mWavFormat;

            mRecBuffer = new CaptureBuffer(bufferdescription, mCapDev);
            mNextCaptureOffset = 0;
        }
        private bool InitNotifications()
        {
            if (null == mRecBuffer)
            {
                MessageBox.Show("Record buffer is not found!");
                return false;
            }
            mNotificationEvent = new AutoResetEvent(false);
            if (null == mNotifyThread)
            {
                mNotifyThread = new Thread(new ThreadStart(WaitThread));
                mNotifyThread.Start();
            }
            BufferPositionNotify[] PositionNotify = new BufferPositionNotify[cNotifyNum + 1];
            for (int i = 0; i < cNotifyNum; i++)
            {
                PositionNotify[i].Offset = (mNotifySize * i) + mNotifySize - 1;
                PositionNotify[i].EventNotifyHandle = mNotificationEvent.Handle;
            }
            mNotify = new Notify(mRecBuffer);
            mNotify.SetNotificationPositions(PositionNotify, cNotifyNum);

            return true;
        }
        private bool InitCaptureDevice()
        {
            CaptureDevicesCollection devices = new CaptureDevicesCollection();
            Guid deviceGuid = Guid.Empty;
            if (devices.Count > 0)
                deviceGuid = devices[0].DriverGuid;
            else
            {
                MessageBox.Show("No device to capture wave!");
                return false;
            }
            try
            {
                mCapDev = new Capture(deviceGuid);
            }
            catch (DirectXException e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
            return true;
        }
        private WaveFormat CreateWaveFormat()
        {
            WaveFormat format = new WaveFormat();
            format.FormatTag = WaveFormatTag.Pcm;   // PCM
            format.SamplesPerSecond = 16000;        // 16KHz
            format.BitsPerSample = 16;              // 16Bit
            format.Channels = 1;                    // Mono
            format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));
            format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond;
            return format;

        }
        private void WaitThread()
        {
            while (true)
            {
                // waiting for notification
                mNotificationEvent.WaitOne(Timeout.Infinite, true);
                // process data captured
                RecordCapturedData();
            }
        }
        #endregion
    }

说的是mixer的大小 
还是采集到的电平值? 

第一个用api,网上有很多资料 

http:-//dev.yesky.-com/386/2252386.shtml

最后的解决方法就是计算缓冲区波形数据,算法是找了一些开源项目,然后分析这些类似项目的算法得到的。
大体算法如下,具体的项目文档我会在近期整理出来分享给大家哈:


C# code private void RecordCapturedData()
        {
            byte[] CaptureData = null;
            int ReadPos;
            int CapturePos;
            int LockSize;
            mRecBuffer.GetCurrentPosition(out CapturePos, out ReadPos);
            LockSize = ReadPos - mNextCaptureOffset;
            if (LockSize < 0)
                LockSize += mBufferSize;
            LockSize -= (LockSize % mNotifySize);
            if (0 == LockSize)
                return;
            CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize);
            mNextCaptureOffset += CaptureData.Length;
            mNextCaptureOffset %= mBufferSize;

上一页  [1] [2] [3] 下一页

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。