comparison src/libaudtag/util.c @ 4887:0ddbd0025174 default tip

added libaudtag. (not used yet.)
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Wed, 05 May 2010 18:26:06 +0900
parents
children
comparison
equal deleted inserted replaced
4886:54b4f7aaca24 4887:0ddbd0025174
1 /*
2 * Copyright 2009 Paula Stanciu
3 *
4 * This file is part of Audacious.
5 *
6 * Audacious is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3 of the License.
9 *
10 * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * Audacious. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * The Audacious team does not consider modular code linking to Audacious or
18 * using our public API to be a derived work.
19 */
20
21 #include <glib.h>
22 #include "util.h"
23 #include <inttypes.h>
24
25 /* convert windows time to unix time */
26 time_t unix_time(guint64 win_time)
27 {
28 guint64 t = (guint64) ((win_time / 10000000LL) - 11644473600LL);
29 return (time_t) t;
30 }
31
32 guint16 get_year(guint64 win_time)
33 {
34 GDate *d = g_date_new();
35 g_date_set_time_t(d, unix_time(win_time));
36 guint16 year = g_date_get_year(d);
37 g_date_free(d);
38 return year;
39 }
40
41 Tuple *makeTuple(Tuple * tuple, const gchar * title, const gchar * artist, const gchar * comment, const gchar * album, const gchar * genre, const gchar * year, const gchar * filePath, int tracnr)
42 {
43
44 tuple_associate_string(tuple, FIELD_ARTIST, NULL, artist);
45 tuple_associate_string(tuple, FIELD_TITLE, NULL, title);
46 tuple_associate_string(tuple, FIELD_COMMENT, NULL, comment);
47 tuple_associate_string(tuple, FIELD_ALBUM, NULL, album);
48 tuple_associate_string(tuple, FIELD_GENRE, NULL, genre);
49 tuple_associate_string(tuple, FIELD_YEAR, NULL, year);
50 tuple_associate_int(tuple, FIELD_TRACK_NUMBER, NULL, tracnr);
51 tuple_associate_string(tuple, FIELD_FILE_PATH, NULL, filePath);
52 return tuple;
53 }
54
55 const gchar *get_complete_filepath(Tuple * tuple)
56 {
57 const gchar *filepath;
58 const gchar *dir;
59 const gchar *file;
60
61 dir = tuple_get_string(tuple, FIELD_FILE_PATH, NULL);
62 file = tuple_get_string(tuple, FIELD_FILE_NAME, NULL);
63 filepath = g_strdup_printf("%s/%s", dir, file);
64 AUDDBG("file path = %s\n", filepath);
65 return filepath;
66 }
67
68 void print_tuple(Tuple * tuple)
69 {
70 #if WMA_DEBUG
71 AUDDBG("--------------TUPLE PRINT --------------------\n");
72 const gchar *title = tuple_get_string(tuple, FIELD_TITLE, NULL);
73 AUDDBG("title = %s\n", title);
74 /* artist */
75 const gchar *artist = tuple_get_string(tuple, FIELD_ARTIST, NULL);
76 AUDDBG("artist = %s\n", artist);
77
78 /* copyright */
79 const gchar *copyright = tuple_get_string(tuple, FIELD_COPYRIGHT, NULL);
80 AUDDBG("copyright = %s\n", copyright);
81
82 /* comment / description */
83
84 const gchar *comment = tuple_get_string(tuple, FIELD_COMMENT, NULL);
85 AUDDBG("comment = %s\n", comment);
86
87 /* codec name */
88 const gchar *codec_name = tuple_get_string(tuple, FIELD_CODEC, NULL);
89 AUDDBG("codec = %s\n", codec_name);
90
91 /* album */
92 const gchar *album = tuple_get_string(tuple, FIELD_ALBUM, NULL);
93 AUDDBG("Album = %s\n", album);
94
95 /*track number */
96 gint track_nr = tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL);
97 AUDDBG("Track nr = %d\n", track_nr);
98
99 /* genre */
100 const gchar *genre = tuple_get_string(tuple, FIELD_GENRE, NULL);
101 AUDDBG("Genre = %s \n", genre);
102
103 /* length */
104 gint length = tuple_get_int(tuple, FIELD_LENGTH, NULL);
105 AUDDBG("Length = %d\n", length);
106
107 /* year */
108 gint year = tuple_get_int(tuple, FIELD_YEAR, NULL);
109 AUDDBG("Year = %d\n", year);
110
111 /* quality */
112 const gchar *quality = tuple_get_string(tuple, FIELD_QUALITY, NULL);
113 AUDDBG("quality = %s\n", quality);
114
115 /* path */
116 const gchar *path = tuple_get_string(tuple, FIELD_FILE_PATH, NULL);
117 AUDDBG("path = %s\n", path);
118
119 /* filename */
120 const gchar *filename = tuple_get_string(tuple, FIELD_FILE_NAME, NULL);
121 AUDDBG("filename = %s\n", filename);
122
123 AUDDBG("-----------------END---------------------\n");
124 #endif
125 }
126
127 void seek(VFSFile * f, long pos)
128 {
129
130 vfs_fseek(f, pos, SEEK_SET);
131 }
132
133 void skip(VFSFile * f, int amount)
134 {
135 vfs_fseek(f, amount, SEEK_CUR);
136 }
137
138 gchar *read_char_data(VFSFile * fd, int size)
139 {
140 gchar *value = g_new0(gchar, size);
141 vfs_fread(value, size, 1, fd);
142 return value;
143 }
144
145 gboolean write_char_data(VFSFile * f, gchar * data, size_t i)
146 {
147 return (vfs_fwrite(data, i, 1, f) == i);
148 }
149
150 gchar *utf8(gunichar2 * s)
151 {
152 g_return_val_if_fail(s != NULL, NULL);
153 return g_utf16_to_utf8(s, -1, NULL, NULL, NULL);
154 }
155
156 gunichar2 *fread_utf16(VFSFile * f, guint64 size)
157 {
158 gunichar2 *p = (gunichar2 *) g_malloc0(size);
159 if (vfs_fread(p, 1, size, f) != size)
160 {
161 g_free(p);
162 p = NULL;
163 }
164 gchar *s = utf8(p);
165 AUDDBG("Converted to UTF8: '%s'\n", s);
166 g_free(s);
167 return p;
168 }
169
170 gboolean write_utf16(VFSFile * f, gunichar2 * data, size_t i)
171 {
172 return (vfs_fwrite(data, i, 1, f) == i);
173 }
174
175 guint8 read_uint8(VFSFile * fd)
176 {
177 guint16 i;
178 if (vfs_fread(&i, 1, 1, fd) == 1)
179 {
180 return i;
181 }
182 return -1;
183 }
184
185 guint16 read_LEuint16(VFSFile * fd)
186 {
187 guint16 a;
188 if (vfs_fget_le16(&a, fd))
189 return a;
190 else
191 return -1;
192 }
193
194 guint16 read_BEuint16(VFSFile * fd)
195 {
196 guint16 a;
197 if (vfs_fget_be16(&a, fd))
198 return a;
199 else
200 return -1;
201 }
202
203 guint32 read_LEuint32(VFSFile * fd)
204 {
205 guint32 a;
206 if (vfs_fget_le32(&a, fd))
207 return a;
208 else
209 return -1;
210 }
211
212 guint32 read_BEuint32(VFSFile * fd)
213 {
214 guint32 a;
215 if (vfs_fget_be32(&a, fd))
216 return a;
217 else
218 return -1;
219 }
220
221 guint64 read_LEuint64(VFSFile * fd)
222 {
223 guint64 a;
224 if (vfs_fget_le64(&a, fd))
225 return a;
226 else
227 return -1;
228 }
229
230 guint64 read_BEuint64(VFSFile * fd)
231 {
232 guint64 a;
233 if (vfs_fget_be64(&a, fd))
234 return a;
235 else
236 return 1;
237 }
238
239 gboolean write_uint8(VFSFile * fd, guint8 val)
240 {
241 return (vfs_fwrite(&val, 1, 1, fd) == 1);
242 }
243
244 gboolean write_LEuint16(VFSFile * fd, guint16 val)
245 {
246 guint16 le_val = GUINT32_TO_LE(val);
247 return (vfs_fwrite(&le_val, 2, 1, fd) == 2);
248 }
249
250 gboolean write_BEuint32(VFSFile * fd, guint32 val)
251 {
252 guint32 be_val = GUINT32_TO_BE(val);
253 return (vfs_fwrite(&be_val, 4, 1, fd) == 4);
254 }
255
256 gboolean write_LEuint32(VFSFile * fd, guint32 val)
257 {
258 guint32 le_val = GUINT32_TO_LE(val);
259 return (vfs_fwrite(&le_val, 4, 1, fd) == 4);
260 }
261
262 gboolean write_LEuint64(VFSFile * fd, guint64 val)
263 {
264 guint64 le_val = GUINT64_TO_LE(val);
265 return (vfs_fwrite(&le_val, 8, 1, fd) == 8);
266 }
267
268 void copyAudioToFile(VFSFile * from, VFSFile * to, guint32 pos)
269 {
270 vfs_fseek(from, pos, SEEK_SET);
271 while (vfs_feof(from) == 0)
272 {
273 gchar buf[4096];
274 gint n = vfs_fread(buf, 1, 4096, from);
275 vfs_fwrite(buf, n, 1, to);
276 }
277 }
278
279 void copyAudioData(VFSFile * from, VFSFile * to, guint32 pos_from, guint32 pos_to)
280 {
281 vfs_fseek(from, pos_from, SEEK_SET);
282 int bytes_read = pos_from;
283 while (bytes_read < pos_to - 4096)
284 {
285 gchar buf[4096];
286 guint32 n = vfs_fread(buf, 1, 4096, from);
287 vfs_fwrite(buf, n, 1, to);
288 bytes_read += n;
289 }
290 if (bytes_read < pos_to)
291 {
292 guint32 buf_size = pos_to - bytes_read;
293 gchar buf2[buf_size];
294 int nn = vfs_fread(buf2, 1, buf_size, from);
295 vfs_fwrite(buf2, nn, 1, to);
296 }
297 }
298
299 gchar *convert_numericgenre_to_text(gint numericgenre)
300 {
301 const struct
302 {
303 gint numericgenre;
304 gchar *genre;
305 }
306 table[] =
307 {
308 {GENRE_BLUES, "Blues"},
309 {GENRE_CLASSIC_ROCK, "Classic Rock"},
310 {GENRE_COUNTRY, "Country"},
311 {GENRE_DANCE, "Dance"},
312 {GENRE_DISCO, "Disco"},
313 {GENRE_FUNK, "Funk"},
314 {GENRE_GRUNGE, "Grunge"},
315 {GENRE_HIPHOP, "Hip-Hop"},
316 {GENRE_JAZZ, "Jazz"},
317 {GENRE_METAL, "Metal"},
318 {GENRE_NEW_AGE, "New Age"},
319 {GENRE_OLDIES, "Oldies"},
320 {GENRE_OTHER, "Other"},
321 {GENRE_POP, "Pop"},
322 {GENRE_R_B, "R&B"},
323 {GENRE_RAP, "Rap"},
324 {GENRE_REGGAE, "Reggae"},
325 {GENRE_ROCK, "Rock"},
326 {GENRE_TECHNO, "Techno"},
327 {GENRE_INDUSTRIAL, "Industrial"},
328 {GENRE_ALTERNATIVE, "Alternative"},
329 {GENRE_SKA, "Ska"},
330 {GENRE_DEATH_METAL, "Death Metal"},
331 {GENRE_PRANKS, "Pranks"},
332 {GENRE_SOUNDTRACK, "Soundtrack"},
333 {GENRE_EURO_TECHNO, "Euro-Techno"},
334 {GENRE_AMBIENT, "Ambient"},
335 {GENRE_TRIP_HOP, "Trip-Hop"},
336 {GENRE_VOCAL, "Vocal"},
337 {GENRE_JAZZ_FUNK, "Jazz+Funk"},
338 {GENRE_FUSION, "Fusion"},
339 {GENRE_TRANCE, "Trance"},
340 {GENRE_CLASSICAL, "Classical"},
341 {GENRE_INSTRUMENTAL, "Instrumental"},
342 {GENRE_ACID, "Acid"},
343 {GENRE_HOUSE, "House"},
344 {GENRE_GAME, "Game"},
345 {GENRE_SOUND_CLIP, "Sound Clip"},
346 {GENRE_GOSPEL, "Gospel"},
347 {GENRE_NOISE, "Noise"},
348 {GENRE_ALTERNROCK, "AlternRock"},
349 {GENRE_BASS, "Bass"},
350 {GENRE_SOUL, "Soul"},
351 {GENRE_PUNK, "Punk"},
352 {GENRE_SPACE, "Space"},
353 {GENRE_MEDITATIVE, "Meditative"},
354 {GENRE_INSTRUMENTAL_POP, "Instrumental Pop"},
355 {GENRE_INSTRUMENTAL_ROCK, "Instrumental Rock"},
356 {GENRE_ETHNIC, "Ethnic"},
357 {GENRE_GOTHIC, "Gothic"},
358 {GENRE_DARKWAVE, "Darkwave"},
359 {GENRE_TECHNO_INDUSTRIAL, "Techno-Industrial"},
360 {GENRE_ELECTRONIC, "Electronic"},
361 {GENRE_POP_FOLK, "Pop-Folk"},
362 {GENRE_EURODANCE, "Eurodance"},
363 {GENRE_DREAM, "Dream"},
364 {GENRE_SOUTHERN_ROCK, "Southern Rock"},
365 {GENRE_COMEDY, "Comedy"},
366 {GENRE_CULT, "Cult"},
367 {GENRE_GANGSTA, "Gangsta"},
368 {GENRE_TOP40, "Top 40"},
369 {GENRE_CHRISTIAN_RAP, "Christian Rap"},
370 {GENRE_POP_FUNK, "Pop/Funk"},
371 {GENRE_JUNGLE, "Jungle"},
372 {GENRE_NATIVE_AMERICAN, "Native American"},
373 {GENRE_CABARET, "Cabaret"},
374 {GENRE_NEW_WAVE, "New Wave"},
375 {GENRE_PSYCHADELIC, "Psychadelic"},
376 {GENRE_RAVE, "Rave"},
377 {GENRE_SHOWTUNES, "Showtunes"},
378 {GENRE_TRAILER, "Trailer"},
379 {GENRE_LO_FI, "Lo-Fi"},
380 {GENRE_TRIBAL, "Tribal"},
381 {GENRE_ACID_PUNK, "Acid Punk"},
382 {GENRE_ACID_JAZZ, "Acid Jazz"},
383 {GENRE_POLKA, "Polka"},
384 {GENRE_RETRO, "Retro"},
385 {GENRE_MUSICAL, "Musical"},
386 {GENRE_ROCK_ROLL, "Rock & Roll"},
387 {GENRE_HARD_ROCK, "Hard Rock"},
388 {GENRE_FOLK, "Folk"},
389 {GENRE_FOLK_ROCK, "Folk-Rock"},
390 {GENRE_NATIONAL_FOLK, "National Folk"},
391 {GENRE_SWING, "Swing"},
392 {GENRE_FAST_FUSION, "Fast Fusion"},
393 {GENRE_BEBOB, "Bebob"},
394 {GENRE_LATIN, "Latin"},
395 {GENRE_REVIVAL, "Revival"},
396 {GENRE_CELTIC, "Celtic"},
397 {GENRE_BLUEGRASS, "Bluegrass"},
398 {GENRE_AVANTGARDE, "Avantgarde"},
399 {GENRE_GOTHIC_ROCK, "Gothic Rock"},
400 {GENRE_PROGRESSIVE_ROCK, "Progressive Rock"},
401 {GENRE_PSYCHEDELIC_ROCK, "Psychedelic Rock"},
402 {GENRE_SYMPHONIC_ROCK, "Symphonic Rock"},
403 {GENRE_SLOW_ROCK, "Slow Rock"},
404 {GENRE_BIG_BAND, "Big Band"},
405 {GENRE_CHORUS, "Chorus"},
406 {GENRE_EASY_LISTENING, "Easy Listening"},
407 {GENRE_ACOUSTIC, "Acoustic"},
408 {GENRE_HUMOUR, "Humour"},
409 {GENRE_SPEECH, "Speech"},
410 {GENRE_CHANSON, "Chanson"},
411 {GENRE_OPERA, "Opera"},
412 {GENRE_CHAMBER_MUSIC, "Chamber Music"},
413 {GENRE_SONATA, "Sonata"},
414 {GENRE_SYMPHONY, "Symphony"},
415 {GENRE_BOOTY_BASS, "Booty Bass"},
416 {GENRE_PRIMUS, "Primus"},
417 {GENRE_PORN_GROOVE, "Porn Groove"},
418 {GENRE_SATIRE, "Satire"},
419 {GENRE_SLOW_JAM, "Slow Jam"},
420 {GENRE_CLUB, "Club"},
421 {GENRE_TANGO, "Tango"},
422 {GENRE_SAMBA, "Samba"},
423 {GENRE_FOLKLORE, "Folklore"},
424 {GENRE_BALLAD, "Ballad"},
425 {GENRE_POWER_BALLAD, "Power Ballad"},
426 {GENRE_RHYTHMIC_SOUL, "Rhythmic Soul"},
427 {GENRE_FREESTYLE, "Freestyle"},
428 {GENRE_DUET, "Duet"},
429 {GENRE_PUNK_ROCK, "Punk Rock"},
430 {GENRE_DRUM_SOLO, "Drum Solo"},
431 {GENRE_A_CAPELLA, "A capella"},
432 {GENRE_EURO_HOUSE, "Euro-House"},
433 };
434
435 gint count;
436
437 for (count = 0; count < G_N_ELEMENTS(table); count++)
438 {
439 if (table[count].numericgenre == numericgenre)
440 {
441 return table[count].genre;
442 }
443 }
444
445 return "Unknown";
446 }