Class: HeadMusic::Rudiment::UnpitchedSound

Inherits:
Base
  • Object
show all
Defined in:
lib/head_music/rudiment/unpitched_sound.rb

Overview

An unpitched sound: a drum hit, a clap, a percussive knock on any instrument. The sound concept is a rudiment, but its vocabulary lives in the instruments catalog, so this class references HeadMusic::Instruments::Instrument at runtime only (inside methods), leaving require order unaffected.

Constant Summary collapse

GENERIC_NAME =
"unpitched"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instrument) ⇒ UnpitchedSound

Returns a new instance of UnpitchedSound.



29
30
31
# File 'lib/head_music/rudiment/unpitched_sound.rb', line 29

def initialize(instrument)
  @instrument = instrument
end

Instance Attribute Details

#instrumentObject (readonly)

Returns the value of attribute instrument.



10
11
12
# File 'lib/head_music/rudiment/unpitched_sound.rb', line 10

def instrument
  @instrument
end

Class Method Details

.genericObject



25
26
27
# File 'lib/head_music/rudiment/unpitched_sound.rb', line 25

def self.generic
  @generic ||= new(nil).freeze
end

.get(value = nil) ⇒ Object

Instances are compared by value rather than interned, because inputs are arbitrary strings and an identity cache keyed on them would grow without bound. Only the generic instrument-less sound is memoized as a singleton.



15
16
17
18
19
20
21
22
23
# File 'lib/head_music/rudiment/unpitched_sound.rb', line 15

def self.get(value = nil)
  return generic if value.nil?
  return value if value.is_a?(HeadMusic::Rudiment::UnpitchedSound)

  instrument = HeadMusic::Instruments::Instrument.get(value)
  return nil unless instrument

  new(instrument)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



53
54
55
# File 'lib/head_music/rudiment/unpitched_sound.rb', line 53

def ==(other)
  other.is_a?(self.class) && name_key == other.name_key
end

#hashObject



59
60
61
# File 'lib/head_music/rudiment/unpitched_sound.rb', line 59

def hash
  [self.class, name_key].hash
end

#nameObject



33
34
35
36
37
# File 'lib/head_music/rudiment/unpitched_sound.rb', line 33

def name
  return GENERIC_NAME unless instrument

  instrument.name
end

#name_keyObject



39
40
41
# File 'lib/head_music/rudiment/unpitched_sound.rb', line 39

def name_key
  instrument&.name_key
end

#pitched?Boolean

Describes the sound, not the instrument: even a knock on a violin body is unpitched.

Returns:

  • (Boolean)


49
50
51
# File 'lib/head_music/rudiment/unpitched_sound.rb', line 49

def pitched?
  false
end

#to_sObject



43
44
45
# File 'lib/head_music/rudiment/unpitched_sound.rb', line 43

def to_s
  name
end