Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ee0f354312 | |||
| bdaa5210f0 | |||
| ad20c9f9d4 | |||
| 48c134ce6a |
@@ -653,6 +653,7 @@ class _QuickCaptureBarState extends ConsumerState<_QuickCaptureBar> {
|
|||||||
VoiceMicButton(
|
VoiceMicButton(
|
||||||
mode: ref.watch(voiceProvider).mode,
|
mode: ref.watch(voiceProvider).mode,
|
||||||
voiceModeActive: ref.watch(voiceProvider).voiceModeActive,
|
voiceModeActive: ref.watch(voiceProvider).voiceModeActive,
|
||||||
|
amplitude: ref.watch(voiceProvider).amplitude,
|
||||||
onTap: _toggleCaptureMic,
|
onTap: _toggleCaptureMic,
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
|
|||||||
@@ -59,22 +59,29 @@ class VoiceState {
|
|||||||
final VoiceMode mode;
|
final VoiceMode mode;
|
||||||
final bool voiceModeActive;
|
final bool voiceModeActive;
|
||||||
final bool available;
|
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({
|
const VoiceState({
|
||||||
this.mode = VoiceMode.idle,
|
this.mode = VoiceMode.idle,
|
||||||
this.voiceModeActive = false,
|
this.voiceModeActive = false,
|
||||||
this.available = true,
|
this.available = true,
|
||||||
|
this.amplitude = 0.0,
|
||||||
});
|
});
|
||||||
|
|
||||||
VoiceState copyWith({
|
VoiceState copyWith({
|
||||||
VoiceMode? mode,
|
VoiceMode? mode,
|
||||||
bool? voiceModeActive,
|
bool? voiceModeActive,
|
||||||
bool? available,
|
bool? available,
|
||||||
|
double? amplitude,
|
||||||
}) =>
|
}) =>
|
||||||
VoiceState(
|
VoiceState(
|
||||||
mode: mode ?? this.mode,
|
mode: mode ?? this.mode,
|
||||||
voiceModeActive: voiceModeActive ?? this.voiceModeActive,
|
voiceModeActive: voiceModeActive ?? this.voiceModeActive,
|
||||||
available: available ?? this.available,
|
available: available ?? this.available,
|
||||||
|
amplitude: amplitude ?? this.amplitude,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,10 +99,23 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
|||||||
StreamSubscription<Amplitude>? _amplitudeSubscription;
|
StreamSubscription<Amplitude>? _amplitudeSubscription;
|
||||||
|
|
||||||
// Recording / silence detection
|
// 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 _recordingStartMs = 0;
|
||||||
int _silenceMs = 0;
|
int _silenceMs = 0;
|
||||||
static const _silenceThresholdDb = -40.0;
|
double _peakDb = -100.0;
|
||||||
static const _silenceDurationMs = 1500;
|
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;
|
static const _minRecordingMs = 300;
|
||||||
|
|
||||||
// Voice mode callbacks
|
// Voice mode callbacks
|
||||||
@@ -229,6 +249,7 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
|||||||
if (!state.voiceModeActive) return;
|
if (!state.voiceModeActive) return;
|
||||||
|
|
||||||
_silenceMs = 0;
|
_silenceMs = 0;
|
||||||
|
_peakDb = -100.0;
|
||||||
_recordingStartMs = DateTime.now().millisecondsSinceEpoch;
|
_recordingStartMs = DateTime.now().millisecondsSinceEpoch;
|
||||||
state = state.copyWith(mode: VoiceMode.recording);
|
state = state.copyWith(mode: VoiceMode.recording);
|
||||||
|
|
||||||
@@ -267,14 +288,33 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
|||||||
void _onAmplitude(Amplitude event) {
|
void _onAmplitude(Amplitude event) {
|
||||||
if (!state.voiceModeActive) return;
|
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 =
|
final elapsed =
|
||||||
DateTime.now().millisecondsSinceEpoch - _recordingStartMs;
|
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;
|
if (elapsed < _minRecordingMs) return;
|
||||||
|
|
||||||
final db = event.current;
|
// Dynamic threshold once we've seen real speech; static fallback
|
||||||
// Guard against NaN / ±Infinity which can arrive on some Android devices
|
// before that so a dead-silent session doesn't spin forever.
|
||||||
// when the recorder is initialising. Treat invalid readings as silence.
|
final threshold =
|
||||||
final isSilent = db.isNaN || db.isInfinite || db < _silenceThresholdDb;
|
_peakDb > _dynamicArmDb ? _peakDb - _dropFromPeakDb : _fallbackThresholdDb;
|
||||||
|
final isSilent = !validDb || db < threshold;
|
||||||
|
|
||||||
if (isSilent) {
|
if (isSilent) {
|
||||||
_silenceMs += 200;
|
_silenceMs += 200;
|
||||||
|
|||||||
@@ -376,6 +376,7 @@ class _BriefingScreenState extends ConsumerState<BriefingScreen>
|
|||||||
VoiceMicButton(
|
VoiceMicButton(
|
||||||
mode: voiceState.mode,
|
mode: voiceState.mode,
|
||||||
voiceModeActive: voiceState.voiceModeActive,
|
voiceModeActive: voiceState.voiceModeActive,
|
||||||
|
amplitude: voiceState.amplitude,
|
||||||
onTap: _toggleVoiceMode,
|
onTap: _toggleVoiceMode,
|
||||||
),
|
),
|
||||||
const SizedBox(width: 6),
|
const SizedBox(width: 6),
|
||||||
|
|||||||
@@ -267,6 +267,7 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
|
|||||||
VoiceMicButton(
|
VoiceMicButton(
|
||||||
mode: voiceState.mode,
|
mode: voiceState.mode,
|
||||||
voiceModeActive: voiceState.voiceModeActive,
|
voiceModeActive: voiceState.voiceModeActive,
|
||||||
|
amplitude: voiceState.amplitude,
|
||||||
onTap: _toggleVoiceMode,
|
onTap: _toggleVoiceMode,
|
||||||
),
|
),
|
||||||
const SizedBox(width: 6),
|
const SizedBox(width: 6),
|
||||||
|
|||||||
@@ -5,51 +5,29 @@ import '../providers/voice_provider.dart';
|
|||||||
/// Animated mic button that reflects the current [VoiceMode].
|
/// Animated mic button that reflects the current [VoiceMode].
|
||||||
///
|
///
|
||||||
/// - idle: muted background, mic_none icon
|
/// - 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
|
/// - transcribing: indigo with spinner
|
||||||
/// - playing: indigo with volume_up icon
|
/// - playing: indigo with volume_up icon
|
||||||
class VoiceMicButton extends StatefulWidget {
|
class VoiceMicButton extends StatelessWidget {
|
||||||
final VoiceMode mode;
|
final VoiceMode mode;
|
||||||
final bool voiceModeActive;
|
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;
|
final VoidCallback? onTap;
|
||||||
|
|
||||||
const VoiceMicButton({
|
const VoiceMicButton({
|
||||||
super.key,
|
super.key,
|
||||||
required this.mode,
|
required this.mode,
|
||||||
required this.voiceModeActive,
|
required this.voiceModeActive,
|
||||||
|
this.amplitude = 0.0,
|
||||||
this.onTap,
|
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) {
|
Color _bgColor(BuildContext context) {
|
||||||
final cs = Theme.of(context).colorScheme;
|
final cs = Theme.of(context).colorScheme;
|
||||||
return switch (widget.mode) {
|
return switch (mode) {
|
||||||
VoiceMode.recording => const Color(0xFFEF4444),
|
VoiceMode.recording => const Color(0xFFEF4444),
|
||||||
VoiceMode.transcribing || VoiceMode.playing => cs.primary,
|
VoiceMode.transcribing || VoiceMode.playing => cs.primary,
|
||||||
VoiceMode.idle => cs.surfaceContainerHighest,
|
VoiceMode.idle => cs.surfaceContainerHighest,
|
||||||
@@ -58,11 +36,10 @@ class _VoiceMicButtonState extends State<VoiceMicButton>
|
|||||||
|
|
||||||
Widget _icon(BuildContext context) {
|
Widget _icon(BuildContext context) {
|
||||||
final cs = Theme.of(context).colorScheme;
|
final cs = Theme.of(context).colorScheme;
|
||||||
final iconColor = widget.mode == VoiceMode.idle
|
final iconColor =
|
||||||
? cs.onSurfaceVariant
|
mode == VoiceMode.idle ? cs.onSurfaceVariant : Colors.white;
|
||||||
: Colors.white;
|
|
||||||
|
|
||||||
return switch (widget.mode) {
|
return switch (mode) {
|
||||||
VoiceMode.transcribing => SizedBox(
|
VoiceMode.transcribing => SizedBox(
|
||||||
width: 18,
|
width: 18,
|
||||||
height: 18,
|
height: 18,
|
||||||
@@ -78,14 +55,14 @@ class _VoiceMicButtonState extends State<VoiceMicButton>
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final isRecording = widget.mode == VoiceMode.recording;
|
final isRecording = mode == VoiceMode.recording;
|
||||||
|
|
||||||
final button = Material(
|
final button = Material(
|
||||||
color: _bgColor(context),
|
color: _bgColor(context),
|
||||||
shape: const CircleBorder(),
|
shape: const CircleBorder(),
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
customBorder: const CircleBorder(),
|
customBorder: const CircleBorder(),
|
||||||
onTap: widget.onTap,
|
onTap: onTap,
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
width: 40,
|
width: 40,
|
||||||
height: 40,
|
height: 40,
|
||||||
@@ -96,22 +73,30 @@ class _VoiceMicButtonState extends State<VoiceMicButton>
|
|||||||
|
|
||||||
if (!isRecording) return button;
|
if (!isRecording) return button;
|
||||||
|
|
||||||
return AnimatedBuilder(
|
// Base pulse so silence still breathes (0.1 floor), scale + glow climb
|
||||||
animation: _pulseAnimation,
|
// linearly with live amplitude.
|
||||||
builder: (_, child) => Container(
|
final amp = amplitude.clamp(0.0, 1.0);
|
||||||
decoration: BoxDecoration(
|
final pulse = 0.1 + amp * 0.9;
|
||||||
shape: BoxShape.circle,
|
|
||||||
boxShadow: [
|
return AnimatedContainer(
|
||||||
BoxShadow(
|
duration: const Duration(milliseconds: 120),
|
||||||
color: const Color(0xFFEF4444).withValues(alpha: 0.35),
|
curve: Curves.easeOut,
|
||||||
blurRadius: 8 * _pulseAnimation.value,
|
decoration: BoxDecoration(
|
||||||
spreadRadius: 2 * _pulseAnimation.value,
|
shape: BoxShape.circle,
|
||||||
),
|
boxShadow: [
|
||||||
],
|
BoxShadow(
|
||||||
),
|
color: const Color(0xFFEF4444).withValues(alpha: 0.2 + pulse * 0.3),
|
||||||
child: child,
|
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