flutter_fft 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 82

Flutter FFT #

Plugin at pub-dev #

https://pub.dev/packages/flutter_fft

Warning: Currently works only on Android! This plugin makes use of platform channels, and only the Java/Android platform channel has been implemented.

The plugin was developed and tested in a Pixel 2 emulator, API 29. Does not work on iOS at the moment, due to the platform channel having yet to be implemented.

Minimum SDK version >= 24: You can update the minimum SDK requirements at "/android/app/build.gradle" in the line minSdkVersion 16.

The following needs to be added to your project's "android/app/src/main/AndroidManifest.xml":

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

This is my first (and currently only) Flutter plugin and Java project.

I was gathering ideas my personal guitar tuner application on Flutter, when I realized that I couldn't find any examples of audio analysis/processing/manipulation with Flutter.

Flutter doesn't have great support for device specific hardware, such as microphone input. Obviously, it is the fundamental pillar for anything that deals with audio processing in real-time.
Since Flutter has "just" started to become mainstream, there are still not many real-world projects or examples around.

The plan I ended up coming up with was to code a platform channel for android, which is basically a way to call native code from within Flutter - i.e. Calling Java functions through Dart code, using Flutter.

The problem with this is that, as it calls native platform code, the "one codebase" Flutter feature is rendered useless, since I would have to code the same thing for both platforms. (Objective-C/Swift for iOS & Java/Kotlin for Android)

Because of that, at the moment, I only coded the android platform channel. An iOS platform channel is in the plans for future versions.

How to use #

As mentioned above, this plugin was purely intended for usage in my personal project, however, since I couldn't find similar implementations, I decided to upload it here, in case anyone else goes through the same process.

Because of this, what you can do with the plugin is very strict: Start recording, get data back from the platform channel, and stop recording.

If you know how to program however, you can easily modify the code for your own needs.

There are many getters and setters for the processed and default data, which are going to be discussed further below.

Simple example:

import 'package:flutter/material.dart';
import 'package:flutter_fft/flutter_fft.dart';

void main() => runApp(Application());

class Application extends StatefulWidget {
  @override
  ApplicationState createState() => ApplicationState();
}

class ApplicationState extends State<Application> {
  double frequency;
  String note;
  bool isRecording;

  FlutterFft flutterFft = new FlutterFft();

  @override
  void initState() {
    isRecording = flutterFft.getIsRecording;
    frequency = flutterFft.getFrequency;
    note = flutterFft.getNote;
    super.initState();
    _async();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Simple flutter fft example",
      theme: ThemeData.dark(),
      color: Colors.blue,
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              isRecording
                  ? Text(
                      "Current note: $note",
                      style: TextStyle(
                        fontSize: 35,
                      ),
                    )
                  : Text(
                      "None yet",
                      style: TextStyle(
                        fontSize: 35,
                      ),
                    ),
              isRecording
                  ? Text(
                      "Current frequency: ${frequency.toStringAsFixed(2)}",
                      style: TextStyle(
                        fontSize: 35,
                      ),
                    )
                  : Text(
                      "None yet",
                      style: TextStyle(
                        fontSize: 35,
                      ),
                    ),
            ],
          ),
        ),
      ),
    );
  }

  _async() async {
    print("starting...");
    await flutterFft.startRecorder();
    setState(() => isRecording = flutterFft.getIsRecording);
    flutterFft.onRecorderStateChanged.listen(
      (data) => {
        setState(
          () => {
            frequency = data[1],
            note = data[2],
          },
        ),
        flutterFft.setNote = note,
        flutterFft.setFrequency = frequency,
      },
    );
  }
}

Methods #

When we instantiate the plugin, a method channel variable is created, with the tag "com.slins.flutterfft/record".

This is the variable responsible for estabilishing a connection between Dart and the platform channel.

For the section below, it is assumed that the plugin was instantiated and stored in a variable called "flutterFft".

Three main methods #

  1. flutterFft.onRecorderStateChanged
    • Stream that listens to the updates from the recorder..
  2. flutterFft.startRecording()
    • Starts recording using the data from the plugin's local instance. In other words, if you want to pass custom values as arguments, you have to set them inside the flutterFft instance before starting the recorder. i.e. flutterFft.setSampleRate = 22050
  3. flutterFft.stopRecording()
    • Stops recording.

Variables, default values, getters, setters and descriptions #

