U
    5d}F                  J   @   s  d 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d	d
dddgZ
edddddddgdddddddgdddddddgdddddddgdddddddgdddddddgdddddddgdddddddgdddddddgdddddddgd
Zdd edddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbgHdD Zdcd Zddd	 Ze	eddedfdfdgdhd
Zdid Zdjd Ze	eddedfdkdldZdmd ZdS )nzMusic notation utilities    N   )cache)ParameterError)deprecate_positional_argskey_to_degreeskey_to_notesmela_to_degreesmela_to_svarathaat_to_degrees	list_mela
list_thaat         	      
               )
ZbilavalZkhamajZkafiZasavariZbhairaviZkalyanZmarvaZpoorviZtodiZbhairavc                 C   s   i | ]\}}||qS  r   ).0ikr   r   9/tmp/pip-unpacked-wheel-8l90aumz/librosa/core/notation.py
<dictcomp>#   s    r   Z	kanakangiZratnangiZ
ganamurthiZ
vanaspathiZ	manavathiZtanarupiZ	senavathiZhanumathodiZdhenukaZnatakapriyaZkokilapriyaZ	rupavathiZgayakapriyaZvakulabharanamZmayamalavagaulaZchakravakomZsuryakanthamZhatakambariZjhankaradhwaniZnatabhairaviZ	keeravaniZkharaharapriyaZgaurimanohariZvarunapriyaZmararanjiniZ	charukesiZ	sarasangiZharikambhojiZdheerasankarabharanamZnaganandiniZ	yagapriyaZragavardhiniZgangeyabhushaniZvagadheeswariZsuliniZ
chalanattaZsalagamZ
jalarnavamZjhalavaraliZnavaneethamZpavaniZ
raghupriyaZ
gavambodhiZ
bhavapriyaZsubhapanthuvaraliZshadvidhamarginiZ
suvarnangiZ	divyamaniZdhavalambariZnamanarayaniZkamavardhiniZ	ramapriyaZgamanasramaZviswambhariZ
syamalangiZshanmukhapriyaZsimhendramadhyamamZ	hemavathiZdharmavathiZneethimathiZ
kanthamaniZrishabhapriyaZlatangiZvachaspathiZmechakalyaniZchitrambariZ
sucharitraZjyotisvarupiniZdhatuvardhiniZnasikabhushaniZkosalamZrasikapriyac                 C   s   t t|   S )a5  Construct the svara indices (degrees) for a given thaat

    Parameters
    ----------
    thaat : str
        The name of the thaat

    Returns
    -------
    indices : np.ndarray
        A list of the seven svara indices (starting from 0=Sa)
        contained in the specified thaat

    See Also
    --------
    key_to_degrees
    mela_to_degrees
    list_thaat

    Examples
    --------
    >>> librosa.thaat_to_degrees('bilaval')
    array([ 0,  2,  4,  5,  7,  9, 11])

    >>> librosa.thaat_to_degrees('todi')
    array([ 0,  1,  3,  6,  7,  8, 11])
    )npZasarray	THAAT_MAPlower)Zthaatr   r   r   r
   u   s    c                 C   s   t | trt|   d }n0d|   k r0dkr>n n
| d }ntd| dg}|d }d|  krndk rn n|ddg nd|  krdk rn n|dd	g nd|  krd
k rn n|ddg nbd
|  krdk rn n|dd	g n:d|  krdk r&n n|ddg n|d	dg |dk rJ|d n
|d |d |d }|dkr|ddg nv|dkr|ddg n\|dkr|ddg nB|d	kr|ddg n(|dkr|ddg n|ddg t	|S )a  Construct the svara indices (degrees) for a given melakarta raga

    Parameters
    ----------
    mela : str or int
        Either the name or integer index ([1, 2, ..., 72]) of the melakarta raga

    Returns
    -------
    degrees : np.ndarray
        A list of the seven svara indices (starting from 0=Sa)
        contained in the specified raga

    See Also
    --------
    thaat_to_degrees
    key_to_degrees
    list_mela

    Examples
    --------
    Melakarta #1 (kanakangi):

    >>> librosa.mela_to_degrees(1)
    array([0, 1, 2, 5, 7, 8, 9])

    Or using a name directly:

    >>> librosa.mela_to_degrees('kanakangi')
    array([0, 1, 2, 5, 7, 8, 9])
    r   r   H    mela={} must be in range [1, 72]$   r   r      r      r         r   r   r   r   r   r   )

