Example #1
0
        internal AudioCapture(AudioDevice audioDevice, string deviceName, int sampleRate, AudioFormat bitDepth, int channels, int bufferSize) : base(audioDevice)
        {
            if (deviceName != null && !AvailableDevices.Contains(deviceName))
            {
                throw new InvalidOperationException(string.Format("CaptureDevice \"{0}\" does not exist.", deviceName));
            }

            if (sampleRate <= 0 || sampleRate >= 44100)
            {
                throw new ArgumentOutOfRangeException("sampleRate", "SampleRate must be larger than 0 and smaller or equals to 44100.");
            }

            if (bitDepth != AudioFormat.Byte8 || bitDepth != AudioFormat.Short16)
            {
                throw new ArgumentException("BitDepth must be either Byte8 oder Short16.", "bitDepth");
            }

            if (channels <= 0 || channels > 2)
            {
                throw new ArgumentOutOfRangeException("channels", "AudioCapture only supports 1 or 2 channels.");
            }

            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException("bufferSize", "BufferSize must be larger than 0.");
            }

            var format = EnumConverter.GetFormat(bitDepth, channels);

            this.handle = new AudioCaptureInternal(deviceName, sampleRate, format, bufferSize);
        }
Example #2
0
        public AudioCapture()
        {
            SampleFormat = ALFormat.Mono16;
            BufferLength = 10;
            SampleRate   = 44100;

            source = Observable.Create <Mat>((observer, cancellationToken) =>
            {
                return(Task.Factory.StartNew(() =>
                {
                    var sampleRate = SampleRate;
                    var bufferLength = BufferLength;
                    var sampleFormat = SampleFormat;
                    var channelCount = SampleFormat == ALFormat.Stereo16 ? 2 : 1;
                    var bufferSize = (int)Math.Ceiling(sampleRate * bufferLength / 1000);
                    var readBuffer = sampleFormat == ALFormat.Stereo16 ? new Mat(bufferSize, channelCount, Depth.S16, 1) : null;
                    var captureInterval = TimeSpan.FromMilliseconds((int)(bufferLength / 2 + 0.5));
                    var captureBufferSize = bufferSize * 4;

                    lock (captureLock)
                    {
                        using (var capture = new OpenTK.Audio.AudioCapture(DeviceName, sampleRate, sampleFormat, captureBufferSize))
                            using (var captureSignal = new ManualResetEvent(false))
                            {
                                capture.Start();
                                while (!cancellationToken.IsCancellationRequested)
                                {
                                    while (capture.AvailableSamples >= bufferSize)
                                    {
                                        var buffer = new Mat(channelCount, bufferSize, Depth.S16, 1);
                                        if (readBuffer != null)
                                        {
                                            capture.ReadSamples(readBuffer.Data, bufferSize);
                                            CV.Transpose(readBuffer, buffer);
                                        }
                                        else
                                        {
                                            capture.ReadSamples(buffer.Data, bufferSize);
                                        }
                                        observer.OnNext(buffer);
                                    }

                                    captureSignal.WaitOne(captureInterval);
                                }
                                capture.Stop();
                            }
                    }
                },
                                             cancellationToken,
                                             TaskCreationOptions.LongRunning,
                                             TaskScheduler.Default));
            })
                     .PublishReconnectable()
                     .RefCount();
        }
Example #3
0
        /// <summary>
        /// コンストラクタ
        /// </summary>
        public Program(int width, int height, OpenTK.Graphics.GraphicsMode mode, string title) : base(width, height, mode, title)
        {
            IList <string> devices = OpenTK.Audio.AudioCapture.AvailableDevices;

            foreach (string device in devices)
            {
                Console.WriteLine(device);
            }

            // オーディオキャプチャ
            this.AudioCapture = new OpenTK.Audio.AudioCapture();
            this.AudioCapture.Start();

            // デバッグ
            Console.WriteLine("[INFO] " + this.AudioCapture.AvailableSamples);
            Console.WriteLine("[INFO] " + this.AudioCapture.CurrentDevice);
            Console.WriteLine("[INFO] " + this.AudioCapture.CurrentError);
            Console.WriteLine("[INFO] " + this.AudioCapture.SampleFormat);
            Console.WriteLine("[INFO] " + this.AudioCapture.SampleFrequency);
            Console.WriteLine("[INFO] Start Audio Capture");
        }