Ejemplo n.º 1
0
        // bufferSize is a SAMPLE COUNT
        // NOTE: we always capture 16 bits/sample
        public WaveCapture(Guid deviceGuid, int Fs, int bufferSize, int timerInterval)
        {
            CaptureBufferDescription desc = new CaptureBufferDescription();
            desc.BufferBytes = bufferSize * 2;
            desc.ControlEffects = false;
            desc.WaveMapped = true;
            desc.Format = new WaveFormat();
            desc.Format.FormatTag = SlimDX.WaveFormatTag.Pcm;
            desc.Format.SamplesPerSecond = Fs;
            desc.Format.Channels = 1;
            desc.Format.BitsPerSample = 16;
            desc.Format.BlockAlignment = 2;
            desc.Format.AverageBytesPerSecond = Fs * 2;

            buf = new byte[bufferSize * 2];

            capture = new DirectSoundCapture(deviceGuid);
            captureBuffer = new CaptureBuffer(capture, desc);

            timer = new Timer();
            timer.Interval = timerInterval;
            timer.Tick += new EventHandler(timer_Tick);
        }
Ejemplo n.º 2
0
        public bool Init()
        {
            DeviceCollection devices = DirectSoundCapture.GetDevices();
            _Devices = new List<SRecordDevice>();
            _Sources = new List<SoundCardSource>();

            int id = 0;
            foreach (DeviceInformation dev in devices)
            {
                DirectSoundCapture ds = new DirectSoundCapture(dev.DriverGuid);

                SRecordDevice device = new SRecordDevice();
                device.Driver = dev.DriverGuid.ToString();
                device.ID = id;
                device.Name = dev.Description;
                device.Inputs = new List<SInput>();

                SInput inp = new SInput();
                inp.Name = "Default";
                inp.Channels = ds.Capabilities.Channels;

                if (inp.Channels > 2)
                    inp.Channels = 2; //more are not supported in vocaluxe

                device.Inputs.Add(inp);
                _Devices.Add(device);

                id++;
                ds.Dispose();
            }

            _DeviceConfig = _Devices.ToArray();
            _initialized = true;

            return true;
        }
Ejemplo n.º 3
0
        /// <summary>
        ///   Worker thread.
        /// </summary>
        /// 
        private void WorkerThread()
        {
            // Get the selected capture device
            DirectSoundCapture captureDevice = new DirectSoundCapture(device);


            // Set the capture format
            WaveFormat format = new WaveFormat();
            format.Channels = 1;
            format.SamplesPerSecond = sampleRate;
            format.FormatTag = sampleFormat.ToWaveFormat();
            format.BitsPerSample = (short)Signal.GetSampleSize(sampleFormat);
            format.BlockAlignment = (short)(format.BitsPerSample / 8);
            format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlignment;

            // Setup the capture buffer
            CaptureBufferDescription captureBufferDescription = new CaptureBufferDescription();
            captureBufferDescription.Format = format;
            captureBufferDescription.BufferBytes = 2 * desiredCaptureSize * format.BlockAlignment;
            captureBufferDescription.WaveMapped = true;
            captureBufferDescription.ControlEffects = false;

            CaptureBuffer captureBuffer = null;
            NotificationPosition[] notifications = new NotificationPosition[2];

            try
            {
                captureBuffer = new CaptureBuffer(captureDevice, captureBufferDescription);

                // Setup the notification positions
                int bufferPortionSize = captureBuffer.SizeInBytes / 2;
                notifications[0] = new NotificationPosition();
                notifications[0].Offset = bufferPortionSize - 1;
                notifications[0].Event = new AutoResetEvent(false);
                notifications[1] = new NotificationPosition();
                notifications[1].Offset = bufferPortionSize - 1 + bufferPortionSize;
                notifications[1].Event = new AutoResetEvent(false);
                captureBuffer.SetNotificationPositions(notifications);

                // Make a copy of the wait handles
                WaitHandle[] waitHandles = new WaitHandle[notifications.Length];
                for (int i = 0; i < notifications.Length; i++)
                    waitHandles[i] = notifications[i].Event;



                // Start capturing
                captureBuffer.Start(true);


                if (sampleFormat == SampleFormat.Format32BitIeeeFloat)
                {
                    float[] currentSample = new float[desiredCaptureSize];

                    while (!stopEvent.WaitOne(0, true))
                    {
                        int bufferPortionIndex = WaitHandle.WaitAny(waitHandles);
                        captureBuffer.Read(currentSample, 0, currentSample.Length, bufferPortionSize * bufferPortionIndex);
                        OnNewFrame(currentSample);
                    }
                }
                else if (sampleFormat == SampleFormat.Format16Bit)
                {
                    short[] currentSample = new short[desiredCaptureSize];

                    while (!stopEvent.WaitOne(0, true))
                    {
                        int bufferPortionIndex = WaitHandle.WaitAny(waitHandles);
                        captureBuffer.Read(currentSample, 0, currentSample.Length, bufferPortionSize * bufferPortionIndex);
                        OnNewFrame(currentSample);
                    }
                }
            }
            catch (Exception ex)
            {
                if (AudioSourceError != null)
                    AudioSourceError(this, new AudioSourceErrorEventArgs(ex.Message));
                else throw;
            }
            finally
            {
                if (captureBuffer != null)
                {
                    captureBuffer.Stop();
                    captureBuffer.Dispose();
                }

                if (captureDevice != null)
                    captureDevice.Dispose();

                for (int i = 0; i < notifications.Length; i++)
                    if (notifications[i].Event != null)
                        notifications[i].Event.Close();
            }
        }