isinstancestrMELAKARTA_MAPr   r   formatextendappendr   array)melaindexdegreesr   upperr   r   r   r      sF    !









)levelT)abbrunicodec                C   s  dddddddddddd	g}t | tr8t|   d
 }n0d|   k rLdkrZn n
| d
 }ntd| |d }|dk rd|d< nd|d< |dk rd|d< nd|d< |d }|dkrd|d< nd|d< |dkrd|d< nd|d< |rdd |D }|sd d |D }t|S )!uL  Spell the Carnatic svara names for a given melakarta raga

    This function exists to resolve enharmonic equivalences between
    pitch classes:

        - Ri2 / Ga1
        - Ri3 / Ga2
        - Dha2 / Ni1
        - Dha3 / Ni2

    For svara outside the raga, names are chosen to preserve orderings
    so that all Ri precede all Ga, and all Dha precede all Ni.

    Parameters
    ----------
    mela : str or int
        the name or numerical index of the melakarta raga

    abbr : bool
        If `True`, use single-letter svara names: S, R, G, ...

        If `False`, use full names: Sa, Ri, Ga, ...

    unicode : bool
        If `True`, use unicode symbols for numberings, e.g., Ri₁

        If `False`, use low-order ASCII, e.g., Ri1.

    Returns
    -------
    svara : list of strings

        The svara names for each of the 12 pitch classes.

    See Also
    --------
    key_to_notes
    mela_to_degrees
    list_mela

    Examples
    --------
    Melakarta #1 (Kanakangi) uses R1, G1, D1, N1

    >>> librosa.mela_to_svara(1)
    ['S', 'R₁', 'G₁', 'G₂', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'N₁', 'N₂', 'N₃']

    #19 (Jhankaradhwani) uses R2 and G2 so the third svara are Ri:

    >>> librosa.mela_to_svara(19)
    ['S', 'R₁', 'R₂', 'G₂', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'N₁', 'N₂', 'N₃']

    #31 (Yagapriya) uses R3 and G3, so third and fourth svara are Ri:

    >>> librosa.mela_to_svara(31)
    ['S', 'R₁', 'R₂', 'R₃', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'N₁', 'N₂', 'N₃']

    #34 (Vagadheeswari) uses D2 and N2, so Ni1 becomes Dha2:

    >>> librosa.mela_to_svara(34)
    ['S', 'R₁', 'R₂', 'R₃', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'D₂', 'N₂', 'N₃']

    #36 (Chalanatta) uses D3 and N3, so Ni2 becomes Dha3:

    >>> librosa.mela_to_svara(36)
    ['S', 'R₁', 'R₂', 'R₃', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'D₂', 'D₃', 'N₃']

    # You can also query by raga name instead of index:

    >>> librosa.mela_to_svara('chalanatta')
    ['S', 'R₁', 'R₂', 'R₃', 'G₃', 'M₁', 'M₂', 'P', 'D₁', 'D₂', 'D₃', 'N₃']
    ZSau   Ri₁Nu   Ga₃u   Ma₁u   Ma₂ZPau   Dha₁u   Ni₃r   r   r    r!   r"   r   u   Ga₁r   u   Ri₂r&   u   Ga₂r   u   Ri₃u   Ni₁r   u   Dha₂r   u   Dha₃r   u   Ni₂c              
   S   s$   g | ]}| td d d dqS ) )ahr   	translater(   	maketransr   sr   r   r   
<listcomp>  s    z!mela_to_svara.<locals>.<listcomp>c              
   S   s$   g | ]}| td dddqS )123)u   ₁u   ₂u   ₃r8   r;   r   r   r   r=     s   )r'   r(   r)   r   r   r*   list)r.   r3   r4   Z	svara_mapZmela_idxr   r1   r   r   r   r	      sR    N





