mb-discid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/LICENSE +25 -0
  2. data/README +23 -0
  3. data/Rakefile +103 -0
  4. data/examples/discid.rb +24 -0
  5. data/ext/discid.c +245 -0
  6. data/ext/extconf.rb +9 -0
  7. metadata +51 -0
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2007, Philipp Wolfer
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+ 1. Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ 2. Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ 3. Neither the name of the RBrainz project nor the names of the
12
+ contributors may be used to endorse or promote products derived from
13
+ this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21
+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README ADDED
@@ -0,0 +1,23 @@
1
+ = MB-DiscID - Ruby bindings for MusicBrainz libdiscid
2
+
3
+ == About
4
+ MB-DiscID provides Ruby bindings for the MusicBrainz DiscID library libdsicid.
5
+ It allows you to calculate MusicBrainz DiscIDs from audio CDs which you can use
6
+ to find the release entry for your CD in the MusicBrainz database.
7
+
8
+ == Installation
9
+ Before installing rdiscid make sure you have libdiscid installed. See
10
+ http://musicbrainz.org/doc/libdiscid for more information on how to do this.
11
+
12
+ === Install with gem
13
+ gem install mb-discid
14
+
15
+ === Installing from source
16
+ rake install
17
+
18
+ == Usage
19
+ See the files in the examples directory for usage information.
20
+
21
+ == License
22
+ Copyright (c) 2007, Philipp Wolfer
23
+ Licensing information can be found in the file LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,103 @@
1
+ # $Id$
2
+ # Copyright (c) 2007, Philipp Wolfer
3
+ # All rights reserved.
4
+ # See LICENSE for permissions.
5
+
6
+ # Rakefile for RDiscID
7
+
8
+ require 'rubygems'
9
+ require 'rake/gempackagetask'
10
+
11
+ task :default do
12
+ puts "Please see 'rake --tasks' for an overview of the available tasks."
13
+ end
14
+
15
+ # Packaging tasks: -------------------------------------------------------
16
+
17
+ PKG_NAME = 'mb-discid'
18
+ PKG_VERSION = '0.0.1'
19
+ PKG_SUMMARY = 'Ruby bindings for libdiscid.'
20
+ PKG_AUTHOR = 'Philipp Wolfer'
21
+ PKG_EMAIL = 'phw@rubyforge.org'
22
+ PKG_HOMEPAGE = 'http://rbrainz.rubyforge.org'
23
+ PKG_DESCRIPTION = <<EOF
24
+ Ruby bindings for libdiscid. See http://musicbrainz.org/doc/libdiscid
25
+ for more information on libdiscid and MusicBrainz.
26
+ EOF
27
+ PKG_FILES = FileList[
28
+ 'Rakefile', 'LICENSE', 'README',
29
+ 'examples/**/*',
30
+ 'ext/**/*.{c,rb}'
31
+ ]
32
+
33
+ spec = Gem::Specification.new do |spec|
34
+ spec.platform = Gem::Platform::RUBY
35
+ spec.name = PKG_NAME
36
+ spec.version = PKG_VERSION
37
+ spec.summary = PKG_SUMMARY
38
+ spec.requirements << 'libdiscid (http://musicbrainz.org/doc/libdiscid)'
39
+ spec.require_path = 'lib'
40
+ spec.autorequire = spec.name
41
+ spec.files = PKG_FILES
42
+ spec.extensions << 'ext/extconf.rb'
43
+ spec.description = PKG_DESCRIPTION
44
+ spec.author = PKG_AUTHOR
45
+ spec.email = PKG_EMAIL
46
+ spec.homepage = PKG_HOMEPAGE
47
+ spec.rubyforge_project = 'rbrainz'
48
+ end
49
+
50
+ Rake::GemPackageTask.new(spec) do |pkg|
51
+ pkg.need_zip = true
52
+ pkg.need_tar_gz= true
53
+ end
54
+
55
+ # Build tasks: -----------------------------------------------------------
56
+
57
+ desc 'Build all the extensions'
58
+ task :build do
59
+ extconf_args = ''
60
+
61
+ unless ENV['DISCID_DIR'].nil?
62
+ extconf_args = "--with-discid-dir=#{ENV['DISCID_DIR']}"
63
+ end
64
+
65
+ cd 'ext' do
66
+ unless system("ruby extconf.rb #{extconf_args}")
67
+ STDERR.puts "ERROR: could not configure extension!\n" +
68
+ "\n#{INFO_NOTE}\n"
69
+ break
70
+ end
71
+
72
+ unless system('make')
73
+ STDERR.puts 'ERROR: could not build extension!'
74
+ break
75
+ end
76
+ end
77
+ end
78
+
79
+ desc 'Install (and build) extensions'
80
+ task :install => [:build] do
81
+ cd 'ext' do
82
+ unless system('make install')
83
+ STDERR.puts 'ERROR: could not install extension!'
84
+ break
85
+ end
86
+ end
87
+ end
88
+
89
+ desc 'Remove extension products'
90
+ task :clobber_build do
91
+ FileList['ext/**/*'].each do |file|
92
+ unless FileList['ext/**/*.{c,rb}'].include?(file)
93
+ rm_r file if File.exists?(file)
94
+ end
95
+ end
96
+ end
97
+
98
+ desc 'Force a rebuild of the extension files'
99
+ task :rebuild => [:clobber_build, :build]
100
+
101
+ desc 'Remove all files created during the build process'
102
+ task :clobber => [:clobber_build, :clobber_package]
103
+
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'DiscID'
4
+
5
+ # Read the device name from the command line or use the default.
6
+ device = $*[0] ? $*[0] : MusicBrainz::DiscID.default_device
7
+
8
+ disc = MusicBrainz::DiscID.new
9
+ disc.read(device)
10
+
11
+ print <<EOF
12
+ DiscID : #{disc.id}
13
+ Submit via : #{disc.submission_url}
14
+ FreeDB ID : #{disc.freedb_id}
15
+ First track: #{disc.first_track_num}
16
+ Last track : #{disc.last_track_num}
17
+ Sectors : #{disc.sectors}
18
+ EOF
19
+
20
+ track = disc.first_track_num
21
+ disc.tracks do |offset, length|
22
+ puts "Track #{track}: Offset #{offset}, Length #{length}"
23
+ track += 1
24
+ end
data/ext/discid.c ADDED
@@ -0,0 +1,245 @@
1
+ /*---------------------------------------------------------------------------
2
+ $Id: discid.c 55 2007-06-01 19:18:12Z phw $
3
+ Copyright (c) 2007, Philipp Wolfer
4
+ All rights reserved.
5
+ See LICENSE for permissions.
6
+
7
+ Ruby bindings for libdiscid. See http://musicbrainz.org/doc/libdiscid
8
+ for more information on libdiscid and MusicBrainz.
9
+ ---------------------------------------------------------------------------*/
10
+
11
+ #include "ruby.h"
12
+ #include "discid/discid.h"
13
+
14
+ /**
15
+ * The MusicBrainz module.
16
+ */
17
+ static VALUE mMusicBrainz;
18
+
19
+ /**
20
+ * The DiscID class.
21
+ */
22
+ static VALUE cDiscID;
23
+
24
+ /**
25
+ * Returns the DiscID as a string.
26
+ *
27
+ * Returns nil if no ID was yet read.
28
+ */
29
+ static VALUE mb_discid_id(VALUE self)
30
+ {
31
+ if (rb_iv_get(self, "@read") == Qfalse)
32
+ return Qnil;
33
+
34
+ DiscId *disc;
35
+ Data_Get_Struct(self, DiscId, disc);
36
+
37
+ return rb_str_new2(discid_get_id(disc));
38
+ }
39
+
40
+ /**
41
+ * Returns a submission URL for the DiscID as a string.
42
+ *
43
+ * Returns nil if no ID was yet read.
44
+ */
45
+ static VALUE mb_discid_submission_url(VALUE self)
46
+ {
47
+ if (rb_iv_get(self, "@read") == Qfalse)
48
+ return Qnil;
49
+
50
+ DiscId *disc;
51
+ Data_Get_Struct(self, DiscId, disc);
52
+
53
+ return rb_str_new2(discid_get_submission_url(disc));
54
+ }
55
+
56
+ /**
57
+ * Return a FreeDB DiscID as a string.
58
+ *
59
+ * Returns nil if no ID was yet read.
60
+ */
61
+ static VALUE mb_discid_freedb_id(VALUE self)
62
+ {
63
+ if (rb_iv_get(self, "@read") == Qfalse)
64
+ return Qnil;
65
+
66
+ DiscId *disc;
67
+ Data_Get_Struct(self, DiscId, disc);
68
+
69
+ return rb_str_new2(discid_get_freedb_id(disc));
70
+ }
71
+
72
+ /**
73
+ * Return the number of the first track on this disc.
74
+ *
75
+ * Returns nil if no ID was yet read.
76
+ */
77
+ static VALUE mb_discid_first_track_num(VALUE self)
78
+ {
79
+ if (rb_iv_get(self, "@read") == Qfalse)
80
+ return Qnil;
81
+
82
+ DiscId *disc;
83
+ Data_Get_Struct(self, DiscId, disc);
84
+
85
+ return INT2FIX(discid_get_first_track_num(disc));
86
+ }
87
+
88
+ /**
89
+ * Return the number of the last track on this disc.
90
+ *
91
+ * Returns nil if no ID was yet read.
92
+ */
93
+ static VALUE mb_discid_last_track_num(VALUE self)
94
+ {
95
+ if (rb_iv_get(self, "@read") == Qfalse)
96
+ return Qnil;
97
+
98
+ DiscId *disc;
99
+ Data_Get_Struct(self, DiscId, disc);
100
+
101
+ return INT2FIX(discid_get_last_track_num(disc));
102
+ }
103
+
104
+ /**
105
+ * Return the length of the disc in sectors.
106
+ *
107
+ * Returns nil if no ID was yet read.
108
+ */
109
+ static VALUE mb_discid_sectors(VALUE self)
110
+ {
111
+ if (rb_iv_get(self, "@read") == Qfalse)
112
+ return Qnil;
113
+
114
+ DiscId *disc;
115
+ Data_Get_Struct(self, DiscId, disc);
116
+
117
+ return INT2FIX(discid_get_sectors(disc));
118
+ }
119
+
120
+ /**
121
+ * Returns an array of [offset, length] tuples for each track.
122
+ *
123
+ * If a block is given this method returns nil and instead iterates over the
124
+ * block calling the block with two arguments |offset, length|.
125
+ *
126
+ * Returns always nil if no ID was yet read. The block won't be called in
127
+ * this case.
128
+ */
129
+ static VALUE mb_discid_tracks(VALUE self)
130
+ {
131
+ if (rb_iv_get(self, "@read") == Qfalse)
132
+ return Qnil;
133
+
134
+ DiscId *disc;
135
+ Data_Get_Struct(self, DiscId, disc);
136
+
137
+ VALUE result = rb_ary_new(); // Array of all [offset, length] tuples
138
+ VALUE tuple; // Array to store one [offset, length] tuple.
139
+ int track = discid_get_first_track_num(disc); // Track number
140
+ while (track <= discid_get_last_track_num(disc))
141
+ {
142
+ tuple = rb_ary_new3(2,
143
+ INT2FIX(discid_get_track_offset(disc, track)),
144
+ INT2FIX(discid_get_track_length(disc, track)) );
145
+
146
+ if (rb_block_given_p())
147
+ rb_yield(tuple);
148
+ else
149
+ rb_ary_push(result, tuple);
150
+
151
+ track++;
152
+ }
153
+
154
+ if (rb_block_given_p())
155
+ return Qnil;
156
+ else
157
+ return result;
158
+ }
159
+
160
+ /**
161
+ * Read the disc ID from the given device.
162
+ *
163
+ * If no device is given the default device of the platform will be used.
164
+ */
165
+ static VALUE mb_discid_read(int argc, VALUE *argv, VALUE self)
166
+ {
167
+ DiscId *disc;
168
+ Data_Get_Struct(self, DiscId, disc);
169
+
170
+ VALUE device = Qnil; // The device string as a Ruby string.
171
+ char* cdevice; // The device string as a C string.
172
+
173
+ // Check the number of arguments
174
+ if (argc > 1)
175
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
176
+ // Convert the given device to a T_STRING
177
+ else if (argc > 0)
178
+ device = rb_funcall(argv[0], rb_intern("to_s"), 0, 0);
179
+
180
+ // Use the default device if none was given.
181
+ if (argc < 1 || RSTRING(device)->len == 0)
182
+ cdevice = discid_get_default_device();
183
+ else
184
+ cdevice = STR2CSTR(device);
185
+
186
+ // Read the discid
187
+ if (discid_read(disc, cdevice) == 0)
188
+ rb_raise(rb_eException, discid_get_error_msg(disc));
189
+ else // Remember that we already read the ID.
190
+ rb_iv_set(self, "@read", Qtrue);
191
+
192
+ return Qnil;
193
+ }
194
+
195
+ /**
196
+ * Construct a new DiscID object.
197
+ *
198
+ * As an optional argument the name of the device to read the ID from
199
+ * may be given. If you don't specify a device here you can later read
200
+ * the ID with the read method.
201
+ */
202
+ VALUE mb_discid_new(int argc, VALUE *argv, VALUE class)
203
+ {
204
+ DiscId *disc = discid_new();
205
+ VALUE tdata = Data_Wrap_Struct(class, 0, discid_free, disc);
206
+ rb_obj_call_init(tdata, 0, 0);
207
+ rb_iv_set(tdata, "@read", Qfalse);
208
+
209
+ // Check the number of arguments
210
+ if (argc > 1)
211
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
212
+ // If a device was given try to read the disc id from this device.
213
+ else if (argc > 0)
214
+ rb_funcall(tdata, rb_intern("read"), 1, argv[0]);
215
+
216
+ return tdata;
217
+ }
218
+
219
+ /**
220
+ * Returns a device string for the default device for this platform.
221
+ */
222
+ VALUE mb_discid_default_device(VALUE class)
223
+ {
224
+ return rb_str_new2(discid_get_default_device());
225
+ }
226
+
227
+ /**
228
+ * Initialize the DiscID class and make it available in Ruby.
229
+ */
230
+ void Init_DiscID() {
231
+ mMusicBrainz = rb_define_module("MusicBrainz");
232
+ cDiscID = rb_define_class_under(mMusicBrainz, "DiscID", rb_cObject);
233
+ rb_define_singleton_method(cDiscID, "new", mb_discid_new, -1);
234
+ rb_define_singleton_method(cDiscID, "default_device",
235
+ mb_discid_default_device, 0);
236
+
237
+ rb_define_method(cDiscID, "read", mb_discid_read, -1);
238
+ rb_define_method(cDiscID, "id", mb_discid_id, 0);
239
+ rb_define_method(cDiscID, "submission_url", mb_discid_submission_url, 0);
240
+ rb_define_method(cDiscID, "freedb_id", mb_discid_freedb_id, 0);
241
+ rb_define_method(cDiscID, "first_track_num", mb_discid_first_track_num, 0);
242
+ rb_define_method(cDiscID, "last_track_num", mb_discid_last_track_num, 0);
243
+ rb_define_method(cDiscID, "sectors", mb_discid_sectors, 0);
244
+ rb_define_method(cDiscID, "tracks", mb_discid_tracks, 0);
245
+ }
data/ext/extconf.rb ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mkmf'
4
+
5
+ if have_library('discid', 'discid_new')
6
+ create_makefile('DiscID')
7
+ else
8
+ 'discid library not found.'
9
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: mb-discid
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-06-01 00:00:00 +02:00
8
+ summary: Ruby bindings for libdiscid.
9
+ require_paths:
10
+ - lib
11
+ email: phw@rubyforge.org
12
+ homepage: http://rbrainz.rubyforge.org
13
+ rubyforge_project: rbrainz
14
+ description: Ruby bindings for libdiscid. See http://musicbrainz.org/doc/libdiscid for more information on libdiscid and MusicBrainz.
15
+ autorequire: mb-discid
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Philipp Wolfer
31
+ files:
32
+ - Rakefile
33
+ - LICENSE
34
+ - README
35
+ - examples/discid.rb
36
+ - ext/discid.c
37
+ - ext/extconf.rb
38
+ test_files: []
39
+
40
+ rdoc_options: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ executables: []
45
+
46
+ extensions:
47
+ - ext/extconf.rb
48
+ requirements:
49
+ - libdiscid (http://musicbrainz.org/doc/libdiscid)
50
+ dependencies: []
51
+