musicutil/src/types.rs

117 lines
2.8 KiB
Rust
Raw Normal View History

2022-08-16 09:05:18 +01:00
use std::path::PathBuf;
use crate::utils::format_detection::FileFormat;
2022-10-22 15:15:37 +01:00
#[derive(Debug, Clone)]
2022-08-16 09:05:18 +01:00
pub struct Tags {
pub title: String,
pub artist: String,
}
impl Default for Tags {
fn default() -> Self {
Tags {
title: "".to_string(),
artist: "".to_string(),
}
}
}
#[derive(Debug, Clone)]
pub struct ReplayGainData {
pub track_gain: String,
pub track_peak: String,
}
#[derive(Debug, Clone)]
pub struct ReplayGainRawData {
pub track_gain: f64,
pub track_peak: f64,
}
impl ReplayGainRawData {
pub fn to_normal(&self, is_ogg_opus: bool) -> ReplayGainData {
if is_ogg_opus {
ReplayGainData {
track_gain: format!("{:.6}", (self.track_gain * 256.0).ceil()),
track_peak: "".to_string(), // Not Required
}
} else {
ReplayGainData {
track_gain: format!("{:.2} dB", self.track_gain),
track_peak: format!("{:.6}", self.track_peak),
}
}
}
}
2022-10-22 21:01:36 +01:00
#[derive(Default, Debug, Clone)]
pub struct AudioFileInfo {
pub tags: Tags,
pub contains_replaygain: bool,
2022-10-22 21:01:36 +01:00
pub supports_replaygain: bool,
pub format: Option<FileFormat>,
2022-08-16 09:05:18 +01:00
}
2022-10-22 15:15:37 +01:00
#[derive(Debug, Clone)]
2022-08-16 09:05:18 +01:00
pub struct File {
pub filename: String,
pub extension: String,
// relative path to file's folder
2022-10-22 14:16:02 +01:00
pub path_to: PathBuf,
// path to folder from source
2022-10-22 14:16:02 +01:00
pub path_from_source: PathBuf,
pub extra_files: Vec<File>,
pub info: AudioFileInfo,
2022-08-16 09:05:18 +01:00
}
impl File {
2022-10-22 14:16:02 +01:00
pub fn from_path(source_dir: String, full_path: PathBuf) -> File {
let full_file_path = PathBuf::from(&source_dir).join(full_path);
2022-08-16 09:05:18 +01:00
let filename = full_file_path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_string();
let extension = full_file_path.extension();
let extension = if let Some(extension) = extension {
extension.to_str().unwrap().to_string()
} else {
"".to_string()
};
2022-08-16 09:05:18 +01:00
let path_from_src = full_file_path.strip_prefix(&source_dir).unwrap();
let mut folder_path_from_src = path_from_src.to_path_buf();
folder_path_from_src.pop();
let path_to = PathBuf::from(&source_dir).join(&folder_path_from_src);
2022-10-22 14:16:02 +01:00
File {
filename,
extension,
path_from_source: folder_path_from_src,
path_to,
2022-10-22 21:01:36 +01:00
extra_files: Vec::new(),
info: AudioFileInfo::default(),
2022-10-22 14:16:02 +01:00
}
2022-08-16 09:05:18 +01:00
}
2022-08-16 09:05:18 +01:00
pub fn join_filename(&self) -> String {
2022-10-22 14:16:02 +01:00
format!("{}.{}", self.filename, self.extension)
2022-08-16 09:05:18 +01:00
}
2022-10-22 14:16:02 +01:00
pub fn join_path_to(&self) -> PathBuf {
PathBuf::from(&self.path_to).join(self.join_filename())
2022-08-16 09:05:18 +01:00
}
2022-10-22 14:16:02 +01:00
pub fn join_path_from_source(&self) -> PathBuf {
PathBuf::from(&self.path_from_source).join(self.join_filename())
2022-08-16 09:05:18 +01:00
}
}