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.

Instance Attribute Summary collapse

Attributes inherited from Format

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

Instance Method Summary collapse

Methods inherited from Format

#mono?, #stereo?

Constructor Details

#initialize(fields) ⇒ UnvalidatedFormat

Constructs a new immutable UnvalidatedFormat.



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

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]
  @speaker_mapping = parse_speaker_mapping(fields[:speaker_mapping])
  @valid_bits_per_sample = fields[:valid_bits_per_sample]
end

Instance Attribute Details

#audio_formatObject (readonly)

Returns the value of attribute audio_format.



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

def audio_format
  @audio_format
end

#sub_audio_format_guidObject (readonly)

Returns the value of attribute sub_audio_format_guid.



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

def sub_audio_format_guid
  @sub_audio_format_guid
end

#valid_bits_per_sampleObject (readonly)

Returns the value of attribute valid_bits_per_sample.



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

def valid_bits_per_sample
  @valid_bits_per_sample
end

Instance Method Details

#to_validated_formatObject



22
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
52
53
54
# File 'lib/wavefile/unvalidated_format.rb', line 22

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

  speaker_mapping = @speaker_mapping
  if !speaker_mapping.nil?
    speaker_mapping = speaker_mapping[0...channels]
  end

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