Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 123 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ num_cpus = "1.17.0"
handlebars = "6.3.2"
tempfile = "3.22.0"
expanduser = "1.2.2"
soundtouch = { version = "0.5.1", default-features = false }

[dev-dependencies]
paste = "1"
Expand Down
26 changes: 26 additions & 0 deletions src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ use symphonia::core::probe::Hint;

mod chromaprint;
mod ebur128;
mod soundtouch_bpm;
mod track_length;

use chromaprint::ChromaprintFingerprintAnalyzer;
use ebur128::EbuR128Analyzer;
use soundtouch_bpm::SoundTouchBpmAnalyzer;
use track_length::TrackLengthAnalyzer;

pub use ebur128::EbuR128AlbumResult;
Expand Down Expand Up @@ -94,6 +96,8 @@ enum CompoundAnalyzerItem {
ChromaprintFingerprint(Box<ChromaprintFingerprintAnalyzer>),
/// EBU R 128 Analyzer.
EbuR128(Box<EbuR128Analyzer>),
/// SoundTouch BPM Analyzer.
SoundTouchBpm(Box<SoundTouchBpmAnalyzer>),
}

impl CompoundAnalyzerItem {
Expand Down Expand Up @@ -130,6 +134,15 @@ impl CompoundAnalyzerItem {
None
}
},
AnalyzerType::SoundTouchBpm => {
match SoundTouchBpmAnalyzer::initialize(config, codec_params) {
Ok(analyzer) => Some(Self::SoundTouchBpm(Box::from(analyzer))),
Err(err) => {
result.soundtouch_bpm = Some(Err(err));
None
}
}
}
}
}

Expand All @@ -139,6 +152,7 @@ impl CompoundAnalyzerItem {
Self::TrackLength(analyzer) => analyzer.is_complete(),
Self::ChromaprintFingerprint(analyzer) => analyzer.is_complete(),
Self::EbuR128(analyzer) => analyzer.is_complete(),
Self::SoundTouchBpm(analyzer) => analyzer.is_complete(),
}
}

Expand Down Expand Up @@ -171,6 +185,13 @@ impl CompoundAnalyzerItem {
false
}
},
Self::SoundTouchBpm(analyzer) => match analyzer.feed(samples) {
Ok(()) => true,
Err(err) => {
result.soundtouch_bpm = Some(Err(err));
false
}
},
}
}

Expand All @@ -189,6 +210,9 @@ impl CompoundAnalyzerItem {
Self::EbuR128(analyzer) => {
result.ebur128 = Some(analyzer.finalize());
}
Self::SoundTouchBpm(analyzer) => {
result.soundtouch_bpm = Some(analyzer.finalize());
}
}
result
}
Expand All @@ -204,6 +228,8 @@ pub struct CompoundAnalyzerResult {
Option<Result<<ChromaprintFingerprintAnalyzer as Analyzer>::Result, AnalyzerError>>,
/// Result of the EBU R 128 analysis.
pub ebur128: Option<Result<<EbuR128Analyzer as Analyzer>::Result, AnalyzerError>>,
/// Result of the SoundTouch BPM analysis.
pub soundtouch_bpm: Option<Result<<SoundTouchBpmAnalyzer as Analyzer>::Result, AnalyzerError>>,
}

impl Analyzer for CompoundAnalyzer {
Expand Down
86 changes: 86 additions & 0 deletions src/analyzer/soundtouch_bpm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2025 Jan Holthuis <jan.holthuis@rub.de>
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy
// of the MPL was not distributed with this file, You can obtain one at
// http://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0

//! SoundTouch BPM analysis.
//!
//! Detects the average Beats-per-minute (BPM) of the audio track using the [SoundTouch Audio
//! Processing Library][soundtouch].
//!
//! [soundtouch]: http://www.surina.net/soundtouch/

use super::{Analyzer, AnalyzerError};
use crate::config::Config;

use symphonia::core::audio::Channels;
use symphonia::core::codecs::CodecParameters;

use soundtouch::BPMDetect;

/// Chromaprint Analyzer.
#[allow(missing_debug_implementations)]
pub struct SoundTouchBpmAnalyzer {
/// The [`BPMDetect`] struct which is doing the actual tempo analysis.
bpm_detect: BPMDetect,
}

/// Analysis result of the Chromaprint analyzer.
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone)]
pub struct SoundTouchBpmResult {
/// Analyzed Beats per Minute (BPM).
pub bpm: f32,
}

impl SoundTouchBpmResult {
/// Return the BPM as a string.
pub fn bpm_string(&self) -> String {
format!("{bpm:.2}", bpm = self.bpm)
}
}

impl Analyzer for SoundTouchBpmAnalyzer {
type Result = SoundTouchBpmResult;

fn initialize(_config: &Config, codec_params: &CodecParameters) -> Result<Self, AnalyzerError> {
let sample_rate = codec_params
.sample_rate
.ok_or(AnalyzerError::MissingSampleRate)?;
let num_channels = codec_params
.channels
.map(Channels::count)
.and_then(|channel_count| u32::try_from(channel_count).ok())
.ok_or(AnalyzerError::MissingAudioChannels)?;

let bpm_detect = BPMDetect::new(num_channels, sample_rate);
let analyzer = Self { bpm_detect };
Ok(analyzer)
}

fn feed(&mut self, samples: &[i16]) -> Result<(), AnalyzerError> {
let samples_float = samples
.iter()
.map(|&sample| sample.into())
.collect::<Vec<f32>>();
self.bpm_detect.input_samples(&samples_float);
Ok(())
}

fn is_complete(&self) -> bool {
// We need to read the entire file to calculate the average BPM.
false
}

fn finalize(mut self) -> Result<Self::Result, AnalyzerError> {
let bpm = self.bpm_detect.get_bpm();
if bpm == 0.0 {
Err(AnalyzerError::Custom("No Beats detected"))
} else {
Ok(Self::Result { bpm })
}
}
}
Loading
Loading