Class: HeadMusic::Instruments::AlternateTuning
- Inherits:
-
Object
- Object
- HeadMusic::Instruments::AlternateTuning
- Defined in:
- lib/head_music/instruments/alternate_tuning.rb
Overview
An alternate tuning for a stringed instrument.
Tunings are defined as semitone adjustments from the standard tuning. For example, "Drop D" tuning lowers the low E string by 2 semitones.
Examples: drop_d = HeadMusic::Instruments::AlternateTuning.get("guitar", "drop_d") drop_d.semitones # => [-2, 0, 0, 0, 0, 0]
When applying a tuning:
- First element applies to the lowest course
- Missing elements are treated as 0 (no change)
- Extra elements are ignored
Constant Summary collapse
- TUNINGS =
YAML.load_file(File.("alternate_tunings.yml", __dir__)).freeze
Instance Attribute Summary collapse
-
#instrument_key ⇒ Object
readonly
Returns the value of attribute instrument_key.
-
#name_key ⇒ Object
readonly
Returns the value of attribute name_key.
-
#semitones ⇒ Object
readonly
Returns the value of attribute semitones.
Class Method Summary collapse
-
.for_instrument(instrument) ⇒ Array<AlternateTuning>
Get all alternate tunings for an instrument.
-
.get(instrument, name) ⇒ AlternateTuning?
Get an alternate tuning by instrument and name.
- .normalize_instrument_key(instrument) ⇒ Object private
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#apply_to(stringing) ⇒ Array<HeadMusic::Rudiment::Pitch>
Apply this tuning to a stringing's standard pitches.
-
#initialize(instrument_key:, name_key:, semitones:) ⇒ AlternateTuning
constructor
A new instance of AlternateTuning.
-
#instrument ⇒ HeadMusic::Instruments::Instrument
The instrument this tuning applies to.
-
#name ⇒ String
Human-readable name for the tuning.
- #to_s ⇒ Object
Constructor Details
#initialize(instrument_key:, name_key:, semitones:) ⇒ AlternateTuning
Returns a new instance of AlternateTuning.
68 69 70 71 72 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 68 def initialize(instrument_key:, name_key:, semitones:) @instrument_key = instrument_key.to_sym @name_key = name_key.to_sym @semitones = Array(semitones) end |
Instance Attribute Details
#instrument_key ⇒ Object (readonly)
Returns the value of attribute instrument_key.
19 20 21 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 19 def instrument_key @instrument_key end |
#name_key ⇒ Object (readonly)
Returns the value of attribute name_key.
19 20 21 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 19 def name_key @name_key end |
#semitones ⇒ Object (readonly)
Returns the value of attribute semitones.
19 20 21 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 19 def semitones @semitones end |
Class Method Details
.for_instrument(instrument) ⇒ Array<AlternateTuning>
Get all alternate tunings for an instrument
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 43 def for_instrument(instrument) instrument_key = normalize_instrument_key(instrument) return [] unless TUNINGS.key?(instrument_key) TUNINGS[instrument_key].map do |name_key, data| new( instrument_key: instrument_key, name_key: name_key, semitones: data["semitones"] || [] ) end end |
.get(instrument, name) ⇒ AlternateTuning?
Get an alternate tuning by instrument and name
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 26 def get(instrument, name) instrument_key = normalize_instrument_key(instrument) name_key = name.to_s data = TUNINGS.dig(instrument_key, name_key) return nil unless data new( instrument_key: instrument_key, name_key: name_key, semitones: data["semitones"] || [] ) end |
.normalize_instrument_key(instrument) ⇒ Object (private)
58 59 60 61 62 63 64 65 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 58 def normalize_instrument_key(instrument) case instrument when HeadMusic::Instruments::Instrument instrument.name_key.to_s else instrument.to_s end end |
Instance Method Details
#==(other) ⇒ Object
93 94 95 96 97 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 93 def ==(other) return false unless other.is_a?(self.class) instrument_key == other.instrument_key && name_key == other.name_key end |
#apply_to(stringing) ⇒ Array<HeadMusic::Rudiment::Pitch>
Apply this tuning to a stringing's standard pitches
89 90 91 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 89 def apply_to(stringing) stringing.pitches_with_tuning(self) end |
#instrument ⇒ HeadMusic::Instruments::Instrument
The instrument this tuning applies to
76 77 78 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 76 def instrument HeadMusic::Instruments::Instrument.get(instrument_key) end |
#name ⇒ String
Human-readable name for the tuning
82 83 84 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 82 def name name_key.to_s.tr("_", " ").split.map(&:capitalize).join(" ") end |
#to_s ⇒ Object
99 100 101 |
# File 'lib/head_music/instruments/alternate_tuning.rb', line 99 def to_s "#{name} (#{instrument_key})" end |