Change bpm to return u16 because of mp4ameta

Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
Serial 2021-07-09 11:08:24 -04:00
parent 7fe21b0315
commit 4cdc4a7589
6 changed files with 63 additions and 20 deletions

View file

@ -150,11 +150,15 @@ impl AudioTagEdit for ApeTag {
self.remove_key("Lyrics")
}
fn bpm(&self) -> Option<&str> {
self.get_value("BPM")
fn bpm(&self) -> Option<u16> {
if let Some(bpm) = self.get_value("BPM") {
return bpm.parse::<u16>().ok();
}
None
}
fn set_bpm(&mut self, bpm: &str) {
self.set_value("BPM", bpm)
fn set_bpm(&mut self, bpm: u16) {
self.set_value("BPM", bpm.to_string())
}
fn remove_bpm(&mut self) {
self.remove_key("BPM")

View file

@ -166,15 +166,17 @@ impl AudioTagEdit for Id3v2Tag {
self.inner.remove_genre()
}
fn bpm(&self) -> Option<&str> {
fn bpm(&self) -> Option<u16> {
if let Some(frame) = self.inner.get("TBPM") {
return frame.content().text();
if let Some(text) = frame.content().text() {
return text.parse::<u16>().ok();
}
}
None
}
fn set_bpm(&mut self, bpm: &str) {
self.inner.set_text("TBPM", bpm)
fn set_bpm(&mut self, bpm: u16) {
self.inner.set_text("TBPM", bpm.to_string())
}
fn remove_bpm(&mut self) {
self.inner.remove("TBPM")

View file

@ -115,10 +115,11 @@ impl AudioTagEdit for Mp4Tag {
self.inner.remove_lyrics()
}
fn set_bpm(&mut self, bpm: &str) {
if let Ok(bpm) = bpm.parse::<u16>() {
self.inner.set_bpm(bpm)
}
fn bpm(&self) -> Option<u16> {
self.inner.bpm()
}
fn set_bpm(&mut self, bpm: u16) {
self.inner.set_bpm(bpm)
}
fn remove_bpm(&mut self) {
self.inner.remove_bpm()

View file

@ -266,11 +266,15 @@ impl AudioTagEdit for OggTag {
self.inner.remove_key("LYRICS")
}
fn bpm(&self) -> Option<&str> {
self.inner.get_value("BPM")
fn bpm(&self) -> Option<u16> {
if let Some(bpm) = self.inner.get_value("BPM") {
return bpm.parse::<u16>().ok();
}
None
}
fn set_bpm(&mut self, bpm: &str) {
self.inner.set_value("BPM", bpm)
fn set_bpm(&mut self, bpm: u16) {
self.inner.set_value("BPM", bpm.to_string())
}
fn remove_bpm(&mut self) {
self.inner.remove_key("BPM")

View file

@ -83,11 +83,11 @@ pub trait AudioTagEdit {
fn remove_lyrics(&mut self) {}
/// Returns the lyrics
fn bpm(&self) -> Option<&str> {
fn bpm(&self) -> Option<u16> {
None
}
/// Sets the lyrics
fn set_bpm(&mut self, _bpm: &str) {}
fn set_bpm(&mut self, _bpm: u16) {}
/// Removes the lyrics
fn remove_bpm(&mut self) {}

View file

@ -22,6 +22,8 @@ macro_rules! add_tags {
println!("Reading file");
let mut tag = Tag::default().read_from_path_signature($file).unwrap();
let file = stringify!($file);
println!("Setting title");
tag.set_title("foo title");
@ -37,6 +39,17 @@ macro_rules! add_tags {
println!("Setting genre");
tag.set_genre("Country");
if file != stringify!("tests/assets/a.mp3")
&& file != stringify!("tests/assets/a.aiff")
&& file != stringify!("tests/assets/a-id3.wav")
{
println!("Setting Lyrics");
tag.set_lyrics("foo bar baz");
}
println!("Setting BPM");
tag.set_bpm(50);
println!("Setting album title");
tag.set_album_title("foo album title");
@ -58,8 +71,6 @@ macro_rules! add_tags {
},
);
let file = stringify!($file);
// Skip this since RIFF INFO doesn't store images, and MP4 doesn't specify what pictures are
if file != stringify!("tests/assets/a.wav") && file != stringify!("tests/assets/a.m4a") {
println!("Setting front cover");
@ -152,6 +163,17 @@ macro_rules! verify_write {
)
};
if file_name != stringify!("tests/assets/a.mp3")
&& file_name != stringify!("tests/assets/a.aiff")
&& file_name != stringify!("tests/assets/a-id3.wav")
{
println!("Verifying lyrics");
assert_eq!(tag.lyrics(), Some("foo bar baz"));
}
println!("Verifying BPM");
assert_eq!(tag.bpm(), Some(50));
println!("Verifying album artist");
assert_eq!(tag.album_artist_str(), Some("foo album artist"));
@ -196,6 +218,16 @@ macro_rules! remove_tags {
assert!(tag.genre().is_none());
tag.remove_genre();
println!("Removing lyrics");
tag.remove_lyrics();
assert!(tag.lyrics().is_none());
tag.remove_lyrics();
println!("Removing BPM");
tag.remove_bpm();
assert!(tag.bpm().is_none());
tag.remove_bpm();
println!("Removing album title");
tag.remove_album_title();
assert!(tag.album_title().is_none());