Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ee0f354312 | |||
| bdaa5210f0 | |||
| ad20c9f9d4 | |||
| 48c134ce6a |
@@ -653,6 +653,7 @@ class _QuickCaptureBarState extends ConsumerState<_QuickCaptureBar> {
|
||||
VoiceMicButton(
|
||||
mode: ref.watch(voiceProvider).mode,
|
||||
voiceModeActive: ref.watch(voiceProvider).voiceModeActive,
|
||||
amplitude: ref.watch(voiceProvider).amplitude,
|
||||
onTap: _toggleCaptureMic,
|
||||
),
|
||||
IconButton(
|
||||
|
||||
@@ -59,22 +59,29 @@ class VoiceState {
|
||||
final VoiceMode mode;
|
||||
final bool voiceModeActive;
|
||||
final bool available;
|
||||
/// Normalized mic amplitude 0.0–1.0 while recording. Drives the live
|
||||
/// pulse on VoiceMicButton so the user has obvious feedback that audio
|
||||
/// is actually being picked up.
|
||||
final double amplitude;
|
||||
|
||||
const VoiceState({
|
||||
this.mode = VoiceMode.idle,
|
||||
this.voiceModeActive = false,
|
||||
this.available = true,
|
||||
this.amplitude = 0.0,
|
||||
});
|
||||
|
||||
VoiceState copyWith({
|
||||
VoiceMode? mode,
|
||||
bool? voiceModeActive,
|
||||
bool? available,
|
||||
double? amplitude,
|
||||
}) =>
|
||||
VoiceState(
|
||||
mode: mode ?? this.mode,
|
||||
voiceModeActive: voiceModeActive ?? this.voiceModeActive,
|
||||
available: available ?? this.available,
|
||||
amplitude: amplitude ?? this.amplitude,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -92,10 +99,23 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
||||
StreamSubscription<Amplitude>? _amplitudeSubscription;
|
||||
|
||||
// Recording / silence detection
|
||||
//
|
||||
// Silence threshold is dynamic: we track the session peak dBFS and treat
|
||||
// "silent" as "current level is at least _dropFromPeakDb below peak."
|
||||
// This auto-calibrates to whatever mic + room the user is on rather than
|
||||
// assuming a fixed ambient level. Until the peak climbs above
|
||||
// _dynamicArmDb we fall back to a conservative static threshold so a
|
||||
// quiet room doesn't spin forever. A grace period at the start of the
|
||||
// recording gives the user time to begin speaking before silence checks
|
||||
// arm.
|
||||
int _recordingStartMs = 0;
|
||||
int _silenceMs = 0;
|
||||
static const _silenceThresholdDb = -40.0;
|
||||
static const _silenceDurationMs = 1500;
|
||||
double _peakDb = -100.0;
|
||||
static const _fallbackThresholdDb = -35.0;
|
||||
static const _dropFromPeakDb = 15.0;
|
||||
static const _dynamicArmDb = -20.0;
|
||||
static const _graceMs = 1500;
|
||||
static const _silenceDurationMs = 2000;
|
||||
static const _minRecordingMs = 300;
|
||||
|
||||
// Voice mode callbacks
|
||||
@@ -229,6 +249,7 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
||||
if (!state.voiceModeActive) return;
|
||||
|
||||
_silenceMs = 0;
|
||||
_peakDb = -100.0;
|
||||
_recordingStartMs = DateTime.now().millisecondsSinceEpoch;
|
||||
state = state.copyWith(mode: VoiceMode.recording);
|
||||
|
||||
@@ -267,14 +288,33 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
||||
void _onAmplitude(Amplitude event) {
|
||||
if (!state.voiceModeActive) return;
|
||||
|
||||
final db = event.current;
|
||||
final validDb = !(db.isNaN || db.isInfinite);
|
||||
|
||||
// Normalize dB to 0..1 for the pulse animation. -60dB is dead-quiet,
|
||||
// 0dB is peak; we clamp and bias so the button visibly breathes even
|
||||
// on soft speech without exploding on loud input.
|
||||
if (validDb) {
|
||||
final norm = ((db + 60.0) / 60.0).clamp(0.0, 1.0);
|
||||
if ((norm - state.amplitude).abs() > 0.02) {
|
||||
state = state.copyWith(amplitude: norm);
|
||||
}
|
||||
if (db > _peakDb) _peakDb = db;
|
||||
}
|
||||
|
||||
final elapsed =
|
||||
DateTime.now().millisecondsSinceEpoch - _recordingStartMs;
|
||||
// Grace period at the start — let the user begin speaking before we
|
||||
// start silence-counting. Also suppresses any false triggers while
|
||||
// the native recorder is still warming up.
|
||||
if (elapsed < _graceMs) return;
|
||||
if (elapsed < _minRecordingMs) return;
|
||||
|
||||
final db = event.current;
|
||||
// Guard against NaN / ±Infinity which can arrive on some Android devices
|
||||
// when the recorder is initialising. Treat invalid readings as silence.
|
||||
final isSilent = db.isNaN || db.isInfinite || db < _silenceThresholdDb;
|
||||
// Dynamic threshold once we've seen real speech; static fallback
|
||||
// before that so a dead-silent session doesn't spin forever.
|
||||
final threshold =
|
||||
_peakDb > _dynamicArmDb ? _peakDb - _dropFromPeakDb : _fallbackThresholdDb;
|
||||
final isSilent = !validDb || db < threshold;
|
||||
|
||||
if (isSilent) {
|
||||
_silenceMs += 200;
|
||||
|
||||
@@ -376,6 +376,7 @@ class _BriefingScreenState extends ConsumerState<BriefingScreen>
|
||||
VoiceMicButton(
|
||||
mode: voiceState.mode,
|
||||
voiceModeActive: voiceState.voiceModeActive,
|
||||
amplitude: voiceState.amplitude,
|
||||
onTap: _toggleVoiceMode,
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
|
||||
@@ -267,6 +267,7 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
|
||||
VoiceMicButton(
|
||||
mode: voiceState.mode,
|
||||
voiceModeActive: voiceState.voiceModeActive,
|
||||
amplitude: voiceState.amplitude,
|
||||
onTap: _toggleVoiceMode,
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
|
||||
@@ -5,51 +5,29 @@ import '../providers/voice_provider.dart';
|
||||
/// Animated mic button that reflects the current [VoiceMode].
|
||||
///
|
||||
/// - idle: muted background, mic_none icon
|
||||
/// - recording: red with pulsing shadow ring
|
||||
/// - recording: red, pulses with live [amplitude] for real-time feedback
|
||||
/// - transcribing: indigo with spinner
|
||||
/// - playing: indigo with volume_up icon
|
||||
class VoiceMicButton extends StatefulWidget {
|
||||
class VoiceMicButton extends StatelessWidget {
|
||||
final VoiceMode mode;
|
||||
final bool voiceModeActive;
|
||||
/// Live mic amplitude 0.0–1.0 while recording. Drives the button scale
|
||||
/// and glow so the user has obvious feedback that audio is being picked
|
||||
/// up. Ignored when not in [VoiceMode.recording].
|
||||
final double amplitude;
|
||||
final VoidCallback? onTap;
|
||||
|
||||
const VoiceMicButton({
|
||||
super.key,
|
||||
required this.mode,
|
||||
required this.voiceModeActive,
|
||||
this.amplitude = 0.0,
|
||||
this.onTap,
|
||||
});
|
||||
|
||||
@override
|
||||
State<VoiceMicButton> createState() => _VoiceMicButtonState();
|
||||
}
|
||||
|
||||
class _VoiceMicButtonState extends State<VoiceMicButton>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late AnimationController _pulseController;
|
||||
late Animation<double> _pulseAnimation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_pulseController = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 900),
|
||||
)..repeat(reverse: true);
|
||||
_pulseAnimation = Tween<double>(begin: 1.0, end: 1.25).animate(
|
||||
CurvedAnimation(parent: _pulseController, curve: Curves.easeInOut),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_pulseController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Color _bgColor(BuildContext context) {
|
||||
final cs = Theme.of(context).colorScheme;
|
||||
return switch (widget.mode) {
|
||||
return switch (mode) {
|
||||
VoiceMode.recording => const Color(0xFFEF4444),
|
||||
VoiceMode.transcribing || VoiceMode.playing => cs.primary,
|
||||
VoiceMode.idle => cs.surfaceContainerHighest,
|
||||
@@ -58,11 +36,10 @@ class _VoiceMicButtonState extends State<VoiceMicButton>
|
||||
|
||||
Widget _icon(BuildContext context) {
|
||||
final cs = Theme.of(context).colorScheme;
|
||||
final iconColor = widget.mode == VoiceMode.idle
|
||||
? cs.onSurfaceVariant
|
||||
: Colors.white;
|
||||
final iconColor =
|
||||
mode == VoiceMode.idle ? cs.onSurfaceVariant : Colors.white;
|
||||
|
||||
return switch (widget.mode) {
|
||||
return switch (mode) {
|
||||
VoiceMode.transcribing => SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
@@ -78,14 +55,14 @@ class _VoiceMicButtonState extends State<VoiceMicButton>
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isRecording = widget.mode == VoiceMode.recording;
|
||||
final isRecording = mode == VoiceMode.recording;
|
||||
|
||||
final button = Material(
|
||||
color: _bgColor(context),
|
||||
shape: const CircleBorder(),
|
||||
child: InkWell(
|
||||
customBorder: const CircleBorder(),
|
||||
onTap: widget.onTap,
|
||||
onTap: onTap,
|
||||
child: SizedBox(
|
||||
width: 40,
|
||||
height: 40,
|
||||
@@ -96,22 +73,30 @@ class _VoiceMicButtonState extends State<VoiceMicButton>
|
||||
|
||||
if (!isRecording) return button;
|
||||
|
||||
return AnimatedBuilder(
|
||||
animation: _pulseAnimation,
|
||||
builder: (_, child) => Container(
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: const Color(0xFFEF4444).withValues(alpha: 0.35),
|
||||
blurRadius: 8 * _pulseAnimation.value,
|
||||
spreadRadius: 2 * _pulseAnimation.value,
|
||||
),
|
||||
],
|
||||
),
|
||||
child: child,
|
||||
// Base pulse so silence still breathes (0.1 floor), scale + glow climb
|
||||
// linearly with live amplitude.
|
||||
final amp = amplitude.clamp(0.0, 1.0);
|
||||
final pulse = 0.1 + amp * 0.9;
|
||||
|
||||
return AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 120),
|
||||
curve: Curves.easeOut,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: const Color(0xFFEF4444).withValues(alpha: 0.2 + pulse * 0.3),
|
||||
blurRadius: 6 + pulse * 14,
|
||||
spreadRadius: 1 + pulse * 5,
|
||||
),
|
||||
],
|
||||
),
|
||||
child: AnimatedScale(
|
||||
scale: 1.0 + pulse * 0.18,
|
||||
duration: const Duration(milliseconds: 120),
|
||||
curve: Curves.easeOut,
|
||||
child: button,
|
||||
),
|
||||
child: button,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user