vibe_analysis
Offline audio analysis in pure Dart. Give it mono PCM samples and it estimates:
- tempo in BPM (spectral flux, then weighted autocorrelation),
- musical key (chroma with Krumhansl-Kessler major and minor profiles),
- energy (loudness mapped to 0..1),
- timbre, rhythm and a rough mood (valence and arousal) as extra features, plus a similarity score between two tracks.
There is no native code and no machine learning. It runs on every Dart platform, including Flutter mobile and desktop. Decoding files is up to you: plug in any decoder, or use the optional ffmpeg one on desktop.
Extracted from the VIBE music player, where it has been used on real libraries.
Install
dependencies:
vibe_analysis: ^0.1.0
or dart pub add vibe_analysis.
Use
import 'package:vibe_analysis/vibe_analysis.dart';
final report = analyzeTrack(samples, sampleRate: 44100); // mono floats, -1..1
print(report.analysis.bpm); // 0 means no clear beat
print(report.analysis.keyLabel); // e.g. "A minor", empty when unclear
print(report.features.mood); // upbeat | intense | relaxed | melancholic
Run it off the UI thread with analyzeTrackInIsolate. Convert 16-bit PCM with pcm16ToFloat, and interleaved audio with toMono.
Compare two tracks:
final sim = featureSimilarity(a.features, b.features); // 0..1
Decode files
The package analyzes samples, not files. To go from a file path to a report, give TrackAnalyzer an AudioDecoder. Implement the one-method interface for any decoder you like (a platform plugin, a WAV reader, a test fake):
class MyDecoder implements AudioDecoder {
@override
Future<DecodedAudio?> decode(String path) async {
final samples = await myDecode(path); // mono Float32List, -1..1
return samples == null ? null : DecodedAudio(samples, sampleRate: 44100);
}
}
final report = await TrackAnalyzer(MyDecoder()).analyzeFile('/music/song.mp3');
// null when decoding fails. The maths runs in a background isolate by default.
ffmpeg decoder (desktop, optional)
import 'package:vibe_analysis/ffmpeg.dart';
final report = await TrackAnalyzer(const FfmpegDecoder()).analyzeFile('/music/song.mp3');
decodeWithFfmpeg(path) returns the samples directly. Both run the ffmpeg program, so it must be installed, and they are not available on the web. They return null if ffmpeg is missing or fails.
Platform support
The analysis (vibe_analysis.dart) is pure Dart. analyzeTrackInIsolate and TrackAnalyzer use isolates, so they need a platform with dart:isolate (not the web). Call analyzeTrack directly on the web. ffmpeg.dart needs dart:io and an ffmpeg install.
What to expect
- Only the middle 75 seconds of a track are used, so cost stays bounded.
- Input is resampled to 8 kHz for analysis. Spectral features describe the 0 to 4 kHz band.
- Tempo can land on half or double time for some music.
bpmStrengthtells you how clear the pulse was. - Key is reported only when the match is confident (
keyis empty otherwise). - Mood is a heuristic mapping of tempo, mode, energy and brightness. It is an estimate, not an understanding of the song.
- Tested on synthetic signals (click tracks, chords, silence, noise). It is not benchmarked against a labelled music dataset.
License
MIT.
Libraries
- ffmpeg
- Optional helper that decodes a file to analysis-ready samples by running
the
ffmpegprogram. Not available on the web. Needs ffmpeg installed. - vibe_analysis
- Offline audio analysis in pure Dart: tempo (BPM), musical key, energy, and timbre, rhythm and mood estimates.