Class: Audio::WAV::File

Inherits:
Object
  • Object
show all
Defined in:
lib/icanhasaudio/wav/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_or_path, mode = 'wb') ⇒ File

Returns a new instance of File.



5
6
7
8
9
10
11
12
# File 'lib/icanhasaudio/wav/file.rb', line 5

def initialize io_or_path, mode = 'wb'
  @io = io_or_path.is_a?(IO) ? io_or_path : File.open(io_or_path, mode)
  @bits = 16
  if block_given?
    yield self
    close
  end
end

Instance Attribute Details

#bitsObject

Returns the value of attribute bits.



4
5
6
# File 'lib/icanhasaudio/wav/file.rb', line 4

def bits
  @bits
end

Instance Method Details

#close(*args) ⇒ Object



44
45
46
# File 'lib/icanhasaudio/wav/file.rb', line 44

def close *args
  @io.close(*args)
end

#seek(*args) ⇒ Object



48
49
50
# File 'lib/icanhasaudio/wav/file.rb', line 48

def seek *args
  @io.seek(*args)
end

#write(*args) ⇒ Object



40
41
42
# File 'lib/icanhasaudio/wav/file.rb', line 40

def write *args
  @io.write(*args)
end

#write_header(size, known_length, channels, samplerate) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/icanhasaudio/wav/file.rb', line 14

def write_header size, known_length, channels, samplerate
  if known_length != 0 && known_length * bits / 8 * channels < size
    size = known_length * bits / 8 * channels + 44
  end

  bytespersec = channels * samplerate * bits / 8
  align = channels * bits / 8

  header = [
    'RIFF',
    u32(size - 8),
    'WAVE',
    'fmt ',
    u32(16),
    u16(1),
    u16(channels),
    u32(samplerate),
    u32(bytespersec),
    u16(align),
    u16(bits),
    'data',
    u32(size - 44),
  ].join
  @io.write header
end