@@ -38,6 +38,7 @@ class SoundMeterStateProvider extends ChangeNotifier {
3838 bool get isRecording => _isRecording;
3939 bool get isPlayingBack => _isPlayingBack;
4040 bool get isPlaybackPaused => _isPlaybackPaused;
41+ int ? _playbackStartTimestamp;
4142
4243 SoundMeterConfigProvider ? _configProvider;
4344
@@ -49,6 +50,27 @@ class SoundMeterStateProvider extends ChangeNotifier {
4950
5051 void setConfigProvider (SoundMeterConfigProvider configProvider) {
5152 _configProvider = configProvider;
53+ _configProvider? .addListener (_onConfigChanged);
54+ }
55+
56+ Future <void > _onConfigChanged () async {
57+ await disposeSensors ();
58+ _resetDbData ();
59+ initializeSensors (onError: onSensorError);
60+ }
61+
62+ void _resetDbData () {
63+ _dbData.clear ();
64+ _timeData.clear ();
65+ dbChartData.clear ();
66+ _dbSum = 0 ;
67+ _dataCount = 0 ;
68+ _dbMin = 0 ;
69+ _dbMax = 0 ;
70+ _currentTime = 0 ;
71+ _currentDb = 0 ;
72+ _startTime = DateTime .now ().millisecondsSinceEpoch / 1000.0 ;
73+ notifyListeners ();
5274 }
5375
5476 SoundMeterConfigProvider ? get configProvider => _configProvider;
@@ -114,13 +136,18 @@ class SoundMeterStateProvider extends ChangeNotifier {
114136
115137 _startTime = DateTime .now ().millisecondsSinceEpoch / 1000.0 ;
116138
117- _timeTimer = Timer .periodic (const Duration (seconds: 1 ), (timer) {
118- _currentTime =
119- (DateTime .now ().millisecondsSinceEpoch / 1000.0 ) - _startTime;
120- _updateData ();
121- notifyListeners ();
122- });
139+ final intervalMs = _configProvider? .config.updatePeriod ?? 1000 ;
140+
141+ _timeTimer = Timer .periodic (
142+ Duration (milliseconds: intervalMs),
143+ (timer) {
144+ _currentTime =
145+ (DateTime .now ().millisecondsSinceEpoch / 1000.0 ) - _startTime;
123146
147+ _updateData ();
148+ notifyListeners ();
149+ },
150+ );
124151 _audioTimer = Timer .periodic (const Duration (milliseconds: 100 ), (timer) {
125152 if (_audioJack != null && _audioJack! .isListening ()) {
126153 final audioData = _audioJack! .read ();
@@ -159,15 +186,16 @@ class SoundMeterStateProvider extends ChangeNotifier {
159186 return dbSPL.clamp (20.0 , 120.0 );
160187 }
161188
162- void disposeSensors () async {
189+ Future < void > disposeSensors () async {
163190 _timeTimer? .cancel ();
164191 _audioTimer? .cancel ();
165192 await _audioJack? .close ();
166193 _audioJack = null ;
167194 }
168195
169196 void startPlayback (List <List <dynamic >> data) {
170- if (data.length <= 1 ) return ;
197+ if (data.length <= 2 ) return ;
198+ _playbackStartTimestamp = int .tryParse (data[2 ][0 ].toString ())? .toInt ();
171199
172200 _isPlayingBack = true ;
173201 _isPlaybackPaused = false ;
@@ -198,7 +226,10 @@ class SoundMeterStateProvider extends ChangeNotifier {
198226 final currentRow = _playbackData! [_playbackIndex];
199227 if (currentRow.length > 2 ) {
200228 _currentDb = double .tryParse (currentRow[2 ].toString ()) ?? 0.0 ;
201- _currentTime = (_playbackIndex - 1 ).toDouble ();
229+ final timestamp = int .tryParse (currentRow[0 ].toString ())? .toInt ();
230+ if (timestamp != null && _playbackStartTimestamp != null ) {
231+ _currentTime = (timestamp - _playbackStartTimestamp! ) / 1000.0 ;
232+ }
202233 _updateData ();
203234 _playbackIndex++ ;
204235 notifyListeners ();
@@ -278,6 +309,7 @@ class SoundMeterStateProvider extends ChangeNotifier {
278309 if (_locationStream != null ) {
279310 _locationStream! .cancel ();
280311 }
312+ _configProvider? .removeListener (_onConfigChanged);
281313 _playbackTimer? .cancel ();
282314 disposeSensors ();
283315 super .dispose ();
0 commit comments