Updating built in Io code to use += instead of x = x + y
[io/quag.git] / addons / TagLib / source / IoTagLib.c
blob2dffd078b8e54d139878ca42fc5ec01cb3d61599
1 /*#io
2 TagLib ioDoc(
3 docCopyright("Steve Dekorte", 2004)
4 docLicense("BSD revised")
5 docCategory("Media")
6 docDescription("""Used to set tags on ape, flac, mp3, mpc, mpeg, and ogg files. The title, artist, album, year, track, genre slots can be written, and those plus the bitRate, sampleRate, channels and length slots can be read. To read, set the path slot and call the load method. To write, set the path and other slots and call the save method.""")
7 */
9 #include "IoTagLib.h"
10 #include "IoState.h"
11 #include "IoNumber.h"
12 #include "IoSeq.h"
13 #include <taglib/tag_c.h>
15 #define DATA(self) ((IoTagLibData *)IoObject_dataPointer(self))
17 IoTag *IoTagLib_newTag(void *state)
19 IoTag *tag = IoTag_newWithName_("TagLib");
20 IoTag_state_(tag, state);
21 IoTag_cloneFunc_(tag, (IoTagCloneFunc *)IoTagLib_rawClone);
22 return tag;
25 IoTagLib *IoTagLib_proto(void *state)
27 IoObject *self = IoObject_new(state);
28 IoObject_tag_(self, IoTagLib_newTag(state));
30 IoState_registerProtoWithFunc_(state, self, IoTagLib_proto);
33 IoMethodTable methodTable[] = {
34 {"load", IoTagLib_load},
35 {"save", IoTagLib_save},
36 {NULL, NULL},
38 IoObject_addMethodTable_(self, methodTable);
41 return self;
44 IoTagLib *IoTagLib_rawClone(IoTagLib *proto)
46 IoObject *self = IoObject_rawClonePrimitive(proto);
47 return self;
50 IoTagLib *IoTagLib_new(void *state)
52 IoObject *proto = IoState_protoWithInitFunction_(state, IoTagLib_proto);
53 return IOCLONE(proto);
56 // -----------------------------------------------------------
59 IoObject *IoTagLib_load(IoTagLib *self, IoObject *locals, IoMessage *m)
61 /*#io
62 docSlot("load", "Loads tag data from the file specified in the path slot. Returns self.")
65 TagLib_File *file;
66 TagLib_Tag *tag;
67 const TagLib_AudioProperties *properties;
68 IoSeq *path = IoObject_symbolGetSlot_(self, IOSYMBOL("path"));
69 IOASSERT(path, "missing path slot");
71 taglib_set_strings_unicode(0);
73 file = taglib_file_new(CSTRING(path));
75 IOASSERT(file, "unable to open file");
77 tag = taglib_file_newTag(file);
78 properties = taglib_file_audioproperties(file);
80 IoObject_setSlot_to_(self, IOSYMBOL("title"), IOSYMBOL(taglib_tag_title(tag)));
81 IoObject_setSlot_to_(self, IOSYMBOL("artist"), IOSYMBOL(taglib_tag_artist(tag)));
82 IoObject_setSlot_to_(self, IOSYMBOL("album"), IOSYMBOL(taglib_tag_album(tag)));
83 IoObject_setSlot_to_(self, IOSYMBOL("year"), IONUMBER(taglib_tag_year(tag)));
84 IoObject_setSlot_to_(self, IOSYMBOL("comment"), IOSYMBOL(taglib_tag_comment(tag)));
85 IoObject_setSlot_to_(self, IOSYMBOL("track"), IONUMBER(taglib_tag_track(tag)));
86 IoObject_setSlot_to_(self, IOSYMBOL("genre"), IOSYMBOL(taglib_tag_genre(tag)));
88 // audio data - can't write this - it's up to the mp3 encoder
89 IoObject_setSlot_to_(self, IOSYMBOL("bitRate"), IONUMBER(taglib_audioproperties_bitrate(properties)));
90 IoObject_setSlot_to_(self, IOSYMBOL("sampleRate"), IONUMBER(taglib_audioproperties_samplerate(properties)));
91 IoObject_setSlot_to_(self, IOSYMBOL("channels"), IONUMBER(taglib_audioproperties_channels(properties)));
92 IoObject_setSlot_to_(self, IOSYMBOL("length"), IONUMBER(taglib_audioproperties_length(properties)));
94 taglib_tag_free_strings();
95 taglib_file_free(file);
97 return self;
100 IoObject *IoTagLib_save(IoTagLib *self, IoObject *locals, IoMessage *m)
102 /*#io
103 docSlot("save", "Saves the tag settings and returns self.")
106 TagLib_File *file;
107 TagLib_Tag *tag;
108 //const TagLib_AudioProperties *properties;
109 IoSeq *path = IoObject_symbolGetSlot_(self, IOSYMBOL("path"));
110 IOASSERT(path, "missing path slot");
112 taglib_set_strings_unicode(0);
114 file = taglib_file_new(CSTRING(path));
116 IOASSERT(file, "unable to open file");
118 tag = taglib_file_newTag(file);
120 taglib_tag_set_title(tag, CSTRING(IoObject_seqGetSlot_(self, IOSYMBOL("title"))));
121 taglib_tag_set_album(tag, CSTRING(IoObject_seqGetSlot_(self, IOSYMBOL("album"))));
122 taglib_tag_set_year(tag, (int)IoObject_doubleGetSlot_(self, IOSYMBOL("year")));
123 taglib_tag_set_comment(tag, CSTRING(IoObject_seqGetSlot_(self, IOSYMBOL("comment"))));
124 taglib_tag_set_track(tag, (int)IoObject_doubleGetSlot_(self, IOSYMBOL("track")));
125 taglib_tag_set_genre(tag, CSTRING(IoObject_seqGetSlot_(self, IOSYMBOL("genre"))));
127 taglib_file_save(file);
129 taglib_tag_free_strings();
130 taglib_file_free(file);
132 return self;