-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
367 lines (322 loc) · 11.8 KB
/
Copy pathmod.rs
File metadata and controls
367 lines (322 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (c) 2026 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
//! Audio analysis.
use crate::config::{AnalyzerType, Config};
use std::path::Path;
use thiserror::Error;
use symphonia::core::audio::sample::Sample;
use symphonia::core::audio::GenericAudioBufferRef;
use symphonia::core::codecs::audio::{AudioDecoder, AudioDecoderOptions};
use symphonia::core::errors::Error as SymphoniaError;
use symphonia::core::formats::Track;
use symphonia::core::formats::{probe::Hint, FormatOptions, FormatReader, TrackType};
use symphonia::core::io::{MediaSourceStream, MediaSourceStreamOptions};
use symphonia::core::meta::MetadataOptions;
mod chromaprint;
mod ebur128;
mod track_length;
use chromaprint::ChromaprintFingerprintAnalyzer;
use ebur128::EbuR128Analyzer;
use track_length::TrackLengthAnalyzer;
pub use ebur128::EbuR128AlbumResult;
/// An error during analysis.
#[derive(Error, Debug)]
pub enum AnalyzerError {
/// I/O Error.
#[error("Input/Output error ({:?})", .0)]
IoError(#[from] std::io::Error),
/// I/O Error.
#[error("Symphonia error ({:?})", .0)]
SymphoniaError(#[from] symphonia::core::errors::Error),
/// No supported audio tracks.
#[error("No supported audio tracks.")]
NoSupportedAudioTracks,
/// Chromaprint Error.
#[error("Chromaprint reset error")]
ChromaprintResetError,
/// Missing Sample Rate.
#[error("missing sample rate")]
MissingSampleRate,
/// Missing Audio Channels.
#[error("missing audio channels")]
MissingAudioChannels,
/// Custom, analyzer-specific error.
#[error("analyzer error: {0}")]
Custom(&'static str),
/// EBU R 128 analysis failed.
#[error("ebur128 failure: {0}")]
EbuR128Error(#[from] ::ebur128::Error),
}
/// Analyzer trait.
pub trait Analyzer
where
Self: Sized,
{
/// Analyzer result type.
type Result;
/// Initialize the analyzer.
fn initialize(config: &Config, track: &Track) -> Result<Self, AnalyzerError>;
/// Feed samples into the analysis.
fn feed(&mut self, samples: &[f32]) -> Result<(), AnalyzerError>;
/// Returns `true` if the Analyzer is complete and does not need additional input.
fn is_complete(&self) -> bool;
/// Finalize the analysis and return the result.
fn finalize(self) -> Result<Self::Result, AnalyzerError>;
}
/// Compound analyzer that runs multiple analyzers at the same time.
struct CompoundAnalyzer {
/// The analyzers in this compound analyzer.
analyzers: Vec<CompoundAnalyzerItem>,
/// The results of the analyzers.
results: CompoundAnalyzerResult,
}
/// An analyzer item for a [`CompoundAnalyzer`].
enum CompoundAnalyzerItem {
/// Track Length Analyzer.
TrackLength(Box<TrackLengthAnalyzer>),
/// Chromaprint Fingerprint Analyzer.
ChromaprintFingerprint(Box<ChromaprintFingerprintAnalyzer>),
/// EBU R 128 Analyzer.
EbuR128(Box<EbuR128Analyzer>),
}
impl CompoundAnalyzerItem {
/// Initialize this analyzer or assign the error to the result struct if an error occurs.
fn initialize_or_assign_result(
analyzer_type: AnalyzerType,
config: &Config,
track: &Track,
result: &mut CompoundAnalyzerResult,
) -> Option<Self> {
match analyzer_type {
AnalyzerType::TrackLength => match TrackLengthAnalyzer::initialize(config, track) {
Ok(analyzer) => Some(Self::TrackLength(Box::from(analyzer))),
Err(err) => {
result.track_length = Some(Err(err));
None
}
},
AnalyzerType::ChromaprintFingerprint => {
match ChromaprintFingerprintAnalyzer::initialize(config, track) {
Ok(analyzer) => Some(Self::ChromaprintFingerprint(Box::from(analyzer))),
Err(err) => {
result.chromaprint_fingerprint = Some(Err(err));
None
}
}
}
AnalyzerType::EbuR128 => match EbuR128Analyzer::initialize(config, track) {
Ok(analyzer) => Some(Self::EbuR128(Box::from(analyzer))),
Err(err) => {
result.ebur128 = Some(Err(err));
None
}
},
}
}
/// Returns `true` if the Analyzer is complete and does not need additional input.
fn is_complete(&self) -> bool {
match self {
Self::TrackLength(analyzer) => analyzer.is_complete(),
Self::ChromaprintFingerprint(analyzer) => analyzer.is_complete(),
Self::EbuR128(analyzer) => analyzer.is_complete(),
}
}
/// Feed samples into the analyzer, or assign the error to the result struct if an error
/// occurs.
fn feed_or_assign_result(
&mut self,
samples: &[f32],
result: &mut CompoundAnalyzerResult,
) -> bool {
match self {
Self::TrackLength(analyzer) => match analyzer.feed(samples) {
Ok(()) => true,
Err(err) => {
result.track_length = Some(Err(err));
false
}
},
Self::ChromaprintFingerprint(analyzer) => match analyzer.feed(samples) {
Ok(()) => true,
Err(err) => {
result.chromaprint_fingerprint = Some(Err(err));
false
}
},
Self::EbuR128(analyzer) => match analyzer.feed(samples) {
Ok(()) => true,
Err(err) => {
result.ebur128 = Some(Err(err));
false
}
},
}
}
/// Finalize the analysis and assign the result to the result struct.
fn finalize_and_assign_result(
self,
mut result: CompoundAnalyzerResult,
) -> CompoundAnalyzerResult {
match self {
Self::TrackLength(analyzer) => {
result.track_length = Some(analyzer.finalize());
}
Self::ChromaprintFingerprint(analyzer) => {
result.chromaprint_fingerprint = Some(analyzer.finalize());
}
Self::EbuR128(analyzer) => {
result.ebur128 = Some(analyzer.finalize());
}
}
result
}
}
/// Compound result type that may contains results from all analyzers.
#[derive(Debug, Default)]
pub struct CompoundAnalyzerResult {
/// Result of the track length analysis.
pub track_length: Option<Result<<TrackLengthAnalyzer as Analyzer>::Result, AnalyzerError>>,
/// Result of the chromaprint fingerprint analysis.
pub chromaprint_fingerprint:
Option<Result<<ChromaprintFingerprintAnalyzer as Analyzer>::Result, AnalyzerError>>,
/// Result of the EBU R 128 analysis.
pub ebur128: Option<Result<<EbuR128Analyzer as Analyzer>::Result, AnalyzerError>>,
}
impl Analyzer for CompoundAnalyzer {
type Result = CompoundAnalyzerResult;
fn initialize(config: &Config, track: &Track) -> Result<Self, AnalyzerError> {
let mut results = CompoundAnalyzerResult::default();
let analyzers = config
.analyzers
.enabled
.iter()
.copied()
.filter_map(|analyzer_type| {
CompoundAnalyzerItem::initialize_or_assign_result(
analyzer_type,
config,
track,
&mut results,
)
})
.collect::<Vec<CompoundAnalyzerItem>>();
Ok(Self { analyzers, results })
}
fn feed(&mut self, samples: &[f32]) -> Result<(), AnalyzerError> {
self.analyzers
.retain_mut(|analyzer| analyzer.feed_or_assign_result(samples, &mut self.results));
Ok(())
}
fn is_complete(&self) -> bool {
self.analyzers.iter().all(CompoundAnalyzerItem::is_complete)
}
fn finalize(self) -> Result<CompoundAnalyzerResult, AnalyzerError> {
Ok(self
.analyzers
.into_iter()
.fold(CompoundAnalyzerResult::default(), |results, analyzer| {
analyzer.finalize_and_assign_result(results)
}))
}
}
/// Audio reader.
struct AudioReader {
/// Audio format reader.
format: Box<dyn FormatReader>,
/// Audio decoder.
decoder: Box<dyn AudioDecoder>,
/// Track ID.
track_id: u32,
}
impl AudioReader {
/// Create an audio reader from the given path.
fn new(path: &impl AsRef<Path>) -> Result<Self, AnalyzerError> {
let path = path.as_ref();
let src = std::fs::File::open(path)?;
let mss = MediaSourceStream::new(Box::new(src), MediaSourceStreamOptions::default());
let mut hint = Hint::new();
#[expect(unused_results)]
if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
hint.with_extension(ext);
}
let meta_opts: MetadataOptions = MetadataOptions::default();
let fmt_opts: FormatOptions = FormatOptions::default();
let format = symphonia::default::get_probe().probe(&hint, mss, fmt_opts, meta_opts)?;
let track = format
.default_track(TrackType::Audio)
.ok_or(AnalyzerError::NoSupportedAudioTracks)?;
let dec_opts: AudioDecoderOptions = AudioDecoderOptions::default();
let decoder = symphonia::default::get_codecs().make_audio_decoder(
track
.codec_params
.as_ref()
.and_then(|params| params.audio())
.ok_or(AnalyzerError::NoSupportedAudioTracks)?,
&dec_opts,
)?;
let track_id = track.id;
Ok(Self {
format,
decoder,
track_id,
})
}
/// Get the codec parameters.
fn track(&self) -> Option<&Track> {
self.format.default_track(TrackType::Audio)
}
/// Read the next packet(s) that belongs to the current track, decode it and return a reference
/// to the decoded audio buffer.
fn next_buffer(&mut self) -> Result<Option<GenericAudioBufferRef<'_>>, SymphoniaError> {
loop {
let packet = match self.format.next_packet() {
Ok(Some(packet)) => packet,
Ok(None) => {
break Ok(None);
}
Err(err) => break Err(err),
};
if packet.track_id != self.track_id {
continue;
}
break self.decoder.decode(&packet).map(Some);
}
}
}
/// Run an analysis.
pub fn analyze(
config: &Config,
path: impl AsRef<Path>,
) -> Result<CompoundAnalyzerResult, AnalyzerError> {
log::debug!("Analyzing file: {}", path.as_ref().display());
let mut reader = AudioReader::new(&path)?;
let track = reader
.track()
.ok_or(AnalyzerError::NoSupportedAudioTracks)?;
let mut analyzer = CompoundAnalyzer::initialize(config, track)?;
let mut sample_buf = None;
while !analyzer.is_complete() {
let audio_buf = match reader.next_buffer() {
Ok(Some(buffer)) => buffer,
Ok(None) => break,
Err(SymphoniaError::DecodeError(err)) => Err(SymphoniaError::DecodeError(err))?,
Err(SymphoniaError::IoError(err)) => Err(err)?,
Err(err) => Err(err)?,
};
if sample_buf.is_none() {
let vec = Vec::<f32>::new();
sample_buf = Some(vec);
}
if let Some(ref mut samples) = &mut sample_buf {
samples.resize(audio_buf.samples_interleaved(), f32::MID);
audio_buf.copy_to_slice_interleaved(&mut *samples);
analyzer.feed(samples)?;
}
}
analyzer.finalize()
}