Class: HeadMusic::Instruments::AlternateTuning

Inherits:
Object
  • Object
show all
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.expand_path("alternate_tunings.yml", __dir__)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_keyObject (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_keyObject (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

#semitonesObject (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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

  • stringing (Stringing)

    The stringing to apply to

Returns:



89
90
91
# File 'lib/head_music/instruments/alternate_tuning.rb', line 89

def apply_to(stringing)
  stringing.pitches_with_tuning(self)
end

#instrumentHeadMusic::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

#nameString

Human-readable name for the tuning

Returns:

  • (String)


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_sObject



99
100
101
# File 'lib/head_music/instruments/alternate_tuning.rb', line 99

def to_s
  "#{name} (#{instrument_key})"
end