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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
/*
Copyright 2010-2011 Vincent Carmona
vinc4mai+taglib@gmail.com
This file is part of ruby-taglib2.
ruby-taglib2 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
ruby-taglib2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ruby-taglib2; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ruby.h>
#include <taglib/tag_c.h>
typedef struct
{
TagLib_File *file;
TagLib_Tag *tag;
const TagLib_AudioProperties *audio;
int closed;
} tgFileData;
VALUE mTagLib;
VALUE eBadFile, eBadTag, eBadAudioProperties, eFileClosed;
VALUE cFile;
static void
free_tgFileData(tgFileData *d)
{
if (!d->closed)
taglib_file_free(d->file);
free(d);
}
TagLib_Tag *
file_tag(VALUE self)
{
tgFileData *d;
TagLib_Tag *tgTag;
Data_Get_Struct(self, tgFileData, d);
if (d->tag==NULL)
{
tgTag=taglib_file_tag(d->file);
if (tgTag==NULL)
rb_raise(eBadTag, "Bad tag");
d->tag=tgTag;
}
return d->tag;
}
const TagLib_AudioProperties *
file_audio(VALUE self)
{
tgFileData *d;
const TagLib_AudioProperties *tgAudio;
Data_Get_Struct(self, tgFileData, d);
if (d->audio==NULL)
{
tgAudio=taglib_file_audioproperties(d->file);
if (tgAudio==NULL)
rb_raise(eBadAudioProperties, "Bad audio properties");
d->audio=tgAudio;
}
return d->audio;
}
void
file_check_closed(VALUE self)
{
tgFileData *d;
Data_Get_Struct(self, tgFileData, d);
if (d->closed)
rb_raise(eFileClosed, "File closed");
}
/*:nodoc:*/
VALUE
file_alloc(VALUE self)
{
tgFileData *data;
data=ALLOC(tgFileData);
data->tag=NULL;
data->audio=NULL;
data->closed=0;
return Data_Wrap_Struct(self, 0, free_tgFileData, data);
}
/*
call-seq: new(path, type=nil)
Creates a new File object.
path: pathname of an audio file as a ruby string
type: a TagLib::Type (i.e TagLib::OggVorbis, TagLib::MP4)
*/
VALUE
file_init(int argc, VALUE* argv, VALUE self)
{
VALUE path, type;
TagLib_File *tgFile;
tgFileData *data;
rb_scan_args(argc, argv, "11", &path, &type);
if (NIL_P(type))
{
tgFile=taglib_file_new(StringValuePtr(path));
}
else
{
taglib_file_new_type(StringValuePtr(path), NUM2INT(type));
}
if (tgFile==NULL)
rb_raise(eBadFile, "Bad file");
Data_Get_Struct(self, tgFileData, data);
data->file=tgFile;
rb_iv_set(self, "@path", path);
rb_iv_set(self, "@type", type);
return Qnil;
}
/*Save modifications to the audio file.*/
VALUE
file_save(VALUE self)
{
tgFileData *data;
file_check_closed(self);
Data_Get_Struct(self, tgFileData, data);
if (taglib_file_save(data->file))
{
return Qtrue;
}
else
{
return Qfalse;
}
}
/*Close audio file*/
VALUE
file_close(VALUE self)
{
tgFileData *data;
Data_Get_Struct(self, tgFileData, data);
if (data->closed)
return self;
taglib_file_free(data->file);
data->closed=1;
return self;
}
/*Get track title*/
VALUE
file_get_title(VALUE self)
{
VALUE str;
file_check_closed(self);
str=rb_str_new2(taglib_tag_title(file_tag(self)));
taglib_tag_free_strings;
return str;
}
/*
call-seq: title=title
Set track title to title
title: a string
*/
VALUE
file_set_title(VALUE self, VALUE title)
{
file_check_closed(self);
taglib_tag_set_title(file_tag(self), StringValuePtr(title));
return title;
}
/*Get track artist*/
VALUE
file_get_artist(VALUE self)
{
VALUE str;
file_check_closed(self);
str=rb_str_new2(taglib_tag_artist(file_tag(self)));
taglib_tag_free_strings;
return str;
}
/*
call-seq: artist=artist
Set track artist to artist
artist: a string
*/
VALUE
file_set_artist(VALUE self, VALUE artist)
{
file_check_closed(self);
taglib_tag_set_artist(file_tag(self), StringValuePtr(artist));
return artist;
}
/*Get track album*/
VALUE
file_get_album(VALUE self)
{
VALUE str;
file_check_closed(self);
str=rb_str_new2(taglib_tag_album(file_tag(self)));
taglib_tag_free_strings;
return str;
}
/*
call-seq: album=album
Set track album to album
album: a string
*/
VALUE
file_set_album(VALUE self, VALUE album)
{
file_check_closed(self);
taglib_tag_set_album(file_tag(self), StringValuePtr(album));
return album;
}
/*Get track comment*/
VALUE
file_get_comment(VALUE self)
{
VALUE str;
file_check_closed(self);
str=rb_str_new2(taglib_tag_comment(file_tag(self)));
taglib_tag_free_strings;
return str;
}
/*
call-seq: comment=comment
Set track comment to comment
comment: a string
*/
VALUE
file_set_comment(VALUE self, VALUE comment)
{
file_check_closed(self);
taglib_tag_set_comment(file_tag(self), StringValuePtr(comment));
return comment;
}
/*Get track genre*/
VALUE
file_get_genre(VALUE self)
{
VALUE str;
file_check_closed(self);
str=rb_str_new2(taglib_tag_genre(file_tag(self)));
taglib_tag_free_strings;
return str;
}
/*
call-seq: genre=genre
Set track genre to genre
genre: a string
*/
VALUE
file_set_genre(VALUE self, VALUE genre)
{
file_check_closed(self);
taglib_tag_set_genre(file_tag(self), StringValuePtr(genre));
return genre;
}
/*Get track year*/
VALUE
file_get_year(VALUE self)
{
file_check_closed(self);
return INT2FIX(taglib_tag_year(file_tag(self)));
}
/*
call-seq: year=year
Set track year to year
year: a Fixnum
*/
VALUE
file_set_year(VALUE self, VALUE year)
{
file_check_closed(self);
taglib_tag_set_year(file_tag(self), NUM2INT(year));
return year;
}
/*Get track number*/
VALUE
file_get_track(VALUE self)
{
file_check_closed(self);
return INT2FIX(taglib_tag_track(file_tag(self)));
}
/*
call-seq: track=number
Set track number to number
number: a Fixnum
*/
VALUE
file_set_track(VALUE self, VALUE track)
{
file_check_closed(self);
taglib_tag_set_track(file_tag(self), NUM2INT(track));
return track;
}
/*Get track length in seconds*/
VALUE
file_get_length(VALUE self)
{
file_check_closed(self);
return INT2NUM(taglib_audioproperties_length(file_audio(self)));
}
/*Get track bitrate in kb/s*/
VALUE
file_get_bitrate(VALUE self)
{
file_check_closed(self);
return INT2NUM(taglib_audioproperties_bitrate(file_audio(self)));
}
/*Get track samplerate in Hz*/
VALUE
file_get_samplerate(VALUE self)
{
file_check_closed(self);
return INT2NUM(taglib_audioproperties_samplerate(file_audio(self)));
}
/*Get the number of channels in the audio stream*/
VALUE
file_get_channels(VALUE self)
{
file_check_closed(self);
return INT2NUM(taglib_audioproperties_channels(file_audio(self)));
}
void
Init_taglib2_ext()
{
/*
ruby-taglib2 is a Ruby interface to TagLib, the audio meta-data library.
require 'taglib2'
tag=TagLib::File.new('music.ogg')
tag.title
tag.genre="my genre"
tag.save
*/
mTagLib=rb_define_module("TagLib");
eBadFile=rb_define_class_under(mTagLib, "BadFile", rb_eException);
eBadTag=rb_define_class_under(mTagLib, "BadTag", rb_eException);
eBadAudioProperties=rb_define_class_under(mTagLib, "BadAudioProperties", rb_eException);
eFileClosed=rb_define_class_under(mTagLib, "FileClosed", rb_eException);
rb_define_const(mTagLib, "MPEG", INT2FIX(TagLib_File_MPEG));
rb_define_const(mTagLib, "OggVorbis", INT2FIX(TagLib_File_OggVorbis));
rb_define_const(mTagLib, "FLAC", INT2FIX(TagLib_File_FLAC));
rb_define_const(mTagLib, "MPC", INT2FIX(TagLib_File_MPC));
rb_define_const(mTagLib, "OggFlac", INT2FIX(TagLib_File_OggFlac));
rb_define_const(mTagLib, "WavPack", INT2FIX(TagLib_File_WavPack));
rb_define_const(mTagLib, "Speex", INT2FIX(TagLib_File_Speex));
rb_define_const(mTagLib, "TrueAudio", INT2FIX(TagLib_File_TrueAudio));
rb_define_const(mTagLib, "MP4", INT2FIX(TagLib_File_MP4));
rb_define_const(mTagLib, "ASF", INT2FIX(TagLib_File_ASF));
cFile=rb_define_class_under(mTagLib, "File", rb_cObject);
rb_define_alloc_func(cFile, file_alloc);
rb_define_method(cFile, "initialize", file_init, -1);
rb_define_method(cFile, "save", file_save, 0);
rb_define_method(cFile, "close", file_close, 0);
rb_define_method(cFile, "title", file_get_title, 0);
rb_define_method(cFile, "title=", file_set_title, 1);
rb_define_method(cFile, "artist", file_get_artist, 0);
rb_define_method(cFile, "artist=", file_set_artist, 1);
rb_define_method(cFile, "album", file_get_album, 0);
rb_define_method(cFile, "album=", file_set_album, 1);
rb_define_method(cFile, "comment", file_get_comment, 0);
rb_define_method(cFile, "comment=", file_set_comment, 1);
rb_define_method(cFile, "genre", file_get_genre, 0);
rb_define_method(cFile, "genre=", file_set_genre, 1);
rb_define_method(cFile, "year", file_get_year, 0);
rb_define_method(cFile, "year=", file_set_year, 1);
rb_define_method(cFile, "track", file_get_track, 0);
rb_define_method(cFile, "track=", file_set_track, 1);
rb_define_method(cFile, "length", file_get_length, 0);
rb_define_method(cFile, "bitrate", file_get_bitrate, 0);
rb_define_method(cFile, "samplerate", file_get_samplerate, 0);
rb_define_method(cFile, "channels", file_get_channels, 0);
}
|