Class: Wavify::Core::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/wavify/core/format.rb,
sig/core.rbs

Overview

Describes the audio sample layout (channels, sample rate, and sample type).

Constant Summary collapse

SUPPORTED_SAMPLE_FORMATS =

Supported symbolic sample format kinds.

%i[pcm float].freeze
PCM_BIT_DEPTHS =

Allowed bit depths for integer PCM.

[8, 16, 24, 32].freeze
FLOAT_BIT_DEPTHS =

Allowed bit depths for floating point samples.

[32, 64].freeze
CHANNEL_POSITIONS =

Speaker positions understood by channel-layout-aware conversions.

%i[
  front_left front_right front_center low_frequency back_left back_right
  front_left_of_center front_right_of_center back_center side_left side_right
  top_center top_front_left top_front_center top_front_right top_back_left
  top_back_center top_back_right
].freeze
DEFAULT_CHANNEL_LAYOUTS =

Conventional speaker order used when no explicit channel layout exists.

{
  1 => %i[front_center],
  2 => %i[front_left front_right],
  3 => %i[front_left front_right front_center],
  4 => %i[front_left front_right back_left back_right],
  5 => %i[front_left front_right front_center back_left back_right],
  6 => %i[front_left front_right front_center low_frequency side_left side_right],
  7 => %i[front_left front_right front_center low_frequency back_center side_left side_right],
  8 => %i[front_left front_right front_center low_frequency back_left back_right side_left side_right]
}.transform_values(&:freeze).freeze
UNSPECIFIED_LAYOUT =

:nodoc:

Object.new.freeze
UNKNOWN_LAYOUT =

:nodoc:

:unknown

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channels:, sample_rate:, bit_depth:, sample_format: :pcm, valid_bits: nil, channel_layout: UNSPECIFIED_LAYOUT) ⇒ Format

Returns a new instance of Format.

Parameters:

  • channels (Integer)

    number of interleaved channels (1..32)

  • sample_rate (Integer)

    sampling rate in Hz

  • bit_depth (Integer)

    bits per sample

  • valid_bits (Integer, nil) (defaults to: nil)

    significant bits within the sample container

  • sample_format (Symbol, String) (defaults to: :pcm)

    :pcm or :float

  • channel_layout (Array<Symbol>, :unknown, nil) (defaults to: UNSPECIFIED_LAYOUT)

    ordered speaker positions

  • channels: (Integer)
  • sample_rate: (Integer)
  • bit_depth: (Integer)
  • sample_format: (Symbol) (defaults to: :pcm)
  • valid_bits: (Integer, nil) (defaults to: nil)
  • channel_layout: (Array[Symbol], :unknown, nil) (defaults to: UNSPECIFIED_LAYOUT)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wavify/core/format.rb', line 43

def initialize(channels:, sample_rate:, bit_depth:, sample_format: :pcm, valid_bits: nil,
               channel_layout: UNSPECIFIED_LAYOUT)
  @channels = validate_channels(channels)
  @sample_rate = validate_sample_rate(sample_rate)
  @sample_format = validate_sample_format(sample_format)
  @bit_depth = validate_bit_depth(bit_depth, @sample_format)
  @valid_bits = validate_valid_bits(valid_bits || @bit_depth)
  requested_layout = if channel_layout.equal?(UNSPECIFIED_LAYOUT) || channel_layout.nil?
                       DEFAULT_CHANNEL_LAYOUTS[@channels]
                     elsif channel_layout == UNKNOWN_LAYOUT
                       nil
                     else
                       channel_layout
                     end
  @channel_layout = validate_channel_layout(requested_layout)
  freeze
end

Instance Attribute Details

#bit_depthInteger (readonly)

Returns the value of attribute bit_depth.

Returns:

  • (Integer)


35
36
37
# File 'lib/wavify/core/format.rb', line 35

def bit_depth
  @bit_depth
end

#channel_layoutArray[Symbol]? (readonly)

Returns the value of attribute channel_layout.

Returns:

  • (Array[Symbol], nil)


