コード例 #1
0
ファイル: AudioPlayer.cs プロジェクト: sandrist/psi
 /// <summary>
 /// Starts playing back audio.
 /// </summary>
 private void OnPipelineRun()
 {
     this.audioDevice = LinuxAudioInterop.Open(
         this.configuration.DeviceName,
         LinuxAudioInterop.Mode.Playback,
         (int)this.configuration.Format.SamplesPerSec,
         this.configuration.Format.Channels,
         LinuxAudioInterop.ConvertFormat(this.configuration.Format));
 }
コード例 #2
0
ファイル: AudioCapture.cs プロジェクト: sandrist/psi
        /// <inheritdoc/>
        public void Start(Action <DateTime> notifyCompletionTime)
        {
            // notify that this is an infinite source component
            notifyCompletionTime(DateTime.MaxValue);

            this.audioDevice = LinuxAudioInterop.Open(
                this.configuration.DeviceName,
                LinuxAudioInterop.Mode.Capture,
                (int)this.configuration.Format.SamplesPerSec,
                this.configuration.Format.Channels,
                LinuxAudioInterop.ConvertFormat(this.configuration.Format));

            this.background = new Thread(new ThreadStart(() =>
            {
                const int blockSize = 256;
                var format          = this.configuration.Format;
                var length          = blockSize * format.BitsPerSample / 8;
                var buf             = new byte[length];

                while (!this.isStopping)
                {
                    try
                    {
                        LinuxAudioInterop.Read(this.audioDevice, buf, blockSize);
                    }
                    catch
                    {
                        if (this.audioDevice != null)
                        {
                            throw;
                        }
                    }

                    // Only create a new buffer if necessary
                    if ((this.buffer.Data == null) || (this.buffer.Length != length))
                    {
                        this.buffer = new AudioBuffer(length, format);
                    }

                    // Copy the data
                    Array.Copy(buf, this.buffer.Data, length);

                    // use the end of the last sample in the packet as the originating time
                    DateTime originatingTime = this.pipeline.GetCurrentTime().AddSeconds((double)length / format.AvgBytesPerSec);

                    // post the data to the output stream
                    this.audioBuffers.Post(this.buffer, originatingTime);
                }
            }))
            {
                IsBackground = true
            };

            this.isStopping = false;
            this.background.Start();
        }
コード例 #3
0
        /// <summary>
        /// Called to start capturing audio from the microphone.
        /// </summary>
        private void OnPipelineStart()
        {
            this.audioDevice = LinuxAudioInterop.Open(
                this.configuration.DeviceName,
                LinuxAudioInterop.Mode.Capture,
                (int)this.configuration.Format.SamplesPerSec,
                this.configuration.Format.Channels,
                LinuxAudioInterop.ConvertFormat(this.configuration.Format));

            new Thread(new ThreadStart(() =>
            {
                const int blockSize = 256;
                var format          = this.configuration.Format;
                var length          = blockSize * format.BitsPerSample / 8;
                var buf             = new byte[length];

                while (this.audioDevice != null)
                {
                    try
                    {
                        LinuxAudioInterop.Read(this.audioDevice, buf, blockSize);
                    }
                    catch (Exception ex)
                    {
                        if (this.audioDevice != null)
                        {
                            throw ex;
                        }
                    }

                    // Only create a new buffer if necessary
                    if ((this.buffer.Data == null) || (this.buffer.Length != length))
                    {
                        this.buffer = new AudioBuffer(length, format);
                    }

                    // Copy the data
                    Array.Copy(buf, this.buffer.Data, length);

                    // use the end of the last sample in the packet as the originating time
                    DateTime originatingTime = this.pipeline.GetCurrentTime().AddSeconds(length / format.AvgBytesPerSec);

                    // post the data to the output stream
                    this.audioBuffers.Post(this.buffer, originatingTime);
                }
            }))
            {
                IsBackground = true
            }.Start();
        }