
    rhZ                       % S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSK	r	SSK
Jr  SSKJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  \R0                  (       a  SSKJr  \R4                  " S5      rSrSrSrSrSr Sr! " S S\RD                  5      r# " S S\RH                  5      r% " S S\RD                  5      r& " S S\RH                  5      r' " S  S!\	RP                  5      r) " S" S#\	RP                  5      r*\%\'/r+S$\,S%'   \-S&:X  a  SSKr\R\                  " \)5        gg)'z
Parses the de Clercq-Temperley popular music flavor of RomanText.
The Clercq-Temperley file format and additional rock corpus analysis
information may be located at https://rockcorpus.midside.com
    )annotationsN)OrderedDict)common)environment)exceptions21)key)metadata)meter)note)prebase)roman)stream)tie)chordzromanText.clercqTemperleyz
% Blitzkrieg Bop

BP: I | IV V | %THIS IS A COMMENT
In: $BP*3 I IV | I | $BP*3 I IV | I | R |*4 I |*4
Vr: $BP*3 I IV | I |
Br: IV | | I | IV I | IV | | ii | IV V |
Co: R |*4 I |*4
S: [A] $In $Vr $Vr $Br $Vr $Vr $Br $Vr $Vr $Co
a  
% Rock Around the Clock
% just a general comment
In: I | | | | | | V | |
Vr: I | | | | IVd7 | | I | | V7 | | I | | %a comment on verse
Vrf: I | | | | IVd7 | | I | | V7 | | I | IV iv | V | . I |
S: [A] $In $Vr $Vr $Vr $Vr $Vr $Vr $Vrf    % 3rd and 6th verses are instrumental
zj
% Simple Gifts
% A wonderful shaker melody
Vr: I | I | %incomplete verse
S: [A] $Vr % Not quite finished!z
% A Change is Gonna Come
Vr: I | | ii7 | vi | I | ii7 . IV V/vi | vi | I |
Br: ii7 | I | ii7 | vi | ii7 | vi | V7/V | V7 |
In: I V6 vi I64 | ii65 V43/ii ii vi6 | bVIId7 . VId7 . | V |
S: [Bb] [12/8] $In $Vr $Vr $Vr $Br $Vr I |
z
% Brown-Eyed Girl

VP: I | IV | I | V |
In: $VP*2
Vr: $VP*4 IV | V | I | vi | IV | V | I | V |       % Second half could be called chorus
Ch: V | | $VP*2 I |*4
Ch2: V | | $VP*3     % Fadeout
S: [G] $In $Vr $Vr $Ch $VP $Vr $Ch2
a  
% Ring Of Fire

In: [3/4] I . IV | [4/4] I | [3/4] . . V7 | [4/4] I |
Vr: I . . IV | [3/4] I . IV | [4/4] I | . . . V | [3/4] I . V | [4/4] I | I . . IV | [3/4] I . IV | [4/4] I | [3/4] . . V | [4/4] I |
Vr2: I . . IV | [3/4] I . IV | [4/4] I | . . . V | [3/4] I . V | [4/4] I | I . IV I | . . . IV | I | . . . V | I | % Or (alternate barring) | [3/4] I . IV | [2/4] I | [3/4] . . IV | [4/4] I | . . . V | I |
Ch: V | IV I | V | IV I | [2/4] | [4/4] . . . V | I . . V | I |       % Or the 2/4 measure could be one measure later
Fadeout: I . . V | I . . V | I . . V |
Co: [2/4] I | [4/4] . . . V | I . . V | $Fadeout
S: [G] $In $Vr $Ch $In*2 $Ch $Vr2 $Ch $Ch $Co
c                      \ rS rSrSrg)CTSongExceptionk    N__name__
__module____qualname____firstlineno____static_attributes__r       [/home/james-whalen/.local/lib/python3.13/site-packages/music21/romanText/clercqTemperley.pyr   r   k       r   r   c                      \ rS rSr% Sr/ SQrSS0rS\S'   SSS jjrS	 r	SS
 jr
\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rSSS jjrSSS jjrSrg)CTSongo   a  
This parser is an object-oriented approach to parsing clercqTemperley text files into music.
It is an advanced method.  Most people should just run:

>>> #_DOCS_SHOW p = converter.parse('clercqTemperley/dt/BrownEyedGirl.cttxt')

or if the file ends in .txt then give the format explicitly as either 'clerqTemperley'
or 'cttxt':

>>> #_DOCS_SHOW p = converter.parse('BrownEyedGirl.txt', format='clercqTemperley')

