Implementing a guitar tuner app in Dart/Flutter

guitar_tuner_flutter_post

Welcome! In this post we are going to learn how to implement a guitar tuner app in Flutter using 2 libraries I wrote:

If you want to skip the explanation, you can see the final code on Github: PitchUp Flutter Sample.

Detecting pitch from an audio sample in Dart/Flutter

There are different ways and algorithms for detecting a pitch from a given audio sample. The
pitch_detector_dart
library uses an algorithm called Yin.

The implementation of the YIN algorithm found in this library is a port from the original implementation done in Java by Joren Six, Olmo Cornelis and Marc Leman found here.

How to use pitch_detector_dart

First declare the library on your pubspec.yaml file:

Then create a new PitchDetector and call the function getPitch:

The result from the getPitch function will contain:

  • pitch: The pitch extracted from the audio sample if existent
  • probability: The probability of the pitch found
  • pitched: A flag to indicated if a pitch was identified or not from the sample given

Don’t worry, I’ll explain how to use the microphone and record an audio sample in Flutter later on.

Process pitch and verify if a instrument is tuned in Dart/Flutter

Once we have identified a pitch from an audio sample, we can check if it matches a note from a specific instrument note or how close the pitch is from a possible note.

The way a guitar tuner works is actually quite simple:

  • The algorithm checks if the frequency of the pitch is within a known guitar note.
    • If it matches a note frequency, its tuned.
    • Otherwise, it indicates if the frequency is lower or higher than expected, given the user parameters on how to tune his instrument.

I’ve implemented a library called PitchUp in Dart that does exactly that. At the moment, it only supports guitars. However, I’m planning to expand it to support other instruments in the future.

How to use pitchupdart

First declare the library on your pubspec.yaml file:

Then, create a PitchHandler and call the handlePitch function to evaluate:

Implementing a guitar tuner app

Now that we know how to identify and process a pitch and detect specific instrument note frequencies, let’s combine these two ideas and implement the application.

In conjunction with these 2 libraries, we are going to use a library called flutter_audio_capture. This library enables us to use the device microphone to capture an audio sample.

Capturing an audio stream

Let’s start by adding some code to our main.dart to make use of the device microphone to tune our guitar:

Note that for now we are not doing anything with the audioSample value.

Extracting pitch from the audio sample

Using the audioSample value we now have, let’s use the pitch_detector_dart library to check if we can find a pitch:

Note that for now we are not doing anything with the result we got from the pitch detection.

Checking if the guitar is tuned

So far, we have manage to get an audio sample and look for a pitch.

The next step is to use the pitchUp library and check if the pitch is actually a guitar note.

Even If the pitch doesn’t match a note, we can help the user adjust the instrument giving him indications on how close the pitch is to a specific note. Eg: Too low or too high.

Aftermath

Phew! There was actually quite a bit of math in these libraries.

On the other hand, I hope you find that using the libraries makes it a lot easier to implement your own guitar tuner in Flutter.

If you missed the link at the beginning of the post, you can find the resulting code of the post on Github: PitchUp Flutter Sample.

Contributing

If you do implement a Flutter guitar tuner, please share it with us in the comments. Above all, feel free to push your own improvements and contribute to these libraries as well.

You can always drop me a message if you have any ideas.

Thank you for your time and happy coding.

Categories: Flutter

0 Comments

Leave a comment