
    0;ji>                        d Z ddlmZmZmZmZmZmZ ddlm	Z	 ddl
Z
ddlZddlmZ ddlmZ ddlmZ dd	lmZ  ed
d          5 Ze                    d          5 Z e
j        ed          Zddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y    ed          dddddededeeee         f         dedededej        fd            Zedddddedede	d         dej        fd             Z eddd!dedede	d         deeeef                  fd"            Z edddddedededeej        eeeef                  f         fd#            Z  ed          dddddedededeej        eeeef                  f         fd$            Z d% Z!d& Z"eddddd'ededede	d         dej        f
d(            Z#eddd!d'ededede	d         deeeef                  f
d)            Z#eddddd'ededededeej        eeeef                  f         f
d*            Z# ed          ddddd'ededededeej        eeeef                  f         f
d+            Z#dS ),z#Functions for interval construction    )
CollectionDictListUnionoverloadIterable)LiteralN)	ArrayLike   )cache)_FloatLike_co)_resource_filezlibrosa.corezintervals.msgpackrbF)strict_map_key
   )level           T)bins_per_octavetuningsortn_binsfmin	intervalsr   r   r   returnc                   t          |t                    r|dk    r&d|t          j        d|t                    z   |z  z  }n|dk    rt          ||          }ns|dk    rt          dg||	          }nY|d
k    rt          ddg||	          }n>|dk    rt          g d||	          }n#t          j        |          }t          |          }t          j	        | |z            }t          j
                            dt          j        |          z  |                                          d|          }|rt          j        |          }||z  S )a  Construct a set of frequencies from an interval set

    Parameters
    ----------
    n_bins : int
        The number of frequencies to generate

    fmin : float > 0
        The minimum frequency

    intervals : str or array of floats in [1, 2)
        If `str`, must be one of the following:
        - `'equal'` - equal temperament
        - `'pythagorean'` - Pythagorean intervals
        - `'ji3'` - 3-limit just intonation
        - `'ji5'` - 5-limit just intonation
        - `'ji7'` - 7-limit just intonation

        Otherwise, an array of intervals in the range [1, 2) can be provided.

    bins_per_octave : int > 0
        If `intervals` is a string specification, how many bins to
        generate per octave.
        If `intervals` is an array, then this parameter is ignored.

    tuning : float
        Deviation from A440 tuning in fractional bins.
        This is only used when `intervals == 'equal'`

    sort : bool
        Sort the intervals in ascending order.

    Returns
    -------
    frequencies : array of float
        The frequencies

    Examples
    --------
    Generate two octaves of Pythagorean intervals starting at 55Hz

    >>> librosa.interval_frequencies(24, fmin=55, intervals="pythagorean", bins_per_octave=12)
    array([ 55.   ,  58.733,  61.875,  66.075,  69.609,  74.334,  78.311,
            82.5  ,  88.099,  92.812,  99.112, 104.414, 110.   , 117.466,
           123.75 , 132.149, 139.219, 148.668, 156.621, 165.   , 176.199,
           185.625, 198.224, 208.828])

    Generate two octaves of 5-limit intervals starting at 55Hz

    >>> librosa.interval_frequencies(24, fmin=55, intervals="ji5", bins_per_octave=12)
    array([ 55.   ,  58.667,  61.875,  66.   ,  68.75 ,  73.333,  77.344,
            82.5  ,  88.   ,  91.667,  99.   , 103.125, 110.   , 117.333,
           123.75 , 132.   , 137.5  , 146.667, 154.687, 165.   , 176.   ,
           183.333, 198.   , 206.25 ])

    Generate three octaves using only three intervals

    >>> intervals = [1, 4/3, 3/2]
    >>> librosa.interval_frequencies(9, fmin=55, intervals=intervals)
    array([ 55.   ,  73.333,  82.5  , 110.   , 146.667, 165.   , 220.   ,
       293.333, 330.   ])
    equalg       @r   dtypepythagoreanr   r   ji3   )primesr   r   ji5   ji7)r#   r&      N)
