Class: Zappa::Wave

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWave

Returns a new instance of Wave.



12
13
14
15
16
# File 'lib/zappa/wave.rb', line 12

def initialize
  @header = RiffHeader.new
  @format = Format.new
  @wave_data = WaveData.new
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



10
11
12
# File 'lib/zappa/wave.rb', line 10

def format
  @format
end

#headerObject

Returns the value of attribute header.



10
11
12
# File 'lib/zappa/wave.rb', line 10

def header
  @header
end

#wave_dataObject

Returns the value of attribute wave_data.



10
11
12
# File 'lib/zappa/wave.rb', line 10

def wave_data
  @wave_data
end

Instance Method Details

#==(other) ⇒ Object



34
35
36
# File 'lib/zappa/wave.rb', line 34

def ==(other)
  other.wave_data == wave_data
end

#data_sizeObject



22
23
24
# File 'lib/zappa/wave.rb', line 22

def data_size
  @wave_data.chunk_size
end

#frame_sizeObject



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

def frame_size
  @format.bits_per_sample * @format.channels / 8
end

#packObject



38
39
40
41
42
43
44
# File 'lib/zappa/wave.rb', line 38

def pack
  pack = @header.pack + @format.pack
  pack += @wave_data.chunk_id
  pack += [@wave_data.chunk_size].pack('V')
  pack += pack_samples(@wave_data.samples)
  pack
end

#path_to(source) ⇒ Object

Private method?



70
71
72
73
74
# File 'lib/zappa/wave.rb', line 70

def path_to(source) # Private method?
  return source if source.class == String
  return source.path if source.class == File
  fail 'cannot unpack type: ' + source.class.to_s
end

#sample_countObject



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

def sample_count
  data_size / frame_size
end

#samplesObject



18
19
20
# File 'lib/zappa/wave.rb', line 18

def samples
  @wave_data.samples
end

#set_samples(samples) ⇒ Object



63
64
65
66
67
68
# File 'lib/zappa/wave.rb', line 63

def set_samples(samples)
  samples_change = (samples.size - @wave_data.samples.size)
  size_change = samples_change * @format.channels * 2
  @header.chunk_size += size_change
  @wave_data.set_samples(samples)
end

#unpack(source) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/zappa/wave.rb', line 46

def unpack(source)
  file = File.open(path_to(source), 'rb')
rescue
  raise 'Unable to open WAV file'
else
  @header = RiffHeader.new(file)
  @format = Format.new(file)
  while sc_header = file.read(8)
    s = SubChunkHeader.new(sc_header)
    if s.chunk_id == 'data'
      unpack_samples(file)
    else
      file.read(s.chunk_size)
    end
  end
end