class Zappa::Wave

Attributes

format[RW]
header[RW]
wave_data[RW]

Public Class Methods

new() click to toggle source
# File lib/zappa/wave.rb, line 12
def initialize
  @header = RiffHeader.new
  @format = Format.new
  @wave_data = WaveData.new
end

Public Instance Methods

==(other) click to toggle source
# File lib/zappa/wave.rb, line 34
def ==(other)
  other.wave_data == wave_data
end
data_size() click to toggle source
# File lib/zappa/wave.rb, line 22
def data_size
  @wave_data.chunk_size
end
frame_size() click to toggle source
# File lib/zappa/wave.rb, line 26
def frame_size
  @format.bits_per_sample * @format.channels / 8
end
pack() click to toggle source
# 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) click to toggle source
# 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_count() click to toggle source
# File lib/zappa/wave.rb, line 30
def sample_count
  data_size / frame_size
end
samples() click to toggle source
# File lib/zappa/wave.rb, line 18
def samples
  @wave_data.samples
end
set_samples(samples) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

pack_samples(samples) click to toggle source
# File lib/zappa/wave.rb, line 78
def pack_samples(samples)
  pack_str = 's' * @format.channels
  samples.map { |f| f.pack(pack_str) }.join
end
unpack_samples(file) click to toggle source
# File lib/zappa/wave.rb, line 83
def unpack_samples(file)
  samples = []
  size = @format.bits_per_sample / 8
  ch = @format.channels
  while (frame_data = file.read(size * ch))
    samples << frame_data.unpack('s' * ch)
  end
  @wave_data.set_samples(samples)
end