1 # Copyright (C) 2004 Neil Stevens <neil@hakubi.us>
2 # Copyright (C) 2010 Vincent Carmona <vinc4mai@gmail.com>
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 # Except as contained in this notice, the name(s) of the author(s) shall not be
22 # used in advertising or otherwise to promote the sale, use or other dealings
23 # in this Software without prior written authorization from the author(s).
35 MAHORO_PRESENT = false
42 extend DL::Importer#Since ruby 1.9
44 extend DL::Importable#Old ruby versions
49 #dlload 'libtag_c.so.0'# for debian-like or use a link.
52 dlload 'libtag_c.dylib'
54 raise 'libtag_c not found or uses a filename not looked for.'
66 extern 'void* taglib_file_new(char*)'
67 extern 'void* taglib_file_new_type(char*, int)'
68 extern 'void taglib_file_free(void*)'
69 extern 'void* taglib_file_tag(void*)'
70 extern 'void* taglib_file_audioproperties(void*)'
71 extern 'void* taglib_file_save(void*)'
73 extern 'char* taglib_tag_title(void*)'
74 extern 'char* taglib_tag_artist(void*)'
75 extern 'char* taglib_tag_album(void*)'
76 extern 'char* taglib_tag_comment(void*)'
77 extern 'char* taglib_tag_genre(void*)'
78 extern 'unsigned int taglib_tag_year(void*)'
79 extern 'unsigned int taglib_tag_track(void*)'
80 extern 'void taglib_tag_set_title(void*, char*)'
81 extern 'void taglib_tag_set_artist(void*, char*)'
82 extern 'void taglib_tag_set_album(void*, char*)'
83 extern 'void taglib_tag_set_comment(void*, char*)'
84 extern 'void taglib_tag_set_genre(void*, char*)'
85 extern 'void taglib_tag_set_year(void*, unsigned int)'
86 extern 'void taglib_tag_set_track(void*, unsigned int)'
88 extern 'int taglib_audioproperties_length(void*)'
89 extern 'int taglib_audioproperties_bitrate(void*)'
90 extern 'int taglib_audioproperties_samplerate(void*)'
91 extern 'int taglib_audioproperties_channels(void*)'
93 class BadPath < Exception
96 class BadFile < Exception
99 class BadTag < Exception
102 class BadAudioProperties < Exception
109 raise BadPath.new unless @path
113 mahoro.flags = Mahoro::NONE
114 mime = mahoro.file(@path)
115 type = taglibForMime(mime)
121 @file = TagLib.taglib_file_new_type(@path, type)
123 @file = TagLib.taglib_file_new(@path)
133 TagLib.taglib_file_save(@file)
138 TagLib.taglib_file_free(@file)
146 #I have had to_s method to be sure that methods return a string and not a char*.
147 #The behaviour is inconsistent depending on ruby version.
148 #I hope that is not too ugly.
152 TagLib.taglib_tag_title(tag).to_s
156 TagLib.taglib_tag_set_title(tag, string)
160 TagLib.taglib_tag_artist(tag).to_s
164 TagLib.taglib_tag_set_artist(tag, string)
168 TagLib.taglib_tag_album(tag).to_s
172 TagLib.taglib_tag_set_album(tag, string)
176 TagLib.taglib_tag_comment(tag).to_s
180 TagLib.taglib_tag_set_comment(tag, string)
184 TagLib.taglib_tag_genre(tag).to_s
188 TagLib.taglib_tag_set_genre(tag, string)
192 TagLib.taglib_tag_year(tag)
196 TagLib.taglib_tag_set_year(tag, uint)
200 TagLib.taglib_tag_track(tag)
204 TagLib.taglib_tag_set_track(tag, uint)
208 TagLib.taglib_audioproperties_length(audio)
212 TagLib.taglib_audioproperties_bitrate(audio)
216 TagLib.taglib_audioproperties_samplerate(audio)
220 TagLib.taglib_audioproperties_channels(audio)
225 @tag ||= TagLib.taglib_file_tag(@file)
226 raise BadTag.new unless @tag
231 @audio ||= TagLib.taglib_file_audioproperties(@file)
232 raise BadAudioProperties.new unless @audio
236 def taglibForMime(mime)
237 return TagLib::MPEG if mime.include?('MP3')
239 if mime.include?('Ogg') or mime.include?('ogg')
240 if mime.include?('Vorbis') or mime.include?('vorbis')
241 return TagLib::OggVorbis
242 elsif mime.include?('FLAC')