-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstreaming.rs
More file actions
165 lines (135 loc) · 5.31 KB
/
streaming.rs
File metadata and controls
165 lines (135 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! Streaming transcription example — WhisperStream API (port of stream.cpp)
//!
//! Demonstrates:
//! 1. Basic streaming with sliding window
//! 2. Stream reuse across multiple sessions via .reset()
use std::path::{Path, PathBuf};
use whisper_cpp_plus::{FullParams, SamplingStrategy, WhisperContext, WhisperStream, WhisperStreamConfig};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let model_path = find_model("ggml-tiny.en.bin")
.ok_or("Model not found. Run: cargo xtask test-setup")?;
println!("Loading model from {:?}...", model_path);
let ctx = WhisperContext::new(&model_path)?;
let params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 })
.language("en");
let config = WhisperStreamConfig {
step_ms: 3000,
length_ms: 10000,
keep_ms: 200,
no_context: true,
..Default::default()
};
let mut stream = WhisperStream::with_config(&ctx, params, config)?;
println!("Stream created.");
// Load audio
let audio = load_audio()?;
println!("Loaded {} samples ({:.1}s)", audio.len(), audio.len() as f64 / 16000.0);
// Feed in 1-second chunks to simulate real-time
let chunk_size = 16000;
for (i, chunk) in audio.chunks(chunk_size).enumerate() {
stream.feed_audio(chunk);
println!("Fed chunk {} ({} samples, buf={})", i + 1, chunk.len(), stream.buffer_size());
// Process any ready steps
while let Some(segments) = stream.process_step()? {
for seg in &segments {
println!(
" [{:.2}s - {:.2}s]: {}",
seg.start_seconds(),
seg.end_seconds(),
seg.text
);
}
}
}
// Flush remaining
let final_segments = stream.flush()?;
for seg in &final_segments {
println!(
" [flush {:.2}s - {:.2}s]: {}",
seg.start_seconds(),
seg.end_seconds(),
seg.text
);
}
println!("Done. Processed {} samples total.", stream.processed_samples());
// === Part 2: Stream reuse across sessions ===
println!("\n=== Stream Reuse Demo ===\n");
demo_stream_reuse(&ctx)?;
Ok(())
}
/// Demonstrates reusing a WhisperStream across multiple sessions
fn demo_stream_reuse(ctx: &WhisperContext) -> Result<(), Box<dyn std::error::Error>> {
let params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 }).language("en");
let mut stream = WhisperStream::with_config(ctx, params, WhisperStreamConfig::default())?;
for session in 1..=3 {
println!("--- Session {} ---", session);
if session > 1 {
stream.reset(); // Reuse stream without recreating context
println!(" (reset — state reused)");
}
// Feed 2 seconds of audio
let audio = load_audio_chunk(2)?;
stream.feed_audio(&audio);
while let Some(segments) = stream.process_step()? {
for seg in &segments {
println!(" [{:.2}s-{:.2}s]: {}", seg.start_seconds(), seg.end_seconds(), seg.text);
}
}
let flush_segs = stream.flush()?;
for seg in &flush_segs {
println!(" [flush {:.2}s-{:.2}s]: {}", seg.start_seconds(), seg.end_seconds(), seg.text);
}
println!(" processed: {} samples\n", stream.processed_samples());
}
Ok(())
}
fn load_audio_chunk(duration_secs: usize) -> Result<Vec<f32>, Box<dyn std::error::Error>> {
let audio = load_audio()?;
let needed = 16000 * duration_secs;
Ok(audio.into_iter().take(needed).collect())
}
fn load_audio() -> Result<Vec<f32>, Box<dyn std::error::Error>> {
// Check env var first
let path = if let Ok(dir) = std::env::var("WHISPER_TEST_AUDIO_DIR") {
let p = format!("{}/jfk.wav", dir);
if Path::new(&p).exists() { Some(p) } else { None }
} else {
None
};
let path = path.or_else(|| {
let paths = [
"../whisper-cpp-plus-sys/whisper.cpp/samples/jfk.wav",
"whisper-cpp-plus-sys/whisper.cpp/samples/jfk.wav",
"samples/audio.wav",
];
paths.iter().find(|p| Path::new(p).exists()).map(|s| s.to_string())
});
let path = path.ok_or("No audio file found. Set WHISPER_TEST_AUDIO_DIR.")?;
println!("Loading audio from {}...", path);
let mut reader = hound::WavReader::open(&path)?;
let spec = reader.spec();
if spec.sample_rate != 16000 {
return Err(format!("Expected 16kHz, got {}Hz", spec.sample_rate).into());
}
let samples: Vec<f32> = reader
.samples::<i16>()
.step_by(spec.channels as usize)
.map(|s| s.unwrap() as f32 / 32768.0)
.collect();
Ok(samples)
}
fn find_model(name: &str) -> Option<PathBuf> {
for env_var in ["WHISPER_TEST_MODEL_DIR", "WHISPER_MODEL_PATH"] {
if let Ok(dir) = std::env::var(env_var) {
let path = Path::new(&dir).join(name);
if path.exists() { return Some(path); }
}
}
let paths = [
format!("tests/models/{}", name),
format!("whisper-cpp-plus/tests/models/{}", name),
format!("../whisper-cpp-plus-sys/whisper.cpp/models/{}", name),
format!("whisper-cpp-plus-sys/whisper.cpp/models/{}", name),
];
paths.iter().find(|p| Path::new(p).exists()).map(PathBuf::from)
}