@@ -2,17 +2,18 @@ import 'dart:async';
2
2
import 'dart:math' ;
3
3
import 'package:fl_chart/fl_chart.dart' ;
4
4
import 'package:pslab/others/logger_service.dart' ;
5
- import 'package:flutter_audio_capture/flutter_audio_capture.dart' ;
6
5
import 'package:flutter/foundation.dart' ;
7
6
import 'package:pslab/constants.dart' ;
7
+ import 'package:pslab/others/audio_jack.dart' ;
8
8
9
9
class SoundMeterStateProvider extends ChangeNotifier {
10
10
double _currentDb = 0.0 ;
11
11
Timer ? _timeTimer;
12
+ Timer ? _audioTimer;
12
13
final List <double > _dbData = [];
13
14
final List <double > _timeData = [];
14
15
final List <FlSpot > dbChartData = [];
15
- FlutterAudioCapture ? _audioCapture ;
16
+ AudioJack ? _audioJack ;
16
17
double _startTime = 0 ;
17
18
double _currentTime = 0 ;
18
19
final int _maxLength = 50 ;
@@ -23,37 +24,34 @@ class SoundMeterStateProvider extends ChangeNotifier {
23
24
24
25
void initializeSensors () async {
25
26
try {
26
- _audioCapture = FlutterAudioCapture ();
27
-
28
- await _audioCapture ! . init ();
27
+ _audioJack = AudioJack ();
28
+ await _audioJack ! . initialize ();
29
+ await _audioJack ! . start ();
29
30
30
31
_startTime = DateTime .now ().millisecondsSinceEpoch / 1000.0 ;
32
+
31
33
_timeTimer = Timer .periodic (const Duration (seconds: 1 ), (timer) {
32
34
_currentTime =
33
35
(DateTime .now ().millisecondsSinceEpoch / 1000.0 ) - _startTime;
34
36
_updateData ();
35
37
notifyListeners ();
36
38
});
37
39
38
- await _audioCapture! .start (
39
- (Float32List audioData) {
40
- _currentDb = _calculateDecibels (audioData);
41
- notifyListeners ();
42
- },
43
- (error) {
44
- logger.e (
45
- "$soundMeterError $error " ,
46
- );
47
- },
48
- sampleRate: 44100 ,
49
- bufferSize: 4096 ,
50
- );
40
+ _audioTimer = Timer .periodic (const Duration (milliseconds: 100 ), (timer) {
41
+ if (_audioJack != null && _audioJack! .isListening ()) {
42
+ final audioData = _audioJack! .read ();
43
+ if (audioData.isNotEmpty) {
44
+ _currentDb = _calculateDecibels (audioData);
45
+ notifyListeners ();
46
+ }
47
+ }
48
+ });
51
49
} catch (e) {
52
50
logger.e ("$soundMeterInitialError $e " );
53
51
}
54
52
}
55
53
56
- double _calculateDecibels (Float32List audioData) {
54
+ double _calculateDecibels (List < double > audioData) {
57
55
if (audioData.isEmpty) return 0.0 ;
58
56
59
57
double sum = 0 ;
@@ -71,9 +69,11 @@ class SoundMeterStateProvider extends ChangeNotifier {
71
69
return dbSPL.clamp (20.0 , 120.0 );
72
70
}
73
71
74
- void disposeSensors () {
75
- _audioCapture? .stop ();
72
+ void disposeSensors () async {
76
73
_timeTimer? .cancel ();
74
+ _audioTimer? .cancel ();
75
+ await _audioJack? .close ();
76
+ _audioJack = null ;
77
77
}
78
78
79
79
@override
0 commit comments