Exemplo n.º 1
0
        /// <summary>
        /// Dispose(bool disposing) executes in two distinct scenarios.
        /// If disposing equals true, the method has been called directly
        /// or indirectly by a user's code. Managed and unmanaged resources
        /// can be disposed.
        /// If disposing equals false, the method has been called by the
        /// runtime from inside the finalizer and you should not reference
        /// other objects. Only unmanaged resources can be disposed.
        /// </summary>
        private void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!this._disposed)
            {
                // Note disposing has been done.
                _disposed = true;

                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    if (_audioBuffer != null)
                    {
                        _audioBuffer.Dispose();
                    }

                    if (_recorder != null)
                    {
                        _recorder.Dispose();
                    }

                    if (_client != null)
                    {
                        _client.Dispose();
                    }
                }

                _audioBuffer = null;
                _recorder    = null;
                _client      = null;
                _syncObject  = null;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// On connection open.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnConnected(object sender, EventArgs e)
        {
            // Create the recorder and audio buffer.
            _recorder = new IO.Audio.WaveRecorder(_device);
            _recorder.DataAvailable     += _recorder_DataAvailable;
            _recorder.RecordingStopped  += _recorder_RecordingStopped;
            _recorder.BufferMilliseconds = _bufferMilliseconds;

            // Open the audio buffer.
            _audioBuffer = new IO.Stream.StreamBufferBase();
            _recorder.Open(_audioBuffer, new IO.Audio.Wave.WaveFormatProvider(_waveSampleRate, _waveBitsPerSample, _waveChannels), IO.Audio.AudioRecordingFormat.WaveInEvent);

            // Create a default audio sample rate.
            // Write the first header data to the buffer.
            Nequeo.IO.Audio.WaveStructure waveStructure = Nequeo.IO.Audio.WaveStructure.CreateDefaultStructure(_waveChannels, _waveSampleRate, _waveBitsPerSample, new byte[0]);
            Nequeo.IO.Audio.WaveFormat    waveFormat    = new Nequeo.IO.Audio.WaveFormat();
            bool ret = waveFormat.Write(_audioBuffer, waveStructure);

            // Get the number of wave header bytes.
            long headerSize = _audioBuffer.Length;

            // Lock the client
            lock (_syncObject)
            {
                byte[] buffer = new byte[(int)headerSize];
                int    read   = _audioBuffer.Read(buffer, 0, (int)headerSize);

                // Send the header data to the speech server.
                _client.Send(buffer.Take(read).ToArray(), false);
            }

            // Start recording.
            _recorder.Start();
            OnRecording?.Invoke(this, new EventArgs());
        }
Exemplo n.º 3
0
        /// <summary>
        /// On connection closed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDisconnected(object sender, EventArgs e)
        {
            if (_recorder != null)
            {
                _recorder.Close();
            }

            if (_audioBuffer != null)
            {
                _audioBuffer.Dispose();
            }

            if (_recorder != null)
            {
                _recorder.Dispose();
            }

            _audioBuffer = null;
            _recorder    = null;
        }