Ejemplo n.º 4
0
            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                {
                    this.Stop();

                    if (this.captureDevice != null)
                    {
                        this.captureDevice.Dispose();
                        this.captureDevice = null;
                    }
                }
            }
Ejemplo n.º 5
0
            public void Start()
            {
                if (this.running)
                {
                    throw new InvalidOperationException();
                }

                if (this.captureDevice == null)
                {
                    this.captureDevice = new DirectSoundCapture(guid);
                }

                this.waveFormat.FormatTag = WaveFormatTag.Pcm; // Change to WaveFormatTag.IeeeFloat for float
                this.waveFormat.BitsPerSample = 16; // Set this to 32 for float
                this.waveFormat.BlockAlignment = (short)(channels * (waveFormat.BitsPerSample / 8));
                this.waveFormat.Channels = this.channels;
                this.waveFormat.SamplesPerSecond = (int)(this.SampleRateKHz * 1000D);
                this.waveFormat.AverageBytesPerSecond =
                    this.waveFormat.SamplesPerSecond *
                    this.waveFormat.BlockAlignment;

                this.bufferPortionCount = 2;

                this.bufferDescription.BufferBytes = this.bufferSize * sizeof(short) * bufferPortionCount * this.channels;
                this.bufferDescription.Format = this.waveFormat;
                this.bufferDescription.WaveMapped = false;

                this.buffer = new CaptureBuffer(this.captureDevice, this.bufferDescription);

                this.bufferPortionSize = this.buffer.SizeInBytes / this.bufferPortionCount;
                this.notifications = new List<NotificationPosition>();

                for (int i = 0; i < this.bufferPortionCount; i++)
                {
                    NotificationPosition notification = new NotificationPosition();
                    notification.Offset = this.bufferPortionCount - 1 + (bufferPortionSize * i);
                    notification.Event = new AutoResetEvent(false);
                    this.notifications.Add(notification);
                }

                this.buffer.SetNotificationPositions(this.notifications.ToArray());
                this.waitHandles = new WaitHandle[this.notifications.Count];

                for (int i = 0; i < this.notifications.Count; i++)
                {
                    this.waitHandles[i] = this.notifications[i].Event;
                }

                this.captureThread = new Thread(new ThreadStart(this.CaptureThread));
                this.captureThread.IsBackground = true;

                this.running = true;
                this.captureThread.Start();
            }