Skip to content

Commit

Permalink
[Noise Meter] Release of 3.2.0 (upgraded audio streamer)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffmatteo committed Mar 30, 2023
1 parent 3ef1d9b commit 5f4cb13
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 18 deletions.
21 changes: 18 additions & 3 deletions packages/noise_meter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
## 3.2.0

- upgrade to `audio_streamer: ^2.3.0`
- upgrade example to include corret iOS permission

## 3.1.0
* upgrade to `audio_streamer: ^2.1.0`
* upgrade example to Android embedding v2

- upgrade to `audio_streamer: ^2.1.0`
- upgrade example to Android embedding v2

## 3.0.3
* improvement to `onError` handling.

- improvement to `onError` handling.

## 3.0.2

- upgrade to audio_streamer 3.0.2 - [PR #390](https://github.com/cph-cachet/flutter-plugins/pull/390)

## 3.0.1

- Fix Hot Restart crash & infinity dB with dispose ([PR #256](https://github.com/cph-cachet/flutter-plugins/pull/256))

## 3.0.0

- Null safety migration
- iOS example podfile update

## 2.0.0

- Changed API to support audio_streamer version 1.3.0
- Changed documentation

## 1.2.0

- Changed to api of a NoiseReading to include max and mean decibel.

## 1.1.5

- Added a getter for the sample rate field.

## 1.1.0

- Moved native source code to the AudioStreamer plugin (https://pub.dev/packages/audio_streamer).

## 1.0.0

- Able to stream NoiseReadings on iOS and Android.
50 changes: 40 additions & 10 deletions packages/noise_meter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,53 @@
A noise meter package for iOS and Android.

## Install
Add ```noise_meter``` as a dependency in `pubspec.yaml`.

Add `noise_meter` as a dependency in `pubspec.yaml`.
For help on adding as a dependency, view the [documentation](https://flutter.io/using-packages/).

On *Android* you need to add a permission to `AndroidManifest.xml`:
On _Android_ you need to add a permission to `AndroidManifest.xml`:

```xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```

On *iOS* enable the following:
* Capabilities > Background Modes > _Audio, AirPlay and Picture in Picture_
* In the Runner Xcode project edit the _Info.plist_ file. Add an entry for _'Privacy - Microphone Usage Description'_

On _iOS_ enable the following:

- Capabilities > Background Modes > _Audio, AirPlay and Picture in Picture_
- In the Runner Xcode project edit the _Info.plist_ file. Add an entry for _'Privacy - Microphone Usage Description'_
- Edit the `Podfile` to include the permission for the microphone:

```ruby
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
# for more infomation: https://github.com/BaseflowIT/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
'PERMISSION_MICROPHONE=1',]
end
end
end
```

## Usage

### Initialization

Keep these three variables accessible:

```dart
bool _isRecording = false;
StreamSubscription<NoiseReading> _noiseSubscription;
NoiseMeter _noiseMeter = new NoiseMeter(onError);
```

### Start listening

The easiest thing to do is to create a new instance of the NoiseMeter every time a new recording is started.

```dart
void start() async {
try {
Expand All @@ -38,10 +60,11 @@ void start() async {
}
```


### On data

When data comes in through the stream, it will be caught by the `onData` method, specified when the subscription was created.
The incoming data points are of type `NoiseReading` which have a single field with a getter, namely the `db` value of type `double`.

```dart
void onData(NoiseReading noiseReading) {
this.setState(() {
Expand All @@ -55,19 +78,22 @@ void onData(NoiseReading noiseReading) {
```

### On errors

Platform errors may occur when recording is interupted. You must decide what happens if such an error occurs.
The [onError] callback must be of type `void Function(Object error)`
or `void Function(Object error, StackTrace)`.

````dart
```dart
void onError(Object error) {
print(error.toString());
_isRecording = false;
}
````
```

### Stop listening

To stop listening, the `.cancel()` method is called on the subscription object.

```dart
void stopRecorder() async {
try {
Expand All @@ -83,17 +109,21 @@ void stopRecorder() async {
}
}
```

## Technical documentation

### Sample rate
The sample rate for both native implementations is 44,100.

The sample rate for both native implementations is 44,100.

### Microphone data

The native implementations record PCM data using the microphone of the device, and uses an audio buffer array to store the incoming data. When the buffer is filled, the contents are emitted to the Flutter side. The incoming floating point values are between -1 and 1 which is the PCM values divided by the max amplitude value which is 2^15.

### Conversion to Decibel

Computing the decibel of a PCM value is done as follows:

```python
db = 20 * log10(2**15 * pcmValue)
```
1 change: 0 additions & 1 deletion packages/noise_meter/example/ios/Flutter/.last_build_id

This file was deleted.

8 changes: 7 additions & 1 deletion packages/noise_meter/example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@ end
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
# for more infomation: https://github.com/BaseflowIT/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
'PERMISSION_MICROPHONE=1',]
end
end
end
end
2 changes: 1 addition & 1 deletion packages/noise_meter/lib/noise_meter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class NoiseMeter {
NoiseMeter([this.onError]);

/// The rate at which the audio is sampled
static int get sampleRate => AudioStreamer.sampleRate;
static Future<int> get sampleRate async => await AudioStreamer.currSampleRate;

/// The stream of noise readings.
Stream<NoiseReading> get noiseStream {
Expand Down
4 changes: 2 additions & 2 deletions packages/noise_meter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: noise_meter
description: A Flutter plugin for collecting noise from the phone's microphone.
version: 3.1.0
version: 3.2.0
homepage: https://github.com/cph-cachet/flutter-plugins/tree/master/packages/noise_meter

environment:
Expand All @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
audio_streamer: ^2.1.0
audio_streamer: ^2.3.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 5f4cb13

Please sign in to comment.