number extras:
Filter:
Overviews (extension) | SimpleNumber | SequenceableCollection

number extras
ExtensionExtension

Additions of psychoacoustic, music theoretical and harmonic math operations to numbers and collections

Some of these added methods are used by the other classes in DissonanceLib. They are also useful for general calculations involving psychoacoustic units (barks, ERB and mels for pitch; phons and sones for loudness), music theory (simple methods for cents, representation of notes, and so on) and harmonic math. This latter means the use of ratios and harmonic measures.

Ratios are handled in DissonanceLib as two element arrays. This may not be the best object oriented way of working but for now, it is more practical than working with a Ratio class, which may come later. There are methods for reducing and dividing ratios, for rationalizing decimal frequency ratios (asRatio and rationalize) and also for calculating harmonic measures (such asharmonicity, harmonic distance and gradus suavitatis). Included are also ancient Greek methods for dividing and generating proportional intervals as well as a few more recent methods by Augusto Novaro.

See HarmonicVector, HarmonicMetric and PitchSet for more info regarding harmonic arithmetic.

Additions to SimpleNumber

Some of these methods also work for collections of numbers, as implemented in SequenceableCollection

cents

Return the value of a frequency interval in cents:

equivalent to:

addCents

Add a cent value to a frequency:

cents2Frq(frq = 1)

The same as the previous method but with a different semantic. If no frq argument is given, it is assumed one, which is semantically the same as converting from cents to frequency ratios:

asNote

Represent a frequency as a note name and a deviation in cents from equal temperament:

asPitchBend(pb = 400)

Calculate pitchbend value for midi playback of microtones (in cents).

pb is the pitch bend ammount (400 = +/- 1 tone, i.e. -200 to +200 cents)

asBark

Return the bark (subjetive pitch) value of a frequency in Hz. See http://en.wikipedia.org/wiki/Bark_scale

barkToFreq

Convert from bark to Hz interpolating from the edges of the critical bandwidth.

barkToHz

Convert from bark to Hz using the inverse of the asBark function.

criticalBW

Gives the size (in Hz) of the critical bandwidth given in barks:

hzToErb

