summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-12-30 18:38:50 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2010-12-30 22:33:22 +0100
commit5799070fea8f11ced32d57c6c38017c7671b0773 (patch)
treebe1eb88c4a77572de7e26e102aa6420a7825c98b
parent2e2221b2eda1c089688b3e87b42b0dc6720f6c59 (diff)
Added an ID3 tag reader.
-rw-r--r--SConstruct1
-rw-r--r--tag.cpp53
-rw-r--r--tag.h21
3 files changed, 75 insertions, 0 deletions
diff --git a/SConstruct b/SConstruct
index bb8f8bd..baf9f5d 100644
--- a/SConstruct
+++ b/SConstruct
@@ -20,6 +20,7 @@ else:
env.Append(CCFLAGS = ['-Wall', '-g'])
env.ParseConfig('pkg-config --cflags --libs libmpg123')
+env.ParseConfig('pkg-config --cflags --libs id3tag')
env.Program('audistd', Glob('*.cpp') + Glob('decoders/*.cpp') + Glob('encoders/*.cpp'))
diff --git a/tag.cpp b/tag.cpp
new file mode 100644
index 0000000..833735a
--- /dev/null
+++ b/tag.cpp
@@ -0,0 +1,53 @@
+#include "tag.h"
+
+#include <id3tag.h>
+
+#include <cstdlib>
+#include <stdexcept>
+
+void ID3Tag::tag_add_string(struct id3_tag *id3tag, const char *type, const char *id) {
+ struct id3_frame *frame = id3_tag_findframe(id3tag, id, 0);
+
+ if(frame == NULL) {
+ return;
+ }
+
+ if(frame->nfields != 2) {
+ throw std::runtime_error("unexpected nfields value");
+ }
+
+ const union id3_field *field = id3_frame_field(frame, 1);
+ unsigned int nstrings = id3_field_getnstrings(field);
+
+ for(unsigned int i = 0; i < nstrings; i++) {
+ const id3_ucs4_t *ucs4 = id3_field_getstrings(field, i);
+
+ if(ucs4 == NULL) {
+ throw std::runtime_error("ucs4 is NULL");
+ }
+
+ id3_utf8_t *utf8 = id3_ucs4_utf8duplicate(ucs4);
+ fields[type] = (char*)utf8;
+ free(utf8);
+ }
+}
+
+ID3Tag::ID3Tag(const std::string filename) {
+ struct id3_file *file = id3_file_open(filename.c_str(), ID3_FILE_MODE_READONLY);
+
+ if(file == NULL) {
+ throw std::runtime_error("failed to open file for reading");
+ }
+
+ struct id3_tag *id3tag = id3_file_tag(file);
+
+ if(id3tag == NULL) {
+ throw std::runtime_error("tag is NULL");
+ }
+
+ tag_add_string(id3tag, "artist", ID3_FRAME_ARTIST);
+ tag_add_string(id3tag, "title", ID3_FRAME_TITLE);
+ tag_add_string(id3tag, "album", ID3_FRAME_ALBUM);
+
+ id3_file_close(file);
+}
diff --git a/tag.h b/tag.h
new file mode 100644
index 0000000..83a1f6d
--- /dev/null
+++ b/tag.h
@@ -0,0 +1,21 @@
+#ifndef TAG_H
+#define TAG_H
+
+#include <string>
+#include <map>
+
+class Tag {
+ public:
+ typedef std::map<std::string, std::string> Fields;
+ Fields fields;
+};
+
+class ID3Tag : public Tag {
+ private:
+ void tag_add_string(struct id3_tag *id3tag, const char *type, const char *id);
+
+ public:
+ ID3Tag(const std::string filename);
+};
+
+#endif