From 9e77b634be652477ee1be67b0b0e862c7fba5a1d Mon Sep 17 00:00:00 2001 From: Marekkon5 Date: Thu, 29 Aug 2024 23:12:06 +0200 Subject: [PATCH] Camelot in renamer --- crates/onetagger-autotag/src/lib.rs | 6 ++---- crates/onetagger-renamer/src/docs.rs | 5 +++-- crates/onetagger-renamer/src/parser.rs | 22 ++++++++++++++++++++++ crates/onetagger-tagger/src/lib.rs | 17 +++++++++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/crates/onetagger-autotag/src/lib.rs b/crates/onetagger-autotag/src/lib.rs index d2d45629..8c0c8a36 100644 --- a/crates/onetagger-autotag/src/lib.rs +++ b/crates/onetagger-autotag/src/lib.rs @@ -23,7 +23,7 @@ use crossbeam_channel::{unbounded, Sender, Receiver}; use onetagger_tag::{AudioFileFormat, Tag, Field, TagDate, CoverType, TagImpl, EXTENSIONS}; use onetagger_shared::Settings; use onetagger_player::AudioSources; -use onetagger_tagger::{Track, AudioFileInfo, TaggerConfig, StylesOptions, AutotaggerSource, AutotaggerSourceBuilder, CAMELOT_NOTES}; +use onetagger_tagger::{Track, AudioFileInfo, TaggerConfig, StylesOptions, AutotaggerSource, AutotaggerSourceBuilder}; use crate::shazam::Shazam; mod shazam; @@ -121,9 +121,7 @@ impl TrackImpl for Track { let mut value = self.key.as_ref().unwrap().to_string(); // Convert to camelot if config.camelot { - if let Some((_, c)) = CAMELOT_NOTES.iter().find(|(o, _)| o == &value) { - value = c.to_string(); - } + value = onetagger_tagger::to_camelot(&value).to_owned(); } tag.set_field(Field::Key, vec![value], config.overwrite_tag(SupportedTag::Key)); } diff --git a/crates/onetagger-renamer/src/docs.rs b/crates/onetagger-renamer/src/docs.rs index 57eba665..7d82f18b 100644 --- a/crates/onetagger-renamer/src/docs.rs +++ b/crates/onetagger-renamer/src/docs.rs @@ -42,7 +42,7 @@ lazy_static! { SymbolDoc::prop("last", "Get the last item in an array"), ]; - pub static ref FUNCTIONS: [SymbolDoc; 15] = [ + pub static ref FUNCTIONS: [SymbolDoc; 17] = [ SymbolDoc::f("lower", "Convert all to lowercase", vec![]), SymbolDoc::f("lowercase", "Convert all to lowercase", vec![]), SymbolDoc::f("upper", "Convert all to uppercase", vec![]), @@ -58,7 +58,8 @@ lazy_static! { SymbolDoc::f("join", "Join array into string with custom separator", vec![DocParameter::s("separator", true)]), SymbolDoc::f("parent", "Get parent folder of path", vec![]), SymbolDoc::f("filename", "Get file/folder name of path", vec![]), - + SymbolDoc::f("camelot", "Convert key to camelot, or keep original", vec![DocParameter::s("value", true)]), + SymbolDoc::f("uncamelot", "Convert key from camelot, or keep original", vec![DocParameter::s("value", true)]), ]; } diff --git a/crates/onetagger-renamer/src/parser.rs b/crates/onetagger-renamer/src/parser.rs index c28621c4..9a609e62 100644 --- a/crates/onetagger-renamer/src/parser.rs +++ b/crates/onetagger-renamer/src/parser.rs @@ -786,6 +786,28 @@ impl Token for TokenFunction { Some(Data::String(a.last()?.to_string())) }, } + }, + // Conver to camelot + "camelot" => { + match data { + Data::String(s) => { + Some(Data::String(onetagger_tagger::to_camelot(s).to_owned())) + }, + Data::Array(a) => { + Some(Data::Array(a.iter().map(|v| onetagger_tagger::to_camelot(v).to_owned()).collect())) + } + } + }, + // Convert from camelot + "uncamelot" => { + match data { + Data::String(s) => { + Some(Data::String(onetagger_tagger::from_camelot(s).to_owned())) + }, + Data::Array(a) => { + Some(Data::Array(a.iter().map(|v| onetagger_tagger::from_camelot(v).to_owned()).collect())) + } + } } f => { error!("Invalid function: {f}!"); diff --git a/crates/onetagger-tagger/src/lib.rs b/crates/onetagger-tagger/src/lib.rs index 288e1069..fdb1b10d 100644 --- a/crates/onetagger-tagger/src/lib.rs +++ b/crates/onetagger-tagger/src/lib.rs @@ -474,6 +474,23 @@ pub const CAMELOT_NOTES: [(&str, &str); 35] = [ ("E", "12B"), ]; +/// Convert to camelot or return original +pub fn to_camelot(key: &str) -> &str { + if let Some((_, v)) = CAMELOT_NOTES.iter().find(|(k, _)| *k == key.trim()) { + return *v; + } + key +} + +/// Convert from camelot to normal key +pub fn from_camelot(key: &str) -> &str { + if let Some((v, _)) = CAMELOT_NOTES.iter().find(|(_, k)| *k == key.trim()) { + return *v; + } + key +} + + pub trait LyricsExt { /// Generate LRC data /// If meta is present, will be written