2009-08-29 7 views
1

VS 2008 SlimDX mars 2009Capturer l'entrée audio du micro à l'aide SlimDX

J'utilise le SlimDX pour capturer l'audio d'entrée et l'afficher dans une barre de progression. J'ai un backgroundworker qui fera la capture dans le fil de fond et mettra à jour la barre de progression.

Cependant, j'entrevois le problème de la valeur à utiliser comme valeur pour mettre à jour la barre de progression.

Tout fonctionne bien et la capture commence. Cependant, l'application échouera lorsque j'essaierai de mettre à jour la barre de progression.

EDIT ==== J'ai modifié mon code dans le DoWork. Cette fois-ci en utilisant Int16 qui donne un nombre rond. Cependant, le problème auquel je suis confronté maintenant est que le buffer va déborder. Il y a quand même de créer un buffer de circulateur qui une fois arrivé à la fin ça recommencera?

Des suggestions?

Un grand merci,

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; 
     } 

Répondre