Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/mext/array/scalarop.rb,
lib/mext/numeric/ftom.rb,
lib/mext/numeric/gold.rb,
lib/mext/numeric/mtof.rb,
lib/mext/numeric/ampdb.rb,
lib/mext/numeric/dbamp.rb,
lib/mext/numeric/mmtot.rb,
lib/mext/numeric/rrand.rb,
lib/mext/numeric/cpspch.rb,
lib/mext/numeric/cround.rb,
lib/mext/numeric/mtopch.rb,
lib/mext/numeric/pchcps.rb,
lib/mext/numeric/pchtom.rb,
lib/mext/numeric/addsemi.rb,
lib/mext/numeric/constants.rb,
lib/mext/numeric/pchtosemi.rb,
lib/mext/numeric/semitopch.rb,
lib/mext/numeric/pitch_fork.rb,
lib/mext/numeric/pchtooctsemi.rb

Overview

Numeric

extensions that apply to any number

Constant Summary collapse

SECONDS_PER_MINUTE =

mm(subdivision = 1.0): metronome to time converter

interprets its receiver as a metronome marking to time in seconds

subdivision the time subdivision

:nodoc:

60.0
DEFAULT_CROUND_EPS =

cround(eps = 1e-8): conditional rounding algorithm

cround will return a rounded number if the starting value is nearer to

its nearest integer by less than +eps+. Otherwise it will not round
the receiver

:nodoc:

1e-8
MIDI_MIDDLE_C =

MIDI_MIDDLE_C: the central C (Do) in MIDI jargon

60
PITCH_MIDDLE_C =

PITCH_MIDDLE_C: the central C (Do) in pitch-class jargon

8.0
CHROMATIC_NOTES_PER_OCTAVE =

CHROMATIC_NOTES_PER_OCTAVE (short: CNPO): the number of notes per octave

CNPO = 12.0
ZERO_PITCH =

ZERO_PITCH (short: ZP): where does the 0.00 pitch-class reside in terms of semitones (+96)

ZP = PITCH_MIDDLE_C * CNPO
ZERO_MIDI_PITCH =

ZERO_MIDI_PITCH (short: ZMP): where does the 0.00 pitch-class reside in terms of midi notes (-36)

ZMP = MIDI_MIDDLE_C - ZERO_PITCH
PITCH_CLASS_CENTS =

PITCH_CLASS_CENTS (short: PCC): the number of parts in every semitone

PCC = 100.0
DEFAULT_PITCH_FORK =

DEFAULT_PITCH_FORK is our ‘A’ (or La) standard

440.0
MIDI_PITCH_FORK =
69.0
@@pitch_fork =
DEFAULT_PITCH_FORK

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pitch_forkObject

pitch_fork(value): gets the current tuning



29
30
31
# File 'lib/mext/numeric/pitch_fork.rb', line 29

def pitch_fork
  @@pitch_fork
end

.pitch_fork=(value) ⇒ Object

pitch_fork=(value): sets the current tuning



22
23
24
# File 'lib/mext/numeric/pitch_fork.rb', line 22

def pitch_fork=(value)
  @@pitch_fork = value
end

.reset_pitch_forkObject

reset_pitch_fork: resets the pitch fork to its default



36
37
38
# File 'lib/mext/numeric/pitch_fork.rb', line 36

def reset_pitch_fork
  @@pitch_fork = DEFAULT_PITCH_FORK
end

Instance Method Details

#addsemi(semi) ⇒ Object

:doc:

addsemi: add semitones to a pitch class float number

interprets its receiver as a pitch class and returns the corresponding addition of semitones

:nodoc:



12
13
14
15
16
# File 'lib/mext/numeric/addsemi.rb', line 12

def addsemi(semi)
  full_semi = self.pchtosemi

  (full_semi + semi).semitopch
end

#ampdbObject

ampdb: linear to dB converter

interprets its receiver as a linear value and returns it in dB



8
9
10
11
# File 'lib/mext/numeric/ampdb.rb', line 8

def ampdb
  raise Mext::NegativeNumeric if self < 0.0
  20*Math::log10(self)
end

#cpspchObject

cpspch: frequency to pitch class converter

interprets its receiver as frequency and returns its corresponing pitch class

:nodoc:



10
11
12
13
14
15
16
# File 'lib/mext/numeric/cpspch.rb', line 10

def cpspch
  raise Mext::NegativeNumeric if self <= 0.0

  midi_note = self.ftom

  midi_note.mtopch
end

#cround(eps = DEFAULT_CROUND_EPS) ⇒ Object



13
14
15
16
# File 'lib/mext/numeric/cround.rb', line 13

def cround(eps = DEFAULT_CROUND_EPS)
  rv = self.round
  ((self - rv).abs < eps) ? rv : self
end

#dbampObject

dbamp: MIDI note to frequency converter

