
    rh]                       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  \R                  " S5      r	S r
SS jr " S	 S
\R                  5      r " S S\R                  5      r\
/r\S:X  a  SSKr\R"                  " \5        gg)z
Various tools and utilities for doing metrical or rhythmic analysis.

See the chapter :ref:`User's Guide Chapter 14: Time Signatures <usersGuide_14_timeSignatures>`
for more information on defining
metrical structures in music21.
    )annotationsN)environment)streamzanalysis.metricalc                V   U R                  [        R                  5       GH  nUR                  SS9S   n[        R
                  " U5      nUR                  R                  SS9  UR                   H  n[        US5      (       a_  UR                  bR  [        R                  SXDR                  UR                  R                  /5        UR                  R                  S:X  a  Ms  [        UR                  UR                   5      5       H  nUR#                  S	5        M     M     GM     U $ )
ay  
Modify a Stream in place by annotating metrical analysis symbols.

This assumes that the Stream is already partitioned into Measures.

>>> s = stream.Stream()
>>> ts = meter.TimeSignature('4/4')
>>> s.insert(0, ts)
>>> n = note.Note(type='eighth')
>>> s.repeatAppend(n, 8)
>>> s.makeMeasures(inPlace=True)
>>> post = analysis.metrical.labelBeatDepth(s)
>>> sOut = []
>>> for n in s.flatten().notes:
...     stars = "".join([l.text for l in n.lyrics])
...     sOut.append("{0:8s} {1}".format(n.beatStr, stars))
>>> print("\n".join(sOut))
1        ****
1 1/2    *
2        **
2 1/2    *
3        ***
3 1/2    *
4        **
4 1/2    *
F)sortByCreationTimer      )depthtiez	note, tiestop*)getElementsByClassr   MeasuregetTimeSignaturescopydeepcopybeatSequencesubdivideNestedHierarchynotesAndRestshasattrr
   environLocal
printDebugtyperangegetBeatDepthoffsetaddLyric)streamInmtstsTempnunused_is         S/home/james-whalen/.local/lib/python3.13/site-packages/music21/analysis/metrical.pylabelBeatDepthr$      s    8 ((8   E :1= r"4414=Aq%  QUU%6''a

(KL55::'!&"5"5ahh"?@

3 A ! 9" O    c                `   [        U 5      S-
  nSn[        U 5       GH  u  p4US:X  a  SUR                  l        M  X1:X  a  X$R                  l        M6  XS-
     R                  R
                  nUR                  R
                  nXS-      R                  R
                  nXV:X  a
  Xg:X  a  SnSn	OhXV:w  a
  Xg:X  a  SnSn	OYXV:X  a
  Xg:w  a  SnSn	OJXV:  a
  Xg:  a  SnSn	O;XV:  a
  Xg:  a  SnSn	O,XVs=:  a  U:  a  O  OS	nS
n	OXVs=:  a  U:  a  O  OSnSn	OSnSn	X-  UR                  l        U	nGM     g)a  
Adds an attribute, 'melodicAccent' to each note's `.editorial`
within a :class:`~music21.stream.Stream` object
according to the method postulated in Joseph M. Thomassen, "Melodic accent: Experiments and
a tentative model," ''Journal of the Acoustical Society of America'', Vol. 71, No. 6 (1982) pp.
1598-1605; with, Erratum, ''Journal of the Acoustical Society of America'', Vol. 73,
No. 1 (1983) p.373, and in David Huron and Matthew Royal,
"What is melodic accent? Converging evidence
from musical practice." ''Music Perception'', Vol. 13, No. 4 (1996) pp. 489-516.

Similar to the humdrum melac_ tool.

.. _melac: https://www.humdrum.org/Humdrum/commands/melac.html

Takes in a Stream of :class:`~music21.note.Note` objects (use `.flatten().notes` to get it, or
better `.flatten().getElementsByClass(note.Note)` to filter out chords)
and adds the attribute to
each.  Note that Huron and Royal's work suggests that melodic accent has a correlation
with metrical accent only for solo works/passages; even treble passages do not have a
strong correlation. (Gregorian chants were found to have a strong ''negative'' correlation
between melodic accent and syllable onsets)

Following Huron's lead, we assign a `melodicAccent` of 1.0 to the first note in a piece
and take the accent marker of the first interval alone to the second note and
of the last interval alone to be the accent of the last note.

Example from Thomassen, figure 5:

>>> s = converter.parse('tinynotation: 7/4 c4 c c d e d d')
>>> analysis.metrical.thomassenMelodicAccent(s.flatten().notes)
>>> for n in s.flatten().notes:
...    (n.pitch.nameWithOctave, n.editorial.melodicAccent)
('C4', 1.0)
('C4', 0.0)
('C4', 0.0)
('D4', 0.33)
('E4', 0.5561)
('D4', 0.17)
('D4', 0.0)

   g      ?r   g        g(\?g(\?gQ?g(\?gQ?gq=
ףp?      ?N)len	enumerate	editorialmelodicAccentpitchps)
r   maxNotesp2Accentir!   lastPsthisPsnextPs
thisAccent
nextAccents
             r#   thomassenMelodicAccentr7   M   s:   Z 8}q HH(#6(+AKK%](0KK%a%&&))a%&&)) 0JJ&"2JJ&"2JJ_JJ_JJ%v%JJ%v%JJJJ$.$9!M $r%   c                      \ rS rSrSrS rSrg)TestExternal   Tc                   SSK Jn  SSK Jn  [        R                  " 5       nUR                  S5      nUR                  U5        UR                  5       nSUl        UR                  US5        UR                  5       nSUl        UR                  US5        UR                  5       n[        U5      nU R                  (       a  UR                  5         g	g	)
z/
Need to test direct meter creation w/o stream
r   )note)meterz4/4r'      r(      N)music21r<   r=   r   StreamTimeSignatureappendNotequarterLengthrepeatAppendmakeMeasuresr$   show)selfr<   r=   sr   r!   s         r#   
testSingleTestExternal.testSingle   s     	!!MMO  '	IIK	q!IIK	q!NN199FFH r%    N)__name__
__module____qualname____firstlineno__rH   rK   __static_attributes__rM   r%   r#   r9   r9      s    Dr%   r9   c                      \ rS rSrSrg)Test   rM   N)rN   rO   rP   rQ   rR   rM   r%   r#   rT   rT      s    r%   rT   __main__)r   zstream.Stream)__doc__
__future__r   r   unittestr@   r   r   Environmentr   r$   r7   TestCaser9   rT   
_DOC_ORDERrN   mainTestrM   r%   r#   <module>r^      s    #    &&':;-^Uv8$$ 8	8 	 
zT r%   