Class Key
In: app/models/key.rb
Parent: ActiveRecord::Base

A key represents a specific theoretic tone mapped to a pitch and a letter index. Keys are defined for all letters A-G and all sharp/flat values within two semitons (e.g. Dbb, D Double-Flat enharmonic with C).

Each key is associated an index between 0-11 to represent the actual pitch within the chromatic scale. Each key is also assigned a "letter index" which represents a relative letter value. This index is crucial as it disambiguates theoretical tone differences such as Eb and D#. Doing so also supports correct double-sharp and double-flat interpretations which are rare but theoretically different from their enharmonic counterparts.

Methods

Constants

Letters = ['C' => 0, 'D' => 1, 'E' => 2, 'F' => 3, 'G' => 4, 'A' => 5, 'B' => 6]
DEFAULT = self['C']

Public Class methods

Finds a key object based on the name: +Key[‘Eb’]+

[Source]

    # File app/models/key.rb, line 34
34:         def self.[](value)
35:                 self.from_name(value) || self.from_index(value)
36:         end

Retreives Fully-Cached Array

[Source]

    # File app/models/key.rb, line 17
17:         def self.cache(conditions = [])
18:                 self.find(:all, :conditions => []) # Rails 2.0 Already Caches!
19:         end

Finds a key given a tonal index 0-11 and a letter index (to disambiguate enharmonic keys)

[Source]

    # File app/models/key.rb, line 22
22:         def self.from_index(value, preferred_letter = nil)
23:                 self.cache.find {|k| k.index == value && (preferred_letter.nil? || k.letter_index == preferred_letter)}
24:         end

Finds a key object given the name (such as Eb)

[Source]

    # File app/models/key.rb, line 27
27:         def self.from_name(value)
28:                 self.cache.find {|k| k.name == value}
29:         end

Returns an array of the 12 primary keys (definitions around the cycle of fourths)

[Source]

    # File app/models/key.rb, line 39
39:         def self.primaries
40:                 self.find_all_by_primary(true)
41:         end

Public Instance methods

Tests whether the current key is enharmonic with another_key.

[Source]

    # File app/models/key.rb, line 46
46:         def enharmonic_with?(another_key)
47:                 self.index == another_key.index
48:         end

[Validate]