
    rh_                    F   S r SSKJ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\R                  5      r S     SS jjr S SS jjr " S S\R&                  5      r\S:X  a  SSKr\R,                  " \5        gg)zT
Writer for the 'RomanText' format (Tymoczko, Gotham, Cuthbert, & Ariza ISMIR 2019)
    )annotationsN)bar)base)metadata)meter)prebase)roman)	romanText)streamc                  V    \ rS rSrSr  S
S jr  SS jr  SS jrS r  SS jr	Sr
g	)RnWriter    a  
Extracts the relevant information from a source
(usually a :class:`~music21.stream.Stream` of
:class:`~music21.roman.RomanNumeral` objects) for
writing to text files in the 'RomanText' format (rntxt).

Writing rntxt is handled externally in the
:meth:`~music21.converter.subConverters.WriteRoman` so
most users will never need to call this class directly, only invoking
it indirectly through .write('rntxt').
Possible exceptions include users who want to convert Roman numerals into rntxt type
strings and want to work with those strings directly, without writing to disk.

For consistency with the
:meth:`~music21.base.Music21Object.show` and :meth:`~music21.base.Music21Object.write`
methods across music21, this class is theoretically callable on
any type of music21 object.
Most relevant use cases will involve calling a
stream containing one or more Roman numerals.
This class supports any such stream:
an :class:`~music21.stream.Opus` object of one or more scores,
a :class:`~music21.stream.Score` with or without :class:`~music21.stream.Part` (s),
a :class:`~music21.stream.Part`, or
a :class:`~music21.stream.Measure`.

>>> scoreBach = corpus.parse('bach/choraleAnalyses/riemenschneider004.rntxt')
>>> rnWriterFromScore = romanText.writeRoman.RnWriter(scoreBach)

The strings are stored in the RnWriter's combinedList variable, starting with the metadata:

>>> rnWriterFromScore.combinedList[0]
'Composer: J. S. Bach'

Composer and work metadata is inherited from score metadata wherever possible.
A composer entry will register directly as will any entries for
workTitle, movementNumber, and movementName
(see :meth:`~music21.romanText.writeRoman.RnWriter.prepTitle` for details).

>>> rnWriterFromScore.combinedList[0] == 'Composer: ' + rnWriterFromScore.composer
True

As always, these metadata entries are user-settable.
Make any adjustments to the metadata before calling this class.

After the metadata, the list continues with strings for each measure in order.
Here's the last in our example:

>>> rnWriterFromScore.combinedList[-1]
'm10 V6/V b2 V b3 I'

In the case of the score, the top part is assumed to contain the Roman numerals.
This is consistent with the parsing of rntxt which involves putting Roman numerals in a part
(the top, and only part) within a score.

In all other cases, the objects are iteratively inserted into larger streams until we end up
with a part object (e.g. measure > part).

>>> rn = roman.RomanNumeral('viio64', 'a')
>>> rnWriterFromRn = romanText.writeRoman.RnWriter(rn)
>>> rnWriterFromRn.combinedList[0]
'Composer: Composer unknown'

>>> rnWriterFromRn.combinedList[-1]
'm0 a: viio64'

OMIT_FROM_DOCS

Users can do these insertions themselves, but don't need to:

>>> m = stream.Measure()
>>> m.insert(0, rn)
>>> rnWriterFromMeasure = romanText.writeRoman.RnWriter(m)
>>> rnWriterFromMeasure.combinedList == rnWriterFromRn.combinedList
True

>>> p = stream.Part()
>>> p.insert(0, m)
>>> rnWriterFromPart = romanText.writeRoman.RnWriter(p)
>>> rnWriterFromPart.combinedList == rnWriterFromMeasure.combinedList
True

