Class: Zappa::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/zappa/wave/format.rb

Constant Summary collapse

FMT_SIZE =
16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Format

Returns a new instance of Format.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/zappa/wave/format.rb', line 8

def initialize(file = nil)
  if file.nil?
    @chunk_id        = 'fmt '
    @chunk_size      = FMT_SIZE
    @audio_format    = 1
    @channels        = 2
    @sample_rate     = 44_100
    @byte_rate       = 176_400
    @block_align     = 4
    @bits_per_sample = 16
  else
    @chunk_id = file.read(4)
    @chunk_size = file.read(4).unpack('V').first
    unpack(file.read(@chunk_size))
  end
end

Instance Attribute Details

#audio_formatObject

Returns the value of attribute audio_format.



3
4
5
# File 'lib/zappa/wave/format.rb', line 3

def audio_format
  @audio_format
end

#bits_per_sampleObject

Returns the value of attribute bits_per_sample.



3
4
5
# File 'lib/zappa/wave/format.rb', line 3

def bits_per_sample
  @bits_per_sample
end

#block_alignObject

Returns the value of attribute block_align.



3
4
5
# File 'lib/zappa/wave/format.rb', line 3

def block_align
  @block_align
end

#byte_rateObject

Returns the value of attribute byte_rate.



3
4
5
# File 'lib/zappa/wave/format.rb', line 3

def byte_rate
  @byte_rate
end

#channelsObject

Returns the value of attribute channels.



3
4
5
# File 'lib/zappa/wave/format.rb', line 3

def channels
  @channels
end

#chunk_sizeObject

Returns the value of attribute chunk_size.



3
4
5
# File 'lib/zappa/wave/format.rb', line 3

def chunk_size
  @chunk_size
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/zappa/wave/format.rb', line 3

def name
  @name
end

#sample_rateObject

Returns the value of attribute sample_rate.



3
4
5
# File 'lib/zappa/wave/format.rb', line 3

def sample_rate
  @sample_rate
end

#unknownObject

Returns the value of attribute unknown.



3
4
5
# File 'lib/zappa/wave/format.rb', line 3

def unknown
  @unknown
end

Instance Method Details

#==(other) ⇒ Object



25
26
27
# File 'lib/zappa/wave/format.rb', line 25

def ==(other)
  pack == other.pack
end

#packObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/zappa/wave/format.rb', line 29

def pack
  fmt = @chunk_id
  fmt += [@chunk_size].pack('V')
  fmt += [@audio_format].pack('v')
  fmt += [@channels].pack('v')
  fmt += [@sample_rate].pack('V')
  fmt += [@byte_rate].pack('V')
  fmt += [@block_align].pack('v')
  fmt + [@bits_per_sample].pack('v')
end

#unpack(data) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/zappa/wave/format.rb', line 40

def unpack(data)
  @audio_format    = data.byteslice(0..1).unpack('v').first
  @channels        = data.byteslice(2..3).unpack('v').first
  @sample_rate     = data.byteslice(4..7).unpack('V').first
  @byte_rate       = data.byteslice(8..11).unpack('V').first
  @block_align     = data.byteslice(12..13).unpack('v').first
  @bits_per_sample = data.byteslice(14..15).unpack('v').first
end