Class: PulseAnalysis::Analysis

Inherits:
Object
  • Object
show all
Defined in:
lib/pulse-analysis/analysis.rb

Constant Summary collapse

MINIMUM_PULSES =
10
MAX_BPM =
280

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sound, options = {}) ⇒ Analysis

Returns a new instance of Analysis.

Parameters:

Options Hash (options):

  • :amplitude_threshold (Float)

    Pulses above this amplitude will be analyzed

  • :length_threshold (Integer)

    Pulse periods longer than this value will be analyzed



14
15
16
17
18
19
# File 'lib/pulse-analysis/analysis.rb', line 14

def initialize(sound, options = {})
  @amplitude_threshold = options[:amplitude_threshold]
  @length_threshold = options[:length_threshold]
  populate_sound(sound)
  @data = AudioData.new(@sound)
end

Instance Attribute Details

#abberationsArray<Integer> (readonly)

Non-zero pulse timing abberations derived from the periods

Returns:

  • (Array<Integer>)


76
77
78
# File 'lib/pulse-analysis/analysis.rb', line 76

def abberations
  @abberations
end

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/pulse-analysis/analysis.rb', line 8

def data
  @data
end

#periodsObject (readonly)

Returns the value of attribute periods.



8
9
10
# File 'lib/pulse-analysis/analysis.rb', line 8

def periods
  @periods
end

#soundObject (readonly)

Returns the value of attribute sound.



8
9
10
# File 'lib/pulse-analysis/analysis.rb', line 8

def sound
  @sound
end

Instance Method Details

#amplitude_thresholdFloat

The threshold (0..1) at which pulses will register as high

Returns:

  • (Float)


82
83
84
# File 'lib/pulse-analysis/analysis.rb', line 82

def amplitude_threshold
  @amplitude_threshold ||= calculate_amplitude_threshold
end

#average_abberationFloat

Average sequential abberation between pulses

Returns:

  • (Float)


70
71
72
# File 'lib/pulse-analysis/analysis.rb', line 70

def average_abberation
  @average_abberation ||= calculate_average_abberation
end

#average_periodFloat

Average number of samples between pulses

Returns:

  • (Float)


32
33
34
# File 'lib/pulse-analysis/analysis.rb', line 32

def average_period
  @average_period ||= @periods.inject(&:+).to_f / num_pulses
end

#largest_abberationInteger

Largest sequential abberation between pulses

Returns:

  • (Integer)


64
65
66
# File 'lib/pulse-analysis/analysis.rb', line 64

def largest_abberation
  @largest_abberation ||= abberations.max || 0
end

#longest_periodInteger

Longest number of samples between pulse

Returns:

  • (Integer)


44
45
46
# File 'lib/pulse-analysis/analysis.rb', line 44

def longest_period
  @longest_period ||= @periods.max
end

#num_pulsesInteger

Number of usable pulses in the audio file

Returns:

  • (Integer)


38
39
40
# File 'lib/pulse-analysis/analysis.rb', line 38

def num_pulses
  @num_pulses ||= @periods.count
end

#runBoolean

Run the analysis

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/pulse-analysis/analysis.rb', line 23

def run
  prepare
  populate_periods
  validate
  true
end

#shortest_periodInteger

Shortest number of samples between pulse

Returns:

  • (Integer)


50
51
52
# File 'lib/pulse-analysis/analysis.rb', line 50

def shortest_period
  @shortest_period ||= @periods.min
end

#tempo_bpmFloat

Tempo of the audio file in beats per minute (BPM) Assumes that the pulse is 16th notes as per the Innerclock Litmus Test

Returns:

  • (Float)


58
59
60
# File 'lib/pulse-analysis/analysis.rb', line 58

def tempo_bpm
  @tempo_bpm ||= calculate_tempo_bpm
end

#valid?Boolean

Validate that the analysis has produced meaningful results

Returns:

  • (Boolean)


100
101
102
# File 'lib/pulse-analysis/analysis.rb', line 100

def valid?
  @periods.count > MINIMUM_PULSES
end

#validateBoolean

Validate that analysis on the given data can produce meaningful results

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
# File 'lib/pulse-analysis/analysis.rb', line 89

def validate
  if valid?
    true
  else
    message = "Could not produce a valid analysis."
    raise(message)
  end
end