>>> s = stream.Score()
>>> s.insert(0, m)
>>> rnWriterFromScoreWithoutPart = romanText.writeRoman.RnWriter(p)
>>> rnWriterFromScoreWithoutPart.combinedList == rnWriterFromMeasure.combinedList
True
c                   SU l         SU l        SU l        SU l        / U l        U   [        U[        R                  5      (       Ga8  [        U[        R                  5      (       an  U Vs/ s H  n[        U5      PM     nnU HL  nUR                   H  nU R                  R                  U5        M      U R                  R                  S5        MN     g [        U[        R                  5      (       a+  UR                  R                  5       nUb  XPl        OXl        O[        U[        R                  5      (       a  Xl        Ol[        U[        R                   5      (       a7  [        R                  " 5       U l        U R                  R#                  SU5        OU R%                  U5      U l        UR&                  (       a  U R)                  UR&                  5        UR&                  R                   (       a  UR&                  R                   U l         UR&                  R                  (       a  UR&                  R                  U l        UR&                  R                  (       a  UR&                  R                  U l        OU R%                  U/5      U l        SU R                    3SU R                   3SU R                   3S	U R                   3S/U l        U R                  [*        R,                     (       d0  U R                  R#                  S[*        R,                  " S
5      5        SU l        U R1                  5         g s  snf )NzComposer unknownzTitle unknown 
r   z
Composer: zTitle: 	Analyst: Proofreader: z4/4)composertitleanalystproofreadercombinedList
isinstancer   StreamOpusr   appendScorepartsfirst	containerPartMeasureinsert_makeContainerr   	prepTitler   TimeSignaturecurrentKeyStringprepSequentialListOfLines)selfobjxconstituentElements
scoreOrSimps         V/home/james-whalen/.local/lib/python3.13/site-packages/music21/romanText/writeRoman.py__init__RnWriter.__init__y   sY    +$
')c6==))#v{{++<?&@Cqx{C#&@"5J'44))003 5%%,,T2 #6 C..IIOO%=%&N%(NC--!$C00!'%%a- "&!4!4S!9||s||,<<(($'LL$9$9DM<<''#&<<#7#7DL<<++'*||'?'?D$ "00#7DN)$--9&tzzl3(7,T-=-=,>?	! ~~e112NN!!!U%8%8%?@%'&&(] 'As   )L?c                    [         R                  " 5       nU H  nUR                  U5        M     [         R                  " 5       nUR	                  SU5        U$ )z
Makes a placeholder container for the unusual cases where this class is called on
generic- or non-stream object as opposed to
a :class:`~music21.stream.Score`, :class:`~music21.stream.Part`,
or :class:`~music21.stream.Measure`.
r   )r   r"   r   r!   r#   )r)   r*   mr+   r    s        r/   r$   RnWriter._makeContainer   sF     NNAHHQK KKM	A    c                   / nUR                   (       a  UR                  UR                   5        UR                  (       a  UR                  SUR                   S35        UR                  (       a5  UR                  UR                  :w  a  UR                  UR                  5        [        U5      S:  a  SR                  U5      U l        gg)aL  
Attempt to prepare a single work title from the score metadata looking at each of
the title, movementNumber and movementName attributes.
Failing that, a placeholder 'Title unknown' stands in.

>>> s = stream.Score()
>>> rnScore = romanText.writeRoman.RnWriter(s)
>>> rnScore.title
'Title unknown'

>>> s.insert(0, metadata.Metadata())
>>> s.metadata.title = 'Fake title'
>>> s.metadata.movementNumber = 123456789
>>> s.metadata.movementName = 'Fake movementName'
>>> rnScoreWithMD = romanText.writeRoman.RnWriter(s)
>>> rnScoreWithMD.title
'Fake title - No.123456789: Fake movementName'
z- No.:r    N)	bestTitler   movementNumbermovementNamer   lenjoin)r)   mdworkingTitles      r/   r%   RnWriter.prepTitle   s    * <<-%(9(9':! <=??"((*##BOO4|q ,/DJ !r5   c           	        U R                   R                  [        R                  5       GHG  nUR                  [        R
                  5      nU(       a}  US   nU R                  R                  SUR                   35        [        U5      S:  aA  USS  Vs/ s H  oDR                  PM     nnSU 3nU R                  R                  SU 35        [        UR                  5      nUR                  b  XqR                  -  nSn[        UR                  [        R                   5      (       a&  UR                  R"                  S:X  a  [%        US	S
US9nUR                  [&        R(                  5      n	U	 HS  n
U
R*                  b  U
R*                  R,                  S:X  d  M,  U R/                  U
5      n[%        UU
R0                  UUS9nMU     [        UR2                  [        R                   5      (       aY  UR2                  R"                  S:X  a?  U[&        R(                     R5                  5       nUc  S	nOUR0                  n[%        UUSUS9nU(       d  GM,  U R                  R                  U5        GMJ     gs  snf )a	  
Prepares a sequential list of text lines, with time signatures and Roman numerals
adding this to the (already prepared) metadata preamble ready for printing.

>>> p = stream.Part()
>>> m = stream.Measure(number=1)
>>> m.insert(0, meter.TimeSignature('4/4'))
>>> m.insert(0, roman.RomanNumeral('V', 'G'))
>>> p.insert(0, m)
>>> testCase = romanText.writeRoman.RnWriter(p)
>>> testCase.combinedList[-1]  # Last entry, after the metadata
'm1 G: V'

This follows the wider rntxt syntax in supporting
Time Signature (:class:`~music21.meter.TimeSignature`) changes and
Repeats marks (:class:`~music21.bar.Repeat`)
but only (currently) between measures.

Let's add a new measure to the stream we started,
with a time signature change beforehand and
both start and end repeats in it:

>>> m2 = stream.Measure(number=2)
>>> m2.insert(0, meter.TimeSignature('3/4'))
>>> m2.leftBarline = bar.Repeat(direction='start')
>>> m2.rightBarline = bar.Repeat(direction='end')
>>> m2.insert(0, roman.RomanNumeral('I', 'G'))
>>> p.insert(0, m2)
>>> testCase = romanText.writeRoman.RnWriter(p)

The last line of the `.combinedList` gives the new measure:

>>> testCase.combinedList[-1]
'm2 ||: I :||'

The line before that gives the time signature change:

>>> testCase.combinedList[-2]
'Time Signature: 3/4'

r   zTime Signature:    Nz.further time signature change(s) unprocessed: zNote: r   startg      ?z||:)measureNumberbeatchordStringinStringendz:||)r    getElementsByClassr   r"   r   r&   r   r   ratioStringr<   strrD   numberSuffixr   leftBarliner   Repeat	directionrnStringr	   RomanNumeraltietypegetChordStringrE   rightBarlinelast)r)   thisMeasuretsThisMeasurefirstTSr+   unprocessedTSsmsgmeasureNumberStringmeasureStringrnsThisMeasurernrF   last_rnrE   s                 r/   r(   "RnWriter.prepSequentialListOfLines   s)   V  >><<V^^LK (::5;N;NOM'*!!((+;G<O<O;P)QR}%)=J12=N%O=Nmm=NN%OJ>JZ[C%%,,vcU^<"%k&?&?"@''3#'?'??#M ;22CJJ??#//99WD (7J.15:2?!+ );;E<N<NON$66>RVV[[G%;"&"5"5b"9K$,;N24''9D6C%/M % ;33SZZ@@#00::eC &e&8&89>>@?D"<<D (7J.25:2?!+ }!!((7s M &Ps   I7c                    UR                   R                  R                  SS5      nX R                  :w  a  X l        U SUR                   3$ [        UR                  5      $ )a  
Produce a string from a Roman numeral with the chord and
the key if that key constitutes a change from the foregoing context.

>>> p = stream.Part()
>>> m = stream.Measure()
>>> m.insert(0, meter.TimeSignature('4/4'))
>>> m.insert(0, roman.RomanNumeral('V', 'G'))
>>> p.insert(0, m)
>>> testCase = romanText.writeRoman.RnWriter(p)
>>> sameKeyChord = testCase.getChordString(roman.RomanNumeral('I', 'G'))
>>> sameKeyChord
'I'

>>> changeKeyChord = testCase.getChordString(roman.RomanNumeral('V', 'D'))
>>> changeKeyChord
'D: V'
-bz: )keytonicPitchNameWithCasereplacer'   figurerK   )r)   r_   	keyStrings      r/   rT   RnWriter.getChordStringO  sU    * FF1199#sC	---$-![299+..ryy>!r5   )r   r   r   r    r'   r   r   N)r*   zbase.Music21Object)r*   zstream.Stream | list)r>   zmetadata.Metadata)r_   zroman.RomanNumeral)__name__
__module____qualname____firstlineno____doc__r0   r$   r%   r(   rT   __static_attributes__ r5   r/   r   r       sF    Vp;)(;)z. 0' 0Hd8L"-"r5   r   c                    U(       aJ  UR                  S5      S   SS nU[        U 5      :w  a#  SU  S3nUSU S3-  nUS	U  S
3-  n[        U5      eOSU  3n[        U5      nUS:X  a	  U SU 3nU$ U SU SU 3nU$ )a  
Creates or extends a string of RomanText such that the output corresponds to a single
measure line.

If the inString is not given, None, or an empty string then this function starts a new line.

>>> lineStarter = romanText.writeRoman.rnString(14, 1, 'G: I')
>>> lineStarter
'm14 G: I'

For any other inString, that string is the start of a measure line continued by the new values

>>> continuation = romanText.writeRoman.rnString(14, 2, 'viio6', 'm14 G: I')
>>> continuation
'm14 G: I b2 viio6'

Naturally, this function requires the measure number of any such continuation to match
that of the inString and raises an error where that is not the case.

As these examples show, the chordString can be a Roman numeral alone (e.g. 'viio6')
or one prefixed by a change of key ('G: I').

r8   r   rB   Nz&The current measureNumber is given as z, but zthe contextual inString (z) refers to zmeasure number z. They should match.r3   z b)splitrK   
ValueErrorintBeat)rD   rE   rF   rG   inStringMeasureNumberr[   bt	newStrings           r/   rP   rP   n  s    8  (s 3A 6qr : !C$66:=/PC.xjEEC_]O3GHHCS/!	 7 }o&	B	Qwj+/	   j2$a}5	r5   c                   [         [        [        [        R                  4n[        X5      (       d  [        SU  SU S35      e[        U [         [        R                  45      (       a  [        U 5      n U S:  a  SU  S3n[        U5      e[        U [        5      (       a  U $ [        U 5      U :X  a  [        U 5      $ [        X5      $ )a  
Converts beats to integers if possible, and otherwise to rounded decimals.
Accepts input as string, int, float, or fractions.Fraction.

>>> testInt = romanText.writeRoman.intBeat(1, roundValue=2)
>>> testInt
1

>>> testFrac = romanText.writeRoman.intBeat(8 / 3, roundValue=2)
>>> testFrac
2.67

>>> testStr = romanText.writeRoman.intBeat('0.666666666', roundValue=2)
>>> testStr
0.67

The roundValue sets the number of decimal places to round to. The default is two:

>>> testRound2 = romanText.writeRoman.intBeat(1.11111111, roundValue=2)
>>> testRound2
1.11

But this can be set to any integer:

>>> testRound1 = romanText.writeRoman.intBeat(1.11111111, roundValue=1)
>>> testRound1
1.1

Raises an error if called on a negative value.
zBeat, (currently z) must be one of .r   zBeat (currently z) must not be negative.)	rK   intfloat	fractionsFractionr   	TypeErrorrt   round)rE   
roundValueoptionsnegativeErrorMessages       r/   ru   ru     s    B C	 2 23Gd$$+D61B7)1MNN$i00122T{ ax!1$7NO-..$ 4yD4yT&&r5   c                  <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)Testi  z
Tests for two analysis cases (the smallest rntxt files in the music21 corpus)
along with two test by modifying those scores.

Additional tests for the standalone functions rnString and intBeat and
for handling the special case of opus objects.
c                    SSK Jn  SnUR                  SU-   5      nU R                  U[        R
                  5        [        U5      nS H  nU R                  XTR                  5        M      g)a:  
As the rntxt input parser handles Opus objects
(i.e. more than one score within the same rntxt files),
this RnWriter also needs to accept that type.

This test parses a fake (tiny) Opus file in three (really tiny!) movements.
Checks ensure that the parsed version is indeed an Opus object and that
the data is faithfully transferred through that process.

In practice, Opus handling will bypass this module in the typical case of a simple
.write() because writing Opus objects explicitly separates them into their constituent
score files prior to invoking this module.
r   	convertera  Composer: Fake composer
        Piece: Fake piece
        Movement: 1
        m1 C: I b3 IV b4 V
        m2 I

        Movement: 2
        m1 G: I
        m3 IV
        m4 V
        m5 I

        Movement: 3
        m1 C: I
        m2 V
        m3 I
        zromantext: )zTitle: Fake piece - No.1:zTitle: Fake piece - No.2:zTitle: Fake piece - No.3:zm2 Izm5 Izm3 IN)	music21r   parseassertIsInstancer   r   r   assertInr   )r)   r   testOpusStringtestOpustestOpusRnWriterr+   s         r/   r   Test.testOpus  sb     	&$ ??=>#ABh4#H-A MM!::;r5   c                :   SSK Jn  UR                  S5      n[        U5      nU R	                  SUR
                  5        UR                  S   R                  S5      R                  S[        R                  " S5      5        UR                  S   R                  S5      R                  S[        R                  " S5      5        [        U5      nS	nS
nU R	                  XTR
                  5        U R	                  XdR
                  5        U R                  UR
                  R                  U5      UR
                  R                  U5      S-   5        UR                  S5      n[        U5      nU R                  UR                  S5        U R                  UR                  S5        U R                  UR
                  S   S5        SUR                  l        SUR                  l        SUR                  l        [        U5      n	U R                  U	R                  S5        g)z~
Tests for two analysis cases (the smallest rntxt files in the music21 corpus)
along with two test by modifying those scores.
r   )corpusz-bach/choraleAnalyses/riemenschneider004.rntxtzm10 V6/V b2 V b3 I   z10/8rB   z5/8zTime Signature: 10/8z;Note: further time signature change(s) unprocessed: ['5/8']zmonteverdi/madrigal.3.8.rntxt
MonteverdizLa piaga c'ho nel corezm57 Iz
Fake titlei[zFake movementNamez,Fake title - No.123456789: Fake movementNameN)r   r   r   r   r   r   r   measurer#   r   r&   assertEqualindexr   r   r   r:   r;   )
r)   r   	scoreBachrnaBach	wonkyBach	tsString1	tsString2
scoreMonternMonteadjustedMontes
             r/   $testTwoCorpusPiecesAndTwoCorruptions)Test.testTwoCorpusPiecesAndTwoCorruptions  s    	#LL!PQ	9%*G,@,@A 	""1%,,Q0C0CF0KL""1%,,Q0C0CE0JKY'	*	Q	i!7!78i!7!78//55i@"//55i@1D	F
 \\"AB
:&))<8(@A--b17;
 %1
!-6
*+>
( ,,,.\]r5   c                6   [         R                  " 5       n[        R                  R	                  U5        [         R
                  " 5       n[        R                  R	                  U5        UR                  SU5        [        R                  R	                  U5        [         R                  " 5       n[	        U5        [         R                  " 5       n[	        U5      nU R                  UR                  / SQ5        [        R                  " SS5      n[	        U5        g)zY
Tests successful init on a range of supported objects (score, part, even RomanNumeral).
r   )zComposer: Composer unknownzTitle: Title unknownr   r   r   viio6GN)r   r   r
   
writeRomanr   r!   r#   r"   Voicer   r   r	   rQ   )r)   sr.   r3   vemptyWriterr_   s          r/   testTypeParsesTest.testTypeParsesC  s    
 LLN%%a(KKM%%a(	A%%a(NNLLNqk11 4
 	 -r5   c                   SSK Jn  [        R                  " S5      nUR	                  USS9n[        U5      nSR                  UR                  5      R                  5       R                  UR                  5       5      (       d   eg )Nr   r   z
            Time Signature: 2/4
            m1 ||: C: I
            m2 V :||
            m3 ||: I :||
            m4 ||: I
            m5a V :||
            m5b I
        r
   )formatr   )
r   r   textwrapdedentr   r   r=   r   stripendswith)r)   r   rntxtr   writers        r/   testRepeatsTest.testRepeatsb  sj    % !  OOE+O6!yy,,-335>>u{{}MMMMr5   c                    [        SSS5      nU R                  US5        [        SSS5      nU R                  US5        U R                  [        5         [        SSS	S
5        S S S 5        g ! , (       d  f       g = f)NrB   zG: Izm1 G: Ir      zb: Vz
m0 b4 b: V   r   zm14 G: I)rP   r   assertRaisesrt   )r)   tests     r/   testRnStringTest.testRnStringq  sf    1f%y)1f%|,z*RGZ0 +**s   A,,
A:c                   [        SSS9nU R                  US5        [        SSS9nU R                  US5        [        SSS9nU R                  US5        [        SSS9nU R                  US5        [        SSS9nU R                  US	5        [        [        R                  " S
S5      SS9nU R                  US	5        [        SSS9nU R                  US5        U R	                  [
        5         [        / SQ5        S S S 5        U R	                  [        5         [        S5        S S S 5        g ! , (       d  f       N8= f! , (       d  f       g = f)NrB      )r   g      ?gzq?g(\?g?gUUUUUU@g\(\@   r   z0.666666666gq=
ףp?)r   rB   r   g      )ru   r   r}   r~   r   r   rt   )r)   testInt
testOneDec
testRound1
testRound2	testFrac1	testFrac2testStrs           r/   testIntBeatTest.testIntBeat{  s   !*!$SQ/
S)ZA6
T*ZA6
S)Ea0	D)I..q!4C	D)-A6$'y)I * z*DM +* *) +*s   /D/E /
D= 
Erq   N)rk   rl   rm   rn   ro   r   r   r   r   r   r   rp   rq   r5   r/   r   r     s)    .<`/^b>N1r5   r   __main__)r   )rD   z	int | strrE   &str | int | float | fractions.FractionrF   rK   rG   z
str | None)r   )rE   r   r   r{   )ro   
__future__r   r}   r   unittestr   r   r   r   r   r   r	   r
   r   ProtoM21Objectr   rP   ru   TestCaser   rk   mainTestrq   r5   r/   <module>r      s    #           
I"w%% I"b
 #%/3//  /f  4'4'r|8 |B zT r5   