-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtaglib.cpp
More file actions
212 lines (170 loc) · 5.48 KB
/
taglib.cpp
File metadata and controls
212 lines (170 loc) · 5.48 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//go:build ignore
#include <cstdint>
#include <cstring>
#include <iostream>
#include "fileref.h"
#include "tpropertymap.h"
char *to_char_array(const TagLib::String &s) {
const std::string str = s.to8Bit(true);
return ::strdup(str.c_str());
}
TagLib::String to_string(const char *s) {
return TagLib::String(s, TagLib::String::UTF8);
}
__attribute__((export_name("malloc"))) void *exported_malloc(size_t size) {
return malloc(size);
}
__attribute__((export_name("taglib_file_tags"))) char **
taglib_file_tags(const char *filename) {
TagLib::FileRef file(filename);
if (file.isNull())
return nullptr;
auto properties = file.properties();
size_t len = 0;
for (const auto &kvs : properties)
len += kvs.second.size();
char **tags = static_cast<char **>(malloc(sizeof(char *) * (len + 1)));
if (!tags)
return nullptr;
size_t i = 0;
for (const auto &kvs : properties)
for (const auto &v : kvs.second) {
TagLib::String row = kvs.first + "\t" + v;
tags[i] = to_char_array(row);
i++;
}
tags[len] = nullptr;
return tags;
}
static const uint8_t CLEAR = 1 << 0;
__attribute__((export_name("taglib_file_write_tags"))) bool
taglib_file_write_tags(const char *filename, const char **tags, uint8_t opts) {
if (!filename || !tags)
return false;
TagLib::FileRef file(filename);
if (file.isNull())
return false;
auto properties = file.properties();
if (opts & CLEAR)
properties.clear();
for (size_t i = 0; tags[i]; i++) {
TagLib::String row(tags[i], TagLib::String::UTF8);
if (auto ti = row.find("\t"); ti != -1) {
auto key = row.substr(0, ti);
auto value = row.substr(ti + 1);
if (value.isEmpty())
properties.erase(key);
else
properties.replace(key, value.split("\v"));
}
}
file.setProperties(properties);
return file.save();
}
struct FileProperties {
uint32_t lengthInMilliseconds;
uint32_t channels;
uint32_t sampleRate;
uint32_t bitrate;
char **imageMetadata;
};
__attribute__((export_name("taglib_file_read_properties"))) FileProperties *
taglib_file_read_properties(const char *filename) {
TagLib::FileRef file(filename);
if (file.isNull() || !file.audioProperties())
return nullptr;
FileProperties *props =
static_cast<FileProperties *>(malloc(sizeof(FileProperties)));
if (!props)
return nullptr;
auto audioProperties = file.audioProperties();
props->lengthInMilliseconds = audioProperties->lengthInMilliseconds();
props->channels = audioProperties->channels();
props->sampleRate = audioProperties->sampleRate();
props->bitrate = audioProperties->bitrate();
const auto &pictures = file.complexProperties("PICTURE");
props->imageMetadata = nullptr;
if (pictures.isEmpty())
return props;
size_t len = pictures.size();
char **imageMetadata =
static_cast<char **>(malloc(sizeof(char *) * (len + 1)));
if (!imageMetadata)
return props;
size_t i = 0;
for (const auto &p : pictures) {
TagLib::String type = p["pictureType"].toString();
TagLib::String desc = p["description"].toString();
TagLib::String mime = p["mimeType"].toString();
TagLib::String row = type + "\t" + desc + "\t" + mime;
imageMetadata[i] = to_char_array(row);
i++;
}
imageMetadata[len] = nullptr;
props->imageMetadata = imageMetadata;
return props;
}
struct ByteData {
uint32_t length;
char *data;
};
__attribute__((export_name("taglib_file_read_image"))) ByteData *
taglib_file_read_image(const char *filename, int index) {
TagLib::FileRef file(filename);
if (file.isNull())
return nullptr;
const auto &pictures = file.complexProperties("PICTURE");
if (pictures.isEmpty())
return nullptr;
if (index < 0 || index >= static_cast<int>(pictures.size()))
return nullptr;
auto v = pictures[index]["data"].toByteVector();
ByteData *bd = static_cast<ByteData *>(malloc(sizeof(ByteData)));
if (!bd)
return nullptr;
bd->length = static_cast<uint32_t>(v.size());
if (bd->length == 0) {
bd->data = nullptr;
return bd;
}
// allocate and copy into module memory to keep it valid for go to read
char *buf = static_cast<char *>(malloc(bd->length));
if (!buf)
return nullptr;
memcpy(buf, v.data(), bd->length);
bd->data = buf;
return bd;
}
__attribute__((export_name("taglib_file_write_image"))) bool
taglib_file_write_image(const char *filename, const char *buf, uint32_t length,
int index, const char *pictureType,
const char *description, const char *mimeType) {
TagLib::FileRef file(filename);
if (file.isNull())
return false;
auto pictures = file.complexProperties("PICTURE");
if (length == 0) {
// remove image at index if it exists
if (index >= 0 && index < static_cast<int>(pictures.size())) {
auto it = pictures.begin();
std::advance(it, index);
pictures.erase(it);
if (!file.setComplexProperties("PICTURE", pictures))
return false;
}
return file.save();
}
TagLib::VariantMap newPicture;
newPicture["data"] = TagLib::ByteVector(buf, length);
newPicture["pictureType"] = to_string(pictureType);
newPicture["description"] = to_string(description);
newPicture["mimeType"] = to_string(mimeType);
// replace image at index, or append if index is out of range
if (index >= 0 && index < static_cast<int>(pictures.size()))
pictures[index] = newPicture;
else
pictures.append(newPicture);
if (!file.setComplexProperties("PICTURE", pictures))
return false;
return file.save();
}