Scale construction#
- oct_subdiv(ratio, octave_limit=0.01365, octave=2, n=5)[source]#
N-TET tuning from Generator Interval.
This function uses a generator interval to suggest numbers of steps to divide the octave.
- Parameters:
ratio (float) – Ratio that corresponds to the generator_interval. For example, by giving the fifth (3/2) as generator interval, this function will suggest to subdivide the octave in 12, 53, etc.
octave_limit (float, default=0.01365) – Approximation of the octave corresponding to the acceptable distance between the ratio of the generator interval after multiple iterations and the octave value. The default value of 0.01365 corresponds to the Pythagorean comma.
octave (int, default=2) – Value of the octave.
n (int, default=5) – Number of suggested octave subdivisions.
- Returns:
Octdiv (List of int) – List of N-TET tunings according to the generator interval.
Octvalue (List of float) – List of the approximations of the octave for each N-TET tuning.
Examples
>>> oct_subdiv(3/2, n=3) ([12, 53, 106], [1.0136432647705078, 1.0020903140410862, 1.0041849974949628])
- compare_oct_div(Octdiv=12, Octdiv2=53, bounds=0.005, octave=2)[source]#
Function that compares steps for two N-TET tunings and returns matching ratios and corresponding degrees
- Parameters:
Octdiv (int, default=12) – First N-TET tuning number of steps.
Octdiv2 (int, default=53) – Second N-TET tuning number of steps.
bounds (float, default=0.005) – Maximum distance between one ratio of Octdiv and one ratio of Octdiv2 to consider a match.
octave (int, default=2) – Value of the octave
- Returns:
avg_ratios (numpy.ndarray) – List of ratios corresponding to the shared steps in the two N-TET tunings
shared_steps (List of tuples) – The two elements of each tuple corresponds to the tuning steps sharing the same interval in the two N-TET tunings
Examples
>>> ratios, shared_steps = compare_oct_div(Octdiv=12, Octdiv2=53, bounds=0.005, octave=2) >>> ratios, shared_steps ([1.124, 1.187, 1.334, 1.499, 1.78, 2.0], [(2, 9), (3, 13), (5, 22), (7, 31), (10, 44), (12, 53)])
- multi_oct_subdiv(peaks, max_sub=100, octave_limit=0.01365, octave=2, n_scales=10, cons_limit=0.1)[source]#
Determine optimal octave subdivisions based on consonant peaks ratios.
This function takes the most consonant peaks ratios and uses them as input for the oct_subdiv function. Each consonant ratio generates a list of possible octave subdivisions. The function then compares these lists and identifies optimal octave subdivisions that are common across multiple generator intervals.
- Parameters:
peaks (List of float) – Peaks represent local maximum in a spectrum.
max_sub (int, default=100) – Maximum number of intervals in N-TET tuning suggestions.
octave_limit (float, default=0.01365) – Approximation of the octave corresponding to the acceptable distance between the ratio of the generator interval after multiple iterations and the octave value.
octave (int, default=2) – value of the octave
n_scales (int, default=10) – Number of N-TET tunings to compute for each generator interval (ratio).
cons_limit (float, default=0.1) – Limit for the consonance of the peaks ratios.
- Returns:
multi_oct_div (List of int) – List of octave subdivisions that fit with multiple generator intervals.
ratios (List of float) – List of the generator intervals for which at least 1 N-TET tuning matches with another generator interval.
Examples
>>> peaks = [2, 3, 9] >>> oct_divs, x = multi_oct_subdiv(peaks, max_sub=100) >>> oct_divs, x ([53], array([1.125, 1.5 ]))
- harmonic_tuning(list_harmonics, octave=2, min_ratio=1, max_ratio=2)[source]#
Generates a tuning based on a list of harmonic positions.
- Parameters:
list_harmonics (List of int) – harmonic positions to use in the scale construction
octave (int) – value of the period reference
min_ratio (float, default=1) – Value of the unison.
max_ratio (float, default=2) – Value of the octave.
- Returns:
ratios (List of float) – Generated tuning.
Examples
>>> list_harmonics = [3, 5, 7, 9] >>> harmonic_tuning(list_harmonics, octave=2, min_ratio=1, max_ratio=2) [1.125, 1.25, 1.5, 1.75]
- euler_fokker_scale(intervals, n=1, octave=2, normalize=True)[source]#
Generates a tuning based on a set of prime factors in the Euler-Fokker Genera.
- Parameters:
intervals (List of int) – Prime factors to use in the scale construction.
n (int, default=1) – The multiplicity of each factor, controlling how many times each is used.
octave (int, default=2) – Value of the period reference.
normalize (bool, default=True) – If True, normalizes the scale to fit within the octave.
- Returns:
scale (List of sympy.Integer or sympy.Rational) – Generated tuning.
Examples
>>> intervals = [3, 5, 7] >>> euler_fokker_scale(intervals, n=1, octave=2, normalize=True) [1, 35/32, 5/4, 21/16, 3/2, 105/64, 7/4, 15/8, 2]
- convergents(interval)[source]#
Return the convergents of the log2 of a ratio. The second value represents the number of steps to divide the octave while the first value represents the number of octaves up before the stacked ratio arrives approximately to the octave value. For example, the output of the interval 1.5 will includes [7, 12], which means that to approximate the fifth (1.5) in a NTET-tuning, you can divide the octave in 12, while stacking 12 fifth will lead to the 7th octave up.
- Parameters:
interval (float) – Interval to find convergent.
- Returns:
convergents (List of lists) – Each sublist corresponds to a pair of convergents.
Examples
>>> convergents(3/2) [(0, 1), (1, 1), (1, 2), (3, 5), (7, 12), (24, 41), (31, 53), (179, 306), (389, 665), (9126, 15601), (18641, 31867)]
- dissmeasure(fvec, amp, model='min')[source]#
Given a list of partials (peak frequencies) in fvec, with amplitudes in amp, this routine calculates the dissonance by summing the roughness of every sine pair based on a model of Plomp-Levelt’s roughness curve. The older model (model=’product’) was based on the product of the two amplitudes, but the newer model (model=’min’) is based on the minimum of the two amplitudes, since this matches the beat frequency amplitude.
- Parameters:
fvec (List) – List of frequency values
amp (List) – List of amplitude values
model (str, default=’min’) – Description of parameter model.
- Returns:
D (float) – Dissonance value
- diss_curve(freqs, amps, denom=1000, max_ratio=2, euler_comp=True, method='min', plot=True, n_tet_grid=None)[source]#
This function computes the dissonance curve and related metrics for a given set of frequencies (freqs) and amplitudes (amps).
- Parameters:
freqs (List (float)) – list of frequencies associated with spectral peaks
amps (List (float)) – list of amplitudes associated with freqs (must be same lenght)
denom (int, default=1000) – Highest value for the denominator of each interval
max_ratio (int, default=2) – Value of the maximum ratio Set to 2 for a span of 1 octave Set to 4 for a span of 2 octaves Set to 8 for a span of 3 octaves Set to 2**n for a span of n octaves
euler (bool, default=True) – When set to True, compute the Euler Gradus Suavitatis for the derived scale.
method (str, default=’min’) – Refer to dissmeasure function for more information.
‘min’
‘product’
plot (bool, default=True) – Plot the dissonance curve.
n_tet_grid (int, default=None) – When an integer is given, dotted lines will be add to the plot at steps of the given N-TET scale
- Returns:
diss (list of floats) – list of dissonance values for all the intervals
intervals (List of tuples) – Each tuple corresponds to the numerator and the denominator of each scale step ratio
ratios (List of floats) – list of ratios that constitute the scale
euler_score (int) – value of consonance of the scale
diss (float) – value of averaged dissonance of the total curve
dyad_sims (List of floats) – list of dyad similarities for each ratio of the scale
- compute_harmonic_entropy_domain_integral(ratios, ratio_interval, spread=0.01, min_tol=1e-15)[source]#
Computes the harmonic entropy of a list of frequency ratios for a given set of possible intervals.
- Parameters:
ratios (List of floats) – List of frequency ratios.
ratio_interval (List of floats) – List of possible intervals to consider.
spread (float, default=0.01) – Controls the width of the Gaussian kernel used to smooth the probability density function of the ratios.
min_tol (float, default=1e-15) – The smallest tolerance value for considering the probability density function.
- Returns:
weight_ratios (ndarray) – Sorted ratios.
HE (ndarray) – Harmonic entropy values for each interval in ratio_interval.
Notes
Harmonic entropy is a measure of the deviation of a set of frequency ratios from the idealized harmonics (integer multiples of a fundamental frequency) and is defined as:
HE = - sum_i(p_i * log2(p_i))
where p_i is the probability of a given ratio in a smoothed probability density function.
The ratio_interval defines a range of possible intervals to consider. The algorithm computes the harmonic entropy of each possible interval in ratio_interval and returns an array of HE values, one for each interval.
- compute_harmonic_entropy_simple_weights(numerators, denominators, ratio_interval, spread=0.01, min_tol=1e-15)[source]#
Compute the harmonic entropy of a set of ratios using simple weights.
- Parameters:
numerators (array-like) – Numerators of the ratios.
denominators (array-like) – Denominators of the ratios.
ratio_interval (array-like) – Interval to compute the harmonic entropy over.
spread (float, default=0.01) – Spread of the normal distribution used to compute the weights.
min_tol (float, default=1e-15) – Minimum tolerance for the weights.
- Returns:
weight_ratios (ndarray) – Sorted weight ratios.
HE (ndarray) – Harmonic entropy.
- harmonic_entropy(ratios, res=0.001, spread=0.01, plot_entropy=True, plot_tenney=False, octave=2)[source]#
Harmonic entropy is a measure of the uncertainty in pitch perception, and it provides a physical correlate of tonalness,one aspect of the psychoacoustic concept of dissonance (Sethares). High tonalness corresponds to low entropy and low tonalness corresponds to high entropy.
- Parameters:
ratios (List of floats) – Ratios between each pairs of frequency peaks.
res (float, default=0.001) – Resolution of the ratio steps.
spread (float, default=0.01) – Spread of the normal distribution used to compute the weights.
plot_entropy (bool, default=True) – When set to True, plot the harmonic entropy curve.
plot_tenney (bool, default=False) – When set to True, plot the tenney heights (y-axis) across ratios (x-axis).
octave (int, default=2) – Value of reference period.
- Returns:
HE_minima (List of floats) – List of ratios corresponding to minima of the harmonic entropy curve
HE_avg (float) – Value of the averaged harmonic entropy
HE (List of floats) – List of harmonic entropy values for each ratio.
- tuning_reduction(tuning, mode_n_steps, function, rounding=4, ratio_type='pos_harm')[source]#
Function that reduces the number of steps in a scale according to the consonance between pairs of ratios.
- tuningList (float)
scale to reduce
- mode_n_stepsint
number of steps of the reduced scale
- functionfunction, default=compute_consonance
function used to compute the consonance between pairs of ratios Choose between:
- roundingint
maximum number of decimals for each step
- ratio_typestr, default=’pos_harm’
Choose between:
‘pos_harm’:a/b when a>b
‘sub_harm’:a/b when a<b
‘all’: pos_harm + sub_harm
- tuning_consonancefloat
Consonance value of the input tuning.
- mode_outList of floats
List of mode intervals.
- mode_consonancefloat
Consonance value of the output mode.
>>> tuning = [1, 1.21, 1.31, 1.45, 1.5, 1.7, 1.875] >>> harm_tuning, mode, harm_mode = tuning_reduction(tuning, mode_n_steps=5, function=dyad_similarity, rounding=4, ratio_type="pos_harm") >>> print('Tuning harmonicity: ', harm_tuning, '
Mode: ‘, mode, ‘ Mode harmonicity: ‘, harm_mode)
Tuning harmonicity: 9.267212965965944 Mode: [1.5, 1, 1.875, 1.7, 1.45] Mode harmonicity: 17.9500338066261
- create_mode(tuning, n_steps, function)[source]#
- Create a mode from a tuning based on the consonance of
subsets of tuning steps.
- Parameters:
tuning (List of floats) – scale to reduce
n_steps (int) – number of steps of the reduced scale
function (function, default=compute_consonance) – function used to compute the consonance between pairs of ratios Choose between:
- Returns:
mode (List of floats) – Reduced tuning.
Examples
>>> tuning = [1, 1.21, 1.31, 1.45, 1.5, 1.7, 1.875] >>> create_mode(tuning, n_steps=5, function=dyad_similarity) [1, 1.45, 1.5, 1.7, 1.875]
- pac_mode(pac_freqs, n, function=<function dyad_similarity>, method='subset')[source]#
Compute the pac mode of a set of frequency pairs.
- Parameters:
pac_freqs (List of tuples) – List of frequency pairs (f1, f2) representing phase-amplitude coupling.
n (int) – Number of steps in the tuning system.
function (function, default=dyad_similarity) – A function that takes two frequencies as input and returns a similarity score.
method (str, default=’subset’) – The method used to compute the pac mode. Possible values:
‘pairwise’
‘subset’
- Returns:
List – The pac mode as a list of frequencies.
- tuning_range_to_MOS(frac1, frac2, octave=2, max_denom_in=100, max_denom_out=100)[source]#
Compute the Moment of Symmetry (MOS) signature for a range of ratios defined by two input fractions, and compute the generative interval for that range.
The MOS signature of a ratio is a tuple of integers representing the number of equally spaced intervals that can fit into an octave when starting from the ratio, going in one direction, and repeating the interval until the octave is filled. For example, the MOS signature of an octave is (1,0) because there is only one interval that fits into an octave when starting from the ratio of 1:1 and going up. The MOS signature of a perfect fifth is (0,1) because there are no smaller intervals that fit into an octave when starting from the ratio of 3:2 and going up, but there is one larger interval that fits, which is the octave above the perfect fifth.
The generative interval is the interval that corresponds to the mediant of the two input fractions. The mediant is the fraction that lies between the two input fractions and corresponds to the interval where small and large steps are equal.
- Parameters:
frac1 (str or float) – First ratio as a string or float.
frac2 (str or float) – Second ratio as a string or float.
octave (float, default=2) – The ratio of an octave.
max_denom_in (int, default=100) – Maximum denominator to use when converting the input fractions to rational numbers.
max_denom_out (int, default=100) – Maximum denominator to use when approximating the generative interval as a rational number.
- Returns:
tuple – A tuple containing: - the mediant as a float, - the mediant as a fraction with a denominator not greater than max_denom_out, - the generative interval as a float, - the generative interval as a fraction with a denominator not greater than max_denom_out, - the MOS signature of the generative interval as a tuple of integers, - the MOS signature of the inverse of the generative interval as a tuple of integers.
- stern_brocot_to_generator_interval(ratio, octave=2)[source]#
Converts a fraction in the stern-brocot tree to a generator interval for moment of symmetry tunings
- Parameters:
ratio (float) – stern-brocot ratio
octave (float, default=2) – Reference period.
- Returns:
gen_interval (float) – Generator interval
- gen_interval_to_stern_brocot(gen)[source]#
Convert a generator interval to fraction in the stern-brocot tree.
- Parameters:
gen (float) – Generator interval.
- Returns:
root_ratio (float) – Fraction in the stern-brocot tree.
- horogram_tree(ratio1, ratio2, limit)[source]#
Compute the next step of the horogram tree.
- Parameters:
ratio1 (float) – First ratio input.
ratio2 (float) – Second ratio input.
limit (int) – Limit for the denominator of the fraction.
- Returns:
next_step (float) – Next step of the horogram tree.
- phi_convergent_point(ratio1, ratio2)[source]#
Compute the phi convergent point of two ratios.
- Parameters:
ratio1 (float) – First ratio input.
ratio2 (float) – Second ratio input.
- Returns:
convergent_point (float) – Phi convergent point of the two ratios.
- Stern_Brocot(n, a=0, b=1, c=1, d=1)[source]#
Compute the Stern-Brocot tree of a given depth.
- Parameters:
n (int) – Depth of the tree.
a, b, c, d (int) – Initial values for the Stern-Brocot recursion. Default is a=0, b=1, c=1, d=1.
- Returns:
list – List of fractions in the Stern-Brocot tree.
- generator_interval_tuning(interval=1.5, steps=12, octave=2, harmonic_min=0)[source]#
Function that takes a generator interval and derives a tuning based on its stacking. interval: float
Generator interval
- steps: int, default=12
Number of steps in the scale. When set to 12 –> 12-TET for interval 3/2
- octave: int, default=2
Value of the octave
- measure_symmetry(generator_interval, max_steps=20, octave=2)[source]#
Measure the maximum deviation in symmetry for a given generator interval.
This function calculates the MOS scales for the given generator interval and determines the maximum deviation in symmetry for the scales.
- Parameters:
generator_interval (int or float) – The generator interval for which MOS scales will be calculated.
max_steps (int, default=20) – The maximum number of steps to consider for each MOS scale calculation.
octave (int, default=2) – The octave size for which the MOS scales will be calculated.
- Returns:
float – The maximum deviation in symmetry for the given generator interval.
Examples
>>> generator_interval = 3/2 >>> measure_symmetry(generator_interval)