.. _usersGuide_97_timespans:

.. WARNING: DO NOT EDIT THIS FILE:
   AUTOMATICALLY GENERATED.
   PLEASE EDIT THE .py FILE DIRECTLY.


Timespan Demos
==============

Work in progress...


.. code:: python

    from music21 import *

.. code:: python

    luca = corpus.parse('luca/gloria').measures(1,8)

.. code:: python

    tsCol = luca.asTimespans()

.. code:: python

    tsCol




.. parsed-literal::
   :class: ipython-result

    <TimespanTree {189} (0.0 to 24.0) <music21.stream.Score 0x10b271fd0>>



.. code:: python

    for v in tsCol.iterateVerticalities():
        print(v)


.. parsed-literal::
   :class: ipython-result

    <Verticality 0.0 {F3 F4 C5}>
    <Verticality 3.0 {F3 F4 D5}>
    <Verticality 3.5 {F3 C4 C5}>
    <Verticality 4.5 {F3 C4 A4}>
    <Verticality 5.0 {F3 C4 F4}>
    <Verticality 5.5 {F3 A3 F4}>
    <Verticality 6.0 {F3 F4}>
    <Verticality 6.5 {F3 F4 A4}>
    <Verticality 7.5 {F3 F4 C5}>
    <Verticality 8.5 {F3 F4 C5}>
    <Verticality 9.0 {C4 E4 G4}>
    <Verticality 10.5 {D4 F4 A4}>
    <Verticality 12.0 {C4 E4 G4}>
    <Verticality 15.0 {G3 G4 D5}>
    <Verticality 16.5 {A3 E4 C5}>
    <Verticality 17.5 {A3 E4 B4}>
    <Verticality 18.0 {D4 F4 A4}>
    <Verticality 18.5 {D4 F4 G4}>
    <Verticality 19.0 {D4 F4}>
    <Verticality 19.5 {C4 E4 G4}>
    <Verticality 20.0 {C4 F4 G4}>
    <Verticality 20.5 {C4 D4 G4}>
    <Verticality 21.0 {C4 E4}>
    <Verticality 21.5 {A3 E4}>
    <Verticality 22.0 {A3 C4 F4}>
    <Verticality 22.5 {B-3 D4 F4}>
    <Verticality 23.0 {G3 B-3 D4}>
    <Verticality 23.5 {B-3 D4 F4}>


.. code:: python

    v4 = list(tsCol.iterateVerticalities())[4]

.. code:: python

    v4




.. parsed-literal::
   :class: ipython-result

    <Verticality 5.0 {F3 C4 F4}>



.. code:: python

    v4.pitchSet




.. parsed-literal::
   :class: ipython-result

    {<music21.pitch.Pitch F3>, <music21.pitch.Pitch C4>, <music21.pitch.Pitch F4>}



.. code:: python

    f = sorted(v4.pitchSet)[0]

.. code:: python

    f




.. parsed-literal::
   :class: ipython-result

    <music21.pitch.Pitch F3>



.. code:: python

    v4.startTimespans




.. parsed-literal::
   :class: ipython-result

    (<PitchedTimespan (5.0 to 6.0) <music21.note.Note F>>,)



.. code:: python

    v4.stopTimespans




.. parsed-literal::
   :class: ipython-result

    (<PitchedTimespan (4.5 to 5.0) <music21.note.Note A>>,)



.. code:: python

    v4.overlapTimespans




.. parsed-literal::
   :class: ipython-result

    (<PitchedTimespan (3.0 to 6.0) <music21.note.Note F>>,
     <PitchedTimespan (4.5 to 5.5) <music21.note.Note C>>)



.. code:: python

    v35 = tsCol.getVerticalityAt(3.5)

.. code:: python

    v35




.. parsed-literal::
   :class: ipython-result

    <Verticality 3.5 {F3 C4 C5}>



.. code:: python

    v35.startTimespans




.. parsed-literal::
   :class: ipython-result

    (<PitchedTimespan (3.5 to 4.5) <music21.note.Note C>>,
     <PitchedTimespan (3.5 to 4.5) <music21.note.Note C>>)



.. code:: python

    v35.stopTimespans




