Class: Awaaz::Utils::SoundConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/awaaz/utils/sound_config.rb

Overview

SoundConfig holds and validates configuration options used for audio processing.

It ensures that only valid options are passed in and provides convenience accessors for common audio processing settings such as sample rate, channel count, amplification factor, and decoder preferences.

Examples:

Creating a SoundConfig with valid options

valid_keys = [:sample_rate, :mono, :amplification_factor, :decoder]
config = Awaaz::Utils::SoundConfig.new(valid_keys, sample_rate: 44100, mono: true)

Instance Method Summary collapse

Constructor Details

#initialize(valid_options, **options) ⇒ SoundConfig

Initializes a new SoundConfig instance.

Parameters:

  • valid_options (Array<Symbol,String>)

    The list of allowed option keys.

  • options (Hash)

    The configuration options to store.

Options Hash (**options):

  • :sample_rate (Integer)

    The audio sample rate in Hz.

  • :mono (Boolean)

    Whether to process audio in mono (true) or stereo (false).

  • :soundread (Boolean)

    Whether to use soundread for processing.

  • :amplification_factor (Integer)

    The amplification factor (default: 32768).

  • :decoder (Symbol, String)

    The preferred audio decoder.

Raises:

  • (ArgumentError)

    If any provided option key is not in valid_options.



30
31
32
33
34
35
# File 'lib/awaaz/utils/sound_config.rb', line 30

def initialize(valid_options, **options)
  @options = options
  @valid_options = valid_options

  prepare
end

Instance Method Details

#amplification_factorInteger

The amplification factor for audio processing.

Returns:

  • (Integer)

    Defaults to 32768.



105
106
107
# File 'lib/awaaz/utils/sound_config.rb', line 105

def amplification_factor
  (from_options(:amplification_factor) || 32_768).to_i
end

#decoder_optionSymbol?

The preferred audio decoder.

Returns:

  • (Symbol, nil)

    The decoder symbol if set, otherwise nil.



114
115
116
# File 'lib/awaaz/utils/sound_config.rb', line 114

def decoder_option
  from_options(:decoder)&.to_sym
end

#monoBoolean

Whether to process audio in mono.

Returns:

  • (Boolean)

    true if mono, otherwise false.



51
52
53
# File 'lib/awaaz/utils/sound_config.rb', line 51

def mono
  from_options(:mono) || true
end

#mono?Boolean

Convenience method to check if audio is mono.

Returns:

  • (Boolean)


69
70
71
# File 'lib/awaaz/utils/sound_config.rb', line 69

def mono?
  mono
end

#num_channelsInteger

The number of audio channels.

Returns:

  • (Integer)

    1 for mono, 2 for stereo.



96
97
98
# File 'lib/awaaz/utils/sound_config.rb', line 96

def num_channels
  mono? ? 1 : 2
end

#resampling_optionSymbol

Resampling option

Returns:

  • (Symbol)

    default :linear



60
61
62
# File 'lib/awaaz/utils/sound_config.rb', line 60

def resampling_option
  from_options(:resampling_option) || :linear
end

#sample_rateInteger

The sample rate for audio processing.

Returns:

  • (Integer)

    The sample rate in Hz (default: 22050).



42
43
44
# File 'lib/awaaz/utils/sound_config.rb', line 42

def sample_rate
  from_options(:sample_rate) || 22_050
end

#soundread?Boolean

Whether to use soundread for audio processing.

Returns:

  • (Boolean)


87
88
89
# File 'lib/awaaz/utils/sound_config.rb', line 87

def soundread?
  from_options(:soundread) == true
end

#stereo?Boolean

Convenience method to check if audio is stereo.

Returns:

  • (Boolean)


78
79
80
# File 'lib/awaaz/utils/sound_config.rb', line 78

def stereo?
  !mono?
end