Advanced: if you want access to a CTSong object itself (for manipulating the input before
converting to a string, etc. then create a CTSong object with one of the following inputs:

1. by passing in the string, with newline characters (\\n) at the end of each line

2. by passing in the filename as a string or path, and have Python
   open the file and read the text

Given this file, you could create a CTSong object with:

>>> exampleClercqTemperley = '''
... % Brown-Eyed Girl
... VP: I | IV | I | V |
... In: $VP*2
... Vr: $VP*4 IV | V | I | vi | IV | V | I | V | % Second half could be called chorus
... Ch: V | | $VP*2 I |*4
... Ch2: V | | $VP*3     % Fadeout
... S: [G] $In $Vr $Vr $Ch $VP $Vr $Ch2
... '''

>>> exCT = romanText.clercqTemperley.exampleClercqTemperley  #_DOCS_HIDE
>>> s = romanText.clercqTemperley.CTSong(exCT)  #_DOCS_HIDE

Or:

>>> #_DOCS_SHOW s = romanText.clercqTemperley.CTSong('C:/Brown-Eyed_Girl.txt')

When you call the .toPart() method on the newly created CTSong object,
the code extracts meaningful properties (such as title, text, comments,
year, rules, home time Signature, and home Key Signature) from the text file
and returns a new Part object.  It also makes these properties available on the
CTSong object.

The toPart() method has two optional labeling parameters, labelRomanNumerals and
labelSubsectionsOnScore. Both are set to True by default. Thus, the created score
will have labels (on the chord's lyric) for each roman numeral as well as for each
section in the song (LHS). In case of a recursive definition (a rule contains a reference
to another rule), both labels are printed, with the deepest
reference on the smallest lyric line.

>>> p = s.toPart()
>>> #_DOCS_SHOW p.show()

.. image:: images/ClercqTemperleyExbrown-eyed_girl.png
   :width: 500

>>> firstRN = p[roman.RomanNumeral][0]
>>> firstRN.lyric
'I\nVP\nIn'

All roman numerals mark which formal division they are in:

>>> 'formalDivision' in firstRN.editorial
True
>>> firstRN.editorial.formalDivision
['VP', 'In']

The second RomanNumeral is at the start of no formal divisions

>>> secondRN = p[roman.RomanNumeral][1]
>>> secondRN.lyric
'IV'
>>> secondRN.editorial.formalDivision
[]

>>> s.title
'Brown-Eyed Girl'

>>> s.homeTimeSig
<music21.meter.TimeSignature 4/4>

>>> s.homeKey
<music21.key.Key of G major>

>>> s.comments
[['Vr:', 'Second half could be called chorus'], ['Ch2:', 'Fadeout']]

Year is not defined as part of the Clercq-Temperley format, but it will be helpful
to have it as a property. So let's assign a year to this song:

>>> s.year = 1967
>>> s.year
1967

Upon calling toPart(), CTRule objects are also created. CTRule objects are
the individual rules that make up the song object. For example,

>>> s.rules
OrderedDict([('VP', <music21.romanText.clercqTemperley.CTRule
                     text='VP: I | IV | I | V |'>),
             ('In', <music21.romanText.clercqTemperley.CTRule text='In: $VP*2'>),
             ('Vr', <music21.romanText.clercqTemperley.CTRule
                     text='Vr: $VP*4 IV | V | I | vi | IV | V | I | V |
                                 % Second half could be called chorus'>),
             ('Ch', <music21.romanText.clercqTemperley.CTRule
                     text='Ch: V | | $VP*2 I |*4'>),
             ('Ch2', <music21.romanText.clercqTemperley.CTRule
                     text='Ch2: V | | $VP*3     % Fadeout'>),
             ('S', <music21.romanText.clercqTemperley.CTRule
                     text='S: [G] $In $Vr $Vr $Ch $VP $Vr $Ch2'>)])

The parser extracts meaningful properties to each rule, such as sectionName,
home time signature of that rule, home key of that rule, and of course the individual
stream from the song corresponding to the rule.

The following examples display the instantiated properties of the second rule (list indexes
start at one) as created above.

>>> rule = s.rules['In']
>>> rule.text
'In: $VP*2'

>>> rule.sectionName
'Introduction'


With this object-oriented approach to parsing the clercq-temperley text file format,
we now have the ability to analyze a large corpus (200 files) of popular music
using the full suite of harmonic tools of music21. We can not only analyze each
song as a whole, as presented in Clercq and Temperley's research, but we can also analyze each
individual section (or rule) of a song. This may provide interesting insight
into popular music beyond our current understanding.

Examples used throughout this class utilize the following Clercq-Temperley text file

>>> BlitzkriegBopCT = '''
... % Blitzkrieg Bop
... BP: I | IV V | %THIS IS A COMMENT
... In: $BP*3 I IV | I | $BP*3 I IV | I | R |*4 I |*4
... Vr: $BP*3 I IV | I |
... Br: IV | | I | IV I | IV | | ii | IV V |
... Co: R |*4 I |*4
... S: [A] $In $Vr $Vr $Br $Vr $Vr $Br $Vr $Vr $Co
... '''

Another example using a different Clercq-Temperley file

RockClockCT =
% Rock Around the Clock
% just a general comment
In: I | | | | | | V | |
Vr: I | | | | IVd7 | | I | | V7 | | I | | %a comment on verse
Vrf: I | | | | IVd7 | | I | | V7 | | I | IV iv | V | . I |
S: [A] $In $Vr $Vr $Vr $Vr $Vr $Vr $Vrf    % 3rd and 6th verses are instrumental

>>> s = romanText.clercqTemperley.CTSong(romanText.clercqTemperley.RockClockCT)
>>> part = s.toPart()
>>> part.highestTime
376.0

>>> s.title
'Rock Around the Clock'

>>> s.homeTimeSig
<music21.meter.TimeSignature 4/4>

>>> s.homeKey
<music21.key.Key of A major>

>>> s.comments
[['just a general comment'],
 ['Vr:', 'a comment on verse'],
 ['S:', '3rd and 6th verses are instrumental']]

>>> s.year = 1952
>>> s.year
1952

>>> s.rules
OrderedDict([('In', <music21.romanText.clercqTemperley.CTRule
                        text='In: I | | | | | | V | |'>),
             ('Vr', <music21.romanText.clercqTemperley.CTRule
                     text='Vr: I | | | | IVd7 | | I | | V7 | | I | | %a comment on verse'>),
             ('Vrf', <music21.romanText.clercqTemperley.CTRule
                     text='Vrf: I | | | | IVd7 | | I | | V7 | | I | IV iv | V | . I |'>),
             ('S', <music21.romanText.clercqTemperley.CTRule
                     text='S: [A] $In $Vr $Vr $Vr $Vr $Vr $Vr $Vrf
                                 % 3rd and 6th verses are instrumental'>)])


>>> rule = s.rules['In']
>>> rule.text
'In: I | | | | | | V | |'

>>> rule.sectionName
'Introduction'

OMIT_FROM_DOCS

one more example, which is the bane of this parser's existence::

    % Ring Of Fire

    In: [3/4] I . IV | [4/4] I | [3/4] . . V7 | [4/4] I |
    Vr: I . . IV | [3/4] I . IV | [4/4] I | . . . V | [3/4] I . V | [4/4] I |
                I . . IV | [3/4] I . IV | [4/4] I | [3/4] . . V | [4/4] I |
    Vr2: I . . IV | [3/4] I . IV | [4/4] I | . . . V | [3/4] I . V | [4/4] I |
                I . IV I | . . . IV | I | . . . V | I |
                % Or (alternate barring) | [3/4] I . IV | [2/4] I |
                [3/4] . . IV | [4/4] I | . . . V | I |
    Ch: V | IV I | V | IV I | [2/4] | [4/4] . . . V |
                I . . V | I |       % Or the 2/4 measure could be one measure later
    Fadeout: I . . V | I . . V | I . . V |
    Co: [2/4] I | [4/4] . . . V | I . . V | $Fadeout
    S: [G] $In $Vr $Ch $In*2 $Ch $Vr2 $Ch $Ch $Co


)texttoParttitlehomeTimeSighomeKeycommentsrulesyearzr
            The year of the CTSong; not formally defined
            by the Clercq-Temperley format.
            dict[str, str]	_DOC_ATTRc                J   S U l         SU l        / U l        [        5       U l        / U l        / U l        [        R                  " 5       U l	        S U l
        S U l        S U l        SU l        SU l        U H  nUS:X  a  X0l         US:X  d  M  X0l
        M     U R                  U5        g )N Tr#   r(   )_titler!   linesr   _rules
keyObjListtsListr   Part_partObjr(   _homeTimeSig_homeKeylabelRomanNumeralslabelSubsectionsOnScoreparse)selftextFilekeywordskws       r   __init__CTSong.__init__T  s    	 "
)4)+13	 "&'+$BW} V|		  	

8r   c                <    SU R                   < SU R                   3$ )Nztitle=z year=)r#   r(   r9   s    r   _reprInternalCTSong._reprInternalp  s    

~VDII;77r   c                   [        U[        5      (       a  SU;   a  SU;   a  UR                  S5      nO1 [        R                  " USSSS9 nUR                  5       nSSS5        W Vs/ s H$  n[        U5      S:w  d  M  UR                  5       PM&     nnX l        SR                  U5      nXPl        g! , (       d  f       N]= f! [         a    [        S	U 35      e[         a    [        S
U 35      ef = fs  snf )a  
Called when a CTSong is created by passing a string or filename;
in the second case, it opens the file
and removes all blank lines, and adds in new line characters
returns pieceString that CTSong can call .expand() on.

>>> exCT = romanText.clercqTemperley.exampleClercqTemperley

This calls parse implicitly:

>>> s = romanText.clercqTemperley.CTSong(exCT)

>>> print(s.text)
% Brown-Eyed Girl
VP: I | IV | I | V |
In: $VP*2
Vr: $VP*4 IV | V | I | vi | IV | V | I | V |       % Second half could be called chorus
Ch: V | | $VP*2 I |*4
Ch2: V | | $VP*3     % Fadeout
S: [G] $In $Vr $Vr $Ch $VP $Vr $Ch2

>>> s.lines[0]
'% Brown-Eyed Girl'

>>> s.lines[-1]
'S: [G] $In $Vr $Vr $Ch $VP $Vr $Ch2'
|S:
rzutf-8replace)encodingerrorsNzCannot find file: z2Invalid File Format; must be string or text file: r   )
isinstancestrsplitioopen	readlinesFileNotFoundErrorr   	Exceptionlenstripr.   joinr!   )r9   r:   r.   
fileOpenedepieceStrings         r   r8   CTSong.parset  s    8 h$$TX=MNN4(EUWWXsWYOS]&002E P %*9EqSVq[E9
ii&	 PO$ G%(:8*&EFF U%H
SU UU :s5   C B4C )C8 C84
C>C C 0C5c                    U R                   S;  a  U R                   $ U R                  S   nUR                  SS5      R                  5       nX l         U$ )at  
Get or set the title of the CTSong. If not specified
explicitly but the clercq-Temperley text exists,
this attribute searches first few lines of text file for title (a string preceded by a '%')
if found, sets title attribute to this string and returns this title)

>>> s = romanText.clercqTemperley.CTSong(romanText.clercqTemperley.textString)
>>> s.title
'Simple Gifts'
)Nr,   r   %r,   )r-   r.   rH   rT   )r9   liner#   s      r   r#   CTSong.title  sJ     ;;j(;;zz!}S"%++-r   c                   / nU R                   SS  H  nSU;   d  M  UR                  5       S   R                  S5      (       aH  UR                  UR                  5       S   X"R	                  S5      S-   S R                  5       /5        Mz  UR                  X"R	                  S5      S-   S R                  5       /5        M     U$ )a  
Get the comments list of all CTRule objects.