.. parsed-literal::
   :class: ipython-result

    (<PitchedTimespan (3.0 to 3.5) <music21.note.Note D>>,
     <PitchedTimespan (3.0 to 3.5) <music21.note.Note F>>)



.. code:: python

    v35.overlapTimespans




.. parsed-literal::
   :class: ipython-result

    (<PitchedTimespan (3.0 to 6.0) <music21.note.Note F>>,)



.. code:: python

    elTsC = v35.startTimespans[0]

.. code:: python

    elTsC, elTsC.element




.. parsed-literal::
   :class: ipython-result

    (<PitchedTimespan (3.5 to 4.5) <music21.note.Note C>>, <music21.note.Note C>)



Test previousVerticality with Rest

.. code:: python

    v22 = tsCol.getVerticalityAt(22.0)

.. code:: python

    v22




.. parsed-literal::
   :class: ipython-result

    <Verticality 22.0 {A3 C4 F4}>



.. code:: python

    v22.startTimespans




.. parsed-literal::
   :class: ipython-result

    (<PitchedTimespan (22.0 to 23.0) <music21.note.Note F>>,
     <PitchedTimespan (22.0 to 22.5) <music21.note.Note C>>)



.. code:: python

    elTSfollowingRest = v22.startTimespans[1]
    elTSfollowingRest




.. parsed-literal::
   :class: ipython-result

    <PitchedTimespan (22.0 to 22.5) <music21.note.Note C>>



.. code:: python

    v22.stopTimespans




.. parsed-literal::
   :class: ipython-result

    (<PitchedTimespan (21.0 to 22.0) <music21.note.Note E>>,
     <PitchedTimespan (21.5 to 22.0) <music21.note.Rest rest>>)



.. code:: python

    elTSfollowingRest.part




.. parsed-literal::
   :class: ipython-result

    <music21.stream.Part Contratenor>



.. code:: python

    score = tree.makeExampleScore()
    score.show()




.. image:: usersGuide_97_timespans_29_0.png



.. code:: python

    tsColSmall = score.asTimespans()

.. code:: python

    for v in tsColSmall.iterateVerticalities():
        if v != []:
            print(v)
            print(v.getAllVoiceLeadingQuartets())
        