c                   C   s   t  S )aU  List melakarta ragas by name and index.

    Melakarta raga names are transcribed from [#]_, with the exception of #45
    (subhapanthuvarali).

    .. [#] Bhagyalekshmy, S. (1990).
        Ragas in Carnatic music.
        South Asia Books.

    Returns
    -------
    mela_map : dict
        A dictionary mapping melakarta raga names to indices (1, 2, ..., 72)

    Examples
    --------
    >>> librosa.list_mela()
    {'kanakangi': 1,
     'ratnangi': 2,
     'ganamurthi': 3,
     'vanaspathi': 4,
     ...}

    See Also
    --------
    mela_to_degrees
    mela_to_svara
    list_thaat
    )r)   copyr   r   r   r   r     s    c                   C   s   t t S )a}  List supported thaats by name.

    Returns
    -------
    thaats : list
        A list of supported thaats

    Examples
    --------
    >>> librosa.list_thaat()
    ['bilaval',
     'khamaj',
     'kafi',
     'asavari',
     'bhairavi',
     'kalyan',
     'marva',
     'poorvi',
     'todi',
     'bhairav']

    See Also
    --------
    list_mela
    thaat_to_degrees
    )rA   r   keysr   r   r   r   r     s    )r4   c                   s  t d| }|std| ddddddd	d
}ddddddd}|d }|d}|| }|ddd  }|dk}	|	r|| | d d }
n|| | d d d }
|dk rd}n8|dkrd}n*d|
  krdk rn nd}n|
