use std::env;
use std::time::Duration;
use sapi_lite::audio::{AudioFormat, AudioStream, BitRate, Channels, SampleRate};
use sapi_lite::stt::{Context, Grammar, RecognitionInput, Recognizer, Rule, SyncContext};
fn create_grammar(ctx: &Context) -> Grammar {
ctx.grammar_builder()
.add_rule(&Rule::sequence(vec![
&Rule::text("Harry Potter and the"),
&Rule::choice(vec![
&Rule::semantic(1, &Rule::text("Philosopher's Stone")),
&Rule::semantic(2, &Rule::text("Chamber of Secrets")),
&Rule::semantic(3, &Rule::text("Prisoner of Azkaban")),
&Rule::semantic(4, &Rule::text("Goblet of Fire")),
&Rule::semantic(5, &Rule::text("Order of the Phoenix")),
&Rule::semantic(6, &Rule::text("Half-Blood Prince")),
&Rule::semantic(7, &Rule::text("Deathly Hallows")),
]),
]))
.build()
.unwrap()
}
fn main() {
let path = {
if let Some(arg) = env::args().nth(1) {
arg
} else {
println!("Please specify the input pathname on the command line.");
return;
}
};
sapi_lite::initialize().unwrap();
let audio_fmt = AudioFormat {
sample_rate: SampleRate::Hz44100,
bit_rate: BitRate::Bits8,
channels: Channels::Stereo,
};
let stream = AudioStream::open_file(path, &audio_fmt).unwrap();
let recog = Recognizer::new().unwrap();
recog
.set_input(RecognitionInput::Stream(stream), false)
.unwrap();
let ctx = SyncContext::new(&recog).unwrap();
let grammar = create_grammar(&ctx);
grammar.set_enabled(true).unwrap();
if let Some(phrase) = ctx.recognize(Duration::from_secs(5)).unwrap() {
println!(
"\"{}\" is the book #{} in the series.",
phrase.text.to_string_lossy(),
phrase.semantics[0].value.as_int().unwrap()
);
} else {
println!("I don't recognize that Harry Potter book.");
}
sapi_lite::finalize();
}