Ogg::Codecs::VorbisComments

See www.xiph.org/vorbis/doc/v-comment.html Methods to pack/unpack vorbis comment packets intended to be included into Codec classes

Public Instance Methods

binary_size(s) click to toggle source
# File lib/ogg/codecs/comments.rb, line 53
def binary_size(s)
  if RUBY_VERSION[0..2] == "1.8"
    s.size
  else
    s.bytes.to_a.size
  end
end
pack_comments(tag, vendor, preamble="") click to toggle source

Pack tag Hash and vendor string into an ogg packet.

# File lib/ogg/codecs/comments.rb, line 35
def pack_comments(tag, vendor, preamble="")
  packet_data = ""
  packet_data << preamble
  
  packet_data << [ vendor.length ].pack("V")
  packet_data << vendor
  
  packet_data << [tag.size].pack("V")
  tag.each do |k,v|
    tag_data = "#{ k }=#{ v }"
    packet_data << [ binary_size(tag_data) ].pack("V")
    packet_data << tag_data
  end
  
  packet_data << "\0001"
  packet_data
end
unpack_comments(packet, preamble="") click to toggle source

unpack a packet, skipping the preamble returns a 2 element array being a Hash of tag/value pairs and the vendor string

# File lib/ogg/codecs/comments.rb, line 10
def unpack_comments(packet, preamble="")
  pio = StringIO.new(packet)
  pio.read(preamble.length)
  
  vendor_length = pio.read(4).unpack("V").first
  vendor = pio.read(vendor_length)
  
  tag = {}
  tag_size = pio.read(4).unpack("V")[0]
  
  tag_size.times do |i|
    size = pio.read(4).unpack("V")[0]
    comment = pio.read(size)
    unless RUBY_VERSION[0..2] == "1.8"
      comment.force_encoding("UTF-8")
    end
    key, val = comment.split(/=/, 2)
    tag[key.downcase] = val
  end
  
  #framing bit = pio.read(1).unpack("C")[0] 
  [ tag, vendor ]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.