Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/FlakyBlueJay/mb_musicbrainzsync. MusicBrainz rating/tag sync plugin for MusicBee
csharp dotnet-framework dotnet-framework-48 musicbee musicbee-plugin musicbrainz musicbrainz-api
Something went wrong. Try again.
7.1 kB · 165 lines
C#
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166using System;using System.IO;using System.Collections.Generic;using static MusicBeePlugin.Plugin;using System.Diagnostics;using System.Linq;using plugin.Properties;
namespace plugin{
/// <summary> /// An object representing a track, based on data from MusicBee and TagLib#.<br/> /// MusicBee does not support MusicBrainz ID metadata, which is needed for this. /// </summary> public class MusicBeeTrack {
public string MusicBrainzRecordingId { get; private set; } public string MusicBrainzReleaseId { get; private set; } public string MusicBrainzReleaseGroupId { get; private set; } public string[] MusicBrainzArtistId { get; private set; } public string FilePath { get; private set; }
// ratings public string Rating { get; private set; } public string AlbumRating { get; private set; }
// for debug and troubleshooting purposes public string Title { get; private set; } public string Artist { get; private set; } public string Album { get; private set; }
// throwing my own UnsupportedFormatException here instead of using TagLib#'s UnsupportedFormat exception for other files to use. // Having TagLib# inside Plugin.cs results in clashes over the TextBox control for some reason. public class UnsupportedFormatException : Exception { public UnsupportedFormatException(string message) : base(message) { } }
// TagLib# does not support these formats. .tak may be supported but I doubt the others will due to how they store metadata. private static readonly HashSet<String> bannedExtensions = new HashSet<String> { ".tak", ".midi", ".mid", ".xm", ".uxm", ".mod" };
public MusicBeeTrack(string path) { string fileExt = Path.GetExtension(path); if (bannedExtensions.Contains(fileExt)) { throw new UnsupportedFormatException($"{fileExt} files are not currently supported by this plugin as it is not currently supported in the underlying tag retrieval library."); } else { var TagLib_TrackData = TagLib.File.Create(path);
Album = mbApiInterface.Library_GetFileTag(path, MetaDataType.Album); // Debug data - not going to be used but useful for debugging purposes. Title = mbApiInterface.Library_GetFileTag(path, MetaDataType.TrackTitle); Artist = mbApiInterface.Library_GetFileTag(path, MetaDataType.Artist); // Rating data - MusicBee seems to output them as strings. Rating = mbApiInterface.Library_GetFileTag(path, MetaDataType.Rating); AlbumRating = mbApiInterface.Library_GetFileTag(path, MetaDataType.RatingAlbum); FilePath = path;
// MusicBrainz tags: single MusicBrainzRecordingId = TagLib_TrackData.Tag.MusicBrainzTrackId; MusicBrainzReleaseId = TagLib_TrackData.Tag.MusicBrainzReleaseId; MusicBrainzReleaseGroupId = TagLib_TrackData.Tag.MusicBrainzReleaseGroupId;
// MusicBrainz tags: multi if (!string.IsNullOrEmpty(TagLib_TrackData.Tag.MusicBrainzArtistId)) { // TagLib#, instead of returning an array or list, returns strings - the separator seemingly depends on the tag format it's read. MusicBrainzArtistId = TagLib_TrackData.Tag.MusicBrainzArtistId.Split(new Char[] { ';', '/', ',' }); for (int i = 0; i < MusicBrainzArtistId.Length; i++) { MusicBrainzArtistId[i] = MusicBrainzArtistId[i].Trim(); } }
}
}
// for the tag functions, we want to get a collection of tags, as defined in the tag binding settings. // it doesn't make as much sense to have these as properties at the moment. /// <summary> /// Grabs all tags from a file, depending on the tag bindings used (defaults to recording, which is also used for global tag bindings)<br/><br/> /// Tag bindings are handled in the main plugin file. /// </summary> public List<string> GetAllTagsFromFile(string type = "recording") { // get the tags the user has chosen to submit List<string> tagsToSearch = new List<string>(); Debug.WriteLine(tagsToSearch.Count); List<string> tagList = new List<string>();
if (type == "recording" || !Settings.Default.separateTagBindings) { if (!string.IsNullOrEmpty(Settings.Default.recordingTagBindings)) { tagsToSearch = Settings.Default.recordingTagBindings.Split(';').ToList(); } } else { switch (type) { case "release": if (!string.IsNullOrEmpty(Settings.Default.releaseTagBindings)) { tagsToSearch = Settings.Default.releaseTagBindings.Split(';').ToList(); } break; case "release-group": if (!string.IsNullOrEmpty(Settings.Default.releaseGroupTagBindings)) { tagsToSearch = Settings.Default.releaseGroupTagBindings.Split(';').ToList(); } break; } } Debug.WriteLine("tagsToSearch: " + string.Join(", ", tagsToSearch)); if (tagsToSearch.Count > 0) { foreach (string tag in tagsToSearch) { // get tag from finding its key in the dictionary then add to get track tags. MetaDataType tagType = tagBindings[tag]; // clean up tags List<string> tagValue = mbApiInterface.Library_GetFileTag(FilePath, tagType).Split(';').ToList(); foreach (string tagString in tagValue) { // do not try to add tags if tagString is empty, otherwise it will add empty strings. if (!string.IsNullOrEmpty(tagString)) { string trimmedTag = tagString.Trim(); if (!tagList.Contains(trimmedTag)) { tagList.Add(trimmedTag); } } }
} if (tagList.Count > 0) { return tagList; } else { return null; } } else { return null; } }
}}