File: opus.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 (39 lines) | stat: -rw-r--r-- 1,239 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
module Ogg::Codecs
  class Opus
    class << self
      include VorbisComments
      # return true/false based on whether the header packet belongs to us
      def match?(header_packet)
        header_packet.start_with?("OpusHead")
      end
      
      #consume header and tag pages, return array of two hashes, info and tags 
      def decode_headers(reader)
        init_pkt, tag_pkt = reader.read_packets(2) # init_pkt, tag_pkt
        info = extract_info(init_pkt)
        info[:tag], info[:tag_vendor] = unpack_comments(tag_pkt, "OpusTags")
        info
      end
      
      # consume pages with old tags/setup packets and rewrite newtags,setup packets
      # return the number of pages written
      def replace_tags(reader, writer, new_tags, vendor)
        _ = reader.read_packets(1) # tag_packet
        writer.write_packets(0, pack_comments(new_tags, vendor, "OpusTags"))
      end
      
      def extract_info(packet)
        _, # opus magic signature
        _, # opus_version,
        channels,
        _, # pre skip
        _, # samplerate,
        _, # output gain
        _ = packet.unpack("a8CCvVvC") # channel map
        
        return { :channels => channels, :samplerate => 48000 }
      end
    end
  end
end