ruby-ogg 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ doc/
2
+ html/
3
+ coverage/
4
+ pkg/
5
+ html/
6
+ .yardoc/
7
+ *~
8
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # This gem's dependencies are specified in ruby-ogg.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,71 @@
1
+ = ruby-ogg
2
+
3
+ == Synopsis
4
+
5
+ ruby-ogg brings pure Ruby love to the Ogg container format. It provides
6
+ a nice abstraction which permits you to access packets from an Ogg bitstream
7
+ without worrying about strange things such as "captures" and "pages".
8
+ For more information about Ogg bitstreams check out the blog post at
9
+ http://dismaldenizen.wordpress.com/2010/08/20/the-ogg-container-format-explained
10
+
11
+ require 'ogg'
12
+
13
+ open("file.ogg", "rb") do |file|
14
+ dec = Ogg::Decoder.new(file) # Create a decoder for an Ogg file
15
+ packet = dec.read_packet # Direct access to packet, no pages!
16
+ # Do something with the packet...
17
+ end
18
+
19
+ == Vorbis
20
+
21
+ Vorbis is not the same as Ogg. Vorbis is an audio codec which is commonly
22
+ packed in the Ogg container format with the file extension ".ogg". Because
23
+ Vorbis has become so deeply associated with Ogg (and I'm such a nice guy), a
24
+ Vorbis metadata decoder is bundled with ruby-ogg. This provides
25
+ a good example of how to use ruby-ogg, and is also fully functional. See the
26
+ Vorbis::Info documentation for more information.
27
+
28
+ require 'vorbis'
29
+
30
+ Vorbis::Info.open('echoplex.ogg') do |info|
31
+ info.comments[:artist].first #=> "Nine Inch Nails"
32
+ info.comments[:title].first #=> "Echoplex"
33
+ info.sample_rate #=> 44100
34
+ end
35
+
36
+ == Installation
37
+
38
+ Add this line to your application's Gemfile:
39
+
40
+ gem 'ruby-ogg'
41
+
42
+ And then execute:
43
+
44
+ $ bundle
45
+
46
+ Or install it yourself as:
47
+
48
+ $ gem install ruby-ogg
49
+
50
+ == Requirements
51
+
52
+ * Ruby 1.8+
53
+ * RubyGems
54
+
55
+ == Features
56
+
57
+ * 100% pure Ruby
58
+ * Read Ogg bitstreams
59
+ * Read Vorbis metadata
60
+ * Optionally validate page integrity with CRCs
61
+ (eg <code>Ogg::Decoder.new(file, :verify_checksum => true)</code>)
62
+
63
+ == TODO
64
+
65
+ * Write Ogg bitstreams
66
+ * Write Vorbis metadata
67
+
68
+ == Links
69
+
70
+ * Bug reports: http://github.com/anibali/ruby-ogg/issues
71
+ * Documentation: http://rdoc.info/github/anibali/ruby-ogg/master/frames
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "yard"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/tc_*.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ YARD::Rake::YardocTask.new
data/lib/ogg.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'rubygems'
1
+ $VERBOSE = false # Suppresses some BinData warnings.
2
2
  require 'bindata'
3
3
 
4
4
  require 'stringio'
@@ -32,10 +32,20 @@ module Ogg
32
32
  # unnecessary. However, if you do need to read pages, that functionality is
33
33
  # available via the read_page method.
34
34
  class Decoder
35
+ # This property determines whether the integrity of pages should be checked
36
+ # using CRCs (a lot slower but more robust).
37
+ attr_accessor :verify_checksum
38
+
35
39
  # Create a new Decoder from an IO which should be open for binary reading.
36
- def initialize io
40
+ def initialize io, opts={}
37
41
  @io = io
38
42
  @packets = []
43
+
44
+ opts = {
45
+ :verify_checksum => false,
46
+ }.merge!(Hash[opts.map {|k, v| [k.to_s.downcase.to_sym, v]}])
47
+
48
+ @verify_checksum = opts[:verify_checksum]
39
49
  end
40
50
 
41
51
  # Moves the file cursor forward to the next potential page. You probably
@@ -63,7 +73,7 @@ module Ogg
63
73
  begin
64
74
  seek_to_page
65
75
  page = Page.read @io
66
- page = nil unless page.verify_checksum
76
+ page = nil if @verify_checksum and not page.correct_checksum?
67
77
  break
68
78
  rescue Exception => ex
69
79
  # False alarm, keep looking...
@@ -147,11 +157,93 @@ module Ogg
147
157
  array :segment_table, :type => :uint8, :initial_length => :page_segments
148
158
  string :data, :read_length => lambda {segment_table.inject(0){|t,e| t+e}}
149
159
 
150
- def verify_checksum
160
+ # Check page data against the header's CRC value.
161
+ def correct_checksum?
151
162
  poly = 0x04c11db7
152
- # TODO: Implement CRC
153
- return true
163
+
164
+ cs = checksum + 0
165
+ self.checksum = 0
166
+
167
+ raw = to_binary_s
168
+
169
+ crc_reg = 0;
170
+
171
+ raw.unpack('C*').each do |byte|
172
+ crc_reg = (crc_reg << 8) ^ CRC_LOOKUP[((crc_reg >> 24) & 0xff) ^ byte]
173
+ crc_reg %= 2**32
174
+ end
175
+
176
+ self.checksum = cs
177
+
178
+ return cs == crc_reg
154
179
  end
180
+
181
+ # Cached CRC lookup table.
182
+ CRC_LOOKUP = [
183
+ 0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9,
184
+ 0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005,
185
+ 0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61,
186
+ 0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd,
187
+ 0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9,
188
+ 0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75,
189
+ 0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011,
190
+ 0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd,
191
+ 0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039,
192
+ 0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5,
193
+ 0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81,
194
+ 0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d,
195
+ 0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49,
196
+ 0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95,
197
+ 0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1,
198
+ 0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d,
199
+ 0x34867077,0x30476dc0,0x3d044b19,0x39c556ae,
200
+ 0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072,
201
+ 0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16,
202
+ 0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca,
203
+ 0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde,
204
+ 0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02,
205
+ 0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066,
206
+ 0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba,
207
+ 0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e,
208
+ 0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692,
209
+ 0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6,
210
+ 0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a,
211
+ 0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e,
212
+ 0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2,
213
+ 0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686,
214
+ 0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a,
215
+ 0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637,
216
+ 0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb,
217
+ 0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f,
218
+ 0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53,
219
+ 0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47,
220
+ 0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b,
221
+ 0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff,
222
+ 0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623,
223
+ 0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7,
224
+ 0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b,
225
+ 0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f,
226
+ 0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3,
227
+ 0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7,
228
+ 0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b,
229
+ 0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f,
230
+ 0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3,
231
+ 0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640,
232
+ 0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c,
233
+ 0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8,
234
+ 0x68860bfd,0x6c47164a,0x61043093,0x65c52d24,
235
+ 0x119b4be9,0x155a565e,0x18197087,0x1cd86d30,
236
+ 0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec,
237
+ 0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088,
238
+ 0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654,
239
+ 0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0,
240
+ 0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c,
241
+ 0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18,
242
+ 0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4,
243
+ 0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0,
244
+ 0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c,
245
+ 0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668,
246
+ 0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4].freeze
155
247
  end
156
248
  end
157
249
 
@@ -0,0 +1,3 @@
1
+ module Ogg
2
+ VERSION = "0.0.3"
3
+ end
data/lib/vorbis.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'rubygems'
2
1
  require 'bindata'
3
2
 
4
3
  module Vorbis
@@ -107,8 +106,6 @@ module Vorbis
107
106
  # A BinData::BasePrimitive which represents a list of comments as per
108
107
  # the Vorbis I comment header specification.
109
108
  class Comments < BinData::BasePrimitive
110
- register(self.name, self)
111
-
112
109
  def read_and_return_value(io)
113
110
  n_comments = read_uint32le(io)
114
111
  comments = InsensitiveHash.new
data/ruby-ogg.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby-ogg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-ogg"
8
+ spec.version = Ogg::VERSION
9
+ spec.authors = ["Aiden Nibali"]
10
+ spec.email = ["dismal.denizen@gmail.com"]
11
+ spec.summary = %q{A pure Ruby library for reading Ogg bitstreams, with a bundled Vorbis metadata reader.}
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/anibali/ruby-ogg"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "yard"
23
+
24
+ spec.add_runtime_dependency "bindata", "~> 1.4"
25
+ end
Binary file
data/test/tc_ogg.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ require 'ogg'
5
+
6
+ class OggTest < Test::Unit::TestCase
7
+ def setup
8
+ @test_dir = File.dirname(File.expand_path(__FILE__))
9
+ end
10
+
11
+ def test_read_page
12
+ dec = Ogg::Decoder.new(open(File.join(@test_dir, "sudo_modprobe.ogg")),
13
+ :verify_checksum => true)
14
+ # First page
15
+ page = dec.read_page
16
+ assert_equal 0, page.page_sequence_number
17
+ assert_equal 0, page.granule_position
18
+ assert_equal 30, page.data.size
19
+ assert_equal "\1vorbis", page.data[0..6]
20
+ # Second page
21
+ page = dec.read_page
22
+ assert_equal 1, page.page_sequence_number
23
+ assert_equal 18446744073709551615, page.granule_position
24
+ assert_equal 4335, page.data.size
25
+ assert_equal "\3vorbis", page.data[0..6]
26
+ end
27
+
28
+ def test_read_packet
29
+ dec = Ogg::Decoder.new(open(File.join(@test_dir, "sudo_modprobe.ogg")),
30
+ :verify_checksum => true)
31
+ # First packet
32
+ packet = dec.read_packet
33
+ assert_equal 30, packet.size
34
+ assert_equal "\1vorbis", packet[0..6]
35
+ # Second packet
36
+ packet = dec.read_packet
37
+ assert_equal 173685, packet.size
38
+ assert_equal "\3vorbis", packet[0..6]
39
+ end
40
+ end
41
+
data/test/tc_vorbis.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ require 'vorbis'
5
+
6
+ class VorbisTest < Test::Unit::TestCase
7
+ def setup
8
+ @test_dir = File.dirname(File.expand_path(__FILE__))
9
+ end
10
+
11
+ def test_info_sudo_modprobe
12
+ Vorbis::Info.open(File.join(@test_dir, "sudo_modprobe.ogg")) do |info|
13
+ assert_equal ['http://linuxoutlaws.com'], info.comments['LICENSE']
14
+ assert_equal ['Sudo Modprobe (The Linux Outlaws Theme)'], info.comments['tItLe']
15
+ assert_equal ['The Linux Outlaws'], info.comments[:artist]
16
+ assert_equal(192000, info.nominal_bitrate)
17
+ assert_equal(44100, info.sample_rate)
18
+ end
19
+ end
20
+ end
21
+
metadata CHANGED
@@ -1,65 +1,126 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ruby-ogg
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Aiden Nibali
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2009-11-24 00:00:00 +11:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2014-09-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: yard
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
16
63
  name: bindata
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.4'
17
70
  type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.0.0
24
- version:
25
- description:
26
- email: dismal.denizen@gmail.com
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.4'
78
+ description: A pure Ruby library for reading Ogg bitstreams, with a bundled Vorbis
79
+ metadata reader.
80
+ email:
81
+ - dismal.denizen@gmail.com
27
82
  executables: []
28
-
29
83
  extensions: []
30
-
31
84
  extra_rdoc_files: []
32
-
33
- files:
34
- - lib/vorbis.rb
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - README.rdoc
89
+ - Rakefile
35
90
  - lib/ogg.rb
36
- has_rdoc: true
37
- homepage: http://rubyforge.org/projects/ruby-ogg/
91
+ - lib/ruby-ogg/version.rb
92
+ - lib/vorbis.rb
93
+ - ruby-ogg.gemspec
94
+ - test/sudo_modprobe.ogg
95
+ - test/tc_ogg.rb
96
+ - test/tc_vorbis.rb
97
+ homepage: https://github.com/anibali/ruby-ogg
38
98
  licenses: []
39
-
40
99
  post_install_message:
41
100
  rdoc_options: []
42
-
43
- require_paths:
101
+ require_paths:
44
102
  - lib
45
- required_ruby_version: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: "0"
50
- version:
51
- required_rubygems_version: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: "0"
56
- version:
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
57
115
  requirements: []
58
-
59
- rubyforge_project: ruby-ogg
60
- rubygems_version: 1.3.5
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.23
61
118
  signing_key:
62
119
  specification_version: 3
63
- summary: A library for reading Ogg bitstreams
64
- test_files: []
65
-
120
+ summary: A pure Ruby library for reading Ogg bitstreams, with a bundled Vorbis metadata
121
+ reader.
122
+ test_files:
123
+ - test/sudo_modprobe.ogg
124
+ - test/tc_ogg.rb
125
+ - test/tc_vorbis.rb
126
+ has_rdoc: