Class: InevitableCacophony::MidiGenerator::FrequencyTable

Inherits:
Object
  • Object
show all
Defined in:
lib/inevitable_cacophony/midi_generator/frequency_table.rb

Defined Under Namespace

Classes: OutOfRange

Constant Summary collapse

MIDI_RANGE =

Range of allowed MIDI 1 indices.

0..127
MIDI_TONIC =

Middle A in MIDI

69
MIDI_OCTAVE_NOTES =

Standard western notes per octave assumed by MIDI

12
STANDARD_MIDI_FREQUENCIES =

12TET values of those notes.

MIDI_OCTAVE_NOTES.times.map do |index|
        OctaveStructure::OCTAVE_RATIO ** (index / MIDI_OCTAVE_NOTES.to_f)
end
FREQUENCY_FUDGE_FACTOR =

Maximum increase/decrease between two frequencies we still treat as “equal”. Approximately 1/30th of human Just Noticeable Difference for pitch.

(1.0/10_000)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(octave_structure, tonic) ⇒ FrequencyTable

Create a frequency table with a given structure and tonic.

Parameters:

  • octave_structure (OctaveStructure)
  • tonic (Integer)

    The tonic frequency in Hertz. This will correspond to Cacophony frequency 1, and MIDI pitch 69



49
50
51
52
# File 'lib/inevitable_cacophony/midi_generator/frequency_table.rb', line 49

def initialize(octave_structure, tonic)
        @tonic = tonic
        @table = build_table(octave_structure, tonic)
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



54
55
56
# File 'lib/inevitable_cacophony/midi_generator/frequency_table.rb', line 54

def table
  @table
end

Instance Method Details

#index_for_ratio(ratio) ⇒ Object

Parameters:

  • ratio (Float)

    The given note as a ratio to the tonic (e.g. A above middle A = 2.0)



58
59
60
61
62
63
64
65
66
67
# File 'lib/inevitable_cacophony/midi_generator/frequency_table.rb', line 58

def index_for_ratio(ratio)
        # TODO: not reliable for approximate matching
        frequency = @tonic * ratio
        
        if (match = table.index(frequency))
                match
        else
                raise OutOfRange.new(frequency, table)
        end
end