isinstancestrnparangefloatpythagorean_intervalsplimit_intervalsarraylenceilmultiplyouterflattenr   )	r   r   r   r   r   r   ratios	n_octaves
all_ratioss	            P/root/voice-cloning/.venv/lib/python3.11/site-packages/librosa/core/intervals.pyinterval_frequenciesr:      s   P )S!! &")AeDDDDWFF -''*?QUVVVFF%%sO$  FF %%1vT  FF %% yy/  F )$$f++ 011I""3")I*>*>#>GGOOQQJ  )WZ((
    .r   r   return_factorsr=   c                     d S N r<   s      r9   r.   r.      s	     Cr;   r!   c                     d S r?   r@   r<   s      r9   r.   r.      	     Cr;   c                     d S r?   r@   r<   s      r9   r.   r.      rB   r;   c                    t          j        |           t          j        t          j        d          z            \  }|dk     }||xx         dz  cc<   |xx         dz  cc<                       t
                    |rt          j        |          }||         }nt          |           }|rt          fd|D                       S t          j	        d|          S )a  Pythagorean intervals

    Intervals are constructed by stacking ratios of 3/2 (i.e.,
    just perfect fifths) and folding down to a single octave::

        1, 3/2, 9/8, 27/16, 81/64, ...

    Note that this differs from 3-limit just intonation intervals
    in that Pythagorean intervals only use positive powers of 3
    (ascending fifths) while 3-limit intervals use both positive
    and negative powers (descending fifths).

    Parameters
    ----------
    bins_per_octave : int
        The number of intervals to generate
    sort : bool
        If `True` then intervals are returned in ascending order.
        If `False`, then intervals are returned in circle-of-fifths order.
    return_factors : bool
        If `True` then return a list of dictionaries encoding the prime factorization
        of each interval as `{2: p2, 3: p3}` (meaning `3**p3 * 2**p2`).
        If `False` (default), return intervals as an array of floating point numbers.

    Returns
    -------
    intervals : np.ndarray or list of dictionaries
        The constructed interval set. All intervals are mapped
        to the range [1, 2).

    See Also
    --------
    plimit_intervals

    Examples
    --------
    Generate the first 12 intervals

    >>> librosa.pythagorean_intervals(bins_per_octave=12)
    array([1.      , 1.067871, 1.125   , 1.201355, 1.265625, 1.351524,
           1.423828, 1.5     , 1.601807, 1.6875  , 1.802032, 1.898437])
    >>> # Compare to the 12-tone equal temperament intervals:
    >>> 2**(np.arange(12)/12)
    array([1.      , 1.059463, 1.122462, 1.189207, 1.259921, 1.33484 ,
           1.414214, 1.498307, 1.587401, 1.681793, 1.781797, 1.887749])

    Or the first 7, in circle-of-fifths order

    >>> librosa.pythagorean_intervals(bins_per_octave=7, sort=False)
    array([1.      , 1.5     , 1.125   , 1.6875  , 1.265625, 1.898437,
           1.423828])

    Generate the first 7, in circle-of-fifths other and factored form

    >>> librosa.pythagorean_intervals(bins_per_octave=7, sort=False, return_factors=True)
    [
        {2: 0, 3: 0},
        {2: -1, 3: 1},
        {2: -3, 3: 2},
        {2: -4, 3: 3},
        {2: -6, 3: 4},
        {2: -7, 3: 5},
        {2: -9, 3: 6}
    ]
    r#   r      c              3   <   K   | ]}|          |         d V  dS ))r   r#   Nr@   ).0ipow2pow3s     r9   	<genexpr>z(pythagorean_intervals.<locals>.<genexpr>   s5      ;;!aT!W--;;;;;;r;   r   )
