Class: WaveFile::UnvalidatedFormat

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

Overview

Represents information about the data format for a Wave file, such as number of channels, bits per sample, sample rate, and so forth. A Format instance is used by Reader to indicate what format to read samples out as, and by Writer to indicate what format to write samples as.

This class is immutable - once a new Format is constructed, it can’t be modified.

Instance Attribute Summary collapse

Attributes inherited from Format

#bits_per_sample, #block_align, #byte_rate, #channels, #sample_format, #sample_rate

Instance Method Summary collapse

Methods inherited from Format

#mono?, #stereo?

Constructor Details

#initialize(fields) ⇒ UnvalidatedFormat

Constructs a new immutable UnvalidatedFormat.



10
11
12
13
14
15
16
17
18
19
# File 'lib/wavefile/unvalidated_format.rb', line 10

def initialize(fields)
  @audio_format = fields[:audio_format]
  @sub_audio_format_guid = fields[:sub_audio_format_guid]
  @channels = fields[:channels]
  @sample_rate = fields[:sample_rate]
  @byte_rate = fields[:byte_rate]
  @block_align = fields[:block_align]
  @bits_per_sample = fields[:bits_per_sample]
  @valid_bits_per_sample = fields[:valid_bits_per_sample]
end

Instance Attribute Details

#audio_formatObject (readonly)

Returns the value of attribute audio_format.



21
22
23
# File 'lib/wavefile/unvalidated_format.rb', line 21

def audio_format
  @audio_format
end

#sub_audio_format_guidObject (readonly)

Returns the value of attribute sub_audio_format_guid.



21
22
23
# File 'lib/wavefile/unvalidated_format.rb', line 21

def sub_audio_format_guid
  @sub_audio_format_guid
end

#valid_bits_per_sampleObject (readonly)

Returns the value of attribute valid_bits_per_sample.



21
22
23
# File 'lib/wavefile/unvalidated_format.rb', line 21

def valid_bits_per_sample
  @valid_bits_per_sample
end

Instance Method Details

#to_validated_formatObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wavefile/unvalidated_format.rb', line 23

def to_validated_format
  if @sub_audio_format_guid.nil?
    audio_format_code = @audio_format
  else
    if @sub_audio_format_guid == SUB_FORMAT_GUID_PCM
      audio_format_code = 1
    elsif @sub_audio_format_guid == SUB_FORMAT_GUID_FLOAT
      audio_format_code = 3
    else
      audio_format_code = nil
    end
  end

  if @valid_bits_per_sample
    if @valid_bits_per_sample != @bits_per_sample
      raise UnsupportedFormatError,
            "Sample container size (#{@bits_per_sample}) and valid bits per sample (#{@valid_bits_per_sample}) " +
            "differ."
    end

    bits_per_sample = @valid_bits_per_sample
  else
    bits_per_sample = @bits_per_sample
  end

  sample_format = "#{FORMAT_CODES.invert[audio_format_code]}_#{bits_per_sample}".to_sym

  Format.new(@channels, sample_format, @sample_rate)
end