Skip to content

Commit

Permalink
Merge pull request cph-cachet#813 from cph-cachet/dev-noise-meter
Browse files Browse the repository at this point in the history
noise_meter updated to 5.0.0 using audio_streamer 4.0.0
  • Loading branch information
bardram authored Sep 21, 2023
2 parents 5e61682 + 967608c commit 201733d
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 232 deletions.
5 changes: 5 additions & 0 deletions packages/noise_meter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 5.0.0

- upgrade to `audio_streamer: ^4.0.0`
- update of example app to handle permissions to access the microphone and other improvements.

## 4.0.1

- upgrade to `audio_streamer: ^3.0.0` ([PR #755](https://github.com/cph-cachet/flutter-plugins/pull/755))
Expand Down
14 changes: 3 additions & 11 deletions packages/noise_meter/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@ MIT License.

Copyright 2019 Copenhagen Center for Health Technology (CACHET) at the Technical University of Denmark (DTU).

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the ”Software”), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ”Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ”AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED ”AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 changes: 18 additions & 0 deletions packages/noise_meter/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

include: package:flutter_lints/flutter.yaml

analyzer:
exclude: [build/**]
language:
strict-casts: true
strict-inference: true
strict-raw-types: false

linter:
rules:
cancel_subscriptions: true
constant_identifier_names: false
depend_on_referenced_packages: false
avoid_print: false
17 changes: 2 additions & 15 deletions packages/noise_meter/example/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
# example
# Noise Meter Example

A new Flutter project.

## Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
An example app for the noise_meter plugin.
137 changes: 67 additions & 70 deletions packages/noise_meter/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,101 +1,98 @@
import 'dart:async';

import 'package:noise_meter/noise_meter.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:permission_handler/permission_handler.dart';

void main() {
runApp(MyApp());
}
void main() => runApp(NoiseMeterApp());

class MyApp extends StatefulWidget {
class NoiseMeterApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
_NoiseMeterAppState createState() => _NoiseMeterAppState();
}

class _MyAppState extends State<MyApp> {
class _NoiseMeterAppState extends State<NoiseMeterApp> {
bool _isRecording = false;
NoiseReading? _latestReading;
StreamSubscription<NoiseReading>? _noiseSubscription;
NoiseMeter? _noiseMeter;

@override
void initState() {
super.initState();
_noiseMeter = NoiseMeter(onError);
}
NoiseMeter? noiseMeter;

@override
void dispose() {
_noiseSubscription?.cancel();
super.dispose();
}

void onData(NoiseReading noiseReading) {
this.setState(() {
_latestReading = noiseReading;
if (!this._isRecording) this._isRecording = true;
});
}
void onData(NoiseReading noiseReading) =>
setState(() => _latestReading = noiseReading);

void onError(Object error) {
print(error);
_isRecording = false;
stop();
}

void start() {
try {
_noiseSubscription = _noiseMeter?.noise.listen(onData);
} catch (err) {
print(err);
}
/// Check if microphone permission is granted.
Future<bool> checkPermission() async => await Permission.microphone.isGranted;

/// Request the microphone permission.
Future<void> requestPermission() async =>
await Permission.microphone.request();

/// Start noise sampling.
Future<void> start() async {
// Create a noise meter, if not already done.
noiseMeter ??= NoiseMeter();

// Check permission to use the microphone.
//
// Remember to update the AndroidManifest file (Android) and the
// Info.plist and pod files (iOS).
if (!(await checkPermission())) await requestPermission();

// Listen to the noise stream.
_noiseSubscription = noiseMeter?.noise.listen(onData, onError: onError);
setState(() => _isRecording = true);
}

/// Stop sampling.
void stop() {
try {
_noiseSubscription?.cancel();
this.setState(() {
this._isRecording = false;
});
} catch (err) {
print(err);
}
_noiseSubscription?.cancel();
setState(() => _isRecording = false);
}

List<Widget> getContent() => <Widget>[
Container(
margin: EdgeInsets.all(25),
child: Column(children: [
Container(
child: Text(_isRecording ? "Mic: ON" : "Mic: OFF",
style: TextStyle(fontSize: 25, color: Colors.blue)),
margin: EdgeInsets.only(top: 20),
),
Container(
child: Text(
'Noise: ${_latestReading?.meanDecibel} dB',
),
margin: EdgeInsets.only(top: 20),
),
Container(
child: Text(
'Max: ${_latestReading?.maxDecibel} dB',
),
)
])),
];

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: getContent())),
floatingActionButton: FloatingActionButton(
Widget build(BuildContext context) => MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: EdgeInsets.all(25),
child: Column(children: [
Container(
child: Text(_isRecording ? "Mic: ON" : "Mic: OFF",
style: TextStyle(fontSize: 25, color: Colors.blue)),
margin: EdgeInsets.only(top: 20),
),
Container(
child: Text(
'Noise: ${_latestReading?.meanDecibel.toStringAsFixed(2)} dB',
),
margin: EdgeInsets.only(top: 20),
),
Container(
child: Text(
'Max: ${_latestReading?.maxDecibel.toStringAsFixed(2)} dB',
),
)
])),
])),
floatingActionButton: FloatingActionButton(
backgroundColor: _isRecording ? Colors.red : Colors.green,
child: _isRecording ? Icon(Icons.stop) : Icon(Icons.mic),
onPressed: _isRecording ? stop : start,
child: _isRecording ? Icon(Icons.stop) : Icon(Icons.mic)),
),
);
}
),
),
);
}
49 changes: 5 additions & 44 deletions packages/noise_meter/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,63 +1,24 @@
name: noise_meter_example
description: Example app for the NoiseMeter package.
description: Example app for the noise_meter package.
publish_to: none

version: 1.0.0+1
version: 5.0.0

environment:
sdk: ">=2.12.0 <3.0.0"
sdk: ">=2.17.0 <4.0.0"
flutter: ">=3.0.0"

dependencies:
flutter:
sdk: flutter
noise_meter:
path: ../

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
permission_handler: ^11.0.0

dev_dependencies:
flutter_test:
sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true

# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.

# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages

# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
30 changes: 0 additions & 30 deletions packages/noise_meter/example/test/widget_test.dart

This file was deleted.

Loading

0 comments on commit 201733d

Please sign in to comment.