flutter_pitch_detection 1.3.1
flutter_pitch_detection: ^1.3.1 copied to clipboard
A Flutter plugin for real-time audio pitch detection using TarsosDSP on Android. Provides frequency, note, octave, volume, and accuracy measurements.
Flutter Pitch Detection Plugin #
A Flutter plugin for real-time pitch detection using TarsosDSP on Android.
Features #
- Real-time pitch detection
- Frequency, note name, octave and MIDI note detection
- Supports different A4 reference frequencies
- Raw audio data access (normalized and PCM formats)
- Volume measurement (normalized and dBFS)
- Pitch accuracy and Pitch Deviation
- Configurable parameters (sample rate, buffer size, etc.)
Installation #
Add to your pubspec.yaml
:
dependencies:
flutter_pitch_detection: ^x.x.x
Then import:
import 'package:flutter_pitch_detection/flutter_pitch_detection.dart';
Quick Start #
Initialize the detector
final pitchDetector = FlutterPitchDetection();
Set Parameters (Optional but recommended)
Set individual parameters:
pitchDetector.setSampleRate(44100);
pitchDetector.setBufferSize(8192);
pitchDetector.setMinPrecision(0.8);
pitchDetector.setToleranceCents(0.6);
pitchDetector.setA4Reference(440.0);
Or set multiple parameters at once:
pitchDetector.setParameters(toleranceCents: 0.6, bufferSize: 8192, sampleRate: 44100, minPrecision: 0.7, a4Reference: 440.0);
Start Detection
StreamSubscription<Map<String, dynamic>>? _pitchSubscription;
await pitchDetector.startDetection();
Retrieve Recording Status
await pitchDetector.isRecording();
Listen to Real-Time Updates
StreamSubscription<Map<String, dynamic>>? _pitchSubscription;
_pitchSubscription = pitchDetector.onPitchDetected.listen((data) async {
await pitchDetector.printNoteOctave();
await pitchDetector.getNote();
await pitchDetector.getOctave();
await pitchDetector.getMidiNote();
await pitchDetector.getFrequency();
await pitchDetector.getAccuracy(toleranceCents);
await pitchDetector.getPitchDeviation();
await pitchDetector.isOnPitch(toleranceCents, minPrecision);
await pitchDetector.getVolume();
await pitchDetector.getVolumeFromDbFS();
await pitchDetector.getToleranceCents();
await pitchDetector.getBufferSize();
await pitchDetector.getSampleRate();
await pitchDetector.getMinPrecision();
await pitchDetector.getA4Reference();
await _pitchDetection.getRawPcmDataFromStream();
await _pitchDetection.getRawDataFromStream();
});
Or
StreamSubscription<Map<String, dynamic>>? _pitchSubscription;
_pitchSubscription = pitchDetector.onPitchDetected.listen((data) async {
data['noteOctave'] ?? "";
data['note'] ?? "";
data['octave'] ?? -1;
data['midiNote'] ?? -1;
data['frequency'] ?? 0;
data['accuracy'] ?? 0;
data['pitchDeviation'] ?? 0;
data['isOnPitch'] ?? false;
data['volume'] ?? 0;
data['volumeDbFS'] ?? 0;
data['toleranceCents'] ?? defaultTolerance;
data['bufferSize'] ?? defaultBufferSize;
data['sampleRate'] ?? defaultSampleRate;
data['minPrecision'] ?? defaultPrecision;
data['a4Reference'] ?? defaultA4Reference;
data['pcmData'] ?? Uint8List(0);
data['streamData'] ?? [];
});
Stop Detection
await _pitchSubscription?.cancel();
_pitchSubscription = null;
await pitchDetector.stopDetection();
Method Reference #
Core Methods
startDetection({int? sampleRate, int? bufferSize, int? overlap,})
Starts real-time pitch detection. Callback returns (frequency, note, octave, accuracy, volume, etc.).stopDetection()
Stops the detection.
Configuration
-
setSampleRate(int rate)
Sets audio sample rate (e.g., 44100). -
setBufferSize(int size)
Sets buffer size (min 7056). -
setMinPrecision(double precision)
Sets minimum pitch confidence threshold (0.0 to 1.0). -
setToleranceCents(int cents)
Sets pitch tolerance in cents (0.0 to 1.0). -
setA4Reference(double a4Reference)
Sets the reference frequency for A4 in Hertz (defaults to 440.0). -
getSampleRate()
Returns current sample rate. -
getBufferSize()
Returns current buffer size. -
getMinPrecision()
Returns current min precision. -
getToleranceCents()
Returns current tolerance. -
getA4Reference()
Returns current reference frequency for A4 in Hertz.
Real-Time Data
onPitchDetected
A real-time event stream that provides continuous pitch detection updates. Subscribe to this stream to receive live audio analysis data, including frequency, note, volume, and accuracy metrics.getFrequency()
Returns current detected frequency (Hz).getNote()
Returns musical note (e.g., "C").getMidiNote()
Returns current MIDI note number. (0-127)getOctave()
Returns note octave (e.g., 4).printNoteOctave()
Logs note+octave (e.g., "C4").isOnPitch()
Returns bool if pitch meets precision.getAccuracy()
Returns pitch confidence in % (0 to 100).getPitchDeviation()
Returns the pitch deviation in cents (-100 and +100).getVolume()
Returns normalized volume (0.0 to 100.0).getVolumeFromDbFS()
Returns volume in dBFS (0.0 to 100.0).isRecording()
Returns bool if detection is active.getRawDataFromStream()
Returns Processed audio data (normalized doubles).getRawPcmDataFromStream()
Returns raw PCM byte data.
Important Notes #
- Android-only: This plugin does not support iOS yet.
- Permissions: Mic permissions are automatically handled on Android.
Example App #
Check the example/ folder for a complete demo app.