Class: HeadMusic::Instruments::AlternateTuning

Inherits:
Object
  • Object
show all
Includes:
ValueEquality
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.



72
73
74
75
76
# File 'lib/head_music/instruments/alternate_tuning.rb', line 72

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.



21
22
23
# File 'lib/head_music/instruments/alternate_tuning.rb', line 21

def instrument_key
  @instrument_key
end

#name_keyObject (readonly)

Returns the value of attribute name_key.



21
22
23
# File 'lib/head_music/instruments/alternate_tuning.rb', line 21

def name_key
  @name_key
end

#semitonesObject (readonly)

Returns the value of attribute semitones.



21
22
23
# File 'lib/head_music/instruments/alternate_tuning.rb', line 21

def semitones
  @semitones
end

Class Method Details

.for_instrument(instrument) ⇒ Array<AlternateTuning>

Get all alternate tunings for an instrument

Parameters:

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/head_music/instruments/alternate_tuning.rb', line 47

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:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/head_music/instruments/alternate_tuning.rb', line 30

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)



62
63
64
65
66
67
68
69
# File 'lib/head_music/instruments/alternate_tuning.rb', line 62

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 Originally defined in module ValueEquality

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



93
94
95
# File 'lib/head_music/instruments/alternate_tuning.rb', line 93

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

#instrumentHeadMusic::Instruments::Instrument

The instrument this tuning applies to



80
81
82
# File 'lib/head_music/instruments/alternate_tuning.rb', line 80

def instrument
  HeadMusic::Instruments::Instrument.get(instrument_key)
end

#nameString

Human-readable name for the tuning

Returns:

  • (String)


86
87
88
# File 'lib/head_music/instruments/alternate_tuning.rb', line 86

def name
  name_key.to_s.tr("_", " ").split.map(&:capitalize).join(" ")
end

#to_sObject



97
98
99
# File 'lib/head_music/instruments/alternate_tuning.rb', line 97

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