comments are stored as a list of comments, each comment on a line as a list. If the
comment is on a rule line, the list contains both the line's LHS (like "In:")
and the comment if the comment is on a line of its own, only the comment is
appended as a list of length one.

The title is not a comment. The title is stored under self.title

#_DOCS_HIDE Please note: the backslashes included in the file
#_DOCS_HIDE below are for sphinx documentation
#_DOCS_HIDE purposes only. They are not permitted in the clercq-temperley file format

    | textString = '''
    | %Simple Gifts
    | % A wonderful shaker melody
    | Vr: I \| I \| %incomplete verse
    | S: [A] $Vr % Not quite finished!'''

>>> s = romanText.clercqTemperley.CTSong(romanText.clercqTemperley.textString)
>>> s
<music21.romanText.clercqTemperley.CTSong title='Simple Gifts' year=None>
>>> s.comments
[['A wonderful shaker melody'], ['Vr:', 'incomplete verse'], ['S:', 'Not quite finished!']]
   Nr[   r   :)r.   rM   endswithappendindexrT   )r9   r&   r\   s      r   r&   CTSong.comments  s    8 JJqrNDd{::<?++C00OOTZZ\!_&*::c?Q+>+?&@&F&F&H%K L OOT**S/A*=*>%?%E%E%G$HI # r   c                $   U R                   (       a  U R                   $ U R                   HX  nUR                  5       nU(       d  M  US   R                  S5      (       d  M7  [	        XS9nX0R                   UR
                  '   MZ     U R                   $ )a  
Get the rules of a CTSong. the Rules is an OrderedDict of
objects of type CTRule. If only a text file
provided, this goes through text file and creates the
rule object out of each line containing
an LHS including the Song line, which should always be last.

>>> s = romanText.clercqTemperley.CTSong(romanText.clercqTemperley.BlitzkriegBopCT)
>>> len(s.rules)
6
>>> for rule in s.rules:
...   (rule, s.rules[rule])
('BP', <music21.romanText.clercqTemperley.CTRule
            text='BP: I | IV V | %THIS IS A COMMENT'>)
('In', <music21.romanText.clercqTemperley.CTRule
            text='In: $BP*3 I IV | I | $BP*3 I IV | I | R |*4 I |*4'>)
('Vr', <music21.romanText.clercqTemperley.CTRule
            text='Vr: $BP*3 I IV | I |'>)
('Br', <music21.romanText.clercqTemperley.CTRule
            text='Br: IV | | I | IV I | IV | | ii | IV V |'>)
('Co', <music21.romanText.clercqTemperley.CTRule
            text='Co: R |*4 I |*4'>)
('S', <music21.romanText.clercqTemperley.CTRule
            text='S: [A] $In $Vr $Vr $Br $Vr $Vr $Br $Vr $Vr $Co'>)

Rules S is where we begin:

>>> s.rules['S']
<music21.romanText.clercqTemperley.CTRule
    text='S: [A] $In $Vr $Vr $Br $Vr $Vr $Br $Vr $Vr $Co'>
r   r`   )parent)r/   r.   rM   ra   CTRuleLHS)r9   r\   lsrules       r   r'   CTSong.rules  sl    D ;;;;JJDBrbennS))d0(,DHH%	  {{r   c                   U R                   (       a  U R                   $ U R                  (       a  SU R                  ;   a  U R                  R                  S5      nU H  nUR                  S5      (       d  M  UR                  5       SS  Hh  nSU;  a+  [        R
                  " S5      U l         U R                   s  s  $ SU;   a.  [        R
                  " USS 5      U l         U R                   s  s  $ Mj     M     U R                   $ )	a]  
gets the initial, or 'home', time signature in a song by looking at the 'S' substring
and returning the provided time signature. If not present, returns a default music21
time signature of 4/4

>>> s = romanText.clercqTemperley.CTSong(romanText.clercqTemperley.textString)
>>> s.homeTimeSig
<music21.meter.TimeSignature 4/4>

>>> change = romanText.clercqTemperley.CTSong(romanText.clercqTemperley.changeIsGonnaCome)
>>> change.homeTimeSig
<music21.meter.TimeSignature 12/8>
>>> change.homeTimeSig.beatSequence
<music21.meter.core.MeterSequence {{1/8+1/8+1/8}+{1/8+1/8+1/8}+{1/8+1/8+1/8}+{1/8+1/8+1/8}}>
rE   rF   r_      [4/4/)r4   r!   rM   
startswithr
   TimeSignature)r9   r.   r\   atoms       r   r$   CTSong.homeTimeSig	  s    " $$$ 99*IIOOD)E??4(( $

Qq 1d?050C0CE0JD-#'#4#44 D[050C0CD2J0OD-#'#4#44  !2     r   c                8   U R                   (       a  U R                   $ U R                  (       a  SU R                  ;   a  U R                  R                  S5      nU H  nUR                  S5      (       d  M  UR                  5       SS  H~  nSU;  a+  [        R
                  " S5      U l         U R                   s  s  $ SU;  aD  [        R                  " USS 5      n[        R
                  " U5      U l         U R                   s  s  $ M     M     U R                   $ )	z
gets the initial, or 'home', Key by looking at the music text and locating
the key signature at the start of the S: rule.

>>> s = romanText.clercqTemperley.CTSong(romanText.clercqTemperley.textString)
>>> s.homeKey
<music21.key.Key of A major>
rE   rF   r_   rm   rn   Crp   rq   )r5   r!   rM   rr   r   Key"convertKeyStringToMusic21KeyString)r9   r.   r\   rt   	m21keyStrs        r   r%   CTSong.homeKey-  s     ====  99*IIOOD)E??4(( $

Qq 1d?,/GGCLDM#'==0 _(+(N(NtTUVXz(ZI,/GGI,>DM#'==0  !2  }}r   c                   Xl         X l        U R                  [        R                     R                  5       (       a  U R                  $ [        R                  " 5       nU R                  S   nUR                  5       n[        U5       H  u  pgUS-   Ul
        M     UR                  U5        UR                  S[        R                  " 5       5        U R                  UR                  l        X0l        U$ )a  
creates a Part object out of a from CTSong and also creates CTRule objects in the process,
filling their .streamFromCTSong attribute with the corresponding smaller inner stream.
Individual attributes of a rule are defined by the entire CTSong, such as
meter and time signature, so creation of CTRule objects typically occurs
only from this method and directly from the clercqTemperley text.

>>> s = romanText.clercqTemperley.CTSong(romanText.clercqTemperley.BlitzkriegBopCT)
>>> partObj = s.toPart()
>>> partObj.highestOffset
380.0
Sr_   r   )r6   r7   r3   r   Measurefirstr2   r'   expand	enumeratenumberrb   insertr	   Metadatar#   )r9   r6   r7   partObj	startRulemeasuresims           r   r"   CTSong.toPartK  s     #5'>$==(..00== ++-JJsO	##%h'DA1uAH (x q(++-.!%r   c                "    U R                  UUS9$ )zI
DEPRECATED: use .toPart() instead.  This method will be removed in v.10
)r6   r7   )r"   )r9   r6   r7   s      r   toScoreCTSong.toScorej  s"     {{.@3J  L 	Lr   )r5   r4   r3   r/   r-   r0   r6   r7   r.   r!   r1   r(   N)r,   )r:   zstr | pathlib.Path)TT)returnzstream.Part)r   r   r   r   __doc__
_DOC_ORDERr*   __annotations__r=   rA   r8   propertyr#   r&   r'   r$   r%   r"   r   r   r   r   r   r   r   o   s    Zv \J !I~ 88, \  & # #J * *X !! !!F  :>L Lr   r   c                      \ rS rSrSrg)CTRuleExceptioniq  r   Nr   r   r   r   r   r   q  r   r   r   c                     \ rS rSr% Sr/ SQrSS0rS\S'   \R                  " S5      r
\R                  " S	5      rS"S#S jjrS rS rS r\" \\SS9r  S$     S%S jjr      S&S jr      S&S jrS'S jrS(S jr      S)S jr        S*S jrS+S jrS,S jrS r\" \\SS9r\S-S j5       rS.S jrS,S jr\" \\SS9r \S  5       r!S!r"g
)/rg   iu  a  
CTRule objects correspond to the individual lines defined in a
:class:`~music21.romanText.clercqTemperley.CTSong` object. They are typically
created by the parser after a CTSong object has been created and the .toPart() method
has been called on that object. The usefulness of each CTRule object is that each
has a :meth:`~music21.romanText.clercqTemperley.CTRUle.streamFromCTSong` attribute,
which is the stream from the entire score that the rule corresponds to.

To parse, put the text into the
)rh   sectionName	musicTextr$   r%   r&   r!   zR
            The full text of the CTRule, including the LHS, chords, and comments.r)   r*   z
(\|\*?\d*)z\*(\d+)Nc                   S U l         Ub  X l        U R                  (       a  U R                  R                  OS =(       d    [        R                  " S5      U l        U R                  (       a  U R                  R                  OS =(       d    [        R                  " S5      U l	        Xl
        SU l        SU l        / U l        SU l        S U l        SU l        g )Nro   rw   r,   F)_parentrf   r$   r
   rs   tsr%   r   rx   keyObjr!   
_musicText_LHSr   lastRegularAtom	lastChord_lastChordIsInSameMeasure)r9   r!   rf   s      r   r=   CTRule.__init__  s     K.2kk4;;**tbH[H[\aHb.2kkt{{**tTPS		.0$&+//4&r   c                "    SU R                   < 3$ )Nztext=)r!   r@   s    r   rA   CTRule._reprInternal  s    tyym$$r   c                B    [         R                  " U R                  5      $ N)r   unwrapWeakrefr   r@   s    r   
_getParentCTRule._getParent  s    ##DLL11r   c                :    [         R                  " U5      U l        g r   )r   wrapWeakrefr   )r9   rf   s     r   
_setParentCTRule._setParent  s    ))&1r   zE
    A reference to the CTSong object housing the CTRule if any.
    )docc           	        U R                   nU R                  nU(       a  Xl         U(       a  X l        U R                  R                  5         U R	                  5        Hc  u  pVnUS:X  a  U R                  XW5        M  US:X  a  U R                  XW5        M8  [        R                  SU< SU< SU R                  < 35        Me     U R                  (       a  U R                   H  nUR                  5       R                  n	U	(       d  M&  U R                  b  U R                  R                  SL d  MN  U R                  S:w  d  M`  U	S   n
[        U
R                   5      S	-   nU
R                   R#                  [$        R&                  " U R                  US
95        U
R(                  R*                  R#                  U R                  5          O   X0l         X@l        U R                  $ )z
The meat of it all -- expand one rule completely and return a list of Measure objects.

Parses within the local time signature context and key context.
$rD   z%Rule found without | or $, ignoring: ,z: in Tr}   r   r_   r   )r   r   r   clear_measureGroupsexpandExpansionContentexpandSimpleContentenvironLocalwarnr!   recursenotesrf   r7   rh   rS   lyricsrb   r   Lyric	editorialformalDivision)r9   	tsContext
keyContextsaveTssaveKeycontentsepnumRepsr   noteIterrnlyricNums               r   r   CTRule.expand  sj    ++G$K%)%8%8%:!G'cz++G=((:!!;G;aweTXT]T]S`ac &; ==]]99;,,H![[0 $ C Ct K HHO!!B"299~1HII$$TZZ%JKLL//66txx@ # }}r   c                   U R                   (       a  XR                   R                  ;  a  [        SU SU  35      eU R                   R                  U   n[        U5       H  nUR	                  U R
                  U R                  5      nU R                  US   U R
                  U R                  5        U Vs/ s H"  nUR                  [        R                  5      PM$     sn H8  nXpR
                  Ld  M  [        R                  " U R
                  5      U l        M:     U R                  R                  U5        M     gs  snf )z`
Expand a rule that contains an expansion (i.e., a $) in it.

Requires CTSong parent to be set.
zCannot expand rule z in r   N)rf   r'   r   ranger   r   r   
insertKsTsgetElementsByClassr
   rs   copydeepcopyr   extend)r9   r   r   rj   r   returnedMeasuresr   
returnedTss           r   r   CTRule.expandExpansionContent  s     {{g[[->->>!$7yTF"KLL{{  )wA#{{477DKK@OO,Q/$++F(8:(81  !33E4G4GH(8:
WW,"mmDGG4DG	: MM  !12  :s   7)D?c                   Sn[         R                  " 5       nUR                  5       n/ nU H  nUR                  S5      (       a|  USS nUS:X  a  [        R
                  " S5      U l        MA  SU;   a  [        R
                  " U5      U l        Md  [        R                  " [        R                  " U5      5      U l
        M  US:X  a<  U R                  (       d  [        S	U  35      eUR                  U R                  5        M  U(       d  M  UR                  U5        Xpl        M     [        U5      n	U	S
:X  a  g U R                  X@R                  U R                  5        [         R"                  " U R                  R$                  R&                  U	-  5      n
U GHX  nUS:X  aA  [(        R*                  " U
S9n/ UR,                  l        S U l        SnUR                  U5        MK  U R3                  U5      n[4        R6                  " XpR                  5      n/ UR,                  l        U R9                  XR0                  5      (       ag  U(       a`  [:        R<                  (       a  U R0                  c   eU R0                  R>                  =R&                  U
-  sl        URA                  5         GM  XR>                  l        U RC                  XR0                  5        Xl        SnUR                  U5        GM[     U RD                  R                  U5        [G        SU5       Hc  n[H        RJ                  " U5      nURM                  [        R
                  [        R                  /5        U RD                  R                  U5        Me     g )NFrn   r_   rq   0ro   rp   .z . w/o previous atom: r   R)quarterLengthT)'r   r~   rM   rr   r
   rs   r   r   rx   ry   r   r   r   rb   rS   r   r   opFracbarDurationr   r   Restr   r   r   fixupChordAtomr   RomanNumeralisSametTYPE_CHECKINGdurationcoreElementsChangedaddOptionalTieAndLyricsr   r   r   r   removeByClass)r9   r   r   lastChordIsInSameMeasurer   atomsregularAtomsrt   atomContentnumAtoms
atomLengthrestr   r   newMs                  r   r   CTRule.expandSimpleContent  s   
 $) NN"$Ds##"1Rj#%#11%8DG K'#11+>DG"%''#*P*PQ\*]"^DK++),B4&*IJJ##D$8$89##D)'+$+ , |$q=77DKK0]]477#6#6#D#Dx#OP
 Ds{yyz:02-!%+0(**40''kk:.0+;;r>>227O#~~999NN++99ZG9))+0:KK-00^^D%'N/3,HHRL+ !, 	Qq'"A==#D 3 3SWW=>MM  & #r   c                   / n/ n/ nU R                   R                  U R                  5      n[        S[	        U5      S5       HQ  nXE   R                  5       nUS-   [	        U5      :  a  XES-      nOSnUS:w  d  US:w  d  M?  UR                  Xg45        MS     U GHm  u  pgUR                  5       n/ n	U H  n
U
R                  S5      (       a  U	(       a%  UR                  SR                  U	5      SS45        / n	U R                  R                  U
5      nUb7  [        UR                  S5      5      nU R                  R                  SU
5      n
OSnUR                  U
SS SU45        M  U	R                  U
5        M     U R                  R                  U5      nUb7  [        UR                  S5      5      nU R                  R                  SU5      nOSnU	(       d	  US	:X  d  GMK  UR                  SR                  U	5      X}45        GMp     U Hq  u  pgnUR                  5       nUS	:X  a:  [        S
 U 5       5      (       a#  SR                  U5      nU(       a  US-  nUS-  nOUS:X  a  S	nUR                  XgU45        Ms     U$ )aM  
Returns a list of 3-tuples where each tuple consists of the
str content, either "|" (a normal measure ) or "$" (an expansion),
and the number of repetitions.  Comments are stripped.

>>> rs = ('In: [A] [4/4] $Vr $BP*3 I IV | I | ' +
...          '$BP*3 I IV | I | | R |*4 I |*4 % This is a comment')
>>> s = romanText.clercqTemperley.CTRule(rs)
>>> s._measureGroups()
[('[A] [4/4]', '|', 1),
 ('Vr', '$', 1), ('BP', '$', 3), ('I IV', '|', 1), ('I', '|', 1),
 ('BP', '$', 3), ('I IV', '|', 1), ('I', '|', 1), ('.', '|', 1),
 ('R', '|', 4), ('I', '|', 4)]

>>> r = romanText.clercqTemperley.CTRule('In: $IP*3 I | | | $BP*2')
>>> r._measureGroups()
[('IP', '$', 3), ('I', '|', 1), ('.', '|', 1), ('.', '|', 1), ('BP', '$', 2)]

>>> r = romanText.clercqTemperley.CTRule('In: [4/4] I V | | | IV |')
>>> r._measureGroups()
[('[4/4] I V', '|', 1), ('.', '|', 1), ('.', '|', 1), ('IV', '|', 1)]
>>> measures = r.expand()
>>> measures[2].show('text')
{0.0} <music21.key.Key of C major>
{0.0} <music21.meter.TimeSignature 4/4>
{0.0} <music21.roman.RomanNumeral V in C major>

>>> r = romanText.clercqTemperley.CTRule('Vr: [4/4] bVII | IV | | [2/4] |')
>>> r._measureGroups()
[('[4/4] bVII', '|', 1), ('IV', '|', 1), ('.', '|', 1), ('[2/4] .', '|', 1)]
>>> measures = r.expand()
>>> measures[2].show('text')
{0.0} <music21.key.Key of C major>
{0.0} <music21.meter.TimeSignature 4/4>
{0.0} <music21.roman.RomanNumeral IV in C major>
>>> measures[3].show('text')
{0.0} <music21.key.Key of C major>
{0.0} <music21.meter.TimeSignature 2/4>
{0.0} <music21.roman.RomanNumeral IV in C major>
>>> measures[3][-1].quarterLength
2.0
r      r_   r,   r    ?NrD   c              3  Z   #    U  H!  oR                  S 5      =(       d    US:H  v   M#     g7f)rn   r,   N)rr   ).0ys     r   	<genexpr>(CTRule._measureGroups.<locals>.<genexpr>  s#     !U1,,s"3">qBw">s   )+r   )SPLITMEASURESrM   r   r   rS   rT   rb   rr   rU   
REPETITIONsearchintgroupsuball)r9   measureGroups1measureGroups2measureGroups3measureGroupTempr   r   r   contentList
contentOutrt   repetitions
expandRepsr   contentSplits                  r   r   CTRule._measureGroups5  sX   V 135757&*&8&8&>&>t~~&Nq#./3A&)//1G1us+,,&1u-"}r	%%wn5 4 +LG!--/K$&J#??3''!&--sxx
/CS!.LM%'
"&//"8"8">K".%():):1)=%>
#222t<%&
"))48S**EF%%d+ $" //005K&k//23oo))"c2SCZ%%sxx
';S&JK; +@ &4!G'"==?Lczc!U!UUU((<0sNG3!!7"9: &4 r   c                    Uc  SnU$ UR                    Vs/ s H  oDR                  PM     nnUR                    Vs/ s H  oDR                  PM     nnXV:X  a  SnU$ SnU$ s  snf s  snf )z
Returns True if the pitches of the RomanNumeral are the same as the pitches
of lastChord.  Returns False if lastChord is None.
FT)pitchesnameWithOctave)r9   r   r   sameprnPlcPs          r   r   CTRule.isSame  sy    
 D  .0ZZ8Z##ZC8-6->->?->##->C?z   9?s
   AA$c                X   U R                  X5      nUSL a!  Ub  UR                  b  SUR                  l        USL a^  U R                  b  U R                  R                  SL a8  UR
                  R                  [        R                  " UR                  SS95        USL aG  UbD  UR                  c7  [        R                  " S5      Ul        [        R                  " S5      Ul        gUSL a?  Ub;  UR                  b-  SUR                  l        [        R                  " S5      Ul        gggg)	zL
Adds ties to chords that are the same.  Adds lyrics to chords that change.
FNstopTr_   r   startcontinue)r   r   typerf   r6   r   rb   r   r   figureTie)r9   r   r   r
  s       r   r   CTRule.addOptionalTieAndLyrics  s     {{2)5=Y2y}}7P!'IMM5=dkk1T[[5S5SW[5[IITZZ		!<=4<I1imm6KGGG,IMWWV_BFT\i3	8Q!+IMMWWV_BF 9R3\r   c                J   U R                   c  X!l        X1l        gX R                   R                  ;  a+  X!l        U R                   R                  R	                  U5        X0R                   R
                  ;  a,  X1l        U R                   R
                  R	                  U5        gg)z
Insert a new time signature or Key into measure m, if it's
not already in the stream somewhere.

Note that the name "ks" is slightly misnamed.  It requires a Key,
not KeySignature object.
N)rf   timeSignaturekeySignaturer1   rb   r0   )r9   r   r   r   s       r   r   CTRule.insertKsTs  sz     ;; O#N[[''' OKK%%b)///#NKK""))&1 0r   c                    SU;   a  UR                  SS5      nSU;   a  UR                  SS5      nUS   R                  5       (       a  SU;   a  UR                  SS5      nU$ )z
changes some CT values into music21 values

