Class: Mext::Music::PitchClass

Inherits:
Object
  • Object
show all
Defined in:
lib/mext/music/pitch_class.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#octaveObject (readonly)

Returns the value of attribute octave.



6
7
8
# File 'lib/mext/music/pitch_class.rb', line 6

def octave
  @octave
end

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



131
132
133
# File 'lib/mext/music/pitch_class.rb', line 131

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
  octave = octave >= 0.0 ? octave.floor : octave.ceil
  semis  = (self.to_semitones + other.to_semitones) % 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

#to_fObject

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

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

: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.octave * 12.0) + self.semi
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