TagLib: Implement preliminary writer class

This is not currently being used anywhere, but may function eventually.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
This commit is contained in:
Christopher Snowhill 2025-02-17 04:25:15 -08:00
parent 73161fdc12
commit c74423c32d

View file

@ -13,11 +13,52 @@
#import "Logging.h"
static NSString *toString(id value) {
if([value isKindOfClass:[NSString class]])
return (NSString *)value;
else if([value isKindOfClass:[NSArray class]]) {
NSArray *arrayValue = value;
return [arrayValue componentsJoinedByString:@", "];
} else if([value isKindOfClass:[NSNumber class]]) {
NSNumber *numberValue = value;
return [NSString stringWithFormat:@"%@", numberValue];
} else {
return @"";
}
}
static int toInt(id value) {
if([value isKindOfClass:[NSNumber class]]) {
NSNumber *numberValue = value;
return [numberValue intValue];
} else {
return 0;
}
}
static TagLib::Tag::ReplayGain toReplaygain(NSDictionary *tags) {
TagLib::Tag::ReplayGain rg;
NSNumber *lReplaygainAlbumGain = [tags valueForKey:@"replaygain_album_gain"];
NSNumber *lReplaygainAlbumPeak = [tags valueForKey:@"replaygain_album_peak"];
NSNumber *lReplaygainTrackGain = [tags valueForKey:@"replaygain_track_gain"];
NSNumber *lReplaygainTrackPeak = [tags valueForKey:@"replaygain_track_peak"];
if(lReplaygainAlbumGain)
rg.setAlbumGain([lReplaygainAlbumGain floatValue]);
if(lReplaygainAlbumPeak)
rg.setAlbumPeak([lReplaygainAlbumPeak floatValue]);
if(lReplaygainTrackGain)
rg.setTrackGain([lReplaygainTrackGain floatValue]);
if(lReplaygainTrackPeak)
rg.setTrackPeak([lReplaygainTrackPeak floatValue]);
return rg;
}
@implementation TagLibMetadataWriter
+ (int)putMetadataInURL:(NSURL *)url tagData:(NSDictionary *)tagData {
NSString *lArtist = @"", *lTitle = @"", *lAlbum = @"", *lGenre = @"";
// int lYear = 0, lTrack = 0;
NSString *lTitle = @"", *lAlbumArtist = @"", *lArtist = @"", *lComposer = @"", *lAlbum = @"", *lUnsyncedLyrics = @"", *lComment = @"", *lGenre = @"", *lCuesheet = @"";
int lYear = 0, lTrack = 0, lDisc = 0;
TagLib::Tag::ReplayGain lReplaygain;
try {
TagLib::FileRef f((const char *)[[url path] UTF8String], false);
@ -25,50 +66,117 @@
const TagLib::Tag *tag = f.tag();
if(tag) {
const TagLib::String pArtist, pTitle, pAlbum, pGenre, pComment;
bool altered = false;
lArtist = [tagData valueForKey:@"artist"];
lTitle = [tagData valueForKey:@"title"];
lAlbum = [tagData valueForKey:@"album"];
lGenre = [tagData valueForKey:@"genre"];
TagLib::String pTitle, pAlbumArtist, pArtist, pComposer, pAlbum, pUnsyncedLyrics, pComment, pGenre, pCuesheet;
int pYear, pTrack, pDisc;
TagLib::Tag::ReplayGain pReplaygain;
f.tag()->setTitle([lTitle UTF8String]);
f.tag()->setArtist([lArtist UTF8String]);
lTitle = toString([tagData valueForKey:@"title"]);
lAlbumArtist = toString([tagData valueForKey:@"albumartist"]);
lArtist = toString([tagData valueForKey:@"artist"]);
lComposer = toString([tagData valueForKey:@"composer"]);
lAlbum = toString([tagData valueForKey:@"album"]);
lUnsyncedLyrics = toString([tagData valueForKey:@"unsyncedlyrics"]);
lComment = toString([tagData valueForKey:@"comment"]);
lGenre = toString([tagData valueForKey:@"genre"]);
lCuesheet = toString([tagData valueForKey:@"cuesheet"]);
lYear = toInt([tagData valueForKey:@"year"]);
lTrack = toInt([tagData valueForKey:@"track"]);
lDisc = toInt([tagData valueForKey:@"disc"]);
lReplaygain = toReplaygain(tagData);
pTitle = tag->title();
pAlbumArtist = tag->albumartist();
pArtist = tag->artist();
pComposer = tag->composer();
pAlbum = tag->album();
pUnsyncedLyrics = tag->unsyncedlyrics();
pComment = tag->comment();
pGenre = tag->genre();
pCuesheet = tag->cuesheet();
pYear = tag->year();
pTrack = tag->track();
pDisc = tag->disc();
pReplaygain = tag->replaygain();
if(pTitle.isEmpty() != ![lTitle length] ||
![lTitle isEqualToString:[NSString stringWithUTF8String:pTitle.toCString(true)]]) {
f.tag()->setTitle(TagLib::String([lTitle UTF8String]));
altered = true;
}
if(pAlbumArtist.isEmpty() != ![lAlbumArtist length] ||
![lAlbumArtist isEqualToString:[NSString stringWithUTF8String:pAlbumArtist.toCString(true)]]) {
f.tag()->setAlbumArtist(TagLib::String([lAlbumArtist UTF8String]));
altered = true;
}
if(pArtist.isEmpty() != ![lArtist length] ||
![lArtist isEqualToString:[NSString stringWithUTF8String:pArtist.toCString(true)]]) {
f.tag()->setArtist(TagLib::String([lArtist UTF8String]));
altered = true;
}
if(pComposer.isEmpty() != ![lComposer length] ||
![lComposer isEqualToString:[NSString stringWithUTF8String:pComposer.toCString(true)]]) {
f.tag()->setComposer(TagLib::String([lComposer UTF8String]));
altered = true;
}
if(pAlbum.isEmpty() != ![lAlbum length] ||
![lAlbum isEqualToString:[NSString stringWithUTF8String:pAlbum.toCString(true)]]) {
f.tag()->setAlbum(TagLib::String([lComposer UTF8String]));
altered = true;
}
if(pUnsyncedLyrics.isEmpty() != ![lUnsyncedLyrics length] ||
![lUnsyncedLyrics isEqualToString:[NSString stringWithUTF8String:pUnsyncedLyrics.toCString(true)]]) {
f.tag()->setUnsyncedLyrics(TagLib::String([lUnsyncedLyrics UTF8String]));
altered = true;
}
if(pComment.isEmpty() != ![lComment length] ||
![lComment isEqualToString:[NSString stringWithUTF8String:pComment.toCString(true)]]) {
f.tag()->setComment(TagLib::String([lComment UTF8String]));
altered = true;
}
if(pGenre.isEmpty() != ![lGenre length] ||
![lGenre isEqualToString:[NSString stringWithUTF8String:pGenre.toCString(true)]]) {
f.tag()->setGenre(TagLib::String([lGenre UTF8String]));
altered = true;
}
if(pCuesheet.isEmpty() != ![lCuesheet length] ||
![lCuesheet isEqualToString:[NSString stringWithUTF8String:pCuesheet.toCString(true)]]) {
f.tag()->setCuesheet(TagLib::String([lCuesheet UTF8String]));
altered = true;
}
if(pYear != lYear) {
f.tag()->setYear(lYear);
altered = true;
}
if(pTrack != lTrack) {
f.tag()->setTrack(lTrack);
altered = true;
}
if(pDisc != lDisc) {
f.tag()->setDisc(lDisc);
altered = true;
}
if(pReplaygain != lReplaygain) {
f.tag()->setReplaygain(lReplaygain);
altered = true;
}
if(altered)
f.save();
}
/*
NSArray *keys = @[@"key1", @"key2", @"key3"];
NSArray *objects = @[@"value1", @"value2", @"value3"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
for (id key in dictionary)
{
DLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}
pArtist = tag->artist();
pTitle = tag->title();;
pAlbum = tag->album();
pGenre = tag->genre();
pComment = tag->comment();
lYear = tag->year();
lTrack = tag->track();
lDisc = tag->disc();
if (!pArtist.isNull())
lArtist = [NSString stringWithUTF8String:pArtist.toCString(true)];
if (!pAlbum.isNull())
lAlbum = [NSString stringWithUTF8String:pAlbum.toCString(true)];
if (!pTitle.isNull())
lTitle = [NSString stringWithUTF8String:pTitle.toCString(true)];
if (!pGenre.isNull())
lGenre = [NSString stringWithUTF8String:pGenre.toCString(true)];
*/
}
} catch (std::exception &e) {
ALog(@"Exception caught writing with TagLib: %s", e.what());