ogginfo-rb 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/ogginfo-rb.rb +40 -12
  2. metadata +1 -1
data/lib/ogginfo-rb.rb CHANGED
@@ -1,19 +1,26 @@
1
- # Useful sites:
2
- # http://en.wikipedia.org/wiki/Ogg#Page_structure
3
- # http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-600004.2
1
+ # This code is distributed under the terms of the Ruby license.
2
+ # See README for information on using ogginfo-rb.
4
3
 
5
4
  require 'stringio'
6
5
 
7
6
  module Ogg
7
+ # See README for usage information.
8
8
  class Info
9
9
  attr_reader :comments
10
10
  attr_reader :duration, :sample_rate, :nominal_bitrate, :channels, :bitrate
11
11
 
12
12
  def initialize path
13
+ raise ArgumentError.new "Argument is not a string" unless path.respond_to? :to_str
14
+
15
+ raise Ogg::ReadError.new "File does not exist" unless File.exists? path
13
16
  file = File.new(path.to_str, 'rb')
17
+ raise Ogg::ReadError.new "File is empty" if file.stat.size == 0
18
+
19
+ first_page = Page.find_page(file)
20
+ raise Ogg::ReadError.new "File does not contain valid Ogg data" unless first_page
14
21
 
15
22
  # Identification header
16
- id_packet = Vorbis::IdentificationHeaderPacket.new(Page.find_page(file))
23
+ id_packet = Vorbis::IdentificationHeaderPacket.new(first_page)
17
24
  @sample_rate = id_packet.audio_sample_rate
18
25
  @nominal_bitrate = id_packet.bitrate_nominal
19
26
  @channels = id_packet.audio_channels
@@ -39,6 +46,8 @@ module Ogg
39
46
  file.close
40
47
  end
41
48
 
49
+ # Get information about the Ogg file at the path specified. An Ogg::Info
50
+ # object is returned and also passed to the block if one is provided.
42
51
  def self.open(path)
43
52
  info = Ogg::Info.new(path)
44
53
  yield info if block_given?
@@ -47,19 +56,33 @@ module Ogg
47
56
 
48
57
  private
49
58
  def calculate_duration file
50
- # This seek is a dirty performance hack (saves reading whole file)
51
- file.seek(-5000, IO::SEEK_END)
59
+ if file.stat.size > 5000
60
+ # This seek is a dirty performance hack (saves reading whole file)
61
+ file.seek(-5000, IO::SEEK_END)
62
+ else
63
+ file.rewind
64
+ end
65
+
52
66
  # Find last page
53
67
  pg = page = Page.find_page(file)
54
68
  while pg
55
69
  page = pg
56
70
  pg = pg.next_page
57
71
  end
58
- # Duration is last granule position divided by sample right
72
+
73
+ # Duration is last granule position divided by sample rate
59
74
  return page.granule_position.to_f / @sample_rate
60
75
  end
61
76
  end
62
77
 
78
+ # This is a superclass for all custom defined Ogg errors
79
+ class Error < StandardError
80
+ end
81
+
82
+ # This error is raised when there is a problem reading the file
83
+ class ReadError < Error
84
+ end
85
+
63
86
  class Page
64
87
  CAPTURE = 'OggS'
65
88
 
@@ -129,11 +152,14 @@ module Ogg
129
152
  return @last_page.next_page
130
153
  end
131
154
  end
132
-
155
+
156
+ # This module specifies packets typically found in Vorbis audio files.
133
157
  module Vorbis
134
- attr_reader :packet_type
135
-
158
+ # This class forms a basis for other Vorbis header packets.
159
+ # See http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-610004.2.1
136
160
  class HeaderPacket < Ogg::Packet
161
+ attr_reader :packet_type
162
+
137
163
  def initialize page
138
164
  super
139
165
 
@@ -144,7 +170,8 @@ module Ogg
144
170
  end
145
171
  end
146
172
  end
147
-
173
+
174
+ # See http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-620004.2.2
148
175
  class IdentificationHeaderPacket < HeaderPacket
149
176
  attr_reader :vorbis_version, :audio_channels, :audio_sample_rate
150
177
  attr_reader :bitrate_maximum, :bitrate_nominal, :bitrate_minimum
@@ -161,7 +188,8 @@ module Ogg
161
188
  blocksizes = @data.read(1).unpack('C').first
162
189
  end
163
190
  end
164
-
191
+
192
+ # See http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-630004.2.3
165
193
  class CommentHeaderPacket < HeaderPacket
166
194
  attr_reader :comments, :vendor_string
167
195
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ogginfo-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aiden Nibali