Streaming input to System.Speech.Recognition.SpeechRecognitionEngine

前端 未结 5 1381
失恋的感觉
失恋的感觉 2020-11-30 06:03

I am trying to do \"streaming\" speech recognition in C# from a TCP socket. The problem I am having is that SpeechRecognitionEngine.SetInputToAudioStream() seems to require

5条回答
  •  鱼传尺愫
    2020-11-30 06:37

    I got live speech recognition working by overriding the stream class:

    class SpeechStreamer : Stream
    {
        private AutoResetEvent _writeEvent;
        private List _buffer;
        private int _buffersize;
        private int _readposition;
        private int _writeposition;
        private bool _reset;
    
        public SpeechStreamer(int bufferSize)
        {
            _writeEvent = new AutoResetEvent(false);
             _buffersize = bufferSize;
             _buffer = new List(_buffersize);
             for (int i = 0; i < _buffersize;i++ )
                 _buffer.Add(new byte());
            _readposition = 0;
            _writeposition = 0;
        }
    
        public override bool CanRead
        {
            get { return true; }
        }
    
        public override bool CanSeek
        {
            get { return false; }
        }
    
        public override bool CanWrite
        {
            get { return true; }
        }
    
        public override long Length
        {
            get { return -1L; }
        }
    
        public override long Position
        {
            get { return 0L; }
            set {  }
        }
    
        public override long Seek(long offset, SeekOrigin origin)
        {
            return 0L;
        }
    
        public override void SetLength(long value)
        {
    
        }
    
        public override int Read(byte[] buffer, int offset, int count)
        {
            int i = 0;
            while (i= _writeposition)
                {
                    _writeEvent.WaitOne(100, true);
                    continue;
                }
                buffer[i] = _buffer[_readposition+offset];
                _readposition++;
                if (_readposition == _buffersize)
                {
                    _readposition = 0;
                    _reset = false;
                }
                i++;
            }
    
            return count;
        }
    
        public override void Write(byte[] buffer, int offset, int count)
        {
            for (int i = offset; i < offset+count; i++)
            {
                _buffer[_writeposition] = buffer[i];
                _writeposition++;
                if (_writeposition == _buffersize)
                {
                    _writeposition = 0;
                    _reset = true;
                }
            }
            _writeEvent.Set();
    
        }
    
        public override void Close()
        {
            _writeEvent.Close();
            _writeEvent = null;
            base.Close();
        }
    
        public override void Flush()
        {
    
        }
    }
    

    ... and using an instance of that as the stream input to the SetInputToAudioStream method. As soon as the stream returns a length or the returned count is less than that requested the recognition engine thinks the input has finished. This sets up a circular buffer that never finishes.

提交回复
热议问题