Module: WavFile

Defined in:
lib/wav-file.rb,
lib/wav-file/wav-file.rb

Defined Under Namespace

Classes: Chunk, Format, WavFormatError

Constant Summary collapse

VERSION =
'0.0.2'

Class Method Summary collapse

Class Method Details

.read(f) ⇒ Object



97
98
99
# File 'lib/wav-file/wav-file.rb', line 97

def WavFile.read(f)
  return readFormat(f), readDataChunk(f)
end

.readAll(f) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/wav-file/wav-file.rb', line 79

def WavFile.readAll(f)
  format = readFormat(f)
  chunks = Array.new
  while !f.eof?
    chunk = Chunk.new(f)
    chunks << chunk
  end
  return format, chunks
end

.readDataChunk(f) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/wav-file/wav-file.rb', line 89

def WavFile.readDataChunk(f)
  format, chunks = readAll(f)
  chunks.each{|c|
    return c if c.name == 'data'
  }
  return nil
end

.readFormat(f) ⇒ Object

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wav-file/wav-file.rb', line 66

def WavFile.readFormat(f)
  f.binmode
  f.seek(0)
  header = f.read(12)
  riff = header.slice(0,4)
  data_size = header.slice(4,4).unpack('V')[0].to_i
  wave = header.slice(8,4)
  raise(WavFormatError) if riff != 'RIFF' or wave != 'WAVE'
  
  formatChunk = Chunk.new(f)
  Format.new(formatChunk)
end

.write(f, format, dataChunks) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/wav-file/wav-file.rb', line 101

def WavFile.write(f, format, dataChunks)
  header_file_size = 4
  dataChunks.each{|c|
    header_file_size += c.data.size + 8
  }
  f.write('RIFF' + [header_file_size].pack('V') + 'WAVE')
  f.write("fmt ")
  f.write([format.to_bin.size].pack('V'))
  f.write(format.to_bin)
  dataChunks.each{|c|
    f.write(c.to_bin)
  }
end