Skip to content

feat: added csv functionalities in soundmeter screen. #2796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 16, 2025
4 changes: 3 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -341,5 +341,7 @@
"gyroscopeHighLimitHint" : "Please provide the maximum limit of lux value to be recorded (0 rad/s to 1000 rad/s)",
"accelerometerConfigurations" : "Accelerometer Configurations",
"accelerometerUpdatePeriodHint" : "Please provide time interval at which data will be updated",
"accelerometerHighLimitHint" : "Please provide the maximum limit of lux value to be recorded"
"accelerometerHighLimitHint" : "Please provide the maximum limit of lux value to be recorded",
"soundmeterSnackBarMessage" : "Unable to access sound sensor",
"dangerous" : "Dangerous"
}
12 changes: 12 additions & 0 deletions lib/l10n/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,18 @@ abstract class AppLocalizations {
/// In en, this message translates to:
/// **'Please provide the maximum limit of lux value to be recorded'**
String get accelerometerHighLimitHint;

/// No description provided for @soundmeterSnackBarMessage.
///
/// In en, this message translates to:
/// **'Unable to access sound sensor'**
String get soundmeterSnackBarMessage;

/// No description provided for @dangerous.
///
/// In en, this message translates to:
/// **'Dangerous'**
String get dangerous;
}

class _AppLocalizationsDelegate
Expand Down
7 changes: 7 additions & 0 deletions lib/l10n/app_localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get fileDeleted => 'File deleted';

@override
String get soundmeterConfig => 'Soundmeter Configurations';

@override
Expand Down Expand Up @@ -1099,4 +1100,10 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get accelerometerHighLimitHint =>
'Please provide the maximum limit of lux value to be recorded';

@override
String get soundmeterSnackBarMessage => 'Unable to access sound sensor';

@override
String get dangerous => 'Dangerous';
}
44 changes: 41 additions & 3 deletions lib/providers/soundmeter_state_provider.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:math';
import 'package:fl_chart/fl_chart.dart';
import 'package:intl/intl.dart';
import 'package:pslab/l10n/app_localizations.dart';
import 'package:pslab/others/logger_service.dart';
import 'package:flutter/foundation.dart';
Expand All @@ -18,13 +19,21 @@ class SoundMeterStateProvider extends ChangeNotifier {
AudioJack? _audioJack;
double _startTime = 0;
double _currentTime = 0;
final int _maxLength = 50;
final int _chartMaxLength = 50;
double _dbMin = 0;
double _dbMax = 0;
double _dbSum = 0;
int _dataCount = 0;
bool _isRecording = false;
List<List<dynamic>> _recordedData = [];
double _recordingStartTime = 0.0;
bool get isRecording => _isRecording;

Function(String)? onSensorError;

void initializeSensors({Function(String)? onError}) async {
onSensorError = onError;

void initializeSensors() async {
try {
_audioJack = AudioJack();
await _audioJack!.initialize();
Expand All @@ -50,9 +59,15 @@ class SoundMeterStateProvider extends ChangeNotifier {
});
} catch (e) {
logger.e("${appLocalizations.soundMeterInitialError} $e");
_handleSensorError(e);
}
}

void _handleSensorError(dynamic error) {
onSensorError?.call(appLocalizations.soundmeterSnackBarMessage);
logger.e("${appLocalizations.soundMeterInitialError} $error");
}

double _calculateDecibels(List<double> audioData) {
if (audioData.isEmpty) return 0.0;

Expand Down Expand Up @@ -87,11 +102,21 @@ class SoundMeterStateProvider extends ChangeNotifier {
void _updateData() {
final db = _currentDb;
final time = _currentTime;
if (_isRecording) {
final relativeTime = time - _recordingStartTime;
final now = DateTime.now();
final dateFormat = DateFormat('yyyy-MM-dd HH:mm:ss.SSS');
_recordedData.add([
dateFormat.format(now),
relativeTime.toStringAsFixed(2),
db.toStringAsFixed(2),
]);
}
_dbData.add(db);
_timeData.add(time);
_dbSum += db;
_dataCount++;
if (_dbData.length > _maxLength) {
if (_dbData.length > _chartMaxLength) {
final removedValue = _dbData.removeAt(0);
_timeData.removeAt(0);
_dbSum -= removedValue;
Expand All @@ -108,6 +133,19 @@ class SoundMeterStateProvider extends ChangeNotifier {
notifyListeners();
}

void startRecording() {
_isRecording = true;
_recordingStartTime = _currentTime;
_recordedData = [];
notifyListeners();
}

List<List<dynamic>> stopRecording() {
_isRecording = false;
notifyListeners();
return _recordedData;
}

double getCurrentDb() => _currentDb;
double getMinDb() => _dbMin;
double getMaxDb() => _dbMax;
Expand Down
Loading
Loading