Class: CafInfo
- Inherits:
-
Object
- Object
- CafInfo
- 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
-
#bits_per_channel ⇒ Object
readonly
the number of bits of sample data for each channel in a frame of data.
-
#bytes_per_packet ⇒ Object
readonly
the number of bytes in a packet of data.
-
#channels_per_frame ⇒ Object
readonly
the number of channels in each frame of data.
-
#filename ⇒ Object
readonly
the original filename.
-
#format ⇒ Object
readonly
a four-character code indicating the general kind of data in the stream.
-
#format_flags ⇒ Object
readonly
flags specific to each format.
-
#frames_per_packet ⇒ Object
readonly
the number of sample frames in each packet of data.
-
#length ⇒ Object
readonly
duration in seconds of audio.
-
#samplerate ⇒ Object
readonly
the number of sample frames per second of the data.
-
#vbr ⇒ Object
readonly
variable bitrate => true or false.
-
#vfr ⇒ Object
readonly
variable frame rate => true or false.
Class Method Summary collapse
-
.open(*params) ⇒ Object
“block version” of CafInfo::new().
Instance Method Summary collapse
-
#audio_content ⇒ Object
this method returns the “audio-only” data boundaries of the file, i.e.
-
#close ⇒ Object
Flush pending modifications and close the file.
-
#flush ⇒ Object
close and reopen the file, i.e.
-
#initialize(filename) ⇒ CafInfo
constructor
Instantiate CafInfo object with name
filename
. -
#reload ⇒ Object
reload (or load for the first time) the file from disk.
-
#rename(new_filename) ⇒ Object
write to another filename at close().
-
#to_s ⇒ Object
inspect inside CafInfo.
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_channel ⇒ Object (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_packet ⇒ Object (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_frame ⇒ Object (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 |
#filename ⇒ Object (readonly)
the original filename
21 22 23 |
# File 'lib/cafinfo.rb', line 21 def filename @filename end |
#format ⇒ Object (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_flags ⇒ Object (readonly)
flags specific to each format
33 34 35 |
# File 'lib/cafinfo.rb', line 33 def format_flags @format_flags end |
#frames_per_packet ⇒ Object (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 |
#length ⇒ Object (readonly)
duration in seconds of audio
24 25 26 |
# File 'lib/cafinfo.rb', line 24 def length @length end |
#samplerate ⇒ Object (readonly)
the number of sample frames per second of the data
27 28 29 |
# File 'lib/cafinfo.rb', line 27 def samplerate @samplerate end |
#vbr ⇒ Object (readonly)
variable bitrate => true or false
49 50 51 |
# File 'lib/cafinfo.rb', line 49 def vbr @vbr end |
#vfr ⇒ Object (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_content ⇒ Object
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 |
#close ⇒ Object
Flush pending modifications and close the file
121 122 123 |
# File 'lib/cafinfo.rb', line 121 def close puts "close" if $DEBUG end |
#flush ⇒ Object
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 |
#reload ⇒ Object
reload (or load for the first time) the file from disk
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_s ⇒ Object
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 |