>>> s = romanText.clercqTemperley.CTRule()
>>> s.fixupChordAtom('iix')
'iio'
>>> s.fixupChordAtom('viih7')
'vii/o7'
>>> s.fixupChordAtom('iia')
'ii+'

xohz/or   a+)rH   islower)r9   rt   s     r   r   CTRule.fixupChordAtom  s^     $;<<S)D$;<<T*D7??<<S)Dr   c                $    [        U5      U l        g r   )rL   r   r9   values     r   _setMusicTextCTRule._setMusicText  s    e*r   c                $   U R                   (       a  U R                   $ U R                  (       d  gU R                  [        U R                  5      S-   S  nSU;   a  UR	                  S5      nUSU nUR                  5       U l         U R                   $ )Nr,   r_   r[   r   )r   r!   rS   rh   rc   rT   )r9   r!   commentStartIndexs      r   _getMusicTextCTRule._getMusicText  st    ????"yyyyTXX*+,$; $

3+,D**,r   a  
        Gets just the music text of the CTRule, excluding the left hand side and comments

        >>> rs = 'In: $BP*3 I IV | I | $BP*3 I IV | I | R |*4 I |*4 % This is a comment'
        >>> s = romanText.clercqTemperley.CTRule(rs)
        >>> s.text
        'In: $BP*3 I IV | I | $BP*3 I IV | I | R |*4 I |*4 % This is a comment'
        >>> s.musicText
        '$BP*3 I IV | I | $BP*3 I IV | I | R |*4 I |*4'
        c                    SU R                   ;   a9  U R                   U R                   R                  S5      S-   S R                  5       $ g)z
