Skip to content

Commit facb568

Browse files
committed
音声認識プラグインでSTTリクエスト送信後のレスポンス読み取り処理を追加
1 parent bcf4d79 commit facb568

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/plugin.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22
use std::fs::File;
3-
use std::io::{BufWriter, Write};
3+
use std::io::{BufRead, BufReader, BufWriter, Write};
44
use std::path::PathBuf;
55
use std::process::{Command, Stdio};
66
use std::time::Duration;
@@ -63,6 +63,7 @@ impl InputStream {
6363
pub struct SpeechToTextPlugin {
6464
process: std::process::Child,
6565
stdin: std::process::ChildStdin,
66+
stdout: BufReader<std::process::ChildStdout>,
6667
chunk_duration: Duration,
6768
input_streams: HashMap<MediaStreamId, InputStream>,
6869
output_file: BufWriter<File>,
@@ -91,6 +92,7 @@ impl SpeechToTextPlugin {
9192
.collect();
9293
Ok(Self {
9394
stdin: process.stdin.take().or_fail()?,
95+
stdout: BufReader::new(process.stdout.take().or_fail()?),
9496
process,
9597
chunk_duration: spec.chunk_duration,
9698
input_streams,
@@ -104,10 +106,11 @@ impl SpeechToTextPlugin {
104106
input_stream_id: MediaStreamId,
105107
chunk: Vec<i16>,
106108
) -> orfail::Result<()> {
109+
let request_id: usize = 0;
107110
let request = nojson::object(|f| {
108111
f.member("jsonrpc", "2.0")?;
109112
f.member("method", "stt")?;
110-
f.member("id", 0)?;
113+
f.member("id", request_id)?;
111114
f.member(
112115
"params",
113116
nojson::object(|f| {
@@ -121,6 +124,23 @@ impl SpeechToTextPlugin {
121124
writeln!(self.stdin, "{request}").or_fail()?;
122125
self.stdin.flush().or_fail()?;
123126

127+
let mut response_line = String::new();
128+
self.stdout.read_line(&mut response_line).or_fail()?;
129+
130+
let json = nojson::RawJson::parse(&response_line).or_fail()?;
131+
let obj = JsonObject::new(json.value()).or_fail()?;
132+
(obj.get_required::<String>("jsonrpc").or_fail()? == "2.0").or_fail()?;
133+
(obj.get_required::<usize>("id").or_fail()? == request_id).or_fail()?;
134+
135+
// TODO: handle error
136+
let result = json
137+
.value()
138+
.to_member("result")
139+
.or_fail()?
140+
.required()
141+
.or_fail()?;
142+
let obj = JsonObject::new(result).or_fail()?;
143+
let text: String = obj.get_required("text").or_fail()?;
124144
todo!()
125145
}
126146
}

0 commit comments

Comments
 (0)