Example #1
0
        private FormatResult checkFormatInternal(AudioFormat format, out SoundIOChannelLayout?layout)
        {
            layout = null;
            if (!Device.SupportsSampleRate(format.SampleRate))
            {
                return(FormatResult.UnsupportedSampleRate);
            }

            bool invalidChannel = true;

            foreach (var l in Device.Layouts)
            {
                if (l.ChannelCount == format.Channels)
                {
                    invalidChannel = false;
                    layout         = l;
                    break;
                }
            }

            if (invalidChannel)
            {
                return(FormatResult.UnsupportedChannel);
            }

            var nativeFormat = Soundio.ToSoundioFormat(format);

            if (nativeFormat == null || !Device.SupportsFormat(nativeFormat.Value))
            {
                return(FormatResult.UnsupportedBitDepth);
            }

            return(FormatResult.Ok);
        }
Example #2
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);
        }
Example #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);
        }