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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#![crate_name = "taglib"]
#![crate_type = "lib"]
extern crate libc;
extern crate taglib_sys as sys;
use libc::{c_char};
use std::ffi::{CString, CStr};
use sys as ll;
fn c_str_to_str(c_str: *const c_char) -> String {
if c_str.is_null() {
String::new()
}
else {
let bytes = unsafe { CStr::from_ptr(c_str).to_bytes() };
String::from_utf8_lossy(bytes).to_string()
}
}
pub struct File {
raw: *mut ll::TagLib_File,
}
#[allow(dead_code)]
pub struct Tag<'a> {
raw: *mut ll::TagLib_Tag,
file: &'a File,
}
#[allow(dead_code)]
pub struct AudioProperties<'a> {
raw: *const ll::TagLib_AudioProperties,
file: &'a File,
}
impl<'a> Tag<'a> {
pub fn title(&self) -> String {
let res = unsafe { ll::taglib_tag_title(self.raw) };
c_str_to_str(res)
}
pub fn set_title(&mut self, title: &str) {
let s = CString::new(title).unwrap().as_ptr();
unsafe { ll::taglib_tag_set_title(self.raw, s); }
}
pub fn artist(&self) -> String {
let res = unsafe { ll::taglib_tag_artist(self.raw) };
c_str_to_str(res)
}
pub fn set_artist(&mut self, artist: &str) {
let s = CString::new(artist).unwrap().as_ptr();
unsafe { ll::taglib_tag_set_artist(self.raw, s); }
}
pub fn album(&self) -> String {
let res = unsafe { ll::taglib_tag_album(self.raw) };
c_str_to_str(res)
}
pub fn set_album(&mut self, album: &str) {
let s = CString::new(album).unwrap().as_ptr();
unsafe { ll::taglib_tag_set_album(self.raw, s); }
}
pub fn comment(&self) -> String {
let res = unsafe { ll::taglib_tag_comment(self.raw) };
c_str_to_str(res)
}
pub fn set_comment(&mut self, comment: &str) {
let s = CString::new(comment).unwrap().as_ptr();
unsafe { ll::taglib_tag_set_comment(self.raw, s); }
}
pub fn genre(&self) -> String {
let res = unsafe { ll::taglib_tag_genre(self.raw) };
c_str_to_str(res)
}
pub fn set_genre(&mut self, genre: &str) {
let s = CString::new(genre).unwrap().as_ptr();
unsafe { ll::taglib_tag_set_genre(self.raw, s); }
}
pub fn year(&self) -> u32 {
unsafe { ll::taglib_tag_year(self.raw) as u32 }
}
pub fn set_year(&mut self, year: u32) {
unsafe { ll::taglib_tag_set_year(self.raw, year); }
}
pub fn track(&self) -> u32 {
unsafe { ll::taglib_tag_track(self.raw) as u32 }
}
pub fn set_track(&mut self, track: u32) {
unsafe { ll::taglib_tag_set_track(self.raw, track); }
}
}
impl<'a> AudioProperties<'a> {
pub fn length(&self) -> u32 {
unsafe { ll::taglib_audioproperties_length(self.raw) as u32 }
}
pub fn bitrate(&self) -> u32 {
unsafe { ll::taglib_audioproperties_bitrate(self.raw) as u32 }
}
pub fn samplerate(&self) -> u32 {
unsafe { ll::taglib_audioproperties_samplerate(self.raw) as u32 }
}
pub fn channels(&self) -> u32 {
unsafe { ll::taglib_audioproperties_channels(self.raw) as u32 }
}
}
#[derive(Copy, Clone, PartialEq)]
pub enum FileType {
MPEG = ll::TAGLIB_FILE_MPEG as isize,
OggVorbis = ll::TAGLIB_FILE_OGG_VORBIS as isize,
FLAC = ll::TAGLIB_FILE_FLAC as isize,
MPC = ll::TAGLIB_FILE_MPC as isize,
OggFlac = ll::TAGLIB_FILE_OGG_FLAC as isize,
WavPack = ll::TAGLIB_FILE_WAV_PACK as isize,
Speex = ll::TAGLIB_FILE_SPEEX as isize,
TrueAudio = ll::TAGLIB_FILE_TRUE_AUDIO as isize,
MP4 = ll::TAGLIB_FILE_MP4 as isize,
ASF = ll::TAGLIB_FILE_ASF as isize
}
#[derive(Debug)]
pub enum FileError {
InvalidFile,
InvalidFileName,
NoAvailableTag,
NoAvailableAudioProperties
}
impl Drop for File {
fn drop(&mut self) {
unsafe { ll::taglib_file_free(self.raw); }
}
}
impl File {
pub fn new(filename: &str) -> Result<File, FileError> {
let filename_c =
match CString::new(filename) {
Ok(s) => s.as_ptr(),
_ => return Err(FileError::InvalidFileName)
};
let f = unsafe { ll::taglib_file_new(filename_c) };
if f.is_null() {
return Err(FileError::InvalidFile);
}
Ok(File { raw: f })
}
pub fn new_type(filename: &str, filetype: FileType) -> Result<File, FileError> {
let filename_c =
match CString::new(filename) {
Ok(s) => s.as_ptr(),
_ => return Err(FileError::InvalidFileName)
};
let f = unsafe { ll::taglib_file_new_type(filename_c, filetype as u32) };
if f.is_null() {
return Err(FileError::InvalidFile);
}
Ok(File { raw: f })
}
pub fn tag(&self) -> Result<Tag, FileError> {
let res = unsafe { ll::taglib_file_tag(self.raw) };
if res.is_null() {
Err(FileError::NoAvailableTag)
}
else {
Ok(Tag { raw: res, file: self })
}
}
pub fn is_valid(&self) -> bool {
unsafe { ll::taglib_file_is_valid(self.raw) != 0 }
}
pub fn audioproperties(&self) -> Result<AudioProperties, FileError> {
let res = unsafe { ll::taglib_file_audioproperties(self.raw) };
if res.is_null() {
Err(FileError::NoAvailableAudioProperties)
}
else {
Ok(AudioProperties { raw: res, file: self })
}
}
pub fn save(&self) -> bool {
unsafe { ll::taglib_file_save(self.raw) != 0 }
}
}