Class: WaveFile::Format

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

Constant Summary collapse

MIN_CHANNELS =

Not using ranges because of 1.8.7 performance problems with Range.max

1
MAX_CHANNELS =
65535
MIN_SAMPLE_RATE =
1
MAX_SAMPLE_RATE =
4_294_967_296
SUPPORTED_SAMPLE_FORMATS =
[:pcm, :float]
SUPPORTED_BITS_PER_SAMPLE =
{
  :pcm => [8, 16, 32],
  :float => [32, 64],
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channels, format_code, sample_rate) ⇒ Format

Returns a new instance of Format.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wavefile/format.rb', line 18

def initialize(channels, format_code, sample_rate)
  channels = normalize_channels(channels)
  sample_format, bits_per_sample = normalize_format_code(format_code)
  validate_channels(channels)
  validate_sample_format(sample_format)
  validate_bits_per_sample(sample_format, bits_per_sample)
  validate_sample_rate(sample_rate)

  @channels = channels
  @sample_format = sample_format
  @bits_per_sample = bits_per_sample
  @sample_rate = sample_rate
  @block_align = (@bits_per_sample / 8) * @channels
  @byte_rate = @block_align * @sample_rate
end

Instance Attribute Details

#bits_per_sampleObject (readonly)

Returns the value of attribute bits_per_sample.



42
43
44
# File 'lib/wavefile/format.rb', line 42

def bits_per_sample
  @bits_per_sample
end

#block_alignObject (readonly)

Returns the value of attribute block_align.



42
43
44
# File 'lib/wavefile/format.rb', line 42

def block_align
  @block_align
end

#byte_rateObject (readonly)

Returns the value of attribute byte_rate.



42
43
44
# File 'lib/wavefile/format.rb', line 42

def byte_rate
  @byte_rate
end

#channelsObject (readonly)

Returns the value of attribute channels.



42
43
44
# File 'lib/wavefile/format.rb', line 42

def channels
  @channels
end

#sample_formatObject (readonly)

Returns the value of attribute sample_format.



42
43
44
# File 'lib/wavefile/format.rb', line 42

def sample_format
  @sample_format
end

#sample_rateObject (readonly)

Returns the value of attribute sample_rate.



42
43
44
# File 'lib/wavefile/format.rb', line 42

def sample_rate
  @sample_rate
end

Instance Method Details

#mono?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/wavefile/format.rb', line 34

def mono?
  @channels == 1
end

#stereo?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/wavefile/format.rb', line 38

def stereo?
  @channels == 2
end