class PulseAnalysis::AudioData

Public Class Methods

new(sound) click to toggle source

@param [PulseAnalysis::Sound] sound

# File lib/pulse-analysis/audio_data.rb, line 10
def initialize(sound)
  @sound = sound
  @data = @sound.data
end

Public Instance Methods

prepare() click to toggle source

Prepare the audio data for analysis @return [Boolean]

# File lib/pulse-analysis/audio_data.rb, line 17
def prepare
  convert_to_mono if convert_to_mono?
  normalize if normalize?
  true
end

Private Instance Methods

convert_to_mono() click to toggle source

Logic for converting a stereo sound to mono @return [Array<Float>]

# File lib/pulse-analysis/audio_data.rb, line 47
def convert_to_mono
  # Use left channel
  @data = @data.map(&:first)
end
convert_to_mono?() click to toggle source

Should the audio data be converted to a single channel? @return [Boolean]

# File lib/pulse-analysis/audio_data.rb, line 41
def convert_to_mono?
  @sound.num_channels > 1
end
normalize() click to toggle source

Normalize the audio data @return [Array<Float>]

# File lib/pulse-analysis/audio_data.rb, line 34
def normalize
  factor = 1.0 / @data.max
  @data.map! { |frame| frame * factor }
end
normalize?() click to toggle source

Should the audio data be normalized? @return [Boolean]

# File lib/pulse-analysis/audio_data.rb, line 27
def normalize?
  headroom = 1.0 - @data.max
  headroom > 0.0
end