Skip to main content

vibe_analysis 0.1.1 copy "vibe_analysis: ^0.1.1" to clipboard
vibe_analysis: ^0.1.1 copied to clipboard

Pure-Dart audio analysis from PCM samples. Estimates tempo (BPM), musical key, energy, timbre and mood. No native code.

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.1

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

Features for a stored result #

featuresOnly(samples, stored) computes only the timbre, rhythm and mood features and uses the tempo, key and energy you already have. Use it to add features to results saved by an earlier version without changing them. featuresOnlyInIsolate runs it on a background isolate.

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. bpmStrength tells you how clear the pulse was.
  • Key is reported only when the match is confident (key is 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.

1
likes
160
points
0
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Pure-Dart audio analysis from PCM samples. Estimates tempo (BPM), musical key, energy, timbre and mood. No native code.

Repository (GitHub)
View/report issues

Topics

#audio #music #dsp #bpm

License

MIT (license)

More

Packages that depend on vibe_analysis