r+   r,   modflog2astypeintargsortrangelistpower)r   r   r=   
log_ratios	too_smallidxrI   rJ   s         @@r9   r.   r.      s    L 9_%%D wtbgajj011J QIyQOOOqOOO ;;sD  %j$$_

 O$$ <;;;;;s;;;;;;8Az"""r;   c                 r   t          j        |          }t          j        |          }t          j        |d          }||z
  }t          j        |d          }||z
  }t          j        ||          t          j        ||          z
  }t          j        |                     ||z   d|z  z
            d          S )u  Compute the harmonic distance between ratios a and b.

    Harmonic distance is defined as `log2(a * b) - 2*log2(gcd(a, b))` [#]_.

    Here we are expressing a and b as prime factorization exponents,
    and the prime basis are provided in their log2 form.

    .. [#] Tenney, James.
        "On ‘Crystal Growth’ in harmonic space (1993–1998)."
        Contemporary Music Review 27.1 (2008): 47-56.
    r   r      )r+   r0   maximumminimumarounddot)logsaba_numa_denb_numb_dengcds           r9   __harmonic_distancere     s     	A
A Jq!EAIEJq!EAIE
 *UE
"
"RZu%=%=
=C 9TXXa!ea#go..222r;   c                     |                     t          j        |                     |                     t          j        |                    k     S )z-Given two tuples of prime powers, break ties.)r\   r+   abs)r^   r_   r]   s      r9   _crystal_tie_breakrh   "  s5    88BF1II"&))!4!444r;   r$   c                     d S r?   r@   r$   r   r   r=   s       r9   r/   r/   '  	     Cr;   c                     d S r?   r@   rj   s       r9   r/   r/   2  rk   r;   c                     d S r?   r@   rj   s       r9   r/   r/   =  rk   r;   c           	      X   t          j        |           } t          j        | t           j                  }g }t	          t          |                     D ]c}dgt          |           z  }d||<   |                    t          |                     d||<   |                    t          |                     d|                                }t                      }	t                      }
t          dgt          |           z            }|
                    |           t          |
          |k     r.t           j        }d}t          |          D ]z\  }}d}|
D ]:}||f|	vr%t          |||          |	||f<   |	||f         |	||f<   ||	||f         z  };||k     s,t          j        ||          rt          |||         |          r|}|}{|                    |          }|
                    |           |D ]U}t          t          j        |          t          j        |          z             }||
vr||vr|                    |           Vt          |
          |k     .t          j        t          |
          t$                    }t          j        |                    |                    \  }}|dk     }||xx         dz  cc<   ||xx         dz  cc<   |                    t,                    }|rt          j        |          }||         }nt	          |          }|rwg }|D ]p}t                      }||         dk    r||          |d<   |                    d t3          | ||                   D                        |                    |           q|S t          j        d|          S )u
  Construct p-limit intervals for a given set of prime factors.

    This function is based on the "harmonic crystal growth" algorithm
    of [#1]_ [#2]_.

    .. [#1] Tenney, James.
        "On ‘Crystal Growth’ in harmonic space (1993–1998)."
        Contemporary Music Review 27.1 (2008): 47-56.

    .. [#2] Sabat, Marc, and James Tenney.
        "Three crystal growth algorithms in 23-limit constrained harmonic space."
        Contemporary Music Review 27, no. 1 (2008): 57-78.

    Parameters
    ----------
    primes : array of odd primes
        Which prime factors are to be used
    bins_per_octave : int
        The number of intervals to construct
    sort : bool
        If `True` then intervals are returned in ascending order.
        If `False`, then intervals are returned in crystal growth order.
    return_factors : bool
        If `True` then return a list of dictionaries encoding the prime factorization
        of each interval as `{2: p2, 3: p3, ...}` (meaning `3**p3 * 2**p2`).
        If `False` (default), return intervals as an array of floating point numbers.

    Returns
    -------
    intervals : np.ndarray or list of dictionaries
        The constructed interval set. All intervals are mapped
        to the range [1, 2).

    See Also
    --------
    pythagorean_intervals

    Examples
    --------
    Compare 3-limit tuning to Pythagorean tuning and 12-TET

    >>> librosa.plimit_intervals(primes=[3], bins_per_octave=12)
    array([1.        , 1.05349794, 1.125     , 1.18518519, 1.265625  ,
           1.33333333, 1.40466392, 1.5       , 1.58024691, 1.6875    ,
           1.77777778, 1.8984375 ])
    >>> # Pythagorean intervals:
    >>> librosa.pythagorean_intervals(bins_per_octave=12)
    array([1.        , 1.06787109, 1.125     , 1.20135498, 1.265625  ,
           1.35152435, 1.42382812, 1.5       , 1.60180664, 1.6875    ,
           1.80203247, 1.8984375 ])
    >>> # 12-TET intervals:
    >>> 2**(np.arange(12)/12)
    array([1.        , 1.05946309, 1.12246205, 1.18920712, 1.25992105,
           1.33483985, 1.41421356, 1.49830708, 1.58740105, 1.68179283,
           1.78179744, 1.88774863])

    Create a 7-bin, 5-limit interval set

    >>> librosa.plimit_intervals(primes=[3, 5], bins_per_octave=7)
    array([1.        , 1.125     , 1.25      , 1.33333333, 1.5       ,
           1.66666667, 1.875     ])

    The same example, but now in factored form

    >>> librosa.plimit_intervals(primes=[3, 5], bins_per_octave=7,
    ...                          return_factors=True)
    [
        {},
        {2: -3, 3: 2},
        {2: -2, 5: 1},
        {2: 2, 3: -1},
        {2: -1, 3: 1},
        {3: -1, 5: 1},
        {2: -3, 3: 1, 5: 1}
    ]
    r   r   rE   r   r   c                 @    i | ]\  }}|d k    |t          |          S )r   )rO   )rG   prS   s      r9   
<dictcomp>z$plimit_intervals.<locals>.<dictcomp>  s*    WWW5EUVJJaUJJJr;   )r+   
atleast_1drM   float64rQ   r1   appendtuplecopydictrR   inf	enumeratere   iscloserh   popr0   r-   rL   r\   rN   rO   rP   updateziprS   )r$   r   r   r=   r]   seedsrH   seedfrontier	distancesr   rootscorebest_ffpointHDs	new_point_new_seedpowsrT   rI   rU   rV   factorsvs                               r9   r/   r/   H  s   h ]6""F76,,,D E3v;; " "sS[[ QU4[[!!!QU4[[!!!! zz||H I I!s6{{"##DT
i..?
*
* !(++ 	 	HAuB * *u:Y..*=dE1*M*MIah'*3AuH*=IeQh'i5))Ezz
2u%% &uhv.>EE  LL((	### 	* 	*ARXi0028A;;>??Hy((XX-E-E))); i..?
*
*> 8DOO5111D wtxx~~..J QIyQOOOqOOO ;;sD  %j$$_

 O$$  	 	AAAw!||Qx!HHWWCQ4H4HWWWXXXNN1 8Az"""r;   )$__doc__typingr   r   r   r   r   r   typing_extensionsr	   msgpacknumpyr+   numpy.typingr
   _cacher   _typingr   
util.filesr   imsgpackopen_fdescload	INTERVALSrO   r*   r-   boolndarrayr:   r.   re   rh   r/   r@   r;   r9   <module>r      s   * ) D D D D D D D D D D D D D D D D % % % % % %      " " " " " "       # # # # # # ' ' ' ' ' ' ^N$788 ?H	t		 ? GL>>>	? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? R i i ii i S*U++,	i
 i i i Zi i i iX 
 %(	    EN	
 Z   
 
!  )-ELT]	$sCx.   
 
!C  )-EI
2:tDcN++,   
 R tEd# d# d#d#(,d#EId#
2:tDcN++,d# d# d# d#N3 3 3@5 5 5
 
 %(    	
 EN Z   
 
 	    	
 DM 
$sCx.   
 
     	
  2:tDcN++,   
 R  u# u# u#u# u# 	u#
 u# 2:tDcN++,u# u# u# u# u# u#s6   BA:.B:A>	>BA>	BBB