Convert from Hz to ERB (Equivalent Rectangular Bandwidth, another scale based on the critical bandwidth: see http://en.wikipedia.org/wiki/Equivalent_rectangular_bandwidth). Also called the ERB-rate of a tone. It is used in the masking analysis methods of LoudnessModel

hzToMel

Yet another subjective pitch scale, not yet used by other classes but implemented for completeness. See http://en.wikipedia.org/wiki/Mel_scale

melToHz

As its name imples.

phonToSone

Convert from phons to sones. http://en.wikipedia.org/wiki/Phon

soneToPhon

Convert sones to phons. http://en.wikipedia.org/wiki/Sone

asPhon(spl, calib = 0)

Return the value in phons for a frequency at a certain db SPL level. Calib should be 0 if loudness is in dB SPL; a positive number if the values are in dBFS (dB's with respect to a full scale sine wave); or negative dB's relative to 0 which is the case when translating from amplitude value to dB with ampdb.

asSone(spl, calib = 0)

Return the value in sons for a frequency at a certain db SPL level. Calib should be 0 if loudness is in dB SPL; a positive number if the values are in dBFS (dB's with respect to a full scale sine wave); or negative dB's relative to 0 which is the case when translating from amplitude value to dB with ampdb.

asDynamic(freq, ref = 100, fff = 3)

Returns a symbol expressing the dynamic value (ppp-fff) of an amplitude and frequency. The number should be an amplitude; it is converted to dBFS, then phon, sone and musical dynamics. The reference value is 100, which assumes that the amplitude value of 1.0 at 1000Hz will translate to 64 sones, equivalent to fff (see also LoudnessModel).

The upper boundary for sones is 64 because equal loudness contours stop at 100 phons. The fff argument is there to adjust how many f's correspond to 64 sones. The default of 3 is subtracted from 6, which is the power of two of 64, yielding 3f's. If it is less than 3, more f's will correspond to that maximum dynamic.

According to one's needs, values can go from, say, ppppp-fffff.

Examples:

For arrays of amps and freqs, you can use an array (of the same size as the amp array) for the freq arg:

bintodb(zeta = 8)

Convert the magnitude of an fft bin to dB. Implemented according to a formula found in the PhD Thesis of Nick Collins; zeta is a calibration factor. Its value is 184262 in the formula, but from my experience, for practical purposes a value of 8 works well with SC's PV_MagBuffer and PV_FreqBuffer (see the topic Live Dissonance).

asWavelength(c = 343)

Return the wavelength size in meters of a frequency, c being the speed of sound in meters/second.The default value corresponds to the speed of sound at 20 degrees Celsius.

minHarmonicityVector(pitchRange = 1, maxPrime = 11)

Return a sequence of largest prime powers for a given harmonicity minimum. Pitch range is in octaves, ex, 0.03.minHarmonicityVector(1,13) yields [12, 8, 3, 2, 1, 1]. They correspond to the powers of the harmonic space bases 2,3,5,7,11,and 13 inside an octave.

NOTE: "A maximum powers sequence includes intervals, the harmonicities of which may lie below the minimum suggested [by this method]...The maximum power sequence guarantees merely that all intervals that are more harmonic than a given minimum [harmonicity] value can be expressed by the sequence. [12, 8, 3, 2, 1, 1] results in as many as 3,964 different intervals within one octave (!), of which only 211 are truly more harmonic than 0.03" ("Two Essays on Theory", C. Barlow (CMJ, 1987)). This is related to the highestPower method, see below.

asRatio

This is like SimpleNumber:asFraction but hacked in order to handle rounding errors for harmonic interpretation of periodic decimals (0.333 will be 1/3 and not 333/1000)

Additions to Integer

Some of these methods are also implemented for SequenceableCollection

indigestibility

Barlow's indigestibility of an integer. A measure of how a number may be psychologically 'digestible' according to its prime factors. See Barlow, C, "Two Essays on Theory", Computer Music Journal, 1987. It is rarely used on its own but is the basis for the harmonicity function.

Formula \xi(\mathcal{N}) = 2 \sum_{r=1}^{\infty} \left ( \frac{ n_{r}(p_{r} -1)^2}{p_{r}} \right)

where: \mathcal{N} = \prod_{r=1}^{\infty} p_{r}^{n_{r}} \mathcal{N}, n, p \in \mathbb{N} p \in prime

Example:

harmonicity

Barlow's harmonicity formula for an interval P/Q. It is the reciprocal of the sum of the indigestibilties of P and Q, with its sign indicating the interval's polarity. \mathcal{H(P,Q)} = \frac{ sgn \left ( \xi( \mathcal{Q}) - \xi(\mathcal{P} \right ) ) }{ \xi(\mathcal{Q}) + \xi(\mathcal{P}) }

Negative polarity means that the interval is weighted towards its upper tone and not towards its lower tone, as with a positive value. As an example, a perfect fifth has its 'weight' on the lower tone, while its inverse, a perfect fourth, has its weight on the highest tone. The absolute value of a harmonicity is called harmonic intensity.

The harmonicity of 1/1 is infinite. There is a flag (clean = true) in the implementation of harmonicity for collections that is used for setting the harmonicity of 1/1 to 2 instead of NaN.

Compare:

Examples:

highestPower

Barlow's formula N(p) from "Two Essays on Theory". It is used by minHarmVector, see above.

divisorSet

Returns an array with all the integers that divide this. It is useful for making scales out of whole numbers. See for example, Erkki Kurenniemi, "Chords, scales, and divisor lattices".

multiples(...primes)

Returns an array with all the multiples of a list of primes up to this integer.

harmonics(max)

Returns an array with the harmonics of a number up to the highest harmonic (max).

primeHarmonics(maxPartial)

Returns a nested array with all the harmonics up to maxPartial of a prime (this) along with all the lower primes.

listOfHarmonics(max)

Returns an array with the harmonics (up to max) of all the primes below (and including) this. It is equivalent to: highest_harmonic.multiples(array of primes up to this).

Working with harmonic ratios

These are methods implemented for SequenceableCollection. They assume arrays of two elements.

ratioDiv(that, reduce = true)

Ratio division. If reduce = true, the result will be expressed in its lowest terms.

reduceRatio

Reduce a ratio to its lowest terms [p,q] , where p and q are coprime.

ratioPost(char = ", ")

Utiity for representing [p,q] ratios as p/q strings. If the array has only two elements, then the string "p/q" will be returned. If the array is an array of [p,q] it will return a string of "p1/q1 ... pn/qn" separated by char.

lcd

The lowest common denominator to an array of [p, q] ratios.

ratioToFreq

Express and array of [p, q] ratios as decimal frequency ratios.

ratioDifferentiate

Return an array of [p, q] ratios expressed as adjacency instead of absolute intervals.

ratioToHarmonics

Convert an array of [p, q] ratios to the harmonics that correspond to those ratios.

harmonicsToRatios

Convert an array of integers to the ratios they represent as harmonics.

subharmonicsToRatios

Convert an array of integers to the ratios they represent as subharmonics.

rationalize(tolerance = 16, metric, type = \size, max)

An important method used in Dissonance to convert an array of decimal frequency ratios into [p,q] ratios.

It first finds a rough approximation using asRatio (explained above). Then it compares from a list of intervals stored in the IntervalTable class (there are several possible tables, see its help file) and chooses the one with the highest harmonic metric within the given tolerance range.

The tolerance (in cents) defines the range within which the search for intervals will be made.

The metric is either an instance of HarmonicMetric or a symbol \harmonicity, \harmonicDistance or \gradusSuavitatis If no metric is specified, \harmonicity will be used. See below for more on harmonic measures.

The type is either \size or \prime. If \size is chosen, intervals in the table with numerator and denominator larger than max (which defaults to [729, 512]) will be ignored. If \prime is chosen, intervals containing prime factors larger than max (which defaults to 19) will be ignored. Default is \size.

Examples:

analyseScale(tolerance = 16, type = \size, maxNum = 729, maxDenom = 521, maxPrime = 31, post = true)

This method is called by rationalize but can also be handy on its own if one wants to classify a set of intervals. The arguments are the same as above with the exception that the information type is not contained in a single argument max but independently in arguments maxNum, maxDenom and maxPrime. Additionally, when used on its own, it outputs the inteval lists. See also ratioToName below.

ratioToName(tolerance = 16, restore = true)

For posting the names of ratios from a list taken from the Huygens-Fokker foundation of around 356 intervals inside and octave, which is why this method works only with octave-reduced intervals.

NOTE: Because IntervalTable as a whole class keeps one interval list loaded at any given time, restore is a flag used to change the table back to the previously used one, as it is common to have a table that encompasses many octaves and more intervals (the default is a table, called \030, has all intervals with harmonicities higher than 0.03) but which don't contain a key for names. In this way, after posting the names, the previous tables are loaded back as they were.

centsToName(tolerance = 16, restore = true)

Same as above but the array is of cent values instead of ratios.

arithmetic, harmonic, geometric means

integerArithmetic and harmonic means Convert an array of integers to the ratios they represent as harmonics.

example of geometric means producing ET

novaroSeries

Augusto Novaro's arithmetic progressions generate interesting harmonic series out of divisions of an interval

katapykne(n = 1)

A method of densification of intervals used by ancient greek harmonists. See John Chalmers "Divisions of the Tetrachord", Chapter 2: "Katapyknosis consists of the division, or rather the filling-in, of a musical interval by multiplying its numerator and denominator by a set of integers of increasing magnitude ... The intervals form a series of microtones which are then recombined to produce the desired melodic division, usually composed of epimore [superparticular] ratios." n is the number of levels of this process

katapykne_ab(a = 1, b = 2)

A variation on katapyknosis where the increment by factors a and b is not a simple arithmetic series .

Harmonic measures

There are 3 main harmonic measures implemented in DissonanceLib. See HarmonicMetric for more info.

harmonicity

See above for a discussion.

harmonicDistance

James Tenney's harmonic distance. Defined as a city-block metric of harmonic lattices (Discussed in his article, "John Cage and the Theory of Harmony", 1983).

Formula for Harmonic Distance HD of a frequency ratio f_{a} / f_{b}: HD(f_{a}, f_{b}) \varpropto \log{(a)} + \log{(b)} = \log{(a \cdot b)}

where: a = f_{a}/\gcd{(f_{a}, f_{b})}, \: b = f_{b}/\gcd{(f_{a}, f_{b})}, \: \textrm{and} \; a \ge b

gradusSuavitatis

Leonhard Euler's Gradus Suavitatis (or degree of softness, 1739). Similar to Barlow's harmonicity in that it deals with divisibility (primes) as a measure of harmonic concordance, but with the disadvantage of giving the same measures for different intervals. (See John Chalmers, "Divisions of the Tetrachord").

The Gradus Suavitatis \Gamma of an number a is: \Gamma{(a)} = 1 + \sum_{k = 1}^{n} e_{k}(p_{k} - 1)

where: a = \prod_{k=1}^{n} p_{k}^{e_{k}}

a \in \mathbb{N}

The Gradus Suavitatis of a ratio a/b is: \Gamma(a \cdot b)

gradusSuavitatisN

Degree of sweetness of a scale or chord.

Convenience methods

The following are designed to work with arrays of pairs of frecuencies and intensities [freq, spl]:

asPhon

Return the values in phons of arrays of [freq, spl] pairs.

asSone

Return the values in sones of arrays of [freq, spl] pairs.

compensateMasking(gradient = 12)

Returns the amplitudes of partials after masking. See LoudnessModel

Values are input as arrays of [spl, freq] pairs.

Other utility methods:

*primes(maxPrime = 11)

A shortcut for making arrays of primes.

removeDuplicates

A shortcut for removing duplicate items in a collection (does not preserve order).