Class: Mext::Music::PitchClass

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

Overview

Mext::Music::PitchClass

this is a representation of pitches in the usual PitchClass representation of csound and other software

Internally, the representation is in semitones. In order to be consistent with the csound representation, a PitchClass of 8.00 represents central C4 (+ca. 261.3 Hz+, depending on tuning), while a PitchClass of 0.00 is 96 semitones below (8 * 12).

The internal representation allows for floating point values, negative values etc. However, since the PitchClass is a geometric representation of pitches, it will never produce a negative frequency value (for semitones that go to -Inf., it’ll produce infinitely small positive values.

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:



35
36
37
# File 'lib/mext/music/pitch_class.rb', line 35

def initialize(fv, semi = nil)
  @semi = semi ? setup_with_two_arguments(fv, semi) : setup_with_one_argument(fv)
end

Class Method Details

.create_from_semitones(s) ⇒ Object

:doc:

create_from_semitones

creates a PitchClass object out of a semitone representation

:nodoc:



220
221
222
# File 'lib/mext/music/pitch_class.rb', line 220

def create_from_semitones(s)
  new(0.0, s)
end

.from_freq(f) ⇒ Object

:doc:

from_freq

returns a pitch class object from a frequency (Hz)

:nodoc:



232
233
234
# File 'lib/mext/music/pitch_class.rb', line 232

def from_freq(f)
  new(f.cpspch)
end

Instance Method Details

#+(other) ⇒ Object

:doc:

++(other)+ (operator plus)

sums two pitch classes

:nodoc:



112
113
114
# File 'lib/mext/music/pitch_class.rb', line 112

def +(other)
  self.class.create_from_semitones((self.inner_representation + other.inner_representation).cround)
end

#-(other) ⇒ Object

:doc:

-(other) (operator minus)

subtracts two pitch classes

:nodoc:



124
125
126
# File 'lib/mext/music/pitch_class.rb', line 124

def -(other)
  self.class.create_from_semitones((self.inner_representation - other.inner_representation).cround)
end

#add_semitones(semi) ⇒ Object

:doc:

add_semitones(semi):

returns a PitchClass the addition of semitones required by argument

  • semi: the semitones to add; can be any sort of Numeric, also

    negative to subtract.
    


193
194
195
# File 'lib/mext/music/pitch_class.rb', line 193

def add_semitones(semi)
  self.class.create_from_semitones((self.inner_representation + semi).cround)
end

#add_semitones!(semi) ⇒ Object

:doc:

add_semitones!(semi):

returns the addition of semitones required in the receiver’s pitch class

  • semi: the semitones to add; can be any sort of Numeric, also

    negative to subtract.
    


206
207
208
209
# File 'lib/mext/music/pitch_class.rb', line 206

def add_semitones!(semi)
  @semi += semi
  self
end

#inner_representationObject Also known as: to_semitones

:doc:

inner_representation

returns the inner representation in semitones (this is a public method since it is used in several comparison methods)

:no_doc:



68
69
70
# File 'lib/mext/music/pitch_class.rb', line 68

def inner_representation
  @semi
end

#interval(other) ⇒ Object

:doc:

interval(other)

computes the interval among two pitch classes (in number of semitones and fractions thereof)

:nodoc:



144
145
146
# File 'lib/mext/music/pitch_class.rb', line 144

def interval(other)
  (other - self).inner_representation
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



180
181
182
# File 'lib/mext/music/pitch_class.rb', line 180

def interval_proportion(prop, other)
  self.interval(other) * prop
end

#octaveObject

:doc:

octave

returns the octave part of the pitch class



45
46
47
48
# File 'lib/mext/music/pitch_class.rb', line 45

def octave
  res = @semi / ::Numeric::CNPO
  res >= 0.0 ? res.floor : res.ceil
end

#semiObject

:doc:

semi

returns the semitone part of the pitch class



56
57
58
# File 'lib/mext/music/pitch_class.rb', line 56

def semi
  @semi >= 0.0 ? (@semi % 12.0) : (@semi % -12.0)
end

#to_fObject

:doc:

to_f

returns the pitch class in float notation

:nodoc:



79
80
81
# File 'lib/mext/music/pitch_class.rb', line 79

def to_f
  self.inner_representation.semitopch
end

#to_freqObject

:doc:

to_freq

returns the pitch class in frequency (Hz)

:nodoc:



90
91
92
# File 'lib/mext/music/pitch_class.rb', line 90

def to_freq
  self.inner_representation.semitopch.pchcps
end

#transpose(semi) ⇒ Object

:doc:

transpose(semitones)

returns a PitchClass transposed by semitones semitones

:nodoc:



155
156
157
# File 'lib/mext/music/pitch_class.rb', line 155

def transpose(semi)
  self.class.create_from_semitones((self.inner_representation + semi).cround)
end

#transpose!(semi) ⇒ Object

:doc:

transpose!(semitones)

transposes the receiver by semitones semitones

:nodoc:



166
167
168
169
# File 'lib/mext/music/pitch_class.rb', line 166

def transpose!(semi)
  @semi += semi
  self
end