use rodio::Decoder;
use std::{fs, io::BufReader, path::Path};
use whisper_rs::{self, WhisperContext, WhisperContextParameters};
#[cfg(feature = "experimental")]
pub fn transcription<MP: AsRef<Path>, AP: AsRef<Path>>(
model_path: MP,
audio_path: AP,
lang: Option<&str>,
) -> String {
let ctx = WhisperContext::new_with_params(
model_path.as_ref().to_str().unwrap(),
WhisperContextParameters::default(),
)
.unwrap();
let mut params =
whisper_rs::FullParams::new(whisper_rs::SamplingStrategy::Greedy { best_of: 1 });
params.set_language(Some(lang.unwrap_or("ja")));
params.set_print_special(false);
params.set_print_progress(false);
params.set_print_realtime(false);
params.set_print_timestamps(false);
let audio_file = fs::File::open(audio_path).unwrap();
let audio_data = Decoder::new(BufReader::new(audio_file)).unwrap();
let data = audio_data.into_iter().collect::<Vec<i16>>();
let mut output = vec![0.0f32; data.len()];
whisper_rs::convert_integer_to_float_audio(&data, &mut output).unwrap();
let mono_data = whisper_rs::convert_stereo_to_mono_audio(&output).unwrap();
let mut state = ctx.create_state().unwrap();
state.full(params, &mono_data).unwrap();
let num_segments = state.full_n_segments().unwrap();
let mut sc = Vec::new();
for i in 0..num_segments {
sc.push(state.full_get_segment_text(i).unwrap());
}
concat_vec_to_string(sc)
}
fn concat_vec_to_string(list: Vec<String>) -> String {
let mut tmp = String::new();
list.iter().for_each(|f| tmp.push_str(f));
tmp
}