Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/mext/numeric/pitch_fork.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/mtopch.rb,
lib/mext/numeric/pchcps.rb,
lib/mext/numeric/pchtom.rb,
lib/mext/numeric/constants.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
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_MIDI_PITCH =

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

ZMP = MIDI_MIDDLE_C - (PITCH_MIDDLE_C * CNPO)
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

#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

#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

mtopch: MIDI note to pitch class converter

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

:nodoc:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mext/numeric/mtopch.rb', line 10

def mtopch

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

  oct + (semi / PCC)

#   intv = self - MIDI_MIDDLE_C
#   intv /= CHROMATIC_NOTES_PER_OCTAVE

#   intv_octave = intv.floor
#   intv_semi = (intv_octave >= 0.0) ? (intv - intv_octave) : (1 - (intv_octave - intv))
#   
#   intv_semi *= CHROMATIC_NOTES_PER_OCTAVE;
#   intv_semi %= CHROMATIC_NOTES_PER_OCTAVE;

#   PITCH_MIDDLE_C + intv_octave + (intv_semi/100.0);

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
13
14
15
16
17
18
19
20
21
# File 'lib/mext/numeric/pchtom.rb', line 10

def pchtom
  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)

  m_octave = ((p_octave - PITCH_MIDDLE_C)*CHROMATIC_NOTES_PER_OCTAVE) + MIDI_MIDDLE_C; # find the appropriate midi octave

  m_octave + 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