Speech
Dictation controls: a button that shows the voice’s level while it listens, a scrolling waveform, and a voice note’s waveform with a scrubber. The app supplies the audio.
Dictation
ui.horizontal(|ui| {
ui.add(
SpeechWaveform::new(&state.levels)
.capturing(state.status.is_capturing())
.size(Size::XSmall)
.width(48.0),
);
if ui
.add(SpeechButton::new(state.status, &state.levels).xsmall())
.clicked()
{
state.status = state.status.toggled();
if state.status == SpeechStatus::Stopping {
state.idle_at = time + 1.0;
} else {
state.levels.reset();
}
}
});
if state.status.is_capturing() {
ui.add(
SpeechWaveform::new(&state.levels)
.capturing(true)
.size(Size::Large)
.width(ui.available_width().min(480.0)),
);
}
Button sizes and states
for size in [Size::XSmall, Size::Small, Size::Medium] {
ui.add(SpeechButton::new(SpeechStatus::Idle, &state.levels).size(size));
}
ui.add(SpeechButton::new(SpeechStatus::Recording, &state.levels));
ui.add(SpeechButton::new(SpeechStatus::Stopping, &state.levels));
ui.add(SpeechButton::new(SpeechStatus::Idle, &state.levels).available(false));
ui.add(SpeechButton::new(SpeechStatus::Idle, &state.levels).disabled(true));
if ui
.add(SpeechButton::new(state.small_status, &state.levels).small())
.clicked()
{
state.small_status = match state.small_status {
SpeechStatus::Idle => SpeechStatus::Recording,
_ => SpeechStatus::Idle,
};
}
Indicator
let primary = tokens(ui.ctx()).primary.background;
ui.add(SpeechIndicator::new(&state.levels).size(Size::Small));
ui.add(SpeechIndicator::new(&state.levels));
ui.add(SpeechIndicator::new(&state.levels).color(primary));
Waveform sizes
for size in [Size::XSmall, Size::Small, Size::Medium, Size::Large] {
ui.add(
SpeechWaveform::new(&state.levels)
.capturing(state.status.is_capturing())
.size(size)
.width(160.0),
);
}
Voice note
let label = if state.playing { "Pause" } else { "Play" };
if ui.add(Button::new(label).small()).clicked() {
state.playing = !state.playing;
}
let knob = state.playing || state.position > 0.0;
ui.add(
AudioWaveform::new(&state.peaks, &mut state.position)
.width(220.0)
.knob(knob),
);
let seconds = (state.position * 12.0).round() as u32;
ui.label(format!("0:{seconds:02} / 0:12"));
The story above is gallery/src/stories/speech.rs, running in your browser. Every snippet on this page is cut from it.