2022-08-02 10:45:37 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
fmt::Debug,
|
|
|
|
ops::{Deref, DerefMut},
|
|
|
|
};
|
2021-12-07 22:22:24 +00:00
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
use librespot_core::FileId;
|
2021-12-07 22:22:24 +00:00
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
use librespot_protocol as protocol;
|
2023-01-17 20:46:14 +00:00
|
|
|
pub use protocol::metadata::audio_file::Format as AudioFileFormat;
|
2021-12-07 22:22:24 +00:00
|
|
|
use protocol::metadata::AudioFile as AudioFileMessage;
|
|
|
|
|
2022-08-02 10:45:37 +00:00
|
|
|
use crate::util::impl_deref_wrapped;
|
|
|
|
|
2022-07-31 22:44:43 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2021-12-07 22:22:24 +00:00
|
|
|
pub struct AudioFiles(pub HashMap<AudioFileFormat, FileId>);
|
|
|
|
|
2022-08-02 10:45:37 +00:00
|
|
|
impl_deref_wrapped!(AudioFiles, HashMap<AudioFileFormat, FileId>);
|
2021-12-07 22:22:24 +00:00
|
|
|
|
2022-01-03 21:20:29 +00:00
|
|
|
impl AudioFiles {
|
|
|
|
pub fn is_ogg_vorbis(format: AudioFileFormat) -> bool {
|
|
|
|
matches!(
|
|
|
|
format,
|
|
|
|
AudioFileFormat::OGG_VORBIS_320
|
|
|
|
| AudioFileFormat::OGG_VORBIS_160
|
|
|
|
| AudioFileFormat::OGG_VORBIS_96
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_mp3(format: AudioFileFormat) -> bool {
|
|
|
|
matches!(
|
|
|
|
format,
|
|
|
|
AudioFileFormat::MP3_320
|
|
|
|
| AudioFileFormat::MP3_256
|
|
|
|
| AudioFileFormat::MP3_160
|
|
|
|
| AudioFileFormat::MP3_96
|
2022-01-03 23:50:45 +00:00
|
|
|
| AudioFileFormat::MP3_160_ENC
|
2022-01-03 21:20:29 +00:00
|
|
|
)
|
|
|
|
}
|
2022-01-03 23:50:45 +00:00
|
|
|
|
|
|
|
pub fn is_flac(format: AudioFileFormat) -> bool {
|
|
|
|
matches!(format, AudioFileFormat::FLAC_FLAC)
|
|
|
|
}
|
2022-01-03 21:20:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 22:22:24 +00:00
|
|
|
impl From<&[AudioFileMessage]> for AudioFiles {
|
|
|
|
fn from(files: &[AudioFileMessage]) -> Self {
|
2023-01-17 20:46:14 +00:00
|
|
|
let audio_files: HashMap<AudioFileFormat, FileId> = files
|
2021-12-07 22:22:24 +00:00
|
|
|
.iter()
|
2022-01-02 23:13:28 +00:00
|
|
|
.filter_map(|file| {
|
2023-01-17 20:46:14 +00:00
|
|
|
let file_id = FileId::from(file.file_id());
|
2022-01-02 23:13:28 +00:00
|
|
|
if file.has_format() {
|
2023-01-17 20:46:14 +00:00
|
|
|
Some((file.format(), file_id))
|
2022-01-02 23:13:28 +00:00
|
|
|
} else {
|
|
|
|
trace!("Ignoring file <{}> with unspecified format", file_id);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2021-12-07 22:22:24 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
AudioFiles(audio_files)
|
|
|
|
}
|
|
|
|
}
|