
    rh\                    x   S r SSKJ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SSSSSSSSSSSSSSS. SS jjrSSSSSSSSSSSSSSSSS. SS jjrSSSSSSSSSSSSSSSSS. SS jjrSS S jjrSSSSSSSSSSSSSSSSS. SS jjrSSSSSSSSSSSSSSSSS. SS jjrSSSSSSSSSSSSSSSSS. SS jjrSSSSSSSSSSSSSSSSS. SS jjr\/r " S S\R2                  5      r " S S\R6                  5      r\S:X  a  SSKr\R<                  " \5        gg)!a}  
Functions for exporting music21 data as braille.

This module was made in consultation with the manual "Introduction to Braille
Music Transcription, Second Edition" by Mary Turner De Garmo, 2005. It is
available from the Library of Congress
`here <https://www.loc.gov/nls/services-and-resources/music-service-and-materials/>`_,
and will henceforth be referred to as BMTM.


The most important function, and the only one that is needed to translate music
into braille, is :func:`~music21.braille.translate.objectToBraille`. This function,
as well as the others, accepts keyword arguments that serve to modify the output.
If no keyword arguments are needed, then using the function is equivalent to
calling :meth:`~music21.base.Music21Object.show` on the music.


Keywords:


* **inPlace** (False): If False, then :meth:`~music21.stream.base.Stream.makeNotation` is called
  on all :class:`~music21.stream.Measure`, :class:`~music21.stream.Part`, and
  :class:`~music21.stream.PartStaff` instances. Copies of those objects are then
  used to transcribe the music. If True, the transcription is done "as is."
  This is useful for strict transcription because
  sometimes :meth:`~music21.stream.base.Stream.makeNotation`
  introduces some unwanted artifacts in the music. However, the music needs
  to be organized into measures for transcription to work.
* **debug** (False): If True, a braille-english representation of the music is returned. Useful
  for knowing how the music was interpreted by the braille transcriber.


The rest of the keywords are segment keywords. A segment is "a group of measures occupying
more than one braille line." Music is divided into segments to "present the music to
the reader in a meaningful manner and to give him convenient reference points to use in
memorization" (BMTM, 71). Some of these keywords are changed automatically in context.

* **cancelOutgoingKeySig** (True): If True, whenever a key signature change is
    encountered, the new signature should be preceded by the old one.
* **descendingChords** (True): If True, then chords are spelled around the highest note.
    If False, then chords are spelled around the lowest note. This keyword is
    overridden by any valid clefs present in the music.
* **dummyRestLength** (None) For a given positive integer n, adds n "dummy rests"
    near the beginning of a segment. Designed for test purposes, as the rests
    are used to demonstrate measure division at the end of braille lines.
* **maxLineLength** (40): The maximum amount of braille characters
    that should be present in a line of braille.
* **segmentBreaks** (None): A list consisting of (measure number, offset start)
    tuples indicating where the music should be broken into segments.

    .. warning::
        Not currently implemented

* **showClefSigns** (False): If True, then clef signs are displayed.
    Since braille does not use clefs or staves to represent music, they would
    instead be shown for referential or historical purposes.
* **showFirstMeasureNumber** (True): If True, then a measure number is shown
    following the heading (if applicable) and preceding the music.
* **showHand** (None): If set to "right" or "left", the corresponding
    hand sign is shown before the music. In keyboard music,
    the hand signs are shown automatically.
* **showHeading** (True): If True, then a braille heading is created above
    the initial segment. A heading consists
    of an initial :class:`~music21.key.KeySignature`, :class:`~music21.meter.TimeSignature`,
    :class:`~music21.tempo.TempoText`, and :class:`~music21.tempo.MetronomeMark`,
    or any subset thereof. The heading
    is centered above the music automatically.
