使用SlimDX从麦克风捕获音频输入

时间:2009-08-29 11:29:39

标签: c# slimdx

VS 2008 SlimDX march 2009

我正在使用SlimDX捕获输入音频并将其显示在进度条中。我有一个后台工作人员将在后台线程中进行捕获并更新进度条。

但是,我遇到的问题是什么值作为更新进度条的值。

一切正常,捕捉开始。但是,当我尝试更新进度条时,应用程序将失败。

编辑==== 我在DoWork中编辑了我的代码。这次使用Int16给出一个圆数。但是,我现在面临的问题是缓冲区会溢出。无论如何要创建一个循环缓冲区,一旦它到达终点,它将重新开始?

有什么建议吗?

非常感谢,

 private DirectSoundCapture soundCapture;
        private WaveFormat wavFormat;
        private CaptureBufferDescription captureBufDescription;
        private CaptureBuffer captureBuff;

        // stopCapturing - flag to stop capturing
        bool stopCapturing = false;

        private void AudioCapture_Load(object sender, EventArgs e)
        {
            this.FillProperties();   
        }

        // Fill wave format properties
        private void FillProperties()
        {
            // Declare a wave audio format to use in getting the input.
            soundCapture = new DirectSoundCapture();

            // Wave Format
            wavFormat = new WaveFormat();
            // 
            wavFormat.FormatTag = SlimDX.WaveFormatTag.IeeeFloat;
            wavFormat.BitsPerSample = 32;
            wavFormat.BlockAlignment = (short)(wavFormat.BitsPerSample / 8);
            wavFormat.Channels = 1;
            wavFormat.SamplesPerSecond = 44100;
            wavFormat.AverageBytesPerSecond =
                wavFormat.SamplesPerSecond * wavFormat.BlockAlignment * wavFormat.Channels;

            // Capture buffer description
            captureBufDescription = new CaptureBufferDescription();
            // 
            captureBufDescription.ControlEffects = true;
            captureBufDescription.WaveMapped = true;
            captureBufDescription.BufferBytes = 8192;
            captureBufDescription.Format = wavFormat;

            captureBuff = new CaptureBuffer(soundCapture, captureBufDescription);   
        }

        // Run capture in background thread to keep the form active
        private void bgwAudioInput_DoWork(object sender, DoWorkEventArgs e)
        {
        Int16[] samples = new Int16[8192 / sizeof(Int16)];
        Int16 audioValue = 0;
        int i = 0;

        captureBuff.Start(true);

        while (captureAudio)
        {
            if (!this.bgwAudioInput.CancellationPending)
            {
                captureBuff.Read(samples, 0, true);
                audioValue = Math.Abs(samples[captureBuff.CurrentReadPosition]);
                this.bgwAudioInput.ReportProgress(audioValue);
            }
        }
        }

        // Start capturing the input audio from the microphone
        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnStop.Enabled = true;

            this.bgwAudioInput.RunWorkerAsync();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            captureBuff.Stop();
            // Exit while loop
            this.bgwAudioInput.CancelAsync();
            stopCapturing = false;

            btnStop.Enabled = false;
            btnStart.Enabled = true;
        }

1 个答案:

答案 0 :(得分:0)

看看this example,也许它会帮助你。