Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -663,5 +663,6 @@
"pin_cap_name": "CAP",
"pin_cap_desc": "Capacitance Measurement Pin",
"pin_res_name": "RES",
"pin_res_desc": "Resistance Measurement Pin"
"pin_res_desc": "Resistance Measurement Pin",
"stopSound" : "Stop Sound"
}
6 changes: 6 additions & 0 deletions lib/l10n/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4103,6 +4103,12 @@ abstract class AppLocalizations {
/// In en, this message translates to:
/// **'Resistance Measurement Pin'**
String get pin_res_desc;

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

class _AppLocalizationsDelegate
Expand Down
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_de.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsDe extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsEn extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_es.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsEs extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_fr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsFr extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_he.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsHe extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_hi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsHi extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsId extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsIt extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_ja.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsJa extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_ml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsMl extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_my.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsMy extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_nb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsNb extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_pt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,9 @@ class AppLocalizationsPt extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}

/// The translations for Portuguese, as used in Brazil (`pt_BR`).
Expand Down
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_ru.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsRu extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_te.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsTe extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_uk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsUk extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_vi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,4 +2155,7 @@ class AppLocalizationsVi extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,9 @@ class AppLocalizationsZh extends AppLocalizations {

@override
String get pin_res_desc => 'Resistance Measurement Pin';

@override
String get stopSound => 'Stop Sound';
}

/// The translations for Chinese, using the Han script (`zh_Hant`).
Expand Down
148 changes: 148 additions & 0 deletions lib/providers/wave_generator_state_provider.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'dart:convert';
import 'dart:math';
import 'dart:math' as math;
import 'dart:typed_data';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:intl/intl.dart';
import 'package:mp_audio_stream/mp_audio_stream.dart';
import 'package:pslab/communication/science_lab.dart';
import 'package:pslab/others/logger_service.dart';
import 'package:pslab/others/wave_generator_constants.dart';
Expand Down Expand Up @@ -65,6 +67,12 @@ class WaveGeneratorStateProvider extends ChangeNotifier {

Position? currentPosition;

AudioStream? _audioStream;

bool isPlayingSound = false;

double _audioAngle = 0.0;

WaveGeneratorStateProvider() {
selectedAnalogWave = WaveConst.wave1;

Expand All @@ -83,6 +91,146 @@ class WaveGeneratorStateProvider extends ChangeNotifier {
_configProvider = configProvider;
}

void toggleSound() {
if (isPlayingSound) {
isPlayingSound = false;

_stopAudioStream();
} else {
isPlayingSound = true;

_startAudioStream();
}

notifyListeners();
}

Future<void> _startAudioStream() async {
_audioStream = getAudioStream();

_audioStream!.init(bufferMilliSec: 1000, channels: 1, sampleRate: 44100);

_audioStream!.resume();

await Future.delayed(const Duration(milliseconds: 100));

_audioAngle = 0.0;

final int bufferSize = 4096;

final double bufferDurationMs = (bufferSize / 44100.0) * 1000.0;

final List<Float32List> bufferPool =
List.generate(5, (_) => Float32List(bufferSize));

int poolIndex = 0;

double generatedAudioMs = 0.0;

Stopwatch stopwatch = Stopwatch()..start();

for (int i = 0; i < 3; i++) {
final buffer = bufferPool[poolIndex];

poolIndex = (poolIndex + 1) % bufferPool.length;

_fillAudioBuffer(buffer, bufferSize);

_audioStream!.push(buffer);

generatedAudioMs += bufferDurationMs;
}

while (isPlayingSound) {
double elapsedRealTimeMs = stopwatch.elapsedMilliseconds.toDouble();

if (generatedAudioMs - elapsedRealTimeMs < 300.0) {
final buffer = bufferPool[poolIndex];

poolIndex = (poolIndex + 1) % bufferPool.length;

_fillAudioBuffer(buffer, bufferSize);

_audioStream!.push(buffer);

generatedAudioMs += bufferDurationMs;
} else {
await Future.delayed(const Duration(milliseconds: 20));
}

if (elapsedRealTimeMs > generatedAudioMs) {
generatedAudioMs = elapsedRealTimeMs;
}
}
}

void _fillAudioBuffer(Float32List buffer, int bufferSize) {
double frequency;

double duty = 0.5;

if (waveGeneratorConstants.modeSelected == WaveConst.square) {
frequency = waveGeneratorConstants
.wave[selectedAnalogWave]![WaveConst.frequency]!
.toDouble();
} else {
frequency = waveGeneratorConstants
.wave[WaveConst.sqr1]![WaveConst.frequency]!
.toDouble();

int currentDuty =
waveGeneratorConstants.wave[selectedDigitalWave]![WaveConst.duty] ??
50;

duty = currentDuty / 100.0;
}

if (frequency <= 0) frequency = 1;

double increment = (2 * math.pi * frequency) / 44100.0;

const double volume = 0.15;

for (int i = 0; i < bufferSize; i++) {
if (waveGeneratorConstants.modeSelected == WaveConst.pwm) {
buffer[i] = (_audioAngle < (2 * math.pi * duty)) ? volume : -volume;
} else {
double safeSin = math.sin(_audioAngle).clamp(-1.0, 1.0);

double sample = safeSin;

if (waveGeneratorConstants
.wave[selectedAnalogWave]![WaveConst.waveType] ==
triangular) {
sample = (2 / math.pi) * math.asin(safeSin);
}

buffer[i] = sample * volume;
}

_audioAngle += increment;

if (_audioAngle >= 2 * math.pi) {
_audioAngle -= 2 * math.pi;
}
}
}

void _stopAudioStream() {
if (_audioStream != null) {
_audioStream!.uninit();

_audioStream = null;
}
}

@override
void dispose() {
_stopAudioStream();
isPlayingSound = false;
super.dispose();
}

Future<void> _getCurrentLocation() async {
bool serviceEnabled;
LocationPermission permission;
Expand Down
15 changes: 12 additions & 3 deletions lib/view/wave_generator_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,30 @@ class _WaveGeneratorScreenState extends State<WaveGeneratorScreen> {
Expanded(
child: TextButton(
style: TextButton.styleFrom(
backgroundColor: primaryRed,
backgroundColor: provider.isPlayingSound
? Colors.grey[800]
: primaryRed,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(6),
),
),
child: Text(
appLocalizations.produceSound,
provider.isPlayingSound
? appLocalizations.stopSound
: appLocalizations.produceSound,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 14,
fontWeight: provider.isPlayingSound
? FontWeight.bold
: FontWeight.normal,
),
),
onPressed: () => {},
onPressed: () {
provider.toggleSound();
},
),
),
const SizedBox(width: 4),
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ dependencies:
image: ^4.1.3
serial: ^0.0.7+1
web: ^1.1.1
mp_audio_stream: ^0.2.2
dependency_overrides:
ffi: ^2.1.3

Expand Down
Loading