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/rrand.rb,
lib/mext/numeric/cpspch.rb,
lib/mext/numeric/mtopch.rb,
lib/mext/numeric/pchcps.rb,
lib/mext/numeric/pchtom.rb

Overview

Numeric

extensions that apply to any number

Constant Summary collapse

MIDI_MIDDLE_C =

pchtom: pitch class to MIDI note converter

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

:nodoc:

60
PITCH_MIDDLE_C =
8.0
CHROMATIC_NOTES_PER_OCTAVE =
12.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

#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
# File 'lib/mext/numeric/mtopch.rb', line 10

def mtopch

  intv = (self - MIDI_MIDDLE_C) / CHROMATIC_NOTES_PER_OCTAVE

  intv_octave = intv.to_i
  intv_semi = (intv - intv_octave) * 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



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mext/numeric/pchtom.rb', line 13

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) ⇒ Object

rrand(upper): 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)
  lobound = self.to_f
  rng = upper.to_f - lobound

  (rand()*rng) + lobound
end