The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Speech::Recognition::Vosk::Recognizer - offline speech recognition using Vosk

SYNOPSIS

  use Speech::Recognition::Vosk::Recognizer;

  my $recognizer = Speech::Recognition::Vosk::Recognizer->new(
      model_dir => 'model-en',
      sample_rate => 44100,
  );

  # record from PulseAudio device 11
  open my $voice, 'ffmpeg -hide_banner -loglevel error -nostats -f pulse -i 11 -t 30 -ac 1 -ar 44100 -f s16le - |';
  binmode $voice, ':raw';

  while( ! eof($voice)) {
      read(STDIN, my $buf, 3200);

      my $complete = $recognizer->accept_waveform($buf);
      my $spoken;
      if( $complete ) {
          $spoken = $recognizer->result();
      } else {
          $spoken = $recognizer->partial_result();
      }
      if( $info->{text}) {
          print $info->{text},"\n";
      } else {
          local $| = 1;
          print $info->{partial}, "\r";
      };
  };
  my $spoken = $recognizer->final_result();
  print $info->{text},"\n";

METHODS

->new

->accept_waveform

  read($voice, my $buf, 3200);
  my $complete = $recognizer->accept_waveform($buf);

Feed more data to the recognizer. Returns if a pause in the speech was detected and a completed utterance is available.

->result

  my $spoken = $recognizer->result();
  print $spoken->{text};

Returns a hashref containing the recognized text.

->partial_result

  my $spoken = $recognizer->partial_result();
  print "$spoken->{partial}\r";

Returns a hashref containing the recognized text so far. The text may change when more data is collected.

->final_result

  my $spoken = $recognizer->final_result();
  print $spoken->{text};

Returns a hashref containing the recognized text.

Call this method at the end of the input to flush any pending data.

SEE ALSO

Vosk - https://alphacephei.com/vosk/ , https://github.com/alphacep/vosk-api

MODELS

Download pretrained models from

https://alphacephei.com/vosk/models