This repository was archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagLib.cs
More file actions
173 lines (164 loc) · 5.34 KB
/
TagLib.cs
File metadata and controls
173 lines (164 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
namespace MediaPlayer;
/// <summary>
/// Wrapper class for TagLibSharp
/// </summary>
public static class TagLib
{
#region Public
/// <summary>
/// Enum for the different tags that can be retrieved from a file
/// </summary>
public enum Tag
{
/// <summary>
/// The title of the song
/// </summary>
Title,
/// <summary>
/// The artist of the song
/// </summary>
Artist,
/// <summary>
/// The album of the song
/// </summary>
Album,
/// <summary>
/// The album artist of the song
/// </summary>
AlbumArtist,
/// <summary>
/// The genre of the song
/// </summary>
Genre,
/// <summary>
/// The year of the song
/// </summary>
Year,
/// <summary>
/// The track number of the song
/// </summary>
Track,
/// <summary>
/// The disc number of the song
/// </summary>
Disc,
/// <summary>
/// The composer of the song
/// </summary>
Composer,
/// <summary>
/// The conductor of the song
/// </summary>
Conductor,
}
private static string _tag;
/// <summary>
/// Gets a tag from an audio file
/// </summary>
/// <param name="filepath">The path to the audio file</param>
/// <param name="tag">The tag to get</param>
/// <returns>String of requested tag</returns>
public static string GetTag(string filepath, Tag tag)
{
var tagLibFile = global::TagLib.File.Create(filepath);
_tag = tag switch
{
Tag.Title => tagLibFile.Tag.Title,
Tag.Artist => tagLibFile.Tag.FirstPerformer,
Tag.Album => tagLibFile.Tag.Album,
Tag.AlbumArtist => tagLibFile.Tag.FirstAlbumArtist,
Tag.Genre => tagLibFile.Tag.FirstGenre,
Tag.Year => tagLibFile.Tag.Year.ToString(),
Tag.Track => tagLibFile.Tag.Track.ToString(),
Tag.Disc => tagLibFile.Tag.Disc.ToString(),
Tag.Composer => tagLibFile.Tag.FirstComposer,
Tag.Conductor => tagLibFile.Tag.Conductor,
_ => null
};
if (_tag == null)
{
ModConsole.Error($"{filepath}'s {tag.ToString()} field is null!");
return null;
}
return _tag;
}
/// <summary>
/// Gets the album art from an audio file
/// </summary>
/// <param name="filepath">The path to the audio file</param>
/// <returns>Texture2D of the album cover</returns>
public static Texture2D GetCover(string filepath)
{
var tagLibFile = global::TagLib.File.Create(filepath);
var picture = tagLibFile.Tag.Pictures[0];
if (picture == null)
{
ModConsole.Error($"{filepath}'s album art field is null!");
return null;
}
var texture = new Texture2D(2, 2);
// ReSharper disable once InvokeAsExtensionMethod, unhollowed unity extensions don't work well, have to call them directly
ImageConversion.LoadImage(texture, picture.Data.Data, false);
return texture;
}
/// <summary>
/// Gets the filename of an audio file
/// </summary>
/// <param name="filepath">The path to the audio file</param>
/// <returns>String of filename</returns>
public static string GetFilename(string filepath)
{
var title = Path.GetFileName(filepath);
return title;
}
#endregion
#region Internal
private static string _privateTag;
internal static string GetTag(int index, Tag tag)
{
var file = Assets.FilePaths[index];
var tagLibFile = global::TagLib.File.Create(file);
_privateTag = tag switch
{
Tag.Title => tagLibFile.Tag.Title,
Tag.Artist => tagLibFile.Tag.FirstPerformer,
Tag.Album => tagLibFile.Tag.Album,
Tag.AlbumArtist => tagLibFile.Tag.FirstAlbumArtist,
Tag.Genre => tagLibFile.Tag.FirstGenre,
Tag.Year => tagLibFile.Tag.Year.ToString(),
Tag.Track => tagLibFile.Tag.Track.ToString(),
Tag.Disc => tagLibFile.Tag.Disc.ToString(),
Tag.Composer => tagLibFile.Tag.FirstComposer,
Tag.Conductor => tagLibFile.Tag.Conductor,
_ => null
};
if (_privateTag == null)
{
ModConsole.Error($"{file}'s {tag.ToString()} field is null!");
return null;
}
return _privateTag;
}
internal static Texture2D GetCover(int index)
{
var file = Assets.FilePaths[index];
var tagLibFile = global::TagLib.File.Create(file);
var picture = tagLibFile.Tag.Pictures[0];
if (picture == null)
{
ModConsole.Error($"{file}'s album art field is null!");
return null;
}
var texture = new Texture2D(2, 2);
// ReSharper disable once InvokeAsExtensionMethod, unhollowed unity extensions don't work well, have to call them directly
ImageConversion.LoadImage(texture, picture.Data.Data, false);
return texture;
}
internal static string GetFilename(int index)
{
var file = Assets.FilePaths[index];
var title = Path.GetFileName(file);
return title;
}
#endregion
}