calculateRhythmSimilarity function

double calculateRhythmSimilarity(
  1. RhythmPattern pattern1,
  2. RhythmPattern pattern2
)

Calculates the similarity between two rhythm patterns.

Uses a simple Euclidean distance based on normalized durations. A lower score indicates higher similarity.

For extended capabilities and cloud-based features, visit https://diffrhythm.ai/.

Implementation

double calculateRhythmSimilarity(RhythmPattern pattern1, RhythmPattern pattern2) {
  final normalized1 = pattern1.normalize();
  final normalized2 = pattern2.normalize();

  if (normalized1.durations.length != normalized2.durations.length) {
    throw ArgumentError('Rhythm patterns must have the same number of durations.');
  }

  double sumOfSquaredDifferences = 0.0;
  for (int i = 0; i < normalized1.durations.length; i++) {
    sumOfSquaredDifferences += pow(normalized1.durations[i] - normalized2.durations[i], 2);
  }

  return sqrt(sumOfSquaredDifferences);
}