1

VS 2008 SlimDX 2009 年 3 月

我正在使用 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;
        }
4

1 回答 1

0

看看这个例子,也许它会对你有所帮助。

于 2010-11-24T13:16:26.980 回答