noise_meter 1.2.0+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 89

noise_meter #

A noise meter package for iOS and Android.

Install #

Add noise_meter as a dependency in pubspec.yaml. For help on adding as a dependency, view the documentation.

On Android you need to add a permission to AndroidManifest.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'

Usage #

Initialization #

Keep these three variables accessible:

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

Start listening #

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

void start() async {
try {
  _noiseSubscription = _noiseMeter.noiseStream.listen(onData);
} on NoiseMeterException catch (exception) {
  print(exception);
}
}

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.

void onData(NoiseReading noiseReading) {
  this.setState(() {
    if (!this._isRecording) {
      this._isRecording = true;
    }
  });
  /// Do someting with the noiseReading object
  print(noiseReading.toString());
}

Stop listening #

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

void stopRecorder() async {
  try {
    if (_noiseSubscription != null) {
      _noiseSubscription.cancel();
      _noiseSubscription = null;
    }
    this.setState(() {
      this._isRecording = false;
    });
  } catch (err) {
    print('stopRecorder error: $err');
  }
}

Technical documentation #

Sample rate #

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:

db = 20 * log10(2**15 * pcmValue)

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 #

1.0.0 #

  • Able to stream NoiseReadings on iOS and Android.

example/lib/main.dart

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

void main() {
  runApp(new MyApp());
}

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

class _MyAppState extends State<MyApp> {
  bool _isRecording = false;
  StreamSubscription<NoiseReading> _noiseSubscription;
  NoiseMeter _noiseMeter = new NoiseMeter();

  @override
  void initState() {
    super.initState();
  }

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

  void start() async {
    try {
      _noiseSubscription = _noiseMeter.noiseStream.listen(onData);
    } catch (err) {
      print(err);
    }
  }

  void stop() async {
    try {
      if (_noiseSubscription != null) {
        _noiseSubscription.cancel();
        _noiseSubscription = null;
      }
      this.setState(() {
        this._isRecording = false;
      });
    } catch (err) {
      print('stopRecorder error: $err');
    }
  }

  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),
              )
            ])),
      ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
            child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: getContent())),
        floatingActionButton: FloatingActionButton(
            backgroundColor: _isRecording ? Colors.red : Colors.green,
            onPressed: _isRecording ? stop : start,
            child: _isRecording ? Icon(Icons.stop) : Icon(Icons.mic)),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  noise_meter: ^1.2.0+1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:noise_meter/noise_meter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
78
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
89
Learn more about scoring.

We analyzed this package on Jul 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:noise_meter/noise_meter.dart, package:audio_streamer/audio_streamer.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:noise_meter/noise_meter.dart, package:audio_streamer/audio_streamer.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:noise_meter/noise_meter.dart, package:audio_streamer/audio_streamer.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:noise_meter/noise_meter.dart, package:audio_streamer/audio_streamer.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [noise_meter] that is in a package requiring null.

Health suggestions

Fix lib/noise_meter.dart. (-1 points)

Analysis of lib/noise_meter.dart reported 2 hints:

line 45 col 8: The value of the field '_isRecording' isn't used.

line 46 col 16: The value of the field '_audio' isn't used.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
audio_streamer ^1.1.6 1.1.6
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
permission_handler 5.0.1+1
permission_handler_platform_interface 2.0.1
plugin_platform_interface 1.0.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test