Exemplo n.º 1
0
        /// <summary>Opens a device for audio recording.</summary>
        /// <param name="deviceName">The device name.</param>
        /// <param name="frequency">The frequency that the data should be captured at.</param>
        /// <param name="sampleFormat">The requested capture buffer format.</param>
        /// <param name="bufferSize">The size of OpenAL's capture internal ring-buffer. This value expects number of samples, not bytes.</param>
        public AudioCapture(string deviceName, int frequency, ALFormat sampleFormat, int bufferSize)
        {
            if (!AudioDeviceEnumerator.IsOpenALSupported)
            {
                throw new DllNotFoundException("openal32.dll");
            }
            if (frequency <= 0)
            {
                throw new ArgumentOutOfRangeException("frequency");
            }
            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException("bufferSize");
            }

            // Try to open specified device. If it fails, try to open default device.
            device_name = deviceName;
            Handle      = Alc.CaptureOpenDevice(deviceName, frequency, sampleFormat, bufferSize);

            if (Handle == IntPtr.Zero)
            {
                Debug.WriteLine(ErrorMessage(deviceName, frequency, sampleFormat, bufferSize));
                device_name = "IntPtr.Zero";
                Handle      = Alc.CaptureOpenDevice(null, frequency, sampleFormat, bufferSize);
            }

            if (Handle == IntPtr.Zero)
            {
                Debug.WriteLine(ErrorMessage("IntPtr.Zero", frequency, sampleFormat, bufferSize));
                device_name = AudioDeviceEnumerator.DefaultRecordingDevice;
                Handle      = Alc.CaptureOpenDevice(AudioDeviceEnumerator.DefaultRecordingDevice, frequency, sampleFormat, bufferSize);
            }

            if (Handle == IntPtr.Zero)
            {
                // Everything we tried failed. Capture may not be supported, bail out.
                Debug.WriteLine(ErrorMessage(AudioDeviceEnumerator.DefaultRecordingDevice, frequency, sampleFormat, bufferSize));
                device_name = "None";

                throw new AudioDeviceException("All attempts to open capture devices returned IntPtr.Zero. See debug log for verbose list.");
            }

            // handle is not null, check for some Alc Error
            CheckErrors();

            SampleFormat    = sampleFormat;
            SampleFrequency = frequency;
        }
Exemplo n.º 2
0
 public AudioCapture(string deviceName, int frequency, ALFormat sampleFormat, int bufferSize)
 {
     if (!AudioDeviceEnumerator.IsOpenALSupported)
     {
         throw new DllNotFoundException("soft_oal.dll");
     }
     if (frequency <= 0)
     {
         throw new ArgumentOutOfRangeException("frequency");
     }
     if (bufferSize <= 0)
     {
         throw new ArgumentOutOfRangeException("bufferSize");
     }
     this.device_name = deviceName;
     this.Handle      = Alc.CaptureOpenDevice(deviceName, frequency, sampleFormat, bufferSize);
     if (this.Handle == IntPtr.Zero)
     {
         this.device_name = "IntPtr.Zero";
         this.Handle      = Alc.CaptureOpenDevice((string)null, frequency, sampleFormat, bufferSize);
     }
     if (this.Handle == IntPtr.Zero)
     {
         this.device_name = AudioDeviceEnumerator.DefaultRecordingDevice;
         this.Handle      = Alc.CaptureOpenDevice(AudioDeviceEnumerator.DefaultRecordingDevice, frequency, sampleFormat, bufferSize);
     }
     if (this.Handle == IntPtr.Zero)
     {
         this.device_name = "None";
         throw new AudioDeviceException("All attempts to open capture devices returned IntPtr.Zero. See debug log for verbose list.");
     }
     else
     {
         this.CheckErrors();
         this.SampleFormat    = sampleFormat;
         this.SampleFrequency = frequency;
     }
 }