* **showLongSlursAndTiesTogether** (None), **showShortSlursAndTiesTogether** (False):
    If False, then the slur on either side of the phrase is reduced by the amount
    that ties are present. If True, then slurs and ties are shown together
    (i.e. the note can have both a slur and a tie). If `showLongSlursAndTiesTogether`
    is `None` (default), its value is programmatically set to be the value of
    ``slurLongPhraseWithBrackets``, described next.
* **slurLongPhraseWithBrackets** (True): If True, then the slur of a
    long phrase (4+ consecutive notes) is brailled using the bracket slur.
    If False, the double slur is used instead.
* **suppressOctaveMarks** (False): If True, then all octave marks are suppressed.
    Designed for test purposes, as octave marks were not presented in BMTM until Chapter 7.
* **upperFirstInNoteFingering** (True): If True, then whenever
    there is a choice fingering (i.e. 5|4), the upper
    number is transcribed before the lower number. If False, the reverse is the case.
    )annotationsN)base)exceptions21)metadata)stream)wordToBraille)alphabet)segmentFT(   )inPlacedebugcancelOutgoingKeySigdescendingChordsdummyRestLengthmaxLineLengthsegmentBreaksshowClefSignsshowFirstMeasureNumbershowHandshowHeadingshowLongSlursAndTiesTogethershowShortSlursAndTiesTogetherslurLongPhraseWithBracketssuppressOctaveMarksupperFirstInNoteFingeringc               $   [        U [        R                  [        R                  [        R                  [        R
                  45      (       a:  [        U 40 SU_SU_SU_SU_SU_SU_SU_SU_S	U	_S
U
_SU_SU_SU_SU_SU_SU_6$ [        U [        R                  5      (       a  [        S5      e[        R
                  " 5       nUR                  U 5        [        U40 SS_SU_SU_SU_SU_SU_SU_SU_S	S_S
U
_SS_SU_SU_SU_SU_SU_6$ )u+  
Translates an arbitrary object to braille.

>>> from music21.braille import translate
>>> samplePart = converter.parse('tinynotation: 3/4 C4 D16 E F G# r4 e2.')
>>> #_DOCS_SHOW samplePart.show()


    .. image:: images/objectToBraille.*
        :width: 700


>>> print(translate.objectToBraille(samplePart))
⠀⠀⠀⠀⠀⠀⠀⠼⠉⠲⠀⠀⠀⠀⠀⠀⠀
⠼⠁⠀⠸⠹⠵⠋⠛⠩⠓⠧⠀⠐⠏⠄⠣⠅


For normal users, you'll just call this, which starts a text editor:


>>> #_DOCS_SHOW samplePart.show('braille')
⠀⠀⠀⠀⠀⠀⠀⠼⠉⠲⠀⠀⠀⠀⠀⠀⠀
⠼⠁⠀⠸⠹⠵⠋⠛⠩⠓⠧⠀⠐⠏⠄⠣⠅


Other examples:


>>> sampleNote = note.Note('C3')
>>> print(translate.objectToBraille(sampleNote))
⠸⠹

>>> sampleDynamic = dynamics.Dynamic('fff')
>>> print(translate.objectToBraille(sampleDynamic))
⠜⠋⠋⠋

>>> sample_voice = stream.Voice([note.Note()])
>>> sample_measure = stream.Measure([sample_voice])
>>> print(translate.objectToBraille(sample_measure))
⠀⠼⠁⠲⠀
⠼⠚⠀⠐⠹

>>> empty_measure = stream.Measure()
>>> print(translate.objectToBraille(empty_measure))
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠼⠙⠲⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠼⠚

Not currently supported: generic `stream.Stream` objects or `stream.Voice` objects:

>>> sample_generic_stream = stream.Stream([note.Note()])
>>> translate.objectToBraille(sample_generic_stream)
Traceback (most recent call last):
music21.braille.translate.BrailleTranslateException: Stream cannot be translated to Braille.
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   'Stream cannot be translated to Braille.TF)
isinstancer   OpusScorePartMeasurestreamToBrailleStreamBrailleTranslateExceptionappendmeasureToBraille)
music21Objr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   music21Measures                     S/home/james-whalen/.local/lib/python3.13/site-packages/music21/braille/translate.pyobjectToBrailler+   l   s   R *v{{FLL&++v~~VWWz !'.!%*! 5I! 1A	!
 0?! .;! .;! .;! 7M! )1! ,7! =Y! >[! ;U! 4G!  :S!! 	!$ 
J	.	.'(QRR)j) "(,"&+" 6J" 2B	"
 1@" /<" /<" /<" 8=" *2" -2" >Z" ?\" <V" 5H"  ;T!" 	"    c                  [        U [        R                  5      (       a:  [        U 40 SU_SU_SU_SU_SU_SU_SU_SU_S	U	_S
U
_SU_SU_SU_SU_SU_SU_6$ [        U [        R                  5      (       a:  [        U 40 SU_SU_SU_SU_SU_SU_SU_SU_S	U	_S
U
_SU_SU_SU_SU_SU_SU_6$ [        U [        R                  5      (       a:  [        U 40 SU_SU_SU_SU_SU_SU_SU_SU_S	U	_S
U
_SU_SU_SU_SU_SU_SU_6$ [        U [        R                  5      (       a:  [        U 40 SU_SU_SU_SU_SU_SU_SU_SU_S	U	_S
U
_SU_SU_SU_SU_SU_SU_6$ U R                  [        R                  5      n[        U5      S:X  a:  [        U 40 SU_SU_SU_SU_SU_SU_SU_SU_S	U	_S
U
_SU_SU_SU_SU_SU_SU_6$ [        S5      e)a  
Translates a :class:`~music21.stream.Measure`.
:class:`~music21.stream.Part`,
:class:`~music21.stream.Score`, or
:class:`~music21.stream.Opus` to braille.

Note: generic :class:`~music21.stream.Stream` instances are not supported.

>>> s = stream.Stream(note.Note())
>>> braille.translate.streamToBraille(s)
Traceback (most recent call last):
music21.braille.translate.BrailleTranslateException: Stream cannot be translated to Braille.
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      r   )r   r   r!   partToBrailler"   r'   r    scoreToBrailler   opusToBraillegetElementsByClass	PartStafflenr%   )music21Streamr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   keyboardPartss                     r*   r#   r#      s    @ ---] $+"' 2F .>	
 -< +8 +8 +8 4J &. )4 :V ;X 8R 1D  7P! 	$ 
M6>>	2	2 "(/"&+" 6J" 2B	"
 1@" /<" /<" /<" 8N" *2" -8" >Z" ?\" <V" 5H"  ;T!" 	"$ 
M6<<	0	0m  &- $)  4H  0@	 
 />  -:  -:  -:  6L  (0  +6  <X  =Z  :T  3F   9R!  	 $ 
M6;;	/	/] %,#( 3G /?	
 .= ,9 ,9 ,9 5K '/ *5 ;W <Y 9S 2E  8Q! 	, "44V5E5EFM
=Qm  &- $)  4H  0@	 
 />  -:  -:  -:  6L  (0  +6  <X  =Z  :T  3F   9R!  	 $ $$M
NNr,   c                 ^^^^^^^^^	^
^^^^^^^^ / mU R                  [        R                  5       H!  nTR                  [	        UT(       + S95        M#     SmUUUUUUUUUU	U
UUUUUUU4S jnU R                  [
        R                  5       H  n[        U[
        R                  5      (       al  Tbe  [        [
        R                  " TU/5      40 ST_ST_ST_ST_ST_S	T_S
T_ST_ST	_ST
_ST_ST_ST_ST_ST_ST_6nTR                  U5        SmM  UmM  U" 5         [        U40 ST_ST_ST_ST_ST_S	T_S
T_ST_ST	_ST
_ST_ST_ST_ST_ST_ST_6nTR                  U5        M     U" 5         SR                  T5      $ )z9
Translates a :class:`~music21.stream.Score` to braille.
)returnBrailleUnicodeNc                    > Tc  g [        T40 ST_ST_ST_ST_ST_ST_ST_ST	_S	T
_S
T_ST_ST_ST_ST_ST_ST_6n TR                  U 5        S mg )Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r/   r&   )braillePrt2allBrailleLinesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   unprocessed_partStaffr   s    r*   +process_unmatched_part_staff_as_single_partCscoreToBraille.<locals>.process_unmatched_part_staff_as_single_part  s     (#$9 &,3&*/& :N& 6F	&
 5D& 3@& 3@& 3@& <R& .6& 1<& B^& C`& @Z& 9L&  ?X!&$ 	{+ $r,   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
)r2   r   Metadatar&   metadataToStringr   r!   r   r3   keyboardPartsToBrailler    r/   join)music21Scorer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   music21Metadatar=   pkeyboard_partsbraillePartr;   r<   s    ````````````````     @@r*   r0   r0   g  sT   , O'::8;L;LM/Z_V_`a N 48% % %2 ,,V[[9a))**$0!7LL"7!;<"#"  " *>	"
 &6" %4" #0" #0" #0" ,B" &" !," 2N" 3P" 0J"  )<!"" /H#"&  &&~6(,%()%79' *07*.3* >R* :J	*
 9H* 7D* 7D* 7D* @V* 2:* 5@* Fb* Gd* D^* =P*  C\!*K$ "";/_ :b 0199_%%r,   c                &   / nU R                  SSS9 H  u  p4Uc  M
  US:X  a  M  U R                  U5      nU(       d  M,  UR                  S5      (       a  MD  SR                  [        R
                  " SU5      5      nUR                  5        SU 3nU(       aK  / nUR                  5        H  n	UR                  [        U	5      5        M     [        S   R                  U5      nUR                  U5        M     S	R                  [        U5      5      $ )
uB  
Convert a Metadata format to a format for BRF.

>>> from music21.braille import translate
>>> corelli = corpus.parse('monteverdi/madrigal.3.1.rntxt')
>>> mdObject = corelli.getElementsByClass(metadata.Metadata).first()
>>> mdObject.__class__
<class 'music21.metadata.Metadata'>

The default is very close to ascii.

>>> print(translate.metadataToString(mdObject))
Alternative Title: 3.1
Composer: Claudio Monteverdi
Title: La Giovinetta Pianta

And in Braille Unicode.

>>> unicodeVersion = translate.metadataToString(mdObject, returnBrailleUnicode=True)
>>> print(unicodeVersion)
⠠⠁⠇⠞⠑⠗⠝⠁⠞⠊⠧⠑⠀⠠⠞⠊⠞⠇⠑⠒⠀⠼⠉⠲⠁
⠠⠉⠕⠍⠏⠕⠎⠑⠗⠒⠀⠠⠉⠇⠁⠥⠙⠊⠕⠀⠠⠍⠕⠝⠞⠑⠧⠑⠗⠙⠊
⠠⠞⠊⠞⠇⠑⠒⠀⠠⠇⠁⠀⠠⠛⠊⠕⠧⠊⠝⠑⠞⠞⠁⠀⠠⠏⠊⠁⠝⠞⠁

Note the difference between the first and then translating back to ASCII Braille:

>>> print(braille.basic.brailleUnicodeToBrailleAscii(unicodeVersion))
,ALTERNATIVE ,TITLE3 #C4A
,COMPOSER3 ,CLAUDIO ,MONTEVERDI
,TITLE3 ,LA ,GIOVINETTA ,PIANTA
TF)returnPrimitivesreturnSortedsoftwarezm21FileInfo: z([A-Z]*[a-z]+)z: r?   )alluniqueNameToNamespaceName
startswithrC   refindalltitlesplitr&   r   r	   sorted)
rE   r8   r;   
uniqueNamevaluenamespaceNamen	outStringoutTempwords
             r*   rA   rA     s    @ O,00$UZ0[
=#"1"K"KJ"W##N33HHRZZ 1:>?wwykE7+	G!)}T23 * **73Iy)3 \4 99VO,--r,   c          	        / nU R                  [        R                  5       HL  nUR                  [	        U40 SU_SU_SU_SU_SU_SU_SU_SU_S	U	_S
U
_SU_SU_SU_SU_SU_SU_65        MN     SR                  U5      $ )z9
Translates an :class:`~music21.stream.Opus` to braille.
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   z

)r2   r   r    r&   r0   rC   )music21Opusr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   scores                      r*   r1   r1     s    , O//=5 #*!& 1E -=	
 ,; *7 *7 *7 3I %- (3 9U :W 7Q 0C  6O!	
 >* ;;''r,   c                   U nU(       d  U R                  SS9n[        R                  " 5       nUR                  U5        [	        U40 SS_SU_SU_SU_SU_S	U_S
U_SU_SU	_SU
_SU_SU_SU_SU_SU_SU_6$ )u(  
Translates a :class:`~music21.stream.Measure` to braille.

>>> p = stream.Part()
>>> p.append(note.Note('C4', type='whole'))
>>> p.makeMeasures(inPlace=True)
>>> p.show('t')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.meter.TimeSignature 4/4>
    {0.0} <music21.note.Note C>
    {4.0} <music21.bar.Barline type=final>
>>> print(braille.translate.objectToBraille(p))
⠀⠀⠼⠙⠲⠀⠀
⠼⠁⠀⠐⠽⠣⠅
>>> print(braille.translate.measureToBraille(p.measure(1)))
⠼⠙⠲⠀⠐⠽⠣⠅

FcautionaryNotImmediateRepeatr   Tr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )makeNotationr   r!   r&   r/   )r)   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   measureToTranscribemusic21Parts                      r*   r'   r'   ?  s    L ),99W\9]++-K*+ !%$ /C +;	
 *9 (5 (5 (5 1G #+ &1 7S 8U 5O .A  4M! r,   c                  U nU(       d  U R                  SS9n[        R                  " UUUUUUUU	U
UUUUUUS9n/ nU HG  nUR                  5       nU(       d  UR	                  U5        M-  UR	                  [        U5      5        MI     SSKJn  [        U5       H  nUU	 M     SR                  U Vs/ s H  n[        U5      PM     sn5      $ s  snf )z
Translates a :class:`~music21.stream.Part` to braille.

This is one of two (w/ keyboardPartsToBraille) main routines.  Runs segment.findSegments
and then for each segment runs transcribe on it.
Fra   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )
beamStatusr?   )
rc   r
   findSegments
transcriber&   strmusic21.braille.basicrg   listrC   )re   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   partToTranscribeallSegmentsallBrailleTextbrailleSegmenttranscriptionrg   xbts                            r*   r/   r/   }  s    2 #&33QV3W&&'7<P8H7F5B5B5B>T083>D`EbB\;NAZ)K  N%&113!!-0!!#n"56 & 1*qM  997"c"g7887s   4Cc                  U R                  SS/5      n[        U5      S:w  a  [        S5      eUS   nUS   nUnU(       d  UR                  SS9nUnU(       d  UR                  SS9n[        R
                  " U4S	UUUUUUU	U
UUUUUUS
.6n[        R
                  " U4SUUUUUUU	U
UUUUUUS
.6n/ n[        UU5       H  u  nn[        R                  " US9nU H  nUU   UU'   M     U H  nUU   UU'   M     UR                  5         U(       d  UR                  UR                  5        Mp  UR                  [        U5      5        M     SR                  U Vs/ s H  n[        U5      PM     sn5      $ s  snf )z
Translates a Score object containing two :class:`~music21.stream.Part` instances to braille,
an upper part and a lower
part. Assumes that the two parts are aligned and well constructed. Bar over bar format is used.
r!   r3   r.   z/Can only translate two keyboard parts at a timer      Fra   right)setHandr   r   r   r   r   r   r   r   r   r   r   r   r   r   left)
lineLengthr?   )r2   r4   r%   rc   r
   rh   zipBrailleGrandSegmentri   r&   brailleTextrj   rC   )keyboardScorer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   parts
staffUpper
staffLowerupperPartToTranscribelowerPartToTranscribe
rhSegments
lhSegmentsro   	rhSegment	lhSegmentbgrhGroupingKeylhGroupingKeyrs   s                                  r*   rB   rB     s   0 ,,fk-BCE
5zQ'(YZZqJqJ& * 7 7UZ 7 [& * 7 7UZ 7 [%%&; (.5;O7G6E4A4A4A=S/72=C_DaA[:M@Y(J" %%&; (.4;O7G6E4A4A4A=S/72=C_DaA[:M@Y(J$ N"%j*"=I((MB&M !*- 8B} '
 'M )- 8B} ' 	!!"..1!!#b'* #>  997"c"g7887s   E9c                      \ rS rSrSrg)r%   i   N)__name__
__module____qualname____firstlineno____static_attributes__r   r,   r*   r%   r%     s    r,   r%   c                  &    \ rS rSrS rS rS rSrg)Testi  c                    SSK Jn  UR                  S5      n[        USS9nU R	                  UR                  5        Vs/ s H  n[        U5      PM     sn/ SQ5        g s  snf )Nr   	converter-tinyNotation: 2/4 c4 d e f8 g a2 B2 c4. d8 e2   r   )r   r   r   music21r   parser+   assertEqual
splitlinesr4   selfr   srr   lines        r*   testTranslateRespectsLineLength$Test.testTranslateRespectsLineLength!  sL    %OOKLAR0?#d)?N?   Ac                    SSK Jn  UR                  S5      n[        USS9nU R	                  UR                  5        Vs/ s H  n[        U5      PM     sn/ SQ5        gs  snf )zT
Tests loosening the constraint on trailing spaces when there is
no other solution.
r   r   r   
   r   )r   r      r   Nr   r   s        r*   testSplitNoteGroupingLineLength$Test.testSplitNoteGroupingLineLength'  sN    
 	&OOKLAR0?#d)?Q?r   c                j    SSK Jn  UR                  S5      nUR                  S5      n[	        U5      ng )Nr   )corpusbeachru   )r   r   r   measurer+   )r   r   r   full_score_measure_1_s        r*   testFullScoreWithPianoTest.testFullScoreWithPiano1  s,    "LL! yy|01r,   r   N)r   r   r   r   r   r   r   r   r   r,   r*   r   r     s    OR2r,   r   __main__)r(   zbase.Music21Objectr   bool | None)r5   z9stream.Measure | stream.Part | stream.Score | stream.Opusr   r   )r   r   )F)rE   zmetadata.Metadatareturnrj   )__doc__
__future__r   rQ   unittestr   r   r   r   r   rk   r   music21.braille.lookupr	   music21.brailler
   r+   r#   r0   rA   r1   r'   r/   rB   
_DOC_ORDERMusic21Exceptionr%   TestCaser   r   mainTestr   r,   r*   <module>r      sY  Qd # 	       / + # ")-%)$("$"&"'+/! $>B27/3(-.2#r" 3<r"l ")-%)$("$"&"'+/! $>B27/3(-.2#DO 3<DOR !(,$(#'!#!%!&*. #=A16.2',-1#h& 2;h&V;.@  '+#'"& " $ %)-"<@05-1&+,0#,( 1:,(b # *.&*%)#%#'#(,1"!&?C3804)./3#< 4=<@  '+#'"& " $ %)-"<@05-1&+,0#99 1:99| $)!&04,0+/)+)-).26$('+EI9>6:/459#W9 :CW9t 

	 = = 	28 22 zT r,   