コード例 #1
0
        /// <summary>
        /// Initialises the WaveOut device
        /// </summary>
        /// <param name="waveProvider">WaveProvider to play</param>
        public void Init(IWaveProvider waveProvider)
        {
            if (playbackState != PlaybackState.Stopped)
            {
                throw new InvalidOperationException("Can't re-initialize during playback");
            }

            if (_connectionHandle != IntPtr.Zero)
            {
                // normally we don't allow calling Init twice, but as experiment, see if we can clean up and go again
                // try to allow reuse of this waveOut device
                // n.b. risky if Playback thread has not exited
                DisposeBuffers();
                CloseWaveOut();
            }

            _waveStream = waveProvider;
            _bufferSize =
                waveProvider.WaveFormat.ConvertLatencyToByteSize(
                    (DesiredLatency + NumberOfBuffers - 1) / NumberOfBuffers);
            _buffer = new byte[_bufferSize];

            lock (waveOutLock)
            {
                var        sampleSpec = FormatConverter.Convert(_waveStream.WaveFormat);
                ChannelMap?channelMap = null;
                BufferAttr?bufferAttr = null;
                _connectionHandle = _api.pa_simple_new(
                    _connection.ServerName,
                    _connection.ApplicationName,
                    StreamDirection.Playback,
                    _connection.Device,
                    _connection.StreamName,
                    ref sampleSpec,
                    ref channelMap,
                    ref bufferAttr,
                    out var error
                    );

                if (_connectionHandle == IntPtr.Zero)
                {
                    throw new Exception("pa_simple_new error");
                }
            }

            playbackState = PlaybackState.Stopped;
        }
コード例 #2
0
        private void OpenWaveInDevice()
        {
            CloseWaveInDevice();

            var        sampleSpec       = FormatConverter.Convert(WaveFormat);
            ChannelMap?channelMap       = null;
            BufferAttr?bufferAttributes = null;

            _connectionHandle = _api.pa_simple_new(
                _connection.ServerName,
                _connection.ApplicationName,
                StreamDirection.Record,
                _connection.Device,
                _connection.StreamName,
                ref sampleSpec,
                ref channelMap,
                ref bufferAttributes,
                out var error
                );
            if (_connectionHandle == IntPtr.Zero)
            {
                throw new Exception("Not able to create recording pulseAudio connection");
            }
        }