.. parsed-literal::
   :class: ipython-result

    <Verticality 0.0 {C3 C#3}>
    []
    <Verticality 1.0 {C#3 D3}>


::


    ---------------------------------------------------------------------------

    AttributeError                            Traceback (most recent call last)

    <ipython-input-30-f77717ce7edf> in <module>()
          2     if v != []:
          3         print(v)
    ----> 4         print(v.getAllVoiceLeadingQuartets())
          5 


    /Users/cuthbert/git/music21base/music21/tree/verticality.py in getAllVoiceLeadingQuartets(self, includeRests, includeOblique, includeNoMotion, returnObjects, partPairNumbers)
        662                             n21 is not None and
        663                             n22 is not None): 
    --> 664                         vlq = VoiceLeadingQuartet(n11, n12, n21, n22)
        665                         filteredList.append(vlq)
        666 


    /Users/cuthbert/git/music21base/music21/voiceLeading.py in __init__(self, v1n1, v1n2, v2n1, v2n2, analyticKey)
        114             self.key = analyticKey
        115         if v1n1 is not None and v1n2 is not None and v2n1 is not None and v2n2 is not None:
    --> 116             self._findIntervals()
        117 
        118     def __repr__(self):


    /Users/cuthbert/git/music21base/music21/voiceLeading.py in _findIntervals(self)
        232 
        233     def _findIntervals(self):
    --> 234         self.vIntervals.append(interval.notesToInterval(self.v1n1, self.v2n1))
        235         self.vIntervals.append(interval.notesToInterval(self.v1n2, self.v2n2))
        236         self.hIntervals.append(interval.notesToInterval(self.v1n1, self.v1n2))


    /Users/cuthbert/git/music21base/music21/interval.py in notesToInterval(n1, n2)
       2498             from music21 import pitch
       2499             n2 = pitch.Pitch()
    -> 2500     gInt = notesToGeneric(n1, n2)
       2501     cInt = notesToChromatic(n1, n2)
       2502     intObj = intervalFromGenericAndChromatic(gInt, cInt)


    /Users/cuthbert/git/music21base/music21/interval.py in notesToGeneric(n1, n2)
       1466 
       1467     '''
    -> 1468     staffDist = n2.diatonicNoteNum - n1.diatonicNoteNum
       1469     genDist = convertStaffDistanceToInterval(staffDist)
       1470     return GenericInterval(genDist)


    AttributeError: 'NoneType' object has no attribute 'diatonicNoteNum'


.. code:: python

    lucaAll = corpus.parse('luca/gloria')
    tsCol = lucaAll.asTimespans()

.. code:: python

    for v in tsCol.iterateVerticalities():
        vlqs = v.getAllVoiceLeadingQuartets()


::


    ---------------------------------------------------------------------------

    AttributeError                            Traceback (most recent call last)

    <ipython-input-32-76b643c51e81> in <module>()
          1 for v in tsCol.iterateVerticalities():
    ----> 2     vlqs = v.getAllVoiceLeadingQuartets()
    

    /Users/cuthbert/git/music21base/music21/tree/verticality.py in getAllVoiceLeadingQuartets(self, includeRests, includeOblique, includeNoMotion, returnObjects, partPairNumbers)
        662                             n21 is not None and
        663                             n22 is not None): 
    --> 664                         vlq = VoiceLeadingQuartet(n11, n12, n21, n22)
        665                         filteredList.append(vlq)
        666 


    /Users/cuthbert/git/music21base/music21/voiceLeading.py in __init__(self, v1n1, v1n2, v2n1, v2n2, analyticKey)
        114             self.key = analyticKey
        115         if v1n1 is not None and v1n2 is not None and v2n1 is not None and v2n2 is not None:
    --> 116             self._findIntervals()
        117 
        118     def __repr__(self):


    /Users/cuthbert/git/music21base/music21/voiceLeading.py in _findIntervals(self)
        232 
        233     def _findIntervals(self):
    --> 234         self.vIntervals.append(interval.notesToInterval(self.v1n1, self.v2n1))
        235         self.vIntervals.append(interval.notesToInterval(self.v1n2, self.v2n2))
        236         self.hIntervals.append(interval.notesToInterval(self.v1n1, self.v1n2))


    /Users/cuthbert/git/music21base/music21/interval.py in notesToInterval(n1, n2)
       2498             from music21 import pitch
       2499             n2 = pitch.Pitch()
    -> 2500     gInt = notesToGeneric(n1, n2)
       2501     cInt = notesToChromatic(n1, n2)
       2502     intObj = intervalFromGenericAndChromatic(gInt, cInt)


    /Users/cuthbert/git/music21base/music21/interval.py in notesToGeneric(n1, n2)
       1466 
       1467     '''
    -> 1468     staffDist = n2.diatonicNoteNum - n1.diatonicNoteNum
       1469     genDist = convertStaffDistanceToInterval(staffDist)
       1470     return GenericInterval(genDist)


    AttributeError: 'NoneType' object has no attribute 'diatonicNoteNum'


.. code:: python

    bach = corpus.parse('bwv66.6')

.. code:: python

    import time
    t = time.time()
    dummy = alpha.theoryAnalysis.theoryAnalyzer.getVLQs(bach, 0, 1) # two parts only of bach
    print(time.time() - t)


::


    ---------------------------------------------------------------------------

    AttributeError                            Traceback (most recent call last)

    /Users/cuthbert/git/music21base/music21/voiceLeading.py in _setKey(self, keyValue)
        137                 if isKey is False:
    --> 138                     raise AttributeError
        139             except AttributeError:


    AttributeError: 

    
    During handling of the above exception, another exception occurred:


    VoiceLeadingQuartetException              Traceback (most recent call last)

    <ipython-input-34-10e1b81420f4> in <module>()
          1 import time
          2 t = time.time()
    ----> 3 dummy = alpha.theoryAnalysis.theoryAnalyzer.getVLQs(bach, 0, 1) # two parts only of bach
          4 print(time.time() - t)


    /Users/cuthbert/git/music21base/music21/alpha/theoryAnalysis/theoryAnalyzer.py in getVLQs(score, partNum1, partNum2)
        374                     defaultKey = score.analyze('key')
        375                 newKey = defaultKey
    --> 376             vlq.key = newKey
        377             #vlq.key = getKeyAtMeasure(score, vlq.v1n1.measureNumber)
        378         allVLQs.extend(vlqs)


    /Users/cuthbert/git/music21base/music21/voiceLeading.py in _setKey(self, keyValue)
        140                 raise VoiceLeadingQuartetException(
        141                     'got a key signature that is not a string or music21 Key ' +
    --> 142                     'object: %s' % keyValue)
        143         self._key = keyValue
        144 


    VoiceLeadingQuartetException: got a key signature that is not a string or music21 Key object: <music21.key.KeySignature of 3 sharps, mode minor>


.. code:: python

    t = time.time()
    tsCol = bach.asTimespans()
    for v in tsCol.iterateVerticalities():
        vlqs = v.getAllVoiceLeadingQuartets()
    print(time.time() - t)  # all parts == 6 pairs


::


    ---------------------------------------------------------------------------

    AttributeError                            Traceback (most recent call last)

    <ipython-input-35-c7589ac93393> in <module>()
          2 tsCol = bach.asTimespans()
          3 for v in tsCol.iterateVerticalities():
    ----> 4     vlqs = v.getAllVoiceLeadingQuartets()
          5 print(time.time() - t)  # all parts == 6 pairs


    /Users/cuthbert/git/music21base/music21/tree/verticality.py in getAllVoiceLeadingQuartets(self, includeRests, includeOblique, includeNoMotion, returnObjects, partPairNumbers)
        662                             n21 is not None and
        663                             n22 is not None): 
    --> 664                         vlq = VoiceLeadingQuartet(n11, n12, n21, n22)
        665                         filteredList.append(vlq)
        666 


    /Users/cuthbert/git/music21base/music21/voiceLeading.py in __init__(self, v1n1, v1n2, v2n1, v2n2, analyticKey)
        114             self.key = analyticKey
        115         if v1n1 is not None and v1n2 is not None and v2n1 is not None and v2n2 is not None:
    --> 116             self._findIntervals()
        117 
        118     def __repr__(self):


    /Users/cuthbert/git/music21base/music21/voiceLeading.py in _findIntervals(self)
        232 
        233     def _findIntervals(self):
    --> 234         self.vIntervals.append(interval.notesToInterval(self.v1n1, self.v2n1))
        235         self.vIntervals.append(interval.notesToInterval(self.v1n2, self.v2n2))
        236         self.hIntervals.append(interval.notesToInterval(self.v1n1, self.v1n2))


    /Users/cuthbert/git/music21base/music21/interval.py in notesToInterval(n1, n2)
       2498             from music21 import pitch
       2499             n2 = pitch.Pitch()
    -> 2500     gInt = notesToGeneric(n1, n2)
       2501     cInt = notesToChromatic(n1, n2)
       2502     intObj = intervalFromGenericAndChromatic(gInt, cInt)


    /Users/cuthbert/git/music21base/music21/interval.py in notesToGeneric(n1, n2)
       1466 
       1467     '''
    -> 1468     staffDist = n2.diatonicNoteNum - n1.diatonicNoteNum
       1469     genDist = convertStaffDistanceToInterval(staffDist)
       1470     return GenericInterval(genDist)


    AttributeError: 'NoneType' object has no attribute 'diatonicNoteNum'


.. code:: python

    meterCol = lucaAll.parts[0].asTimespans(classList=(meter.TimeSignature,))

.. code:: python

    meterCol




.. parsed-literal::
   :class: ipython-result

    <TimespanTree {4} (0.0 to 275.0) <music21.stream.Part Cantus>>



.. code:: python

    print(meterCol)


.. parsed-literal::
   :class: ipython-result

    <TimespanTree {4} (0.0 to 275.0) <music21.stream.Part Cantus>>
    	<PitchedTimespan (0.0 to 0.0) <music21.meter.TimeSignature 6/8>>
    	<PitchedTimespan (81.0 to 81.0) <music21.meter.TimeSignature 2/4>>
    	<PitchedTimespan (185.0 to 185.0) <music21.meter.TimeSignature 6/8>>
    	<PitchedTimespan (275.0 to 275.0) <music21.meter.TimeSignature 3/4>>

