vorbis_comment 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/vcedit.h ADDED
@@ -0,0 +1,52 @@
1
+ /*
2
+ * Copyright (C) 2000-2001 Michael Smith (msmith at xiph org)
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation, version 2.
7
+ *
8
+ * This library is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with this library; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16
+ * MA 02110-1301 USA
17
+ */
18
+
19
+ #ifndef __VCEDIT_H
20
+ #define __VCEDIT_H
21
+
22
+ #ifdef __cplusplus
23
+ extern "C" {
24
+ #endif
25
+
26
+ #include <stdio.h>
27
+ #include <ogg/ogg.h>
28
+ #include <vorbis/codec.h>
29
+
30
+ typedef enum {
31
+ VCEDIT_ERR_SUCCESS = 0,
32
+ VCEDIT_ERR_OPEN,
33
+ VCEDIT_ERR_INVAL,
34
+ VCEDIT_ERR_TMPFILE,
35
+ VCEDIT_ERR_REOPEN
36
+ } vcedit_error;
37
+
38
+ typedef struct vcedit_state_St vcedit_state;
39
+
40
+ vcedit_state *vcedit_state_new (const char *filename);
41
+ void vcedit_state_ref (vcedit_state *state);
42
+ void vcedit_state_unref (vcedit_state *state);
43
+ vorbis_comment *vcedit_comments (vcedit_state *state);
44
+ vcedit_error vcedit_open (vcedit_state *state);
45
+ vcedit_error vcedit_write (vcedit_state *state);
46
+
47
+ #ifdef __cplusplus
48
+ }
49
+ #endif
50
+
51
+ #endif /* __VCEDIT_H */
52
+
File without changes
@@ -0,0 +1,19 @@
1
+ spec = Gem::Specification.new do |s|
2
+ s.name = "vorbis_comment"
3
+ s.version = "1.0.0"
4
+ s.author = "Jeremy Evans"
5
+ s.email = "code@jeremyevans.net"
6
+ s.homepage = "http://rubyforge.org/projects/vorbiscomment/"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.summary = "Vorbis Comment Reader/Writer Library"
9
+ s.files = Dir["*"]
10
+ s.require_paths = ["."]
11
+ s.extensions << 'extconf.rb'
12
+ s.autorequire = "vorbis_comment"
13
+ s.test_files = Dir["test/*"]
14
+ s.has_rdoc = true
15
+ s.rdoc_options = %w'--inline-source --line-numbers'
16
+ s.add_dependency('cicphash', [">= 1.0.0"])
17
+ s.rubyforge_project = 'vorbiscomment'
18
+ end
19
+
data/vorbis_comment.rb ADDED
@@ -0,0 +1,164 @@
1
+ #!/usr/bin/env ruby
2
+ # This library implements a Vorbis Comment reader/writer.
3
+ # If called from the command line, it prints out the contents of the APEv2 tag
4
+ # for the given filename arguments.
5
+ #
6
+ # ruby-vorbis_comment is a pure Ruby library for manipulating Vorbis comments.
7
+ # It wraps libvorbis and libogg, so it should be completely standards
8
+ # compatible. Vorbis comment is the standard tagging format for Ogg Vorbis,
9
+ # FLAC, and Speex files.
10
+ #
11
+ # The library includes a C extension, which may or may not build cleanly on all
12
+ # architectures. It is developed and tested on OpenBSD i386 and amd64. If it
13
+ # doesn't work on your machine, please try to get it to work and send in a
14
+ # patch.
15
+ #
16
+ # This library tries to be API compatible with ruby-apetag, a library for
17
+ # reading and writing APE tags, the standard tagging format for Musepack and
18
+ # Monkey's Audio, which can also be used with MP3s as an alternative to ID3v2.
19
+ #
20
+ # General Use:
21
+ #
22
+ # require 'vorbiscomment'
23
+ # a = VorbisComment.new('file.ogg')
24
+ # a.exists? # if it already has an vorbis comment
25
+ # a.fields # a CICPHash of fields, keys are strings, values are lists of strings
26
+ # a.pretty_print # string suitable for pretty printing
27
+ # a.update{|fields| fields['Artist']='Test Artist'; fields.delete('Year')}
28
+ # # Update the tag with the added/changed/deleted fields
29
+ # # Note that you should do: a.update{|fields| fields.replace('Test'=>'Test')}
30
+ # # and NOT: a.update{|fields| fields = {'Test'=>'Test'}}
31
+ # # You need to update/modify the fields given, not reassign it
32
+ # a.remove! # clear the list of vorbis comments from the file
33
+ #
34
+ # To run the tests for the library, run test_vorbis_comment.rb.
35
+ #
36
+ # If you find any bugs, would like additional documentation, or want to submit a
37
+ # patch, please use Rubyforge (http://rubyforge.org/projects/vorbis_comment/).
38
+ # One known bug is in the libvorbis library, in that it doesn't like files less
39
+ # than 8K in size.
40
+ #
41
+ # The most current source code can be accessed via anonymous SVN at
42
+ # svn://code.jeremyevans.net/ruby-vorbis_comment/. Note that the library isn't
43
+ # modified on a regular basis, so it is unlikely to be different from the latest
44
+ # release.
45
+ #
46
+ # The pure ruby part of this library is copyright Jeremy Evans and is released
47
+ # under the MIT License.
48
+ #
49
+ # The C part of this library is copyright Jeremy Evans and Tilman Sauerbeck and
50
+ # is licensed under the GNU LGPL.
51
+ #
52
+ # Copyright (c) 2007 Jeremy Evans
53
+ #
54
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
55
+ # of this software and associated documentation files (the "Software"), to deal
56
+ # in the Software without restriction, including without limitation the rights
57
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
58
+ # copies of the Software, and to permit persons to whom the Software is
59
+ # furnished to do so, subject to the following conditions:
60
+ #
61
+ # The above copyright notice and this permission notice shall be included in
62
+ # all copies or substantial portions of the Software.
63
+ #
64
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
65
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
66
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
67
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
68
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
69
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
70
+ # SOFTWARE.
71
+
72
+ require 'cicphash'
73
+
74
+ # Contains all of the Vorbis comment fields found in the filename/file given.
75
+ class VorbisComment
76
+ attr_reader :filename
77
+ BAD_KEY_RE = /[\x00-\x1f=\x7e-\xff]/
78
+
79
+ # Set the filename to be used
80
+ def initialize(filename)
81
+ @filename = filename.to_s
82
+ end
83
+
84
+ # Check for the existance of the a vorbis comment in the file.
85
+ # Because all Vorbis bitstreams should have a comment, any file missing
86
+ # a comment probably isn't a vorbis file, or it's possible the file is
87
+ # corrupt.
88
+ def exists?
89
+ begin
90
+ fields
91
+ true
92
+ rescue InvalidDataError
93
+ false
94
+ end
95
+ end
96
+
97
+ # Clear all fields from the vorbis comment
98
+ def remove!
99
+ update{|fields| fields.clear}
100
+ end
101
+
102
+ # Get all the fields in the vorbis comment
103
+ def fields
104
+ return @fields if @fields
105
+ @fields = CICPHash.new
106
+ read_fields
107
+ @fields
108
+ end
109
+
110
+ # Return a string suitable for pretty printing, used by the command line
111
+ def pretty_print
112
+ begin
113
+ fields.sort.collect{|key, value| "#{key}: #{value.join(', ')}"}.join("\n")
114
+ rescue OpenError
115
+ "FILE NOT FOUND!"
116
+ rescue InvalidDataError, InvalidCommentError
117
+ "CORRUPT TAG!"
118
+ end
119
+ end
120
+
121
+ # Update the vorbis comment with the changes that occur to fields inside the
122
+ # block. Make sure to modify the fields given and not reassign them.
123
+ def update(&block)
124
+ yield fields
125
+ write_fields(normalize_fields)
126
+ fields
127
+ end
128
+
129
+ private
130
+ # Return a array with key/value pairs so that keys with multiple values
131
+ # have multiple entries. This is used mainly to make the C extension
132
+ # easier to code.
133
+ def normalize_fields
134
+ comments = []
135
+ fields.each do |key, values|
136
+ key = key.to_s
137
+ raise InvalidCommentError if key =~ BAD_KEY_RE
138
+ values = [values] unless values.is_a?(Array)
139
+ values = values.collect{|value| value.to_s}
140
+ values.each do |value|
141
+ value.unpack('U*') rescue (raise InvalidCommentError)
142
+ comments << [key, value]
143
+ end
144
+ end
145
+ comments.sort
146
+ end
147
+
148
+ # Add a value for the given key to the list of values for that key, creating
149
+ # the list if this is the first value for that key. This is used to make
150
+ # the C extension easier to code.
151
+ def add_to_fields(key, value)
152
+ (fields[key] ||= []) << value
153
+ end
154
+ end
155
+
156
+ require 'vorbis_comment_ext' # define read_fields and write_fields
157
+
158
+ # If called directly from the command line, treat all arguments as filenames
159
+ # and pretty print the vorbis comment fields for each filename.
160
+ if __FILE__ == $0
161
+ ARGV.each do |filename|
162
+ puts filename, '-'*filename.length, VorbisComment.new(filename).pretty_print, ''
163
+ end
164
+ end
@@ -0,0 +1,161 @@
1
+ /*
2
+ * Copyright (C) 2007 Jeremy Evans (code at jeremyevans net)
3
+ * Copyright (C) 2006 Tilman Sauerbeck (tilman at code-monkey de)
4
+ *
5
+ * This library is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation, version 2.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
+ * MA 02110-1301 USA
18
+ */
19
+
20
+ #include <ruby.h>
21
+ #include <st.h>
22
+ #include <stdio.h>
23
+ #include <stdbool.h>
24
+ #include <errno.h>
25
+ #include <assert.h>
26
+ #include <limits.h>
27
+ #include <unistd.h>
28
+ #include <sys/types.h>
29
+ #include <sys/stat.h>
30
+ #include <ogg/ogg.h>
31
+ #include <vorbis/codec.h>
32
+
33
+ #include "vcedit.h"
34
+
35
+ static VALUE eOpen, eInvalidData, eInvalidComment, eTempFile, eReopen;
36
+
37
+ /*
38
+ * call-seq:
39
+ * object.read_fields -> fields
40
+ *
41
+ * Reads the comments from the file into the appropriate data structure.
42
+ * Returns the fields (a precreated CICPHash). Do not call this directly.
43
+ */
44
+ VALUE read_fields (VALUE self) {
45
+ vcedit_state *state;
46
+ vorbis_comment *vc;
47
+ VALUE fields, filename;
48
+ VALUE k, v;
49
+ int add_to_fields;
50
+ int i;
51
+
52
+ filename = rb_iv_get(self, "@filename");
53
+ state = vcedit_state_new(StringValuePtr(filename));
54
+ if (!state)
55
+ rb_raise (rb_eNoMemError, "Out of Memory");
56
+
57
+ switch (vcedit_open (state)) {
58
+ case VCEDIT_ERR_OPEN:
59
+ vcedit_state_unref(state);
60
+ rb_raise (eOpen, "Cannot open file");
61
+ case VCEDIT_ERR_INVAL:
62
+ vcedit_state_unref(state);
63
+ rb_raise (eInvalidData, "Invalid data");
64
+ default:
65
+ break;
66
+ }
67
+
68
+ vc = vcedit_comments(state);
69
+
70
+ fields = rb_iv_get(self, "@fields");
71
+ rb_funcall(fields, rb_intern("clear"), 0);
72
+ add_to_fields = rb_intern("add_to_fields");
73
+
74
+ /* check whether all comments are well-formed */
75
+ for (i = 0; i < vc->comments; i++) {
76
+ char *ptr, *content = vc->user_comments[i];
77
+
78
+ ptr = strchr (content, '=');
79
+ if (!ptr || ptr == content) {
80
+ rb_funcall(fields, rb_intern("clear"), 0);
81
+ vcedit_state_unref(state);
82
+ rb_raise (eInvalidComment, "invalid comment - %s", content);
83
+ }
84
+
85
+ k = rb_str_new (content, ptr - content);
86
+ v = rb_str_new2 (ptr + 1);
87
+ rb_funcall(self, add_to_fields, 2, k, v);
88
+ }
89
+
90
+ vcedit_state_unref(state);
91
+ return fields;
92
+ }
93
+
94
+ /*
95
+ * call-seq:
96
+ * object.write_fields(comments) -> comments
97
+ *
98
+ * Writes the comments to the file. Do not call this directly.
99
+ */
100
+ VALUE write_fields (VALUE self, VALUE comments) {
101
+ vcedit_state *state;
102
+ vorbis_comment *vc;
103
+ struct RArray *items, *comment;
104
+ VALUE filename;
105
+ int i, j;
106
+
107
+ filename = rb_iv_get(self, "@filename");
108
+ state = vcedit_state_new(StringValuePtr(filename));
109
+ if (!state)
110
+ rb_raise (rb_eNoMemError, "Out of Memory");
111
+
112
+ switch (vcedit_open (state)) {
113
+ case VCEDIT_ERR_OPEN:
114
+ vcedit_state_unref(state);
115
+ rb_raise (eOpen, "Cannot open file");
116
+ case VCEDIT_ERR_INVAL:
117
+ vcedit_state_unref(state);
118
+ rb_raise (eInvalidData, "Invalid data");
119
+ default:
120
+ break;
121
+ }
122
+
123
+ vc = vcedit_comments(state);
124
+ vorbis_comment_clear(vc);
125
+ vorbis_comment_init(vc);
126
+
127
+ items = RARRAY(comments);
128
+ for (i = 0; i < items->len; i++) {
129
+ comment = RARRAY(items->ptr[i]);
130
+ vorbis_comment_add_tag(vc, StringValuePtr(comment->ptr[0]), StringValuePtr(comment->ptr[1]));
131
+ }
132
+
133
+ switch (vcedit_write (state)) {
134
+ case VCEDIT_ERR_INVAL:
135
+ vcedit_state_unref(state);
136
+ rb_raise (eInvalidData, "Invalid data");
137
+ case VCEDIT_ERR_TMPFILE:
138
+ vcedit_state_unref(state);
139
+ rb_raise (eTempFile, "Cannot create temporary file");
140
+ case VCEDIT_ERR_REOPEN:
141
+ vcedit_state_unref(state);
142
+ rb_raise (eReopen, "Cannot reopen file");
143
+ default:
144
+ break;
145
+ }
146
+ vcedit_state_unref(state);
147
+
148
+ return comments;
149
+ }
150
+
151
+ void Init_vorbis_comment_ext(void) {
152
+ VALUE VorbisComment;
153
+ VorbisComment = rb_define_class("VorbisComment", rb_cObject);
154
+ rb_define_private_method(VorbisComment, "read_fields", read_fields, 0);
155
+ rb_define_private_method(VorbisComment, "write_fields", write_fields, 1);
156
+ eOpen = rb_define_class_under (VorbisComment, "OpenError", rb_eStandardError);
157
+ eInvalidData = rb_define_class_under (VorbisComment, "InvalidDataError", rb_eStandardError);
158
+ eInvalidComment = rb_define_class_under (VorbisComment, "InvalidCommentError", rb_eStandardError);
159
+ eTempFile = rb_define_class_under (VorbisComment, "TempFileError", rb_eStandardError);
160
+ eReopen = rb_define_class_under (VorbisComment, "ReopenError", rb_eStandardError);
161
+ }
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: vorbis_comment
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2007-09-11 00:00:00 -07:00
8
+ summary: Vorbis Comment Reader/Writer Library
9
+ require_paths:
10
+ - .
11
+ email: code@jeremyevans.net
12
+ homepage: http://rubyforge.org/projects/vorbiscomment/
13
+ rubyforge_project: vorbiscomment
14
+ description:
15
+ autorequire: vorbis_comment
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
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
+ - Jeremy Evans
31
+ files:
32
+ - LICENSE
33
+ - extconf.rb
34
+ - test
35
+ - vcedit.c
36
+ - vcedit.h
37
+ - vorbis_comment.gemspec
38
+ - vorbis_comment.rb
39
+ - vorbis_comment_ext.c
40
+ - vorbis_comment-1.0.0.gem
41
+ test_files:
42
+ - test/test_vorbis_comment.rb
43
+ - test/manyfields.ogg
44
+ - test/corrupt.ogg
45
+ - test/lt8k.ogg
46
+ - test/blank.ogg
47
+ - test/title.ogg
48
+ rdoc_options:
49
+ - --inline-source
50
+ - --line-numbers
51
+ extra_rdoc_files: []
52
+
53
+ executables: []
54
+
55
+ extensions:
56
+ - extconf.rb
57
+ requirements: []
58
+
59
+ dependencies:
60
+ - !ruby/object:Gem::Dependency
61
+ name: cicphash
62
+ version_requirement:
63
+ version_requirements: !ruby/object:Gem::Version::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 1.0.0
68
+ version: