Ejemplo n.º 1
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());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Recording has stopped.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _recorder_RecordingStopped(object sender, IO.Audio.StoppedEventArgs e)
        {
            // Lock the client
            lock (_syncObject)
            {
                // Get the number of bytes left in the buffer.
                long length = _audioBuffer.Length;

                // Read audio data in the buffer.
                byte[] buffer = new byte[(int)length];
                int    read   = _audioBuffer.Read(buffer, 0, (int)length);

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

                // Stopped.
                OnStopRecording?.Invoke(this, new EventArgs());
            }
        }