Get the comment of a CTRule object.

>>> rs = 'In: $BP*3 I IV | I | $BP*3 I IV | I | R |*4 I |*4 % This is a comment'
>>> s = romanText.clercqTemperley.CTRule(rs)
>>> s.comment
'This is a comment'
r[   r_   N)r!   rc   rT   r@   s    r   commentCTRule.comment  s@     $))99TYY__S1A567==??r   c                H   U R                   (       a  U R                   $ SnU R                  (       ar  U R                  R                  5       S   R                  S5      (       aA  U R                   H0  nUS:X  a#  UR	                  5       U l         U R                   s  $ X-   nM2     gg)Nr,   r   r`   )r   r!   rM   ra   rT   )r9   rh   chars      r   _getLHSCTRule._getLHS  sz    999999*1-66s;;		3; #		DI99$j	 " r   c                $    [        U5      U l        g r   )rL   r   r$  s     r   _setLHSCTRule._setLHS)  s    J	r   aZ  
        Get the LHS (Left Hand Side) of the CTRule.
        If not specified explicitly but CTtext present, searches
        first characters up until ':' for rule and returns string)

        >>> rs = 'In: $BP*3 I IV | R |*4 I |*4 % This is a comment'
        >>> s = romanText.clercqTemperley.CTRule(rs)
        >>> s.LHS
        'In'
        c                   SnSU R                   ;   a  SU R                   SS -   nU$ SU R                   ;   a  SU R                   SS -   nU$ SU R                   ;   a  S	U R                   SS -   nU$ S
U R                   ;   a  SU R                   SS -   nU$ SU R                   ;   a  SU R                   SS -   nU$ U R                   S:X  a  SU R                   SS -   nU$ U R                   S:X  a  SnU$ U R                   nU$ )a]  
Returns the expanded version of the Left-hand side (LHS) such as
Introduction, Verse, etc. if
text is present (uses LHS to expand)

