bliss-audio 0.11.2

A song analysis library for making playlists
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! The default decoder module. It uses [ffmpeg](https://ffmpeg.org/) in
//! order to decode and resample songs. A very good choice for 99% of
//! the users.

use crate::decoder::{Decoder, PreAnalyzedSong};
use crate::{BlissError, BlissResult, CHANNELS, SAMPLE_RATE};
use ::log::warn;
use ffmpeg_next;
use ffmpeg_next::codec::threading::{Config, Type as ThreadingType};
use ffmpeg_next::util::channel_layout::ChannelLayout;
use ffmpeg_next::util::error::Error;
use ffmpeg_next::util::error::EINVAL;
use ffmpeg_next::util::format::sample::{Sample, Type};
use ffmpeg_next::util::frame::audio::Audio;
use ffmpeg_next::util::log;
use ffmpeg_next::util::log::level::Level;
use ffmpeg_next::{media, util};
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
use std::thread;
use std::time::Duration;

use std::path::Path;

/// The actual FFmpeg decoder.
///
/// To use it, one might write `use FFmpegDecoder as Decoder;`,
/// `use super::decoder::Decoder as DecoderTrait;`, and then use
/// `Decoder::song_from_path`
pub struct FFmpegDecoder;

struct SendChannelLayout(ChannelLayout);
// Safe because the other thread just reads the channel layout
unsafe impl Send for SendChannelLayout {}

impl FFmpegDecoder {
    fn resample_frame(
        rx: Receiver<Audio>,
        in_codec_format: Sample,
        sent_in_channel_layout: SendChannelLayout,
        in_rate: u32,
        mut sample_array: Vec<f32>,
        empty_in_channel_layout: bool,
    ) -> BlissResult<Vec<f32>> {
        let in_channel_layout = sent_in_channel_layout.0;
        let mut resample_context = ffmpeg_next::software::resampling::context::Context::get(
            in_codec_format,
            in_channel_layout,
            in_rate,
            Sample::F32(Type::Packed),
            ffmpeg_next::util::channel_layout::ChannelLayout::MONO,
            SAMPLE_RATE,
        )
        .map_err(|e| {
            BlissError::DecodingError(format!(
                "while trying to allocate resampling context: {e:?}",
            ))
        })?;

        let mut resampled = ffmpeg_next::frame::Audio::empty();
        let mut something_happened = false;
        for mut decoded in rx.iter() {
            #[cfg(not(feature = "ffmpeg_7_0"))]
            let is_channel_layout_empty = decoded.channel_layout() == ChannelLayout::empty();
            #[cfg(feature = "ffmpeg_7_0")]
            let is_channel_layout_empty = decoded.channel_layout().is_empty();

            // If the decoded layout is empty, it means we forced the
            // "in_channel_layout" to something default, not that
            // the format is wrong.
            if empty_in_channel_layout && is_channel_layout_empty {
                decoded.set_channel_layout(in_channel_layout);
            } else if in_codec_format != decoded.format()
                || (in_channel_layout != decoded.channel_layout())
                || in_rate != decoded.rate()
            {
                warn!("received decoded packet with wrong format; file might be corrupted.");
                continue;
            }
            something_happened = true;
            resampled = ffmpeg_next::frame::Audio::empty();
            resample_context
                .run(&decoded, &mut resampled)
                .map_err(|e| {
                    BlissError::DecodingError(format!("while trying to resample song: {e:?}"))
                })?;
            FFmpegDecoder::push_to_sample_array(&resampled, &mut sample_array);
        }
        if !something_happened {
            return Ok(sample_array);
        }
        // TODO when ffmpeg-next will be active again: shouldn't we allocate
        // `resampled` again?
        loop {
            match resample_context.flush(&mut resampled).map_err(|e| {
                BlissError::DecodingError(format!("while trying to resample song: {e:?}"))
            })? {
                Some(_) => {
                    FFmpegDecoder::push_to_sample_array(&resampled, &mut sample_array);
                }
                None => {
                    if resampled.samples() == 0 {
                        break;
                    }
                    FFmpegDecoder::push_to_sample_array(&resampled, &mut sample_array);
                }
            };
        }
        Ok(sample_array)
    }

    fn push_to_sample_array(frame: &ffmpeg_next::frame::Audio, sample_array: &mut Vec<f32>) {
        if frame.samples() == 0 {
            return;
        }
        // Account for the padding
        let actual_size = util::format::sample::Buffer::size(
            Sample::F32(Type::Packed),
            CHANNELS,
            frame.samples(),
            false,
        );
        let f32_frame: Vec<f32> = frame.data(0)[..actual_size]
            .chunks_exact(4)
            .map(|x| {
                let mut a: [u8; 4] = [0; 4];
                a.copy_from_slice(x);
                f32::from_le_bytes(a)
            })
            .collect();
        sample_array.extend_from_slice(&f32_frame);
    }
}

impl Decoder for FFmpegDecoder {
    fn decode(path: &Path) -> BlissResult<PreAnalyzedSong> {
        ffmpeg_next::init().map_err(|e| {
            BlissError::DecodingError(format!(
                "ffmpeg init error while decoding file '{}': {:?}.",
                path.display(),
                e
            ))
        })?;
        log::set_level(Level::Quiet);
        let mut song = PreAnalyzedSong {
            path: path.into(),
            ..Default::default()
        };
        let mut ictx = ffmpeg_next::format::input(&path).map_err(|e| {
            BlissError::DecodingError(format!(
                "while opening format for file '{}': {:?}.",
                path.display(),
                e
            ))
        })?;
        let (mut decoder, stream, expected_sample_number) = {
            let input = ictx.streams().best(media::Type::Audio).ok_or_else(|| {
                BlissError::DecodingError(format!(
                    "No audio stream found for file '{}'.",
                    path.display()
                ))
            })?;
            let mut context = ffmpeg_next::codec::context::Context::from_parameters(
                input.parameters(),
            )
            .map_err(|e| {
                BlissError::DecodingError(format!(
                    "Could not load the codec context for file '{}': {:?}",
                    path.display(),
                    e
                ))
            })?;
            context.set_threading(Config {
                kind: ThreadingType::Frame,
                count: 0,
                #[cfg(not(feature = "ffmpeg_6_0"))]
                safe: true,
            });
            let decoder = context.decoder().audio().map_err(|e| {
                BlissError::DecodingError(format!(
                    "when finding decoder for file '{}': {:?}.",
                    path.display(),
                    e
                ))
            })?;

            // Add SAMPLE_RATE to have one second margin to avoid reallocating if
            // the duration is slightly more than estimated
            // TODO>1.0 another way to get the exact number of samples is to decode
            // everything once, compute the real number of samples from that,
            // allocate the array with that number, and decode again. Check
            // what's faster between reallocating, and just have one second
            // leeway.
            let expected_sample_number = (SAMPLE_RATE as f32 * input.duration() as f32
                / input.time_base().denominator() as f32)
                .ceil()
                + SAMPLE_RATE as f32;
            (decoder, input.index(), expected_sample_number)
        };
        let sample_array: Vec<f32> = Vec::with_capacity(expected_sample_number as usize);
        if let Some(title) = ictx.metadata().get("title") {
            song.title = match title {
                "" => None,
                t => Some(t.to_string()),
            };
        };
        if let Some(artist) = ictx.metadata().get("artist") {
            song.artist = match artist {
                "" => None,
                a => Some(a.to_string()),
            };
        };
        if let Some(album) = ictx.metadata().get("album") {
            song.album = match album {
                "" => None,
                a => Some(a.to_string()),
            };
        };
        if let Some(genre) = ictx.metadata().get("genre") {
            song.genre = match genre {
                "" => None,
                g => Some(g.to_string()),
            };
        };
        if let Some(track_number) = ictx.metadata().get("track") {
            song.track_number = match track_number {
                "" => None,
                t => t
                    .parse::<i32>()
                    .ok()
                    .or_else(|| t.split_once('/').and_then(|(n, _)| n.parse::<i32>().ok())),
            };
        };
        if let Some(disc_number) = ictx.metadata().get("disc") {
            song.disc_number = match disc_number {
                "" => None,
                t => t
                    .parse::<i32>()
                    .ok()
                    .or_else(|| t.split_once('/').and_then(|(n, _)| n.parse::<i32>().ok())),
            };
        };
        if let Some(album_artist) = ictx.metadata().get("album_artist") {
            song.album_artist = match album_artist {
                "" => None,
                t => Some(t.to_string()),
            };
        };

        #[cfg(not(feature = "ffmpeg_7_0"))]
        let is_channel_layout_empty = decoder.channel_layout() == ChannelLayout::empty();
        #[cfg(feature = "ffmpeg_7_0")]
        let is_channel_layout_empty = decoder.channel_layout().is_empty();

        let (empty_in_channel_layout, in_channel_layout) = {
            if is_channel_layout_empty {
                (true, ChannelLayout::default(decoder.channels().into()))
            } else {
                (false, decoder.channel_layout())
            }
        };
        decoder.set_channel_layout(in_channel_layout);

        let in_channel_layout_to_send = SendChannelLayout(in_channel_layout);

        let (tx, rx) = mpsc::channel();
        let in_codec_format = decoder.format();
        let in_codec_rate = decoder.rate();
        let child = thread::spawn(move || {
            FFmpegDecoder::resample_frame(
                rx,
                in_codec_format,
                in_channel_layout_to_send,
                in_codec_rate,
                sample_array,
                empty_in_channel_layout,
            )
        });
        for (s, packet) in ictx.packets() {
            if s.index() != stream {
                continue;
            }
            match decoder.send_packet(&packet) {
                Ok(_) => (),
                Err(Error::Other { errno: EINVAL }) => {
                    return Err(BlissError::DecodingError(format!(
                        "wrong codec opened for file '{}.",
                        path.display(),
                    )))
                }
                Err(Error::Eof) => {
                    warn!(
                        "Premature EOF reached while decoding file '{}'.",
                        path.display()
                    );
                    drop(tx);
                    song.sample_array = child.join().unwrap()?;
                    return Ok(song);
                }
                Err(e) => warn!("{} when decoding file '{}'", e, path.display()),
            };

            loop {
                let mut decoded = ffmpeg_next::frame::Audio::empty();
                match decoder.receive_frame(&mut decoded) {
                    Ok(_) => {
                        tx.send(decoded).map_err(|e| {
                        BlissError::DecodingError(format!(
                            "while sending decoded frame to the resampling thread for file '{}': {:?}",
                            path.display(),
                            e,
                        ))
                    })?;
                    }
                    Err(_) => break,
                }
            }
        }

        // Flush the stream
        let packet = ffmpeg_next::codec::packet::Packet::empty();
        match decoder.send_packet(&packet) {
            Ok(_) => (),
            Err(Error::Other { errno: EINVAL }) => {
                return Err(BlissError::DecodingError(format!(
                    "wrong codec opened for file '{}'.",
                    path.display()
                )))
            }
            Err(Error::Eof) => {
                warn!(
                    "Premature EOF reached while decoding file '{}'.",
                    path.display()
                );
                drop(tx);
                song.sample_array = child.join().unwrap()?;
                return Ok(song);
            }
            Err(e) => warn!("error while decoding {}: {}", path.display(), e),
        };

        loop {
            let mut decoded = ffmpeg_next::frame::Audio::empty();
            match decoder.receive_frame(&mut decoded) {
                Ok(_) => {
                    tx.send(decoded).map_err(|e| {
                        BlissError::DecodingError(format!(
                        "while sending decoded frame to the resampling thread for file '{}': {:?}",
                        path.display(),
                        e
                    ))
                    })?;
                }
                Err(_) => break,
            }
        }

        drop(tx);
        song.sample_array = child.join().unwrap()?;
        let duration_seconds = song.sample_array.len() as f32 / SAMPLE_RATE as f32;
        song.duration = Duration::from_nanos((duration_seconds * 1e9_f32).round() as u64);
        Ok(song)
    }
}

#[cfg(test)]
mod tests {
    use crate::decoder::ffmpeg::FFmpegDecoder as Decoder;
    use crate::decoder::Decoder as DecoderTrait;
    use crate::decoder::PreAnalyzedSong;
    use crate::AnalysisOptions;
    use crate::BlissError;
    use crate::Song;
    use crate::SAMPLE_RATE;
    use adler32::RollingAdler32;
    use pretty_assertions::assert_eq;
    use std::num::NonZero;
    use std::path::Path;

    fn _test_decode(path: &Path, expected_hash: u32) {
        let song = Decoder::decode(path).unwrap();
        let mut hasher = RollingAdler32::new();
        for sample in song.sample_array.iter() {
            hasher.update_buffer(&sample.to_le_bytes());
        }

        assert_eq!(expected_hash, hasher.hash());
    }

    #[test]
    fn test_tags() {
        let song = Decoder::decode(Path::new("data/s16_mono_22_5kHz.flac")).unwrap();
        assert_eq!(song.artist, Some(String::from("David TMX")));
        assert_eq!(
            song.album_artist,
            Some(String::from("David TMX - Album Artist"))
        );
        assert_eq!(song.title, Some(String::from("Renaissance")));
        assert_eq!(song.album, Some(String::from("Renaissance")));
        assert_eq!(song.track_number, Some(2));
        assert_eq!(song.disc_number, Some(1));
        assert_eq!(song.genre, Some(String::from("Pop")));
        // Test that there is less than 10ms of difference between what
        // the song advertises and what we compute.
        assert!((song.duration.as_millis() as f32 - 11070.).abs() < 10.);
    }

    #[test]
    fn test_special_tags() {
        // This file has tags like `DISC: 02/05` and `TRACK: 06/24`.
        let song = Decoder::decode(Path::new("data/special-tags.mp3")).unwrap();
        assert_eq!(song.disc_number, Some(2));
        assert_eq!(song.track_number, Some(6));
    }

    #[test]
    fn test_unsupported_tags_format() {
        // This file has tags like `TRACK: 02test/05`.
        let song = Decoder::decode(Path::new("data/unsupported-tags.mp3")).unwrap();
        assert_eq!(song.track_number, None);
    }

    #[test]
    fn test_empty_tags() {
        let song = Decoder::decode(Path::new("data/no_tags.flac")).unwrap();
        assert_eq!(song.artist, None);
        assert_eq!(song.title, None);
        assert_eq!(song.album, None);
        assert_eq!(song.track_number, None);
        assert_eq!(song.disc_number, None);
        assert_eq!(song.genre, None);
    }

    #[test]
    fn test_resample_mono() {
        let path = Path::new("data/s32_mono_44_1_kHz.flac");
        let expected_hash = 0xa0f8b8af;
        _test_decode(&path, expected_hash);
    }

    #[test]
    fn test_resample_multi() {
        let path = Path::new("data/s32_stereo_44_1_kHz.flac");
        let expected_hash = 0xbbcba1cf;
        _test_decode(&path, expected_hash);
    }

    #[test]
    fn test_resample_stereo() {
        let path = Path::new("data/s16_stereo_22_5kHz.flac");
        let expected_hash = 0x1d7b2d6d;
        _test_decode(&path, expected_hash);
    }

    #[test]
    fn test_decode_mono() {
        let path = Path::new("data/s16_mono_22_5kHz.flac");
        // Obtained through
        // ffmpeg -i data/s16_mono_22_5kHz.flac -ar 22050 -ac 1 -c:a pcm_f32le
        // -f hash -hash addler32 -
        let expected_hash = 0x5e01930b;
        _test_decode(&path, expected_hash);
    }

    #[test]
    fn test_decode_mp3() {
        let path = Path::new("data/s32_stereo_44_1_kHz.mp3");
        // Obtained through
        // ffmpeg -i data/s16_mono_22_5kHz.mp3 -ar 22050 -ac 1 -c:a pcm_f32le
        // -f hash -hash addler32 -
        let expected_hash = 0x69ca6906;
        _test_decode(&path, expected_hash);
    }

    #[test]
    #[cfg(feature = "ffmpeg")]
    fn test_dont_panic_no_channel_layout() {
        let path = Path::new("data/no_channel.wav");
        let expected_hash = 0xd594429c;
        _test_decode(&path, expected_hash);
    }

    #[test]
    fn test_decode_right_capacity_vec() {
        let path = Path::new("data/s16_mono_22_5kHz.flac");
        let song = Decoder::decode(&path).unwrap();
        let sample_array = song.sample_array;
        assert_eq!(
            sample_array.len() + SAMPLE_RATE as usize,
            sample_array.capacity()
        );

        let path = Path::new("data/s32_stereo_44_1_kHz.flac");
        let song = Decoder::decode(&path).unwrap();
        let sample_array = song.sample_array;
        assert_eq!(
            sample_array.len() + SAMPLE_RATE as usize,
            sample_array.capacity()
        );

        let path = Path::new("data/capacity_fix.ogg");
        let song = Decoder::decode(&path).unwrap();
        let sample_array = song.sample_array;
        assert!(sample_array.len() as f32 / sample_array.capacity() as f32 > 0.90);
        assert!(sample_array.len() as f32 / (sample_array.capacity() as f32) < 1.);
    }

    #[test]
    fn test_decode_errors() {
        assert_eq!(
        Decoder::decode(Path::new("nonexistent")).unwrap_err(),
        BlissError::DecodingError(String::from(
            "while opening format for file 'nonexistent': ffmpeg::Error(2: No such file or directory)."
        )),
    );
        assert_eq!(
            Decoder::decode(Path::new("data/picture.png")).unwrap_err(),
            BlissError::DecodingError(String::from(
                "No audio stream found for file 'data/picture.png'."
            )),
        );
    }

    #[test]
    fn test_decode_wav() {
        let expected_hash = 0xde831e82;
        _test_decode(Path::new("data/piano.wav"), expected_hash);
    }

    #[test]
    fn test_try_from() {
        let pre_analyzed_song = PreAnalyzedSong::default();
        assert!(<PreAnalyzedSong as TryInto<Song>>::try_into(pre_analyzed_song).is_err());
    }

    #[test]
    fn test_analyze_paths() {
        let analysis = Decoder::analyze_paths(["data/nonexistent", "data/piano.flac"])
            .map(|s| s.1.is_ok())
            .collect::<Vec<_>>();
        assert_eq!(analysis, vec![false, true]);
    }

    #[test]
    fn test_analyze_paths_with_cores() {
        // Analyze with a number of cores greater than the system's number of cores.
        let analysis = Decoder::analyze_paths_with_options(
            [
                "data/nonexistent",
                "data/piano.flac",
                "data/nonexistent.cue",
            ],
            AnalysisOptions {
                number_cores: NonZero::new(usize::MAX).unwrap(),
                ..Default::default()
            },
        )
        .map(|s| s.1.is_ok())
        .collect::<Vec<_>>();
        assert_eq!(analysis, vec![false, true, false]);
    }

    #[test]
    fn test_analyze_paths_with_cores_empty_paths() {
        let analysis = Decoder::analyze_paths_with_options::<&str, [_; 0]>(
            [],
            AnalysisOptions {
                number_cores: NonZero::new(1).unwrap(),
                ..Default::default()
            },
        )
        .collect::<Vec<_>>();
        assert_eq!(analysis, vec![]);
    }
}