35
36
37
# File 'lib/wavify/core/format.rb', line 35

def channel_layout
  @channel_layout
end

#channelsInteger (readonly)

Returns the value of attribute channels.

Returns:

  • (Integer)


35
36
37
# File 'lib/wavify/core/format.rb', line 35

def channels
  @channels
end

#sample_formatSymbol (readonly)

Returns the value of attribute sample_format.

Returns:

  • (Symbol)


35
36
37
# File 'lib/wavify/core/format.rb', line 35

def sample_format
  @sample_format
end

#sample_rateInteger (readonly)

Returns the value of attribute sample_rate.

Returns:

  • (Integer)


35
36
37
# File 'lib/wavify/core/format.rb', line 35

def sample_rate
  @sample_rate
end

#valid_bitsInteger (readonly)

Returns the value of attribute valid_bits.

Returns:

  • (Integer)


35
36
37
# File 'lib/wavify/core/format.rb', line 35

def valid_bits
  @valid_bits
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Value equality for audio format parameters.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
# File 'lib/wavify/core/format.rb', line 106

def ==(other)
  return false unless other.is_a?(Format)

    @channels == other.channels &&
    @sample_rate == other.sample_rate &&
    @bit_depth == other.bit_depth &&
    @valid_bits == other.valid_bits &&
    @sample_format == other.sample_format &&
    @channel_layout == other.channel_layout
end

#block_alignInteger

Returns bytes per audio frame (all channels).

Returns:

  • (Integer)

    bytes per audio frame (all channels)



88
89
90
# File 'lib/wavify/core/format.rb', line 88

def block_align
  @channels * bytes_per_sample
end

#byte_rateInteger

Returns bytes per second for this format.

Returns:

  • (Integer)

    bytes per second for this format



93
94
95
# File 'lib/wavify/core/format.rb', line 93

def byte_rate
  @sample_rate * block_align
end

#bytes_per_sampleInteger

Returns bytes used by one sample value.

Returns:

  • (Integer)

    bytes used by one sample value



98
99
100
# File 'lib/wavify/core/format.rb', line 98

def bytes_per_sample
  @bit_depth / 8
end

#hashInteger

Returns hash value compatible with #eql?.

Returns:

  • (Integer)

    hash value compatible with #eql?



120
121
122
# File 'lib/wavify/core/format.rb', line 120

def hash
  [@channels, @sample_rate, @bit_depth, @valid_bits, @sample_format, @channel_layout].hash
end

#mono?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/wavify/core/format.rb', line 79

def mono?
  @channels == 1
end

#stereo?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/wavify/core/format.rb', line 83

def stereo?
  @channels == 2
end

#with(channels: nil, sample_rate: nil, bit_depth: nil, sample_format: nil, valid_bits: nil, channel_layout: UNSPECIFIED_LAYOUT) ⇒ Format

Returns a new format with one or more fields replaced.

Parameters:

  • channels: (Integer, nil) (defaults to: nil)
  • sample_rate: (Integer, nil) (defaults to: nil)
  • bit_depth: (Integer, nil) (defaults to: nil)
  • sample_format: (Symbol, nil) (defaults to: nil)
  • valid_bits: (Integer, nil) (defaults to: nil)
  • channel_layout: (Array[Symbol], :unknown, nil) (defaults to: UNSPECIFIED_LAYOUT)

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wavify/core/format.rb', line 64

def with(channels: nil, sample_rate: nil, bit_depth: nil, sample_format: nil, valid_bits: nil,
         channel_layout: UNSPECIFIED_LAYOUT)
  target_channels = channels || @channels
  target_bit_depth = bit_depth || @bit_depth
  target_sample_format = sample_format || @sample_format
  self.class.new(
    channels: target_channels,
    sample_rate: sample_rate || @sample_rate,
    bit_depth: target_bit_depth,
    sample_format: target_sample_format,
    valid_bits: valid_bits || preserved_valid_bits(target_bit_depth, target_sample_format),
    channel_layout: resolved_channel_layout(channel_layout, target_channels)
  )
end