forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaveform_section.dart
More file actions
169 lines (148 loc) · 5.21 KB
/
Copy pathwaveform_section.dart
File metadata and controls
169 lines (148 loc) · 5.21 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
166
167
168
169
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:omi/models/playback_state.dart';
import 'package:omi/providers/sync_provider.dart';
import 'package:omi/utils/l10n_extensions.dart';
import 'package:omi/widgets/waveform_painter.dart';
class WaveformSection extends StatefulWidget {
final int seconds;
final List<double>? waveformData;
final bool isProcessingWaveform;
final PlaybackState playbackState;
final bool isPlaying;
const WaveformSection({
super.key,
required this.seconds,
required this.waveformData,
required this.isProcessingWaveform,
required this.playbackState,
required this.isPlaying,
});
@override
State<WaveformSection> createState() => _WaveformSectionState();
}
class _WaveformSectionState extends State<WaveformSection> {
Timer? _progressUpdateTimer;
double _lastProgress = 0.0;
@override
void initState() {
super.initState();
_startProgressTimer();
}
@override
void dispose() {
_progressUpdateTimer?.cancel();
super.dispose();
}
void _startProgressTimer() {
// Use 250ms interval instead of 100ms to reduce CPU usage while maintaining smooth playback
_progressUpdateTimer = Timer.periodic(const Duration(milliseconds: 250), (timer) {
if (mounted && widget.isPlaying) {
final currentProgress = widget.playbackState.playbackProgress;
if ((currentProgress - _lastProgress).abs() > 0.01) {
_lastProgress = currentProgress;
setState(() {});
}
}
});
}
void _handleWaveformTap(TapDownDetails details, BoxConstraints constraints, SyncProvider syncProvider) {
if (widget.playbackState.canPlayOrShare && syncProvider.totalDuration.inMilliseconds > 0 && widget.isPlaying) {
final localPosition = details.localPosition;
final containerWidth = constraints.maxWidth;
final progress = (localPosition.dx / containerWidth).clamp(0.0, 1.0);
final seekPosition = Duration(milliseconds: (progress * syncProvider.totalDuration.inMilliseconds).round());
// Perform seek operation asynchronously to avoid blocking UI
Future.microtask(() => syncProvider.seekToPosition(seekPosition));
}
}
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 24),
child: Column(
children: [
Expanded(child: _buildWaveformVisualization(context)),
const SizedBox(height: 16),
_buildTimeIndicators(context),
],
),
);
}
Widget _buildWaveformVisualization(BuildContext context) {
if (widget.isProcessingWaveform) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const CircularProgressIndicator(color: Colors.white70, strokeWidth: 2),
const SizedBox(height: 12),
Text(context.l10n.loadingYourRecording, style: const TextStyle(color: Colors.white70, fontSize: 12)),
],
),
);
}
return Consumer<SyncProvider>(
builder: (context, syncProvider, child) {
return LayoutBuilder(
builder: (context, constraints) {
return GestureDetector(
onTapDown: (details) => _handleWaveformTap(details, constraints, syncProvider),
child: Container(
width: double.infinity,
height: double.infinity,
child: RepaintBoundary(
child: CustomPaint(
painter: WaveformPainter(
isPlaying: widget.isPlaying,
waveformData: widget.waveformData,
playbackProgress: _lastProgress,
),
),
),
),
);
},
);
},
);
}
Widget _buildTimeIndicators(BuildContext context) {
final totalDur = Duration(seconds: widget.seconds);
// Always show 4 time markers like in ss1.jpeg (0:00, 0:01, 0:02, 0:03)
List<String> timeMarkers = [];
final intervalSeconds = (totalDur.inSeconds / 3).ceil(); // Divide into 3 intervals for 4 markers
for (int i = 0; i <= 3; i++) {
final seconds = i * intervalSeconds;
if (seconds <= totalDur.inSeconds) {
timeMarkers.add(_formatTimeMarker(Duration(seconds: seconds)));
}
}
// Ensure we always have exactly 4 markers
while (timeMarkers.length < 4) {
timeMarkers.add(_formatTimeMarker(totalDur));
}
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: timeMarkers
.map(
(marker) => Text(
marker,
style: Theme.of(
context,
).textTheme.labelMedium!.copyWith(color: Colors.grey.shade500, fontWeight: FontWeight.w400),
),
)
.toList(),
),
);
}
String _formatTimeMarker(Duration duration) {
final minutes = duration.inMinutes.remainder(60);
final seconds = duration.inSeconds.remainder(60);
return '${minutes}:${seconds.toString().padLeft(2, '0')}';
}
}