Class: Wavify::Core::Format
- Inherits:
-
Object
- Object
- Wavify::Core::Format
- 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
-
#bit_depth ⇒ Integer
readonly
Returns the value of attribute bit_depth.
-
#channel_layout ⇒ Array[Symbol]?
readonly
Returns the value of attribute channel_layout.
-
#channels ⇒ Integer
readonly
Returns the value of attribute channels.
-
#sample_format ⇒ Symbol
readonly
Returns the value of attribute sample_format.
-
#sample_rate ⇒ Integer
readonly
Returns the value of attribute sample_rate.
-
#valid_bits ⇒ Integer
readonly
Returns the value of attribute valid_bits.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Value equality for audio format parameters.
-
#block_align ⇒ Integer
Bytes per audio frame (all channels).
-
#byte_rate ⇒ Integer
Bytes per second for this format.
-
#bytes_per_sample ⇒ Integer
Bytes used by one sample value.
-
#hash ⇒ Integer
Hash value compatible with #eql?.
-
#initialize(channels:, sample_rate:, bit_depth:, sample_format: :pcm, valid_bits: nil, channel_layout: UNSPECIFIED_LAYOUT) ⇒ Format
constructor
A new instance of Format.
- #mono? ⇒ Boolean
- #stereo? ⇒ Boolean
-
#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.
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.
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_depth ⇒ Integer (readonly)
Returns the value of attribute bit_depth.
35 36 37 |
# File 'lib/wavify/core/format.rb', line 35 def bit_depth @bit_depth end |
#channel_layout ⇒ Array[Symbol]? (readonly)
Returns the value of attribute channel_layout.
35 36 37 |
# File 'lib/wavify/core/format.rb', line 35 def channel_layout @channel_layout end |
#channels ⇒ Integer (readonly)
Returns the value of attribute channels.
35 36 37 |
# File 'lib/wavify/core/format.rb', line 35 def channels @channels end |
#sample_format ⇒ Symbol (readonly)
Returns the value of attribute sample_format.
35 36 37 |
# File 'lib/wavify/core/format.rb', line 35 def sample_format @sample_format end |
#sample_rate ⇒ Integer (readonly)
Returns the value of attribute sample_rate.
35 36 37 |
# File 'lib/wavify/core/format.rb', line 35 def sample_rate @sample_rate end |
#valid_bits ⇒ Integer (readonly)
Returns the value of attribute valid_bits.
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.
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_align ⇒ Integer
Returns 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_rate ⇒ Integer
Returns 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_sample ⇒ Integer
Returns 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 |
#hash ⇒ Integer
Returns 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
79 80 81 |
# File 'lib/wavify/core/format.rb', line 79 def mono? @channels == 1 end |
#stereo? ⇒ 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.
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 |