Class: FramyMP3::File

Inherits:
Object
  • Object
show all
Defined in:
lib/framy_mp3/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ File

Returns a new instance of File.



8
9
10
11
12
13
14
15
16
17
# File 'lib/framy_mp3/file.rb', line 8

def initialize(stream)
  @stream = stream
  @frames = []
  @tags = []

  while (object = next_object)
    frames << object if object.frame?
    tags << object if object.tag?
  end
end

Instance Attribute Details

#framesObject (readonly)

Returns the value of attribute frames.



6
7
8
# File 'lib/framy_mp3/file.rb', line 6

def frames
  @frames
end

#tagsObject (readonly)

Returns the value of attribute tags.



6
7
8
# File 'lib/framy_mp3/file.rb', line 6

def tags
  @tags
end

Instance Method Details

#id3v1_tagObject



51
52
53
# File 'lib/framy_mp3/file.rb', line 51

def id3v1_tag
  tags.find(&:v1?)
end

#id3v2_tagObject



47
48
49
# File 'lib/framy_mp3/file.rb', line 47

def id3v2_tag
  tags.find(&:v2?)
end

#to_blob(options = {}) ⇒ Object Also known as: to_s



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/framy_mp3/file.rb', line 19

def to_blob(options={})
  blob = ""

  keep_xing_headers = vbr? ? false : options.fetch(:keep_xing_headers, false)

  StringIO.open(blob, "wb") do |outfile|
    initial_tag = id3v2_tag
    outfile << initial_tag.data unless initial_tag.nil?
    outfile << xing_header.data unless keep_xing_headers
    (keep_xing_headers ? frames : frames.reject(&:xing_header?)).each do |frame|
      outfile << frame.data
    end
    closing_tag = id3v1_tag
    outfile << closing_tag.data unless closing_tag.nil?
  end

  blob
end

#total_frame_bytesObject



59
60
61
# File 'lib/framy_mp3/file.rb', line 59

def total_frame_bytes
  frames.map { |frame| frame.data.bytesize }.sum
end

#total_framesObject



55
56
57
# File 'lib/framy_mp3/file.rb', line 55

def total_frames
  frames.count
end

#variable_bitrate?Boolean Also known as: vbr?

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/framy_mp3/file.rb', line 39

def variable_bitrate?
  initial_bitrate = frames.first.bitrate
  frames[1..-1].any? do |frame|
    frame.bitrate != initial_bitrate
  end
end