Exemplo n.º 1
0
        private void initInternal(AudioFormat format)
        {
            if (Device == null)
            {
                throw new Exception("No device is selected");
            }

            if (Device.ProbeError != 0)
            {
                throw new Exception($"Probe Error : {Device.ProbeError}");
            }

            var native = Soundio.ToSoundioFormat(format);

            if (!native.HasValue)
            {
                throw new NotSupportedException("Format is not supported : " + format);
            }

            _instream                  = Device.CreateInStream();
            _instream.Format           = native.Value;
            _instream.SampleRate       = format.SampleRate;
            _instream.ReadCallback     = ReadCallback;
            _instream.OverflowCallback = () => Overflow?.Invoke(this, EventArgs.Empty);
            _instream.ErrorCallback    = () => UnrecoverableError?.Invoke(this, EventArgs.Empty);
            _instream.SoftwareLatency  = DesiredLatency.TotalSeconds;
            _instream.Open();

            // Open後にチャンネルは設定しないと動作しない模様
            if (Device.CurrentLayout.ChannelCount != format.Channels)
            {
                checkFormatInternal(format, out var channelLayout);
                if (!channelLayout.HasValue)
                {
                    throw new NotSupportedException("No suitable channel layout found : " + format.Channels);
                }

                _instream.Layout = channelLayout.Value;
            }
            _instream.SoftwareLatency = DesiredLatency.TotalSeconds;

            Format          = Soundio.ToManagedFormat(_instream.Format, _instream.SampleRate, _instream.Layout.ChannelCount);
            SoftwareLatency = TimeSpan.FromSeconds(_instream.SoftwareLatency);


            var bytesPerSample = _instream.BytesPerSample;
            var capacity       = Format.SampleRate * Format.Channels * bytesPerSample *
                                 _bufferDuration.TotalSeconds;

            _ringBuffer = new RingBuffer <byte>((uint)capacity);
        }
Exemplo n.º 2
0
        public void Start()
        {
            _logger.Info("Запуск процессора сообщений");
            _messageCounter       = 0;
            _reader.DataReceived += (s, e) => Task.Run(() => ProcessMessage(e.Data));
            _reader.ReadError    += (s, e) => UnrecoverableError?.Invoke(this, e);

            try
            {
                _reader.Open(_logRecordFormat);
                _publisher.Open();
                _timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(Properties.Settings.Default.PublishRate));
            }
            catch (Exception ex)
            {
                UnrecoverableError?.Invoke(this, new ErrorEventArgs(ex));
            }
        }
Exemplo n.º 3
0
        private void initInternal(AudioFormat format)
        {
            if (Device == null)
            {
                throw new Exception("No device is selected");
            }

            if (Device.ProbeError != 0)
            {
                throw new OutputInitializationException($"Probe Error : {Device.ProbeError}");
            }

            _outstream = Device.CreateOutStream();
            _outstream.WriteCallback     = (min, max) => write_callback(_outstream, min, max);
            _outstream.UnderflowCallback = () => Underflow?.Invoke(this, new UnderflowEventArgs(null));
            _outstream.ErrorCallback     = () => UnrecoverableError?.Invoke(this, EventArgs.Empty);
            _outstream.SampleRate        = format.SampleRate;
            _outstream.SoftwareLatency   = DesiredLatency.TotalSeconds;

            var soundioFormat = Soundio.ToSoundioFormat(format);

            _outstream.Format = soundioFormat ?? SoundIOFormat.Invalid;

            if (_outstream.LayoutErrorMessage != null)
            {
                var msg = _outstream.LayoutErrorMessage;
                Console.WriteLine($"Channel Layout Error : {msg}");
            }

            _outstream.Open();
            _api.FlushEvents();

            Format          = Soundio.ToManagedFormat(_outstream.Format, _outstream.SampleRate, _outstream.Layout.ChannelCount);
            SoftwareLatency = TimeSpan.FromSeconds(_outstream.SoftwareLatency);

            var bytesPerSample = _outstream.BytesPerSample;
            var capacity       = Format.SampleRate * Format.Channels * bytesPerSample *
                                 _bufferDuration.TotalSeconds;

            _ringBuffer = new RingBuffer <byte>((uint)capacity);
        }
Exemplo n.º 4
0
        private void PublishDataPoints()
        {
            if (_reader.MessageCounter == _messageCounter)
            {
                UnrecoverableError?.Invoke(this, new ErrorEventArgs(new FatalException("Число сообщений не изменилось")));
            }

            _messageCounter = _reader.MessageCounter;

            foreach (var queue in _logQueues)
            {
                lock (_locker)
                {
                    if (queue.Value.Count > 0)
                    {
                        _logger.Info("Отправка {0} точек по логу {1}", queue.Value.Count, queue.Key);
                        _publisher.Publish(_logTypes[queue.Key], queue.Value);
                        queue.Value.Clear();
                    }
                }
            }
        }