interprets its receiver as dB and returns its linear value



8
9
10
# File 'lib/mext/numeric/dbamp.rb', line 8

def dbamp
  10.0**(self/20.0)
end

#ftomObject

ftom: frequency converter to MIDI note

interprets its receiver as a frequency and returns its corresponing MIDI note



9
10
11
12
# File 'lib/mext/numeric/ftom.rb', line 9

def ftom
  raise Mext::NegativeNumeric if self < 0.0
  MIDI_PITCH_FORK + (12.0*Math::log2(self/self.class.pitch_fork))
end

#gold(p = 1.0) ⇒ Object

gold(power = 1.0): golden section ruler

returns the value of the golden section elevated to the power of power

:nodoc:



15
16
17
# File 'lib/mext/numeric/gold.rb', line 15

def gold(p = 1.0)
  self * (Math::GOLDEN_PROPORTION**(p))
end

#mmtot(subdiv = 1.0) ⇒ Object



13
14
15
# File 'lib/mext/numeric/mmtot.rb', line 13

def mmtot(subdiv = 1.0)
  (SECONDS_PER_MINUTE*subdiv) / self
end

#mtofObject

mtof: MIDI note to frequency converter

interprets its receiver as a MIDI note and returns its frequency in Hertz



8
9
10
# File 'lib/mext/numeric/mtof.rb', line 8

def mtof
  self.class.pitch_fork * (2.0**((self - MIDI_PITCH_FORK)/12.0))
end

#mtopchObject

:doc:

mtopch: MIDI note to pitch class converter

interprets its receiver as a MIDI note and returns its corresponing pitch class

:nodoc:



11
12
13
14
15
# File 'lib/mext/numeric/mtopch.rb', line 11

def mtopch

  (self - ZMP).semitopch

end

#pchcpsObject

pchcps: pitch class converter to frequency

interprets its receiver as pitch class and returns its corresponing frequency

:nodoc:



10
11
12
13
14
# File 'lib/mext/numeric/pchcps.rb', line 10

def pchcps
  m_note = self.pchtom

  m_note.mtof
end

#pchtomObject

pchtom: pitch class to MIDI note converter

interprets its receiver as a pitch class and returns its corresponing MIDI note

:nodoc:



10
11
12
# File 'lib/mext/numeric/pchtom.rb', line 10

def pchtom
  self.pchtosemi + ZMP
end

#pchtooctsemiObject

:doc:

pchtooctsemi: pitch class to [octave, semitones] converter

interprets its receiver as a pitch class and returns an array in the format [octave, semitones]

:nodoc:



12
13
14
15
16
17
18
19
20
21
# File 'lib/mext/numeric/pchtooctsemi.rb', line 12

def pchtooctsemi
  p_octave = self.to_i
  p_note = (self - p_octave) * 100
  ref = self < 0.0 ? -CHROMATIC_NOTES_PER_OCTAVE : CHROMATIC_NOTES_PER_OCTAVE

  p_octave += (p_note / CHROMATIC_NOTES_PER_OCTAVE).to_i # cater for octave wrapping
  p_note   = (p_note % ref);                             # reduce note in a 0-11 space (keeping track of sign)

  [p_octave, p_note]
end

#pchtosemiObject

:doc:

pchtosemi: pitch class to semitones converter

interprets its receiver as a pitch class and returns the corresponding quantity in semitones

:nodoc:



12
13
14
15
16
# File 'lib/mext/numeric/pchtosemi.rb', line 12

def pchtosemi
  (p_octave, p_note) = self.pchtooctsemi

  (p_octave * CNPO) + p_note
end

#rrand(upper = 0.0) ⇒ Object

rrand(upper = 0): random number generator

returns a random number in the range receiver-upper bound

If any of the numbers (the receiver or the argument) are Floats the method will return a Float. If both arguments are integers then an Integer will be returned.

(this method is present in the SuperCollider sclang interpreter)

:nodoc:



16
17
18
19
20
21
# File 'lib/mext/numeric/rrand.rb', line 16

def rrand(upper = 0.0)
  lobound = self.to_f
  rng = upper.to_f - lobound

  (rand()*rng) + lobound
end

#semitopchObject

:doc:

semitopch: semitone to pitch class converter

interprets its receiver as a semitone quantity (starting from 0 at pitch class 0.00) and returns its corresponing pitch class

:nodoc:



12
13
14
15
16
17
18
19
20
21
# File 'lib/mext/numeric/semitopch.rb', line 12

def semitopch

  oct  = (self / CNPO).to_i
  semi = self - (oct * CNPO)
  semi %= CNPO
  semi = (self >= 0.0) ? semi : -((CNPO - semi) % CNPO)

  oct + (semi / PCC)

end

#smul(other) ⇒ Object



46
47
48
# File 'lib/mext/array/scalarop.rb', line 46

def smul(other)
  self * other
end