Module: Ogg::Codecs::VorbisComments

Included in:
Opus, Speex, Vorbis
Defined in:
lib/ogg/codecs/comments.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#binary_size(s) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/ogg/codecs/comments.rb', line 56

def binary_size(s)
  if RUBY_VERSION[0..2] == "1.8"
    s.size
  else
    s.dup.force_encoding("BINARY").size
  end
end

#pack_comments(tag, vendor, preamble = "") ⇒ Object

Pack tag Hash and vendor string into an ogg packet.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ogg/codecs/comments.rb', line 35

def pack_comments(tag, vendor, preamble="")
  packet_data = "".force_encoding("binary")
  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 }"
    unless RUBY_VERSION[0..2] == "1.8"
      tag_data.force_encoding("binary")
    end
    packet_data << [ binary_size(tag_data) ].pack("V")
    packet_data << tag_data
  end
  
  packet_data << "\001"
  packet_data
end

#unpack_comments(packet, preamble = "") ⇒ Object

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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