Currently supported abbreviations:

* In: Introduction
* Br: Bridge
* Vr: Verse
* Ch: Chorus
* Fadeout: Fadeout
* S: Song

>>> s = romanText.clercqTemperley.CTRule('Vr2: $BP*3 I IV | I |')
>>> s.sectionName
'Verse2'
r,   InIntroductionr   NBrBridgeVrVerseChChorusTgTagr}   Songr_   Fadeout)rh   )r9   r   s     r   r   CTRule.sectionName7  s.   ( 488(488AB<7K  TXX"TXXab\1K  TXX!DHHQRL0K  TXX"TXXab\1K  TXX$((12,.K  XX_ 488AB</K
 	 XX"#K  ((Kr   )r   r   r   r   r   r   r   r   rf   r!   r   )r,   N)rf   zCTSong | None)NN)r   zmeter.TimeSignature | Noner   zkey.Key | Noner   zlist[stream.Measure])r   rL   r   r   r   None)r   zlist[tuple[str, str, int]])r   roman.RomanNumeralr   chord.Chord | Noner   bool)r   rE  r   rF  r   rD  )r   zstream.Measurer   zmeter.TimeSignaturer   zkey.Keyr   rD  )rt   rL   r   rL   )r%  rL   r   rD  )r   z
str | None)r   rL   )#r   r   r   r   r   r   r*   r   recompiler   r   r=   rA   r   r   r   rf   r   r   r   r   r   r   r   r   r&  r*  r   r-  r1  r4  rh   r   r   r   r   r   rg   rg   u  s   	 [J U!I~ 
 JJ}-MJ'J5$%22 j* 3 	F /3#'-+- !- 
	-^33 3 
	30B'B' B' 
	B'HdN %% $% 
	%*2$2*2 #2 (,2.,%  	< 	I    7G 	* 	C $ $r   rg   c                      \ rS rSrSrg)Testi`  r   Nr   r   r   r   rK  rK  `  r   r   rK  c                  $    \ rS rSrSrS rS rSrg)TestExternalid  Tc                    SSK Jn  UR                  [        5      nUR	                  5       nU R
                  (       a  UR                  5         g g )Nr   )clercqTemperley)music21.romanTextrO  r   BlitzkriegBopCTr"   show)r9   rO  sr   s       r   testBTestExternal.testBg  s5    5""?3((*99LLN r   c                    g r   r   r@   s    r   x_testATestExternal.x_testAn  s    r   r   N)r   r   r   r   rR  rT  rW  r   r   r   r   rM  rM  d  s    Dr   rM  z
list[type]r   __main__)/r   
__future__r   r   rN   pathlibrH  typingr   unittestcollectionsr   music21r   r   r   r   r	   r
   r   r   r   r   r   r   r   Environmentr   rQ  RockClockCT
textStringchangeIsGonnaComeexampleClercqTemperley
RingFireCTMusic21Exceptionr   ProtoM21Objectr   r   rg   TestCaserK  rM  r   r   r   mainTestr   r   r   <module>rj     s0  
 #  	  	   #            ??&&'BC	$
 	 
"	l33 	@LW## @LD	l33 	gW## gV	8 	8$$ \ !&)
J )zT r   