updated on Sun Jan 15 20:01:04 UTC 2012
[aur-mirror.git] / zik / taglib.rb
blobb31d946810b6a272a091e51e8e96785215dd093d
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).
25 require 'dl'
26 require 'dl/import'
28 begin
29         require 'mahoro'
30         module TagLib
31                 MAHORO_PRESENT = true
32         end
33 rescue Exception => e
34         module TagLib
35                 MAHORO_PRESENT = false
36         end
37 end
39 module TagLib
41 begin
42         extend DL::Importer#Since ruby 1.9
43 rescue NameError
44         extend DL::Importable#Old ruby versions
45 end
47 begin
48         dlload 'libtag_c.so'
49         #dlload 'libtag_c.so.0'# for debian-like or use a link.
50 rescue
51         begin
52                 dlload 'libtag_c.dylib'
53         rescue
54                 raise 'libtag_c not found or uses a filename not looked for.'
55         end
56 end
58 File_Type =
60         MPEG = 0,
61         OggVorbis = 1,
62         FLAC = 2,
63         MPC = 3
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
94 end
96 class BadFile < Exception
97 end
99 class BadTag < Exception
102 class BadAudioProperties < Exception
105 class File
107         def initialize(p)
108                 @path = p
109                 raise BadPath.new unless @path
111                 if MAHORO_PRESENT
112                         mahoro = Mahoro.new
113                         mahoro.flags = Mahoro::NONE
114                         mime = mahoro.file(@path)
115                         type = taglibForMime(mime)
116                 else
117                         type = nil
118                 end
120                 if type
121                         @file = TagLib.taglib_file_new_type(@path, type)
122                 else
123                         @file = TagLib.taglib_file_new(@path)
124                 end
126                 unless @file
127                         @path = nil
128                         raise BadFile.new
129                 end
130         end
132         def save
133                 TagLib.taglib_file_save(@file)
134         end
136         def close
137                 if @file
138                         TagLib.taglib_file_free(@file)
139                 end
140                 @path = nil
141                 @file = nil
142                 @tag = nil
143                 @audio = nil
144         end
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.
149 #Vincent
151         def title
152                 TagLib.taglib_tag_title(tag).to_s
153         end
155         def title=(string)
156                 TagLib.taglib_tag_set_title(tag, string)
157         end
159         def artist
160                 TagLib.taglib_tag_artist(tag).to_s
161         end
163         def artist=(string)
164                 TagLib.taglib_tag_set_artist(tag, string)
165         end
167         def album
168                 TagLib.taglib_tag_album(tag).to_s
169         end
171         def album=(string)
172                 TagLib.taglib_tag_set_album(tag, string)
173         end
175         def comment
176                 TagLib.taglib_tag_comment(tag).to_s
177         end
179         def comment=(string)
180                 TagLib.taglib_tag_set_comment(tag, string)
181         end
183         def genre
184                 TagLib.taglib_tag_genre(tag).to_s
185         end
187         def genre=(string)
188                 TagLib.taglib_tag_set_genre(tag, string)
189         end
191         def year
192                 TagLib.taglib_tag_year(tag)
193         end
195         def year=(uint)
196                 TagLib.taglib_tag_set_year(tag, uint)
197         end
199         def track
200                 TagLib.taglib_tag_track(tag)
201         end
203         def track=(uint)
204                 TagLib.taglib_tag_set_track(tag, uint)
205         end
207         def length
208                 TagLib.taglib_audioproperties_length(audio)
209         end
211         def bitrate
212                 TagLib.taglib_audioproperties_bitrate(audio)
213         end
215         def samplerate
216                 TagLib.taglib_audioproperties_samplerate(audio)
217         end
219         def channels
220                 TagLib.taglib_audioproperties_channels(audio)
221         end
223 private
224         def tag
225                 @tag ||= TagLib.taglib_file_tag(@file)
226                 raise BadTag.new unless @tag
227                 @tag
228         end
230         def audio
231                 @audio ||= TagLib.taglib_file_audioproperties(@file)
232                 raise BadAudioProperties.new unless @audio
233                 @audio
234         end
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')
243                                 return TagLib::FLAC
244                         end
245                 end
247                 return nil
248         end