File: comments.rb

package info (click to toggle)
ruby-ogginfo 0.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252 kB
  • sloc: ruby: 2,010; makefile: 8
file content (64 lines) | stat: -rw-r--r-- 1,796 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require "stringio"

module Ogg::Codecs
  # See http://www.xiph.org/vorbis/doc/v-comment.html
  # Methods to pack/unpack vorbis comment packets
  # intended to be included into Codec classes
  module VorbisComments
    # unpack a packet, skipping the preamble
    # returns a 2 element array being a Hash of tag/value pairs and the vendor string
    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
    
    # Pack tag Hash and vendor string into an ogg packet.
    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

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