Class: CafInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/cafinfo.rb,
lib/caf/extension_modules.rb

Overview

ruby -d to display debugging infos

Defined Under Namespace

Modules: CafFileMethods, HashKeys

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ CafInfo

Instantiate CafInfo object with name filename.



55
56
57
58
59
60
# File 'lib/cafinfo.rb', line 55

def initialize(filename)
  warn("#{self.class}::new() does not take block; use #{self.class}::open() instead") if block_given?
  @filename = filename
  @header = {}
  reload
end

Instance Attribute Details

#bits_per_channelObject (readonly)

the number of bits of sample data for each channel in a frame of data. 0 if the data format does not contain separate samples for each channel (for instance any compressed format)



46
47
48
# File 'lib/cafinfo.rb', line 46

def bits_per_channel
  @bits_per_channel
end

#bytes_per_packetObject (readonly)

the number of bytes in a packet of data.



36
37
38
# File 'lib/cafinfo.rb', line 36

def bytes_per_packet
  @bytes_per_packet
end

#channels_per_frameObject (readonly)

the number of channels in each frame of data



42
43
44
# File 'lib/cafinfo.rb', line 42

def channels_per_frame
  @channels_per_frame
end

#filenameObject (readonly)

the original filename



21
22
23
# File 'lib/cafinfo.rb', line 21

def filename
  @filename
end

#formatObject (readonly)

a four-character code indicating the general kind of data in the stream



30
31
32
# File 'lib/cafinfo.rb', line 30

def format
  @format
end

#format_flagsObject (readonly)

flags specific to each format



33
34
35
# File 'lib/cafinfo.rb', line 33

def format_flags
  @format_flags
end

#frames_per_packetObject (readonly)

the number of sample frames in each packet of data



39
40
41
# File 'lib/cafinfo.rb', line 39

def frames_per_packet
  @frames_per_packet
end

#lengthObject (readonly)

duration in seconds of audio



24
25
26
# File 'lib/cafinfo.rb', line 24

def length
  @length
end

#samplerateObject (readonly)

the number of sample frames per second of the data



27
28
29
# File 'lib/cafinfo.rb', line 27

def samplerate
  @samplerate
end

#vbrObject (readonly)

variable bitrate => true or false



49
50
51
# File 'lib/cafinfo.rb', line 49

def vbr
  @vbr
end

#vfrObject (readonly)

variable frame rate => true or false



52
53
54
# File 'lib/cafinfo.rb', line 52

def vfr
  @vfr
end

Class Method Details

.open(*params) ⇒ Object

“block version” of CafInfo::new()



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cafinfo.rb', line 93

def self.open(*params)
  m = self.new(*params)
  ret = nil
  if block_given?
    begin
      ret = yield(m)
    ensure
      m.close
    end
  else
    ret = m
  end
  ret
end

Instance Method Details

#audio_contentObject

this method returns the “audio-only” data boundaries of the file, i.e. data chunk content. Returned value is an array

position_in_the_file, length_of_the_data


116
117
118
# File 'lib/cafinfo.rb', line 116

def audio_content
  [@chunks[:data].position, @chunks[:data].size]
end

#closeObject

Flush pending modifications and close the file



121
122
123
# File 'lib/cafinfo.rb', line 121

def close
  puts "close" if $DEBUG
end

#flushObject

close and reopen the file, i.e. commit changes to disk and reload it



127
128
129
130
# File 'lib/cafinfo.rb', line 127

def flush
  close
  reload
end

#reloadObject

reload (or load for the first time) the file from disk

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cafinfo.rb', line 63

def reload
  raise(Caf::Error, "empty file") unless File.size?(@filename)

  @file = File.new(filename, "rb")
  @file.extend(CafFileMethods)

  @chunks = {}

  begin
    @header = read_file_header
    chunk = read_audio_description_chunk
    @chunks[chunk.chunk_type.to_sym] = chunk
    puts "Found chunk: #{@chunks[chunk.chunk_type.to_sym]}" if $DEBUG

    until @file.eof? do
      chunk = read_chunk_header
      chunk.read_data(@file)
      @chunks[chunk.chunk_type.to_sym] = chunk

      puts "Found chunk: #{chunk}" if $DEBUG
    end
  ensure
    @file.close
  end

  raise(Caf::Error, "data chunk not found") if @chunks[:data].nil?
  @length = (@frames_per_packet * @chunks[:data].size) / (@samplerate * @bytes_per_packet)
end

#rename(new_filename) ⇒ Object

write to another filename at close()



109
110
111
# File 'lib/cafinfo.rb', line 109

def rename(new_filename)
  @filename = new_filename
end

#to_sObject

inspect inside CafInfo



133
134
135
136
137
# File 'lib/cafinfo.rb', line 133

def to_s
  s = "CAF (version #{@header[:file_version]}) #{@format} #{"VFR " if @vfr}#{@vbr ? "VBR" : "CBR"} #{@samplerate} Hz length #{@length} sec."
  s << ""
  s
end