Class: Mext::Music::PitchClass
- Inherits:
-
Object
- Object
- Mext::Music::PitchClass
- Defined in:
- lib/mext/music/pitch_class.rb
Instance Attribute Summary collapse
-
#octave ⇒ Object
readonly
Returns the value of attribute octave.
-
#semi ⇒ Object
readonly
Returns the value of attribute semi.
Class Method Summary collapse
-
.from_freq(f) ⇒ Object
:doc:.
Instance Method Summary collapse
-
#+(other) ⇒ Object
:doc:.
-
#-(other) ⇒ Object
:doc:.
-
#initialize(fv, semi = nil) ⇒ PitchClass
constructor
Mext::Music::PitchClass.new(float_value, semi = nil):.
-
#interval(other) ⇒ Object
:doc:.
-
#interval_proportion(prop, other) ⇒ Object
:doc:.
-
#to_f ⇒ Object
:doc:.
-
#to_freq ⇒ Object
:doc:.
-
#to_semitones ⇒ Object
:doc:.
-
#transpose(semi) ⇒ Object
:doc:.
Constructor Details
#initialize(fv, semi = nil) ⇒ PitchClass
Mext::Music::PitchClass.new(float_value, semi = nil):
pitch class object, where argument is:
float_value: a pitch class in float notation (i.e. 8.00 for middle C, etc.)
+or+ an octave value when given in octave, semi pair (see below)
semi: (optional) when this argument is not nil, then it is assumed
that the first argument is the octave, and this one is the
semitone value
:nodoc:
20 21 22 |
# File 'lib/mext/music/pitch_class.rb', line 20 def initialize(fv, semi = nil) semi ? setup_with_two_arguments(fv, semi) : setup_with_one_argument(fv) end |
Instance Attribute Details
#octave ⇒ Object (readonly)
Returns the value of attribute octave.
6 7 8 |
# File 'lib/mext/music/pitch_class.rb', line 6 def octave @octave end |
#semi ⇒ Object (readonly)
Returns the value of attribute semi.
6 7 8 |
# File 'lib/mext/music/pitch_class.rb', line 6 def semi @semi end |
Class Method Details
.from_freq(f) ⇒ Object
:doc:
from_freq
returns a pitch class object from a frequency (Hz)
:nodoc:
144 145 146 |
# File 'lib/mext/music/pitch_class.rb', line 144 def from_freq(f) new(f.cpspch) end |
Instance Method Details
#+(other) ⇒ Object
:doc:
++(other)+ (operator plus)
sums two pitch classes
:nodoc:
68 69 70 71 72 73 74 |
# File 'lib/mext/music/pitch_class.rb', line 68 def +(other) octave = ((self.to_semitones + other.to_semitones) / 12.0).cround octave = octave >= 0.0 ? octave.floor : octave.ceil semis = (self.to_semitones + other.to_semitones).cround % 12.0 phase = octave >= 0.0 ? 1 : -1 PitchClass.new(octave, phase*semis) end |
#-(other) ⇒ Object
:doc:
-(other) (operator minus)
subtracts two pitch classes
:nodoc:
84 85 86 87 |
# File 'lib/mext/music/pitch_class.rb', line 84 def -(other) tot_semi = self.to_semitones - other.to_semitones PitchClass.new(0.0, tot_semi) end |
#interval(other) ⇒ Object
:doc:
interval(other)
computes the interval among two pitch classes (in number of semitones and fractions thereof)
:nodoc:
107 108 109 |
# File 'lib/mext/music/pitch_class.rb', line 107 def interval(other) other.to_semitones - self.to_semitones end |
#interval_proportion(prop, other) ⇒ Object
:doc:
interval_proportion(prop, other):
returns the interval proportion (in semitones) given
-
prop: a proportional factor (should be in the range 0-1) -
other: the other pitch
131 132 133 |
# File 'lib/mext/music/pitch_class.rb', line 131 def interval_proportion(prop, other) self.interval(other) * prop end |
#to_f ⇒ Object
:doc:
to_f
returns the pitch class in float notation
:nodoc:
31 32 33 34 35 36 37 |
# File 'lib/mext/music/pitch_class.rb', line 31 def to_f # # we suppose here that the pitch_class data is well-formed inside the # object # self.octave + (self.semi / ::Numeric::PCC) end |
#to_freq ⇒ Object
:doc:
to_freq
returns the pitch class in frequency (Hz)
:nodoc:
46 47 48 |
# File 'lib/mext/music/pitch_class.rb', line 46 def to_freq self.to_f.pchcps end |
#to_semitones ⇒ Object
:doc:
to_semitones
returns the PitchClass to a number of semitones
95 96 97 |
# File 'lib/mext/music/pitch_class.rb', line 95 def to_semitones self.to_f.pchtosemi end |
#transpose(semi) ⇒ Object
:doc:
transpose(semitones)
returns the transposition in semitones
:nodoc:
118 119 120 |
# File 'lib/mext/music/pitch_class.rb', line 118 def transpose(semi) self + PitchClass.new(0.0, semi) end |