| 1 | /* |
|---|
| 2 | * Copyright 2007 Romain Beauxis |
|---|
| 3 | * |
|---|
| 4 | * This file is part of ocaml-taglib. |
|---|
| 5 | * |
|---|
| 6 | * ocaml-taglib is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU Lesser General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * ocaml-taglib is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU Lesser General Public License |
|---|
| 17 | * along with ocaml-taglib; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 19 | * |
|---|
| 20 | * As a special exception to the GNU Library General Public License, you may |
|---|
| 21 | * link, statically or dynamically, a "work that uses the Library" with a publicly |
|---|
| 22 | * distributed version of the Library to produce an executable file containing |
|---|
| 23 | * portions of the Library, and distribute that executable file under terms of |
|---|
| 24 | * your choice, without any of the additional requirements listed in clause 6 |
|---|
| 25 | * of the GNU Library General Public License. |
|---|
| 26 | * By "a publicly distributed version of the Library", we mean either the unmodified |
|---|
| 27 | * Library as distributed by INRIA, or a modified version of the Library that is |
|---|
| 28 | * distributed under the conditions defined in clause 3 of the GNU Library General |
|---|
| 29 | * Public License. This exception does not however invalidate any other reasons why |
|---|
| 30 | * the executable file might be covered by the GNU Library General Public License. |
|---|
| 31 | * |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /* |
|---|
| 36 | * Ocaml interface to taglib |
|---|
| 37 | * |
|---|
| 38 | * @author Romain Beauxis |
|---|
| 39 | */ |
|---|
| 40 | |
|---|
| 41 | #include <taglib/tag_c.h> |
|---|
| 42 | #include <string.h> |
|---|
| 43 | |
|---|
| 44 | #include <caml/alloc.h> |
|---|
| 45 | #include <caml/callback.h> |
|---|
| 46 | #include <caml/fail.h> |
|---|
| 47 | #include <caml/memory.h> |
|---|
| 48 | |
|---|
| 49 | CAMLprim value caml_taglib_init() |
|---|
| 50 | { |
|---|
| 51 | CAMLparam0(); |
|---|
| 52 | /* Desactivates string memory management */ |
|---|
| 53 | taglib_set_string_management_enabled(0) ; |
|---|
| 54 | CAMLreturn(Val_unit); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | CAMLprim value caml_taglib_set_strings_unicode(value b) |
|---|
| 58 | { |
|---|
| 59 | CAMLparam1(b); |
|---|
| 60 | |
|---|
| 61 | taglib_set_strings_unicode(Bool_val(b)) ; |
|---|
| 62 | |
|---|
| 63 | CAMLreturn(Val_unit); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /* Returns #defined values that are used for C functions |
|---|
| 67 | * remove initial Taglib_ to avoid define :) */ |
|---|
| 68 | CAMLprim value caml_taglib_priv_value_int(value name) |
|---|
| 69 | { |
|---|
| 70 | CAMLparam1(name); |
|---|
| 71 | char *s = String_val(name); |
|---|
| 72 | if (!strcmp(s,"File_MPEG")) |
|---|
| 73 | CAMLreturn(Val_int(TagLib_File_MPEG)) ; |
|---|
| 74 | if (!strcmp(s,"File_OggVorbis")) |
|---|
| 75 | CAMLreturn(Val_int(TagLib_File_OggVorbis)) ; |
|---|
| 76 | if (!strcmp(s,"File_FLAC")) |
|---|
| 77 | CAMLreturn(Val_int(TagLib_File_FLAC)) ; |
|---|
| 78 | if (!strcmp(s,"File_MPC")) |
|---|
| 79 | CAMLreturn(Val_int(TagLib_File_MPC)) ; |
|---|
| 80 | |
|---|
| 81 | caml_failwith("Invalid value"); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | #define Taglib_file_val(v) ((TagLib_File *)v) |
|---|
| 85 | #define Taglib_file_const_val(v) ((const TagLib_File *)v) |
|---|
| 86 | #define Taglib_tag_val(v) ((TagLib_Tag *)v) |
|---|
| 87 | #define Taglib_tag_const_val(v) ((const TagLib_Tag *)v) |
|---|
| 88 | #define Taglib_audioproperties_val(v) ((const TagLib_AudioProperties *)v) |
|---|
| 89 | |
|---|
| 90 | CAMLprim value caml_taglib_file_new(value name) |
|---|
| 91 | { |
|---|
| 92 | CAMLparam1(name); |
|---|
| 93 | TagLib_File *f = taglib_file_new((const char *)String_val(name)) ; |
|---|
| 94 | |
|---|
| 95 | if (f == NULL) |
|---|
| 96 | caml_raise_constant(*caml_named_value("taglib_exn_not_found")); |
|---|
| 97 | |
|---|
| 98 | CAMLreturn((value)f); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | CAMLprim value caml_taglib_file_new_type(value name, value type) |
|---|
| 102 | { |
|---|
| 103 | CAMLparam2(name,type); |
|---|
| 104 | TagLib_File *f = taglib_file_new_type((const char *)String_val(name),Int_val(type)) ; |
|---|
| 105 | |
|---|
| 106 | if (f == NULL) |
|---|
| 107 | caml_raise_constant(*caml_named_value("taglib_exn_not_found")); |
|---|
| 108 | |
|---|
| 109 | CAMLreturn((value)f); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | CAMLprim value caml_taglib_file_free(value f) |
|---|
| 113 | { |
|---|
| 114 | CAMLparam1(f); |
|---|
| 115 | taglib_file_free(Taglib_file_val(f)); |
|---|
| 116 | |
|---|
| 117 | CAMLreturn(Val_unit); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | CAMLprim value caml_taglib_file_tag(value f) |
|---|
| 121 | { |
|---|
| 122 | CAMLparam1(f); |
|---|
| 123 | TagLib_Tag *t = taglib_file_tag(Taglib_file_val(f)) ; |
|---|
| 124 | |
|---|
| 125 | if (t == NULL) |
|---|
| 126 | caml_raise_constant(*caml_named_value("taglib_exn_not_found")); |
|---|
| 127 | |
|---|
| 128 | CAMLreturn((value)t); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | CAMLprim value caml_taglib_file_audioproperties(value f) |
|---|
| 132 | { |
|---|
| 133 | CAMLparam1(f); |
|---|
| 134 | TagLib_AudioProperties *p = taglib_file_audioproperties(Taglib_file_val(f)) ; |
|---|
| 135 | |
|---|
| 136 | if (p == NULL) |
|---|
| 137 | caml_raise_constant(*caml_named_value("taglib_exn_not_found")); |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | CAMLreturn((value)p); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | CAMLprim value caml_taglib_file_save(value f) |
|---|
| 144 | { |
|---|
| 145 | CAMLparam1(f); |
|---|
| 146 | CAMLreturn(Val_bool(taglib_file_save(Taglib_file_val(f)))); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | CAMLprim value caml_taglib_tag_get_string(value t, value name) |
|---|
| 150 | { |
|---|
| 151 | CAMLparam2(t,name); |
|---|
| 152 | CAMLlocal1(ans); |
|---|
| 153 | const TagLib_Tag *tag = Taglib_tag_const_val(t) ; |
|---|
| 154 | char *s = String_val(name); |
|---|
| 155 | char *tmp; |
|---|
| 156 | |
|---|
| 157 | if (!strcmp(s,"title")) |
|---|
| 158 | tmp = taglib_tag_title(tag) ; |
|---|
| 159 | else if (!strcmp(s,"artist")) |
|---|
| 160 | tmp = taglib_tag_artist(tag) ; |
|---|
| 161 | else if (!strcmp(s,"album")) |
|---|
| 162 | tmp = taglib_tag_album(tag) ; |
|---|
| 163 | else if (!strcmp(s,"comment")) |
|---|
| 164 | tmp = taglib_tag_comment(tag) ; |
|---|
| 165 | else if (!strcmp(s,"genre")) |
|---|
| 166 | tmp = taglib_tag_genre(tag) ; |
|---|
| 167 | else |
|---|
| 168 | caml_failwith("Invalid value"); |
|---|
| 169 | |
|---|
| 170 | ans = caml_copy_string(tmp) ; |
|---|
| 171 | free(tmp) ; |
|---|
| 172 | CAMLreturn(ans); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | CAMLprim value caml_taglib_tag_get_int(value t, value name) |
|---|
| 176 | { |
|---|
| 177 | CAMLparam2(t,name); |
|---|
| 178 | const TagLib_Tag *tag = Taglib_tag_const_val(t) ; |
|---|
| 179 | char *s = String_val(name); |
|---|
| 180 | int tmp; |
|---|
| 181 | |
|---|
| 182 | if (!strcmp(s,"year")) |
|---|
| 183 | tmp = taglib_tag_year(tag) ; |
|---|
| 184 | else if (!strcmp(s,"track")) |
|---|
| 185 | tmp = taglib_tag_track(tag) ; |
|---|
| 186 | else |
|---|
| 187 | caml_failwith("Invalid value"); |
|---|
| 188 | |
|---|
| 189 | CAMLreturn(Val_int(tmp)); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | CAMLprim value caml_taglib_tag_set_string(value t, value name, value v) |
|---|
| 193 | { |
|---|
| 194 | CAMLparam3(t,name, v); |
|---|
| 195 | TagLib_Tag *tag = Taglib_tag_val(t) ; |
|---|
| 196 | char *s = String_val(name); |
|---|
| 197 | char *x = String_val(v) ; |
|---|
| 198 | |
|---|
| 199 | if (!strcmp(s,"title")) |
|---|
| 200 | taglib_tag_set_title(tag,x) ; |
|---|
| 201 | else if (!strcmp(s,"artist")) |
|---|
| 202 | taglib_tag_set_artist(tag,x) ; |
|---|
| 203 | else if (!strcmp(s,"album")) |
|---|
| 204 | taglib_tag_set_album(tag,x) ; |
|---|
| 205 | else if (!strcmp(s,"comment")) |
|---|
| 206 | taglib_tag_set_comment(tag,x) ; |
|---|
| 207 | else if (!strcmp(s,"genre")) |
|---|
| 208 | taglib_tag_set_genre(tag,x) ; |
|---|
| 209 | else |
|---|
| 210 | caml_failwith("Invalid value"); |
|---|
| 211 | |
|---|
| 212 | CAMLreturn(Val_unit); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | CAMLprim value caml_taglib_tag_set_int(value t, value name, value v) |
|---|
| 216 | { |
|---|
| 217 | CAMLparam3(t,name, v); |
|---|
| 218 | TagLib_Tag *tag = Taglib_tag_val(t) ; |
|---|
| 219 | char *s = String_val(name); |
|---|
| 220 | int x = Int_val(v) ; |
|---|
| 221 | |
|---|
| 222 | if (!strcmp(s,"year")) |
|---|
| 223 | taglib_tag_set_year(tag,x) ; |
|---|
| 224 | else if (!strcmp(s,"track")) |
|---|
| 225 | taglib_tag_set_track(tag,x) ; |
|---|
| 226 | else |
|---|
| 227 | caml_failwith("Invalid value"); |
|---|
| 228 | |
|---|
| 229 | CAMLreturn(Val_unit); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | CAMLprim value caml_taglib_audioproperties_get_int(value p, value name) |
|---|
| 233 | { |
|---|
| 234 | CAMLparam2(p,name); |
|---|
| 235 | const TagLib_AudioProperties *prop = Taglib_audioproperties_val(p) ; |
|---|
| 236 | char *s = String_val(name); |
|---|
| 237 | int tmp; |
|---|
| 238 | |
|---|
| 239 | if (!strcmp(s,"length")) |
|---|
| 240 | tmp = taglib_audioproperties_length(prop) ; |
|---|
| 241 | else if (!strcmp(s,"bitrate")) |
|---|
| 242 | tmp = taglib_audioproperties_bitrate(prop) ; |
|---|
| 243 | else if (!strcmp(s,"samplerate")) |
|---|
| 244 | tmp = taglib_audioproperties_samplerate(prop) ; |
|---|
| 245 | else if (!strcmp(s,"channels")) |
|---|
| 246 | tmp = taglib_audioproperties_channels(prop) ; |
|---|
| 247 | else |
|---|
| 248 | caml_failwith("Invalid value"); |
|---|
| 249 | |
|---|
| 250 | CAMLreturn(Val_int(tmp)); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|