dkrd}ddddddddd d!d"d#g}dd$dd%ddd&dd'd!d(d#g}d)d*d+d,d-d.d/g}d0d1d2d3d4d5g}|
}|
dkrp|d#krpd}|rtd|d d D ]}|| \}}|||< q|}n>d|
 d }td|d d D ]}|| \}}|||< q|}|st	d6d7d8d9d: t
 fd;d<|D }|S )=u	  Lists all 12 note names in the chromatic scale, as spelled according to
    a given key (major or minor).

    This function exists to resolve enharmonic equivalences between different
    spellings for the same pitch (e.g. C♯ vs D♭), and is primarily useful when producing
    human-readable outputs (e.g. plotting) for pitch content.

    Note names are decided by the following rules:

    1. If the tonic of the key has an accidental (sharp or flat), that accidental will be
       used consistently for all notes.

    2. If the tonic does not have an accidental, accidentals will be inferred to minimize
       the total number used for diatonic scale degrees.

    3. If there is a tie (e.g., in the case of C:maj vs A:min), sharps will be preferred.

    Parameters
    ----------
    key : string
        Must be in the form TONIC:key.  Tonic must be upper case (``CDEFGAB``),
        key must be lower-case (``maj`` or ``min``).

        Single accidentals (``b!♭`` for flat, or ``#♯`` for sharp) are supported.

        Examples: ``C:maj, Db:min, A♭:min``.

    unicode : bool
        If ``True`` (default), use Unicode symbols (♯𝄪♭𝄫)for accidentals.

        If ``False``, Unicode symbols will be mapped to low-order ASCII representations::

            ♯ -> #, 𝄪 -> ##, ♭ -> b, 𝄫 -> bb

    Returns
    -------
    notes : list
        ``notes[k]`` is the name for semitone ``k`` (starting from C)
        under the given key.  All chromatic notes (0 through 11) are
        included.

    See Also
    --------
    midi_to_note

    Examples
    --------
    `C:maj` will use all sharps

    >>> librosa.key_to_notes('C:maj')
    ['C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']

    `A:min` has the same notes

    >>> librosa.key_to_notes('A:min')
    ['C', 'C♯', 'D', 'D♯', 'E', 'F', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']

    `A♯:min` will use sharps, but spell note 0 (`C`) as `B♯`

    >>> librosa.key_to_notes('A#:min')
    ['B♯', 'C♯', 'D', 'D♯', 'E', 'E♯', 'F♯', 'G', 'G♯', 'A', 'A♯', 'B']

    `G♯:maj` will use a double-sharp to spell note 7 (`G`) as `F𝄪`:

    >>> librosa.key_to_notes('G#:maj')
    ['B♯', 'C♯', 'D', 'D♯', 'E', 'E♯', 'F♯', 'F𝄪', 'G♯', 'A', 'A♯', 'B']

    `F♭:min` will use double-flats

    >>> librosa.key_to_notes('Fb:min')
    ['D𝄫', 'D♭', 'E𝄫', 'E♭', 'F♭', 'F', 'G♭', 'A𝄫', 'A♭', 'B𝄫', 'B♭', 'C♭']
    K   ^(?P<tonic>[A-Ga-g])(?P<accidental>[#♯b!♭]?):(?P<scale>(maj|min)(or)?)$Improper key format: {:s}r   r   r   r   r   r   r   CDEFGABr   #r5   b!   ♯   ♭tonic
accidentalscaleNr   majr#   FTr   rG   u   C♯rH   u   D♯rI   rJ   u   F♯rK   u   G♯rL   u   A♯rM   u   D♭u   E♭u   G♭u   A♭u   B♭)r   u   E♯)r   u   B♯)r   u   F𝄪)r   u   C𝄪)r   u   G𝄪)r   u   D𝄪)r   u   A𝄪)r   u   C♭)r   u   F♭)r   u   B𝄫)r   u   E𝄫)r   u   A𝄫)r   u   D𝄫rP   z##rQ   Zbb)rS   u   𝄪rT   u   𝄫c                 3   s   | ]}|  V  qd S )N)r9   )r   nZtranslationsr   r   	<genexpr>z  s     zkey_to_notes.<locals>.<genexpr>)rematchr   r*   groupr1   r   ranger(   r:   rA   )keyr4   r]   	pitch_mapacc_maprU   rV   offsetrW   majorZtonic_numberZ
use_sharpsZnotes_sharpZ
notes_flatZsharp_correctionsZflat_correctionsZn_sharpsrY   r/   namenotesZn_flatsr   rZ   r   r     st    M
	
c           	      C   s   t tdddddddgtdddddd	d
gd}td| }|sRtd| dddddddd}ddddddd}|d }|d}|| }|ddd 	 }|| ||  | d S )u  Construct the diatonic scale degrees for a given key.

    Parameters
    ----------
    key : str
        Must be in the form TONIC:key.  Tonic must be upper case (``CDEFGAB``),
        key must be lower-case (``maj`` or ``min``).

        Single accidentals (``b!♭`` for flat, or ``#♯`` for sharp) are supported.

        Examples: ``C:maj, Db:min, A♭:min``.

    Returns
    -------
    degrees : np.ndarray
        An array containing the semitone numbers (0=C, 1=C#, ... 11=B)
        for each of the seven scale degrees in the given key, starting
        from the tonic.

    See Also
    --------
    key_to_notes

    Examples
    --------
    >>> librosa.key_to_degrees('C:maj')
    array([ 0,  2,  4,  5,  7,  9, 11])

    >>> librosa.key_to_degrees('C#:maj')
    array([ 1,  3,  5,  6,  8, 10,  0])

    >>> librosa.key_to_degrees('A:min')
    array([ 9, 11,  0,  2,  4,  5,  7])

    r   r   r   r   r   r   r   r   r   r   )rX   minrD   rE   rF   r   rN   rO   rU   rV   rW   Nr#   )
dictr   r-   r\   r]   r   r*   r^   r1   r   )	r`   rf   r]   ra   rb   rU   rV   rc   rW   r   r   r   r     s"    $ 
)__doc__r\   Znumpyr   _cacher   Zutil.exceptionsr   Zutil.decoratorsr   __all__rh   r   	enumerater)   r
   r   r	   r   r   r   r   r   r   r   r   <module>   s   
JRc ! 3