use hound;
use std::rc::Rc;
use ttspico as pico;
fn main() {
let sys = pico::System::new(4 * 1024 * 1024).expect("Could not init system");
let ta_res =
pico::System::load_resource(Rc::clone(&sys), "ttspico-sys/build/pico/lang/en-US_ta.bin")
.expect("Failed to load TA");
let sg_res = pico::System::load_resource(
Rc::clone(&sys),
"ttspico-sys/build/pico/lang/en-US_lh0_sg.bin",
)
.expect("Failed to load SG");
println!(
"TA: {}, SG: {}",
ta_res.borrow().name().unwrap(),
sg_res.borrow().name().unwrap()
);
let voice = pico::System::create_voice(sys, "TestVoice").expect("Failed to create voice");
voice
.borrow_mut()
.add_resource(ta_res)
.expect("Failed to add TA to voice");
voice
.borrow_mut()
.add_resource(sg_res)
.expect("Failed to add SG to voice");
let mut engine = unsafe { pico::Voice::create_engine(voice).expect("Failed to create engine") };
let mut text_bytes: &[u8] = b"1, 2, 3, Hello Rust!\0"; while text_bytes.len() > 0 {
let n_put = engine
.put_text(text_bytes)
.expect("pico_putTextUtf8 failed");
text_bytes = &text_bytes[n_put..];
}
let mut pcm_data = vec![0i16; 0];
let mut pcm_buf = [0i16; 1024];
'tts: loop {
let (n_written, status) = engine
.get_data(&mut pcm_buf[..])
.expect("pico_getData error");
pcm_data.extend(&pcm_buf[..n_written]);
if status == ttspico::EngineStatus::Idle {
break 'tts;
}
}
let spec = hound::WavSpec {
channels: 1,
sample_rate: 16000,
bits_per_sample: 16,
sample_format: hound::SampleFormat::Int,
};
let mut writer = hound::WavWriter::create("speech.wav", spec).unwrap();
for sample in pcm_data {
writer.write_sample(sample).unwrap();
}
}