VariableDefault ValueTypeGetterSetterDescription
_tuning["E4", "B3", "G3", "D3", "A2", "E2"]List<String>flutterFft.getTuningflutterFft.setTuningController for the tuning target. Format: ["E4", "B3", "G3", "D3", "A2", "E2"] (The detected frequency is compared to these values in order to resolve the detection data.)
_isRecordingfalseboolflutterFft.getIsRecordingflutterFft.setIsRecordingController for the recorder state.
_subscriptionDuration0.25doubleflutterFft.getSubscriptionDurationflutterFft.setSubscriptionDurationController for the interval between platform channel function calls.
_numChannels1intflutterFft.getNumChannelsflutterFft.setNumChannelsController for the number of channels that gets passed to the pitch detector.
_sampleRate44100intflutterFft.getSampleRateflutterFft.setSampleRateController for the sample rate that gets passed to the pitch detector.
_androidAudioSourceAndroidAudioSource.MICAndroidAudioSourceflutterFft.getAndroidAudioSourceflutterFft.setAndroidAudioSourceController for the audio source. (Microphone, etc.)
_tolerance1.00doubleflutterFft.getToleranceflutterFft.setToleranceController for the tolerance. (How far apart can the current frequency from the target frequency in order to be considered on pitch)
_frequency0doubleflutterFft.getFrequencyflutterFft.setFrequencyController for the frequency.
_note""StringflutterFft.getNoteflutterFft.setNoteController for the note
_target0doubleflutterFft.getTargetflutterFft.setTargetController for the target frequency. (Based on the current selected tuning, calculate the nearest frequency in tune to be considered as the target, i.e: IF currentNote == A && A.frequency.distanceToB IS SmallestTargetDistance -> _target = A.frequency.distanceToB)
_distance0doubleflutterFft.getDistanceflutterFft.setDistanceController for the distance between the current frequency and the target frequency.
_octave0intflutterFft.getOctaveflutterFft.setOctaveController for the detected octave.
_nearestNote""StringflutterFft.getNearestNoteflutterFft.setNearestNoteController for nearest note. (Based on the current note)
_nearestTarget0doubleflutterFft.getNearestTargetflutterFft.setNearestTargetController for nearest target. (Second smallest distance, as the smallest distance is already _target)
_nearestDistance0doubleflutterFft.getNearestDistanceflutterFft.setNearestDistanceController for nearest distance. (Second smallest distance)
_nearestOctave0intflutterFft.getNearestOctaveflutterFft.setNearestOctaveController for nearest octave. (Based on the "nearest" data)
_isOnPitchfalseboolflutterFft.getIsOnPitchflutterFft.setIsOnPitchController for the pitch

Changelog #

All notable changes to this project will be documented in this file.

Unreleased #

1.0.1 - Unknown #

Added #

  • Yet to figure out.

Changed #

  • Code refactored.

Removed #

  • Nothing.

1.0.0 - 2019-01-11 #

Added #

  • Plugin release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_fft/flutter_fft.dart';

void main() => runApp(Application());

class Application extends StatefulWidget {
  @override
  ApplicationState createState() => ApplicationState();
}

class ApplicationState extends State<Application> {
  double
      frequency; // Frequency variable which will be updated with th-> data returned by the plugin.
  String
      note; // Note variable which will be updated with th-> data returned by the plugin
  bool
      isRecording; // Is recording variable which will be updated with th-> data returned by the plugin

  FlutterFft flutterFft = new FlutterFft();

  @override
  void initState() {
    // Initialize some values, then call the "_initialize()" function, which will initialize the recorder.
    isRecording = flutterFft.getIsRecording;
    frequency = flutterFft.getFrequency;
    note = flutterFft.getNote;
    super.initState();
    _initialize();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Simple flutter fft example",
      theme: ThemeData.dark(),
      color: Colors.blue,
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              // If the plugin is recording, return the current note, otherwise, return "Not recording..."
              isRecording
                  ? Text(
                      "Current note: $note",
                      style: TextStyle(
                        fontSize: 35,
                      ),
                    )
                  : Text(
                      "Not recording",
                      style: TextStyle(
                        fontSize: 35,
                      ),
                    ),
              // If the plugin is recording, return the current frequency, otherwise, return "Not recording..."
              isRecording
                  ? Text(
                      "Current frequency: ${frequency.toStringAsFixed(2)}",
                      style: TextStyle(
                        fontSize: 35,
                      ),
                    )
                  : Text(
                      "Not recording",
                      style: TextStyle(
                        fontSize: 35,
                      ),
                    ),
            ],
          ),
        ),
      ),
    );
  }

  _initialize() async {
    print("Starting recorder...");
    await flutterFft
        .startRecorder(); // Waits for the recorder to properly start.
    print("Recorder started.");
    setState(() => isRecording = flutterFft
        .getIsRecording); // Set the local "isRecording" variable to true once the recorder has started.

    // Listens to the update stream, whenever there's ne-> data, update the local "frequency" and "note"
    // with one of the values returned by the plugin.
    // Also update the plugin's local note and frequency variables.
    flutterFft.onRecorderStateChanged.listen(
      (data) => {
        setState(
          () => {
            // Data indexes at the end of file.
            frequency = data[1],
            note = data[2],
          },
        ),
        flutterFft.setNote = note,
        flutterFft.setFrequency = frequency,
      },
    );
  }

  // Tolerance (int) -> data[0]
  // Frequency (double) -> data[1];
  // Note (string) -> data[2];
  // Target (double) -> data[3];
  // Distance (double) -> data[4];
  // Octave (int) -> data[5];

  // NearestNote (string) -> data[6];
  // NearestTarget (double) -> data[7];
  // NearestDistance (double) -> data[8];
  // NearestOctave (int) -> data[9];

  // IsOnPitch (bool) -> data[10];
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_fft: ^1.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:flutter_fft/flutter_fft.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
65
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]
82
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because:

  • package:flutter_fft/flutter_fft.dart that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because:

  • package:flutter_fft/flutter_fft.dart that declares support for platforms: android, ios

Package does not support Flutter platform web

Because:

  • package:flutter_fft/flutter_fft.dart that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because:

  • package:flutter_fft/flutter_fft.dart that declares support for platforms: android, ios

Package not compatible with SDK dart

Because:

  • flutter_fft that is a package requiring null.

Health issues and suggestions

Document public APIs. (-1 points)

76 out of 76 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety