feat: OpenWhispr-inspired transcription polish pass
Major quality pass on top of Phase 2. Five substantive changes plus
cross-cutting touches across audio, hotkey, transcription, and Tauri
command layers.
Transcription quality
- Long-audio chunking in commands/transcription.rs: Parakeet and large
file transcription now chunk-and-recompose with overlap trimming, so
the live-path chunking advantage extends to file-based workflows.
- Stateful live speech gate in commands/live.rs on top of the earlier
duplicate-boundary filtering — distinguishes start-of-speech from
mid-speech and holds state across chunks.
Auto-learning corrections
- New crates/ai-formatting/src/correction_learning.rs: extracts user
text corrections from viewer edits and proposes additions to the
active profile's vocabulary.
- src-tauri/src/commands/profiles.rs bridge for frontend-driven
confirmation of learned terms.
- src/routes/viewer/+page.svelte hooks the learning path into the
segment-edit flow so corrections feed profile_terms without a
separate 'train this profile' UX.
Transcript profile provenance
- Migration v8 (crates/storage/src/migrations.rs) adds profile_id to
transcripts, defaulting to DEFAULT_PROFILE_ID so existing rows stay
valid.
- crates/storage/src/database.rs: TranscriptRow + CRUD carry profile_id.
- src-tauri/src/commands/transcripts.rs: add_transcript accepts and
persists profile_id.
- DictationPage.svelte + FilesPage.svelte send activeProfileId on
capture so learned corrections are attributed to the right profile.
Cleanup prompt contract
- crates/ai-formatting/src/llm_client.rs hardened: the CLEANUP_PROMPT
now specifies concrete do/do-not rules, ready for a real model-backed
cleanup pass. The llm_client is still a stub — kon-llm remains unwired
— but the prompt shape is final.
Cross-cutting polish
- Minor touches in audio (capture/decode/resample), hotkey (lib/linux/stub),
core, transcription (concurrency/model_manager/local_engine/whisper_rs),
and the rest of src-tauri/src/commands/*: error-path tightening, log
clarity, TS-migration follow-ups (@ts-nocheck additions for incremental
typing).
Verified locally: npm run check, cargo test -p kon-ai-formatting,
cargo test -p kon-storage, cargo test -p kon --lib commands::live::tests,
cargo check — all green.
Scope boundary: kon-llm crate is still a stub; task extraction remains
rule-based. Bundled local-LLM runtime is the next clean step and is not
in this commit.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::mpsc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use cpal::{FromSample, Sample, SampleFormat, SizedSample};
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use cpal::{FromSample, Sample, SampleFormat, SizedSample};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use kon_core::error::{KonError, Result};
|
||||
@@ -134,9 +134,7 @@ impl MicrophoneCapture {
|
||||
|
||||
/// Start capturing from the device whose name matches `device_name` exactly.
|
||||
/// If no match is found, returns an error rather than silently falling back.
|
||||
pub fn start_with_device(
|
||||
device_name: &str,
|
||||
) -> Result<(Self, mpsc::Receiver<AudioChunk>)> {
|
||||
pub fn start_with_device(device_name: &str) -> Result<(Self, mpsc::Receiver<AudioChunk>)> {
|
||||
let host = cpal::default_host();
|
||||
let devices = host
|
||||
.input_devices()
|
||||
@@ -172,12 +170,10 @@ impl MicrophoneCapture {
|
||||
.and_then(|d| device_display_name(&d))
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut all_devices: Vec<cpal::Device> =
|
||||
host.input_devices()
|
||||
.map_err(|e| {
|
||||
KonError::AudioCaptureFailed(format!("input_devices: {e}"))
|
||||
})?
|
||||
.collect();
|
||||
let mut all_devices: Vec<cpal::Device> = host
|
||||
.input_devices()
|
||||
.map_err(|e| KonError::AudioCaptureFailed(format!("input_devices: {e}")))?
|
||||
.collect();
|
||||
|
||||
// Sort: default first, then non-monitor, then monitor-as-last-resort.
|
||||
all_devices.sort_by_key(|d| {
|
||||
@@ -186,10 +182,10 @@ impl MicrophoneCapture {
|
||||
let is_monitor = is_monitor_name(&n);
|
||||
// Smaller key = tried first.
|
||||
match (is_default, is_monitor) {
|
||||
(true, false) => 0, // default, real input
|
||||
(false, false) => 1, // any other real input
|
||||
(true, true) => 2, // default but is a monitor (very rare)
|
||||
(false, true) => 3, // monitor source — last resort
|
||||
(true, false) => 0, // default, real input
|
||||
(false, false) => 1, // any other real input
|
||||
(true, true) => 2, // default but is a monitor (very rare)
|
||||
(false, true) => 3, // monitor source — last resort
|
||||
}
|
||||
});
|
||||
|
||||
@@ -281,7 +277,11 @@ fn device_display_name(device: &cpal::Device) -> Option<String> {
|
||||
/// `pipewire` / `default` → `None`
|
||||
fn extract_card_id(name: &str) -> Option<&str> {
|
||||
let rest = name.split("CARD=").nth(1)?;
|
||||
Some(rest.split(|c: char| c == ',' || c == ';').next().unwrap_or(rest))
|
||||
Some(
|
||||
rest.split(|c: char| c == ',' || c == ';')
|
||||
.next()
|
||||
.unwrap_or(rest),
|
||||
)
|
||||
}
|
||||
|
||||
/// Read `/proc/asound/cards` and return a map from ALSA card short name
|
||||
@@ -311,17 +311,28 @@ fn load_alsa_card_descriptions() -> std::collections::HashMap<String, String> {
|
||||
// digit (the card ID, right-aligned to 2 chars for readable
|
||||
// formatting). Continuation lines are indented beyond that.
|
||||
let trimmed = line.trim_start();
|
||||
if !trimmed.chars().next().map(|c| c.is_ascii_digit()).unwrap_or(false) {
|
||||
if !trimmed
|
||||
.chars()
|
||||
.next()
|
||||
.map(|c| c.is_ascii_digit())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let Some(open) = trimmed.find('[') else { continue };
|
||||
let Some(close) = trimmed[open..].find(']') else { continue };
|
||||
let Some(open) = trimmed.find('[') else {
|
||||
continue;
|
||||
};
|
||||
let Some(close) = trimmed[open..].find(']') else {
|
||||
continue;
|
||||
};
|
||||
let short_name = trimmed[open + 1..open + close].trim().to_string();
|
||||
if short_name.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let after_bracket = &trimmed[open + close + 1..];
|
||||
let Some(colon) = after_bracket.find(':') else { continue };
|
||||
let Some(colon) = after_bracket.find(':') else {
|
||||
continue;
|
||||
};
|
||||
// Format: "USB-Audio - Blue Microphones"
|
||||
// We keep everything after the " - " if present, otherwise
|
||||
// the whole post-colon fragment.
|
||||
@@ -346,9 +357,9 @@ fn open_and_validate(
|
||||
name: &str,
|
||||
require_audio: bool,
|
||||
) -> Result<(MicrophoneCapture, mpsc::Receiver<AudioChunk>)> {
|
||||
let config = device.default_input_config().map_err(|e| {
|
||||
KonError::AudioCaptureFailed(format!("default_input_config: {e}"))
|
||||
})?;
|
||||
let config = device
|
||||
.default_input_config()
|
||||
.map_err(|e| KonError::AudioCaptureFailed(format!("default_input_config: {e}")))?;
|
||||
let sample_rate = config.sample_rate();
|
||||
let channels = config.channels() as u16;
|
||||
let format = config.sample_format();
|
||||
@@ -370,9 +381,36 @@ fn open_and_validate(
|
||||
let (err_tx, err_rx) = mpsc::sync_channel::<CaptureRuntimeError>(16);
|
||||
|
||||
let stream = match format {
|
||||
SampleFormat::F32 => build_input_stream::<f32>(&device, &config, sample_rate, channels, tx, dropped_chunks.clone(), err_tx.clone(), name.to_string()),
|
||||
SampleFormat::I16 => build_input_stream::<i16>(&device, &config, sample_rate, channels, tx, dropped_chunks.clone(), err_tx.clone(), name.to_string()),
|
||||
SampleFormat::U16 => build_input_stream::<u16>(&device, &config, sample_rate, channels, tx, dropped_chunks.clone(), err_tx.clone(), name.to_string()),
|
||||
SampleFormat::F32 => build_input_stream::<f32>(
|
||||
&device,
|
||||
&config,
|
||||
sample_rate,
|
||||
channels,
|
||||
tx,
|
||||
dropped_chunks.clone(),
|
||||
err_tx.clone(),
|
||||
name.to_string(),
|
||||
),
|
||||
SampleFormat::I16 => build_input_stream::<i16>(
|
||||
&device,
|
||||
&config,
|
||||
sample_rate,
|
||||
channels,
|
||||
tx,
|
||||
dropped_chunks.clone(),
|
||||
err_tx.clone(),
|
||||
name.to_string(),
|
||||
),
|
||||
SampleFormat::U16 => build_input_stream::<u16>(
|
||||
&device,
|
||||
&config,
|
||||
sample_rate,
|
||||
channels,
|
||||
tx,
|
||||
dropped_chunks.clone(),
|
||||
err_tx.clone(),
|
||||
name.to_string(),
|
||||
),
|
||||
other => {
|
||||
return Err(KonError::AudioCaptureFailed(format!(
|
||||
"unsupported sample format {other:?}"
|
||||
@@ -386,8 +424,8 @@ fn open_and_validate(
|
||||
.map_err(|e| KonError::AudioCaptureFailed(format!("stream.play: {e}")))?;
|
||||
|
||||
// Validation window: collect chunks for DEVICE_VALIDATION_MS, compute RMS.
|
||||
let deadline = std::time::Instant::now()
|
||||
+ std::time::Duration::from_millis(DEVICE_VALIDATION_MS);
|
||||
let deadline =
|
||||
std::time::Instant::now() + std::time::Duration::from_millis(DEVICE_VALIDATION_MS);
|
||||
let mut collected: Vec<AudioChunk> = Vec::new();
|
||||
let mut total_samples = 0_usize;
|
||||
let mut sum_sq: f64 = 0.0;
|
||||
@@ -513,11 +551,15 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn monitor_pattern_detection() {
|
||||
assert!(is_monitor_name("alsa_output.pci-0000_00_1f.3.analog-stereo.monitor"));
|
||||
assert!(is_monitor_name(
|
||||
"alsa_output.pci-0000_00_1f.3.analog-stereo.monitor"
|
||||
));
|
||||
assert!(is_monitor_name("Monitor of Built-in Audio Analog Stereo"));
|
||||
assert!(is_monitor_name("Some Loopback Device"));
|
||||
assert!(!is_monitor_name("Blue Yeti USB"));
|
||||
assert!(!is_monitor_name("alsa_input.pci-0000_00_1f.3.analog-stereo"));
|
||||
assert!(!is_monitor_name(
|
||||
"alsa_input.pci-0000_00_1f.3.analog-stereo"
|
||||
));
|
||||
assert!(!is_monitor_name(""));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,12 @@ pub fn decode_audio_file(path: &Path) -> Result<AudioSamples> {
|
||||
}
|
||||
|
||||
let probed = symphonia::default::get_probe()
|
||||
.format(&hint, mss, &FormatOptions::default(), &MetadataOptions::default())
|
||||
.format(
|
||||
&hint,
|
||||
mss,
|
||||
&FormatOptions::default(),
|
||||
&MetadataOptions::default(),
|
||||
)
|
||||
.map_err(|e| KonError::AudioDecodeFailed(format!("Unsupported format: {e}")))?;
|
||||
|
||||
let mut format = probed.format;
|
||||
@@ -76,8 +81,7 @@ pub fn decode_audio_file(path: &Path) -> Result<AudioSamples> {
|
||||
|
||||
let spec = *decoded.spec();
|
||||
let channels = spec.channels.count();
|
||||
let mut sample_buf =
|
||||
SampleBuffer::<f32>::new(decoded.capacity() as u64, spec);
|
||||
let mut sample_buf = SampleBuffer::<f32>::new(decoded.capacity() as u64, spec);
|
||||
sample_buf.copy_interleaved_ref(decoded);
|
||||
|
||||
let buf = sample_buf.samples();
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use rubato::{SincFixedIn, SincInterpolationParameters, SincInterpolationType, Resampler, WindowFunction};
|
||||
use rubato::{
|
||||
Resampler, SincFixedIn, SincInterpolationParameters, SincInterpolationType, WindowFunction,
|
||||
};
|
||||
|
||||
use kon_core::constants::WHISPER_SAMPLE_RATE;
|
||||
use kon_core::error::{KonError, Result};
|
||||
@@ -32,15 +34,9 @@ pub fn resample_to_16khz(audio: &AudioSamples) -> Result<AudioSamples> {
|
||||
};
|
||||
|
||||
let mut resampler = SincFixedIn::<f32>::new(
|
||||
ratio,
|
||||
1.1,
|
||||
params,
|
||||
chunk_size,
|
||||
1, // mono
|
||||
ratio, 1.1, params, chunk_size, 1, // mono
|
||||
)
|
||||
.map_err(|e| {
|
||||
KonError::AudioDecodeFailed(format!("Resampler init failed: {e}"))
|
||||
})?;
|
||||
.map_err(|e| KonError::AudioDecodeFailed(format!("Resampler init failed: {e}")))?;
|
||||
|
||||
let samples = audio.samples();
|
||||
let mut output_samples: Vec<f32> = Vec::new();
|
||||
@@ -55,9 +51,9 @@ pub fn resample_to_16khz(audio: &AudioSamples) -> Result<AudioSamples> {
|
||||
}
|
||||
|
||||
let input = vec![chunk];
|
||||
let result = resampler.process(&input, None).map_err(|e| {
|
||||
KonError::AudioDecodeFailed(format!("Resample failed: {e}"))
|
||||
})?;
|
||||
let result = resampler
|
||||
.process(&input, None)
|
||||
.map_err(|e| KonError::AudioDecodeFailed(format!("Resample failed: {e}")))?;
|
||||
|
||||
if !result.is_empty() && !result[0].is_empty() {
|
||||
output_samples.extend_from_slice(&result[0]);
|
||||
@@ -90,8 +86,7 @@ mod tests {
|
||||
let rate = 48000;
|
||||
let duration_secs = 1.0;
|
||||
let num_samples = (rate as f64 * duration_secs) as usize;
|
||||
let samples: Vec<f32> =
|
||||
(0..num_samples).map(|i| (i as f32 * 0.001).sin()).collect();
|
||||
let samples: Vec<f32> = (0..num_samples).map(|i| (i as f32 * 0.001).sin()).collect();
|
||||
|
||||
let input = AudioSamples::new(samples, rate, 1);
|
||||
let output = resample_to_16khz(&input).unwrap();
|
||||
|
||||
@@ -24,8 +24,7 @@
|
||||
// produced by the padding leaks into the saved audio file.
|
||||
|
||||
use rubato::{
|
||||
Resampler, SincFixedIn, SincInterpolationParameters, SincInterpolationType,
|
||||
WindowFunction,
|
||||
Resampler, SincFixedIn, SincInterpolationParameters, SincInterpolationType, WindowFunction,
|
||||
};
|
||||
|
||||
use kon_core::constants::WHISPER_SAMPLE_RATE;
|
||||
@@ -78,11 +77,7 @@ impl StreamingResampler {
|
||||
INPUT_CHUNK,
|
||||
1, // mono
|
||||
)
|
||||
.map_err(|e| {
|
||||
KonError::AudioDecodeFailed(format!(
|
||||
"StreamingResampler init failed: {e}"
|
||||
))
|
||||
})?;
|
||||
.map_err(|e| KonError::AudioDecodeFailed(format!("StreamingResampler init failed: {e}")))?;
|
||||
|
||||
Ok(Self::Sinc {
|
||||
resampler,
|
||||
@@ -98,7 +93,11 @@ impl StreamingResampler {
|
||||
pub fn push_samples(&mut self, mono: &[f32]) -> Result<Vec<f32>> {
|
||||
match self {
|
||||
Self::Passthrough => Ok(mono.to_vec()),
|
||||
Self::Sinc { resampler, residual, .. } => {
|
||||
Self::Sinc {
|
||||
resampler,
|
||||
residual,
|
||||
..
|
||||
} => {
|
||||
if mono.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
@@ -128,7 +127,11 @@ impl StreamingResampler {
|
||||
pub fn flush(&mut self) -> Result<Vec<f32>> {
|
||||
match self {
|
||||
Self::Passthrough => Ok(Vec::new()),
|
||||
Self::Sinc { resampler, residual, ratio } => {
|
||||
Self::Sinc {
|
||||
resampler,
|
||||
residual,
|
||||
ratio,
|
||||
} => {
|
||||
if residual.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
@@ -139,9 +142,7 @@ impl StreamingResampler {
|
||||
|
||||
let input = vec![chunk];
|
||||
let result = resampler.process(&input, None).map_err(|e| {
|
||||
KonError::AudioDecodeFailed(format!(
|
||||
"StreamingResampler flush failed: {e}"
|
||||
))
|
||||
KonError::AudioDecodeFailed(format!("StreamingResampler flush failed: {e}"))
|
||||
})?;
|
||||
|
||||
let Some(mut out) = result.into_iter().next() else {
|
||||
@@ -183,8 +184,7 @@ mod tests {
|
||||
let from_rate = 48_000u32;
|
||||
let secs = 1.0;
|
||||
let n = (from_rate as f64 * secs) as usize;
|
||||
let samples: Vec<f32> =
|
||||
(0..n).map(|i| (i as f32 * 0.001).sin()).collect();
|
||||
let samples: Vec<f32> = (0..n).map(|i| (i as f32 * 0.001).sin()).collect();
|
||||
|
||||
let mut r = StreamingResampler::new(from_rate).unwrap();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user