
fX                 @   s6  d  d l  m Z m Z d  d l Z d  d l Z d  d l Z d Z d Z d d i Z d  Z	 d Z
 d Z d Z d d	 d
 d d g Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d    d  e  Z Gd! d"   d" e  Z Gd# d$   d$ e  Z Gd% d&   d& e  Z Gd' d(   d( e  Z Gd) d*   d* e  Z Gd+ d,   d, e  Z Gd- d   d e  Z d. d/   Z  d0 d1   Z! d2 d3   Z" d4 d5   Z# d6 d7   Z$ d S)8    )divisionprint_functionNHEADi  pan
      MIDIFileMAJORMINORSHARPSFLATSc               @   s.   e  Z d  Z d Z d d d d d d  Z d S)	MIDIEventzH
    The class to contain the MIDI Event (placed on MIDIEventList).
    unknownr   c             C   s(   | |  _  | |  _ | |  _ | |  _ d  S)N)typetimeordinsertion_order)selfr   r   ordinalr    r   3/usr/really-local/MIDIUtil/src/midiutil/MidiFile.py__init__*   s    			zMIDIEvent.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r   &   s   r   c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	GenericEventz@
    The event class from which specific events are derived
    c             C   s(   | |  _  | |  _ | |  _ | |  _ d  S)N)r   r   r   r   )r   Z
event_typer   r   r   r   r   r   r   5   s    			zGenericEvent.__init__c             C   s   |  j  | j  k s$ |  j | j k r( d S|  j d k r_ |  j | j k s[ |  j | j k r_ d S|  j d k r |  j | j k r d S|  j d k r |  j | j k s |  j | j k r d S|  j d k r |  j | j k r d S|  j d k r d Sd
 S)a  
        Equality operator for Generic Events and derived classes.

        In the processing of the event list, we have need to remove duplicates.
        To do this we rely on the fact that the classes are hashable, and must
        therefore have an equality operator (__hash__() and __eq__() must both
        be defined).

        This is the most embarrassing portion of the code, and anyone who knows
        about OO programming would find this almost unbelievable. Here we have
        a base class that knows specifics about derived classes, thus breaking
        the very spirit of OO programming.

        I suppose I should go back and restructure the code, perhaps removing
        the derived classes altogether. At some point perhaps I will.
        FnotetempoprogramChange	trackNamecontrollerEventpitchWheelEventSysExUniversalSysExT)r!   r"   r#   r$   )r   r   pitchchannelr   programNumberr    )r   otherr   r   r   __eq__<   s$    $$zGenericEvent.__eq__c             C   s   t  |  j  } | d | d >} | d A| d ?A} | d | d >} | d | d >A} | d	 | d
 >} | d A| d ?A} | S)aK  
        Return a hash code for the object.

        This is needed for the removal of duplicate objects from the event
        list.  The only real requirement for the algorithm is that the hash of
        equal objects must be equal.  There is probably great opportunity for
        improvements in the hashing function.
        i]~   l   <B    igV   l   ldD' 	   l   Fz    l   	Oj    )intr   )r   ar   r   r   __hash__e   s    
zGenericEvent.__hash__N)r   r   r   r   r   r)   r2   r   r   r   r   r   1   s   )r   c                   s1   e  Z d  Z d Z d d d   f d d  Z   S)Notez*
    A class that encapsulates a note
    r.   Nr   c	       	         sP   | |  _  | |  _ | |  _ | |  _ | |  _ t t |   j d | | |  d  S)Nr   )r%   durationvolumer&   
annotationsuperr3   r   )	r   r&   r%   r   r4   r5   r   r6   r   )	__class__r   r   r   }   s    					zNote.__init__)r   r   r   r   r   r   r   )r8   r   r3   y   s   r3   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)Tempoz6
    A class that encapsulates a tempo meta-event
    r.   r   c                s6   t  d |  |  _ t t |   j d | | |  d  S)Ni r   )r0   r   r7   r9   r   )r   r   r   r   r   )r8   r   r   r      s    zTempo.__init__)r   r   r   r   r   r   r   )r8   r   r9      s   r9   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)	Copyrightz5
    A class that encapsulates a copyright event
    r   r   c                s5   | j  d  |  _ t t |   j d | | |  d  S)Nz
ISO-8859-1r:   )encodenoticer7   r:   r   )r   r   r<   r   r   )r8   r   r   r      s    zCopyright.__init__)r   r   r   r   r   r   r   )r8   r   r:      s   r:   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)Textz0
    A class that encapsulates a text event
    r   r   c                s5   | j  d  |  _ t t |   j d | | |  d  S)Nz
ISO-8859-1r=   )r;   textr7   r=   r   )r   r   r>   r   r   )r8   r   r   r      s    zText.__init__)r   r   r   r   r   r   r   )r8   r   r=      s   r=   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)KeySignaturez0
    A class that encapsulates a text event
    r   r   c                s>   | |  _  | |  _ | |  _ t t |   j d | | |  d  S)Nr?   )accidentalsaccidental_typemoder7   r?   r   )r   r   r@   rA   rB   r   r   )r8   r   r   r      s
    			zKeySignature.__init__)r   r   r   r   r   r   r   )r8   r   r?      s   r?   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)ProgramChangez;
    A class that encapsulates a program change event.
    r   r   c                s5   | |  _  | |  _ t t |   j d | | |  d  S)Nr   )r'   r&   r7   rC   r   )r   r&   r   r'   r   r   )r8   r   r   r      s    		zProgramChange.__init__)r   r   r   r   r   r   r   )r8   r   rC      s   rC   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)
SysExEventz>
    A class that encapsulates a System Exclusive  event.
    r   r   c                s5   | |  _  | |  _ t t |   j d | | |  d  S)Nr#   )manIDpayloadr7   rD   r   )r   r   rE   rF   r   r   )r8   r   r   r      s    		zSysExEvent.__init__)r   r   r   r   r   r   r   )r8   r   rD      s   rD   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)UniversalSysExEventzH
    A class that encapsulates a Universal System Exclusive  event.
    r   r   c	       	         sP   | |  _  | |  _ | |  _ | |  _ | |  _ t t |   j d | | |  d  S)Nr$   )realTimesysExChannelcodesubcoderF   r7   rG   r   )	r   r   rH   rI   rJ   rK   rF   r   r   )r8   r   r   r      s    					zUniversalSysExEvent.__init__)r   r   r   r   r   r   r   )r8   r   rG      s   rG   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)ControllerEventz;
    A class that encapsulates a program change event.
    r   r   c                s>   | |  _  | |  _ | |  _ t t |   j d | | |  d  S)Nr!   )	parameterr&   controller_numberr7   rL   r   )r   r&   r   rN   rM   r   r   )r8   r   r   r      s
    			zControllerEvent.__init__)r   r   r   r   r   r   r   )r8   r   rL      s   rL   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)PitchWheelEventz?
    A class that encapsulates a pitch wheel change event.
    r   r   c                s5   | |  _  | |  _ t t |   j d | | |  d  S)Nr"   )r&   pitch_wheel_valuer7   rO   r   )r   r&   r   rP   r   r   )r8   r   r   r      s    		zPitchWheelEvent.__init__)r   r   r   r   r   r   r   )r8   r   rO      s   rO   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)	TrackNamez;
    A class that encapsulates a program change event.
    r   c                s5   | j  d  |  _ t t |   j d | | |  d  S)Nz
ISO-8859-1r    )r;   r    r7   rQ   r   )r   r   r    r   r   )r8   r   r   r      s    zTrackName.__init__)r   r   r   r   r   r   r   )r8   r   rQ      s   rQ   c                   s.   e  Z d  Z d Z d d   f d d  Z   S)TimeSignaturez5
    A class that encapsulates a time signature.
    r   c                sG   | |  _  | |  _ | |  _ | |  _ t t |   j d | | |  d  S)NrR   )	numeratordenominatorclocks_per_ticknotes_per_quarterr7   rR   r   )r   r   rS   rT   rU   rV   r   r   )r8   r   r   r     s    				zTimeSignature.__init__)r   r   r   r   r   r   r   )r8   r   rR      s   rR   c               @   sW  e  Z d  Z d Z d d   Z d d d d  Z d d d	  Z d d
 d  Z d d d  Z d d d  Z	 d d d d d  Z
 d d d  Z d d d  Z d d d  Z d d d  Z d d d  Z d d d  Z d d  d d d! d"  Z d# d$   Z d% d&   Z d' d(   Z d) d*   Z d+ d,   Z d- d.   Z d/ d0   Z d1 d2   Z d S)3	MIDITrackz0
    A class that encapsulates a MIDI track
    c             C   sa   t  j d d d d d  |  _ d |  _ d |  _ d |  _ g  |  _ g  |  _ | |  _ | |  _	 d	 S)
z)Initialize the MIDITrack object.
        cccc   M   T   r   kr       FN)
structpackheaderString
dataLengthMIDIdataclosed	eventListMIDIEventListremdepdeinterleave)r   removeDuplicatesrg   r   r   r   r     s    						zMIDITrack.__init__Nr   c             C   s2   |  j  j t | | | | | d | d |  d S)z5
        Add a note by chromatic MIDI number
        r6   r   N)rd   appendr3   )r   r&   r%   r   r4   r5   r6   r   r   r   r   addNoteByNumber  s    zMIDITrack.addNoteByNumberc             C   s)   |  j  j t | | | | d |  d S)z)
        Add a controller event.
        r   N)rd   ri   rL   )r   r&   r   rN   rM   r   r   r   r   addControllerEvent'  s    zMIDITrack.addControllerEventc             C   s&   |  j  j t | | | d |  d S)z*
        Add a pitch wheel event.
        r   N)rd   ri   rO   )r   r&   r   rP   r   r   r   r   addPitchWheelEvent1  s    zMIDITrack.addPitchWheelEventc             C   s#   |  j  j t | | d |  d S)z4
        Add a tempo change (or set) event.
        r   N)rd   ri   r9   )r   r   r   r   r   r   r   addTempo7  s    zMIDITrack.addTempoc             C   s&   |  j  j t | | | d |  d S)z$
        Add a SysEx event.
        r   N)rd   ri   rD   )r   r   rE   rF   r   r   r   r   addSysEx>  s    zMIDITrack.addSysEx   Fc          
   C   s/   |  j  j t | | | | | | d |  d S)z.
        Add a Universal SysEx event.
        r   N)rd   ri   rG   )r   r   rJ   rK   rF   rI   rH   r   r   r   r   addUniversalSysExE  s    zMIDITrack.addUniversalSysExc             C   s&   |  j  j t | | | d |  d S)z-
        Add a program change event.
        r   N)rd   ri   rC   )r   r&   r   programr   r   r   r   addProgramChangeO  s    zMIDITrack.addProgramChangec             C   s#   |  j  j t | | d |  d S)z)
        Add a track name event.
        r   N)rd   ri   rQ   )r   r   r    r   r   r   r   addTrackNameV  s    zMIDITrack.addTrackNamec          	   C   s,   |  j  j t | | | | | d |  d S)z'
        Add a time signature.
        r   N)rd   ri   rR   )r   r   rS   rT   rU   rV   r   r   r   r   addTimeSignature]  s    	zMIDITrack.addTimeSignaturec             C   s#   |  j  j t | | d |  d S)z(
        Add a copyright notice
        r   N)rd   ri   r:   )r   r   r<   r   r   r   r   addCopyrightf  s    zMIDITrack.addCopyrightc             C   s)   |  j  j t | | | | d |  d S)z(
        Add a copyright notice
        r   N)rd   ri   r?   )r   r   r@   rA   rB   r   r   r   r   addKeySignaturem  s    zMIDITrack.addKeySignaturec             C   s#   |  j  j t | | d |  d S)z"
        Add a text event
        r   N)rd   ri   r=   )r   r   r>   r   r   r   r   addTextv  s    zMIDITrack.addTextTc          
   C   s   t  j d |  } | t  j d t |   } x] | D]U \ } } | t  j d |  } t |  }	 x$ |	 D] }
 | t  j d |
  } qj Wq5 W|  j j t d | | d d | d |  d S)z1
        Change the tuning of MIDI notes
        z>Br         r   N)r^   r_   lenfrequencyTransformrd   ri   rG   )r   tuningsrI   rH   tuningProgamr   rF   Z
noteNumber	frequencyZMIDIFreqencybyter   r   r   changeNoteTuning}  s    zMIDITrack.changeNoteTuningc             C   s2  x|  j  D]} | j d k r t d | j t | j | j  } | j | _ | j | _ | j	 | _	 |  j
 j |  t d | j | j t | j d | j  } | j | _ | j | _ | j	 | _	 |  j
 j |  q
 | j d k r)t d | j t | j | j  } | j | _ |  j
 j |  q
 | j d k ryt d | j t | j | j  } | j | _ |  j
 j |  q
 | j d k rt d | j t | j | j  } | j | _ |  j
 j |  q
 | j d	 k r1t d	 | j t | j | j  } | j | _ | j | _ | j | _ |  j
 j |  q
 | j d
 k rt d | j t | j | j  } | j | _ | j	 | _	 |  j
 j |  q
 | j d k rt d | j t | j | j  } | j | _ |  j
 j |  q
 | j d k rEt d | j t | j | j  } | j | _ | j	 | _	 | j | _ |  j
 j |  q
 | j d k rt d | j t | j | j  } | j | _ | j	 | _	 |  j
 j |  q
 | j d k rt d | j t | j | j  } | j | _ | j | _ |  j
 j |  q
 | j d k r}t d | j t | j | j  } | j | _ | j | _ | j | _ | j | _ | j | _ |  j
 j |  q
 | j d k rt d | j t | j | j  } | j | _ | j | _ | j  | _  | j! | _! |  j
 j |  q
 t" d | j   q
 W|  j
 j# d t$  |  j% r.|  j&   d S)z
        Process the event list, creating a MIDIEventList

        For each item in the event list, one or more events in the MIDIEvent
        list are created.
        r   NoteOnNoteOffg?r   r9   r:   r=   r?   r   rC   r    rQ   r!   rL   r"   rO   r#   r$   rR   z)Error in MIDITrack: Unknown event type %skeyN)'rd   r   r   r   TICKSPERBEATr   r   r%   r5   r&   re   ri   r4   r   r<   r>   r@   rA   rB   r'   r    rN   rM   rP   rE   rF   rH   rI   rJ   rK   rS   rT   rU   rV   
ValueErrorsortsort_eventsrg   deInterleaveNotes)r   thingeventr   r   r   processEventList  s    
"	zMIDITrack.processEventListc             C   sB   d d   |  j  D } t | j    |  _  |  j  j d t  d S)z
        Remove duplicates from the eventList.

        This function will remove duplicates from the eventList. This is
        necessary because we the MIDI event stream can become confused
        otherwise.
        c             S   s   i  |  ] } d  |  q S)r   r   ).0itemr   r   r   
<dictcomp>  s   	 z.MIDITrack.removeDuplicates.<locals>.<dictcomp>r   N)rd   listkeysr   r   )r   ZtempDictr   r   r   rh     s    zMIDITrack.removeDuplicatesc             C   s7   |  j  r d Sd |  _  |  j r) |  j   |  j   d S)a=  
        Called to close a track before writing

        This function should be called to "close a track," that is to
        prepare the actual data stream for writing. Duplicate events are
        removed from the eventList, and the MIDIEventList is created.

        Called by the parent MIDIFile object.
        NT)rc   rf   rh   r   )r   r   r   r   
closeTrack  s    			
zMIDITrack.closeTrackc             C   sP   |  j    |  j t j d d d d d  7_ t j d t |  j   |  _ d S)zN
        Write the meta data and note data to the packed MIDI stream.
        ZBBBBr      /   z>LN)writeEventsToStreamrb   r^   r_   rz   ra   )r   r   r   r   writeMIDIStream&  s    
$zMIDITrack.writeMIDIStreamc             C   s  d } d } x |  j  D] } | | j } d } t | j  } x$ | D] } | t j d |  } qE Wt d |  \ } } | | }	 | |	 }
 | j |
 | _ d } t | j  } x$ | D] } | t j d |  } q Wt d |  \ } } | | } q Wx|  j  D]} | j d k rd+ | j B} t | j  } x) | D]! } |  j t j d |  7_ q?W|  j t j d |  7_ |  j t j d | j	  7_ |  j t j d | j
  7_ q| j d k rod, | j B} t | j  } x) | D]! } |  j t j d |  7_ qW|  j t j d |  7_ |  j t j d | j	  7_ |  j t j d | j
  7_ q| j d
 k rMd } d } t j d | j  } | d d  } t | j  } x) | D]! } |  j t j d |  7_ qW|  j t j d |  7_ |  j t j d |  7_ |  j t j d d  7_ |  j | 7_ q| j d k rEd } d } t | j  } x) | D]! } |  j t j d |  7_ q~W|  j t j d |  7_ |  j t j d |  7_ t | j  } t |  } x9 t t |   D]% } |  j t j d | |  7_ qW|  j | j 7_ q| j d k r=d } d } t | j  } x) | D]! } |  j t j d |  7_ qvW|  j t j d |  7_ |  j t j d |  7_ t | j  } t |  } x9 t t |   D]% } |  j t j d | |  7_ qW|  j | j 7_ q| j d k r_d } d } t | j  } x) | D]! } |  j t j d |  7_ qnW|  j t j d |  7_ |  j t j d |  7_ |  j t j d d  7_ |  j t j d | j  7_ |  j t j d | j  7_ |  j t j d | j  7_ |  j t j d | j  7_ q| j d k rRd } d } d } t | j  } x) | D]! } |  j t j d |  7_ qW|  j t j d |  7_ |  j t j d |  7_ |  j t j d |  7_ |  j t j d | j | j  7_ |  j t j d | j  7_ q| j d k rd- | j B} t | j  } x) | D]! } |  j t j d |  7_ qW|  j t j d |  7_ |  j t j d | j  7_ q| j d k rt | j  } x) | D]! } |  j t j d |  7_ q
W|  j t j d d  7_ |  j t j d d  7_ t | j  } t |  } x< t d t |   D]% } |  j t j d | |  7_ qW|  j | j 7_ q| j d k r	d. | j B} t | j  } x) | D]! } |  j t j d |  7_ q	W|  j t j d |  7_ |  j t j d | j  7_ |  j t j d | j  7_ q| j d k rV
d/ | j B} t | j  } x* | D]" } |  j t j d |  |  _ q	W| j d! d" ?} | j d! d# @} |  j t j d |  |  _ |  j t j d |  |  _ |  j t j d |  |  _ q| j d$ k rTd% } t | j  } x) | D]! } |  j t j d |  7_ q
W|  j t j d |  7_ t t | j  d  } x) | D]! } |  j t j d |  7_ q
W|  j t j d | j  7_ |  j | j 7_ |  j t j d d&  7_ q| j d' k rd% } t | j  } x) | D]! } |  j t j d |  7_ qW|  j t j d |  7_ t t | j  d(  } x) | D]! } |  j t j d |  7_ qW| j r+|  j t j d d#  7_ n |  j t j d d)  7_ |  j t j d | j  7_ |  j t j d | j   7_ |  j t j d | j!  7_ |  j | j 7_ |  j t j d d&  7_ qWd* S)0zD
        Write the events in MIDIEvents to the MIDI stream.
        g        r]   z>Br   r   r-      r   rx   r9   r   Q   z>Lr   r.   r=   br:   ry   rR   X   r?   Y   z>brC   r*   rQ   BrL      rO      i       ro   r#         r$   r,   ~   N               )"re   r   writeVarLengthr^   r_   readVarLengthr   r&   rb   r%   r5   r   rz   r>   ranger<   rS   rT   rU   rV   r@   rA   rB   r'   r    rN   rM   rP   rF   rE   rH   rI   rJ   rK   )r   ZpreciseTimeZ
actualTimer   Z
testBufferZvarTimeZtimeByteZ
roundedValdiscardZroundedTimedeltarJ   rK   ZfourbiteZ	threebiteZpayloadLengthZpayloadLengthVariZevent_subtypera   ZdataLenghtVarZMSBZLSBZlenByter   r   r   r   7  sH   

!!##!!!#! 	zMIDITrack.writeEventsToStreamc             C   s  g  } i  } xZ|  j  D]O} | j d k r t | j  t | j  | k rz | t | j  t | j  j | j  n& | j g | t | j  t | j  <| j |  q | j d k rXt | t | j  t | j   d k r$| t | j  t | j  j   | _ | j |  qe| t | j  t | j  j   | j |  q | j |  q W| |  _  |  j  j	 d t
  d S)a7  
        Correct Interleaved notes.

        Because we are writing multiple notes in no particular order, we
        can have notes which are interleaved with respect to their start
        and stop times. This method will correct that. It expects that the
        MIDIEventList has been time-ordered.
        r   r   r   r   N)re   r   strr%   r&   ri   r   rz   popr   r   )r   tempEventListstackr   r   r   r   r     s"    
"-&,)$	zMIDITrack.deInterleaveNotesc             C   s   t  |  j  d k r d Sg  } | r+ | n d } d } x> |  j D]3 } | j | } | | | _ | } | j |  qA W| |  _ d S)z
        Adjust Times to be relative, and zero-origined.

        If adjust is True, the track will be shifted. Regardelss times
        are converted to relative values here.
        r   Ng        )rz   re   r   ri   )r   originZadjustr   Zinternal_originZrunningTimer   adjustedTimer   r   r   adjustTimeAndOrigin  s    zMIDITrack.adjustTimeAndOriginc             C   s4   | j  |  j  | j  |  j  | j  |  j  d S)z&
        Write track to disk.
        N)writer`   ra   rb   )r   
fileHandler   r   r   
writeTrack2  s    zMIDITrack.writeTrack)r   r   r   r   r   rj   rk   rl   rm   rn   rp   rr   rs   rt   ru   rv   rw   r   r   rh   r   r   r   r   r   r   r   r   r   r   rW     s2   		t'rW   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)
MIDIHeaderaJ  
    Class to encapsulate the MIDI header structure.

    This class encapsulates a MIDI header structure. It isn't used for much,
    but it will create the appropriately packed identifier string that all
    MIDI files should contain. It is used by the MIDIFile class to create a
    complete and well formed MIDI pattern.

    c             C   s   t  j d d d d d  |  _ t  j d d  |  _ t  j d |  |  _ | |  _ | d	 k rc d	 n d
 } t  j d | |  |  _ t  j d t  |  _ d S)z( Initialize the data structures
        rX   rY   rZ      h   dz>L   z>Hr   r   N)	r^   r_   r`   
headerSizeformatnumeric_format	numTracksr   ticksPerBeat)r   r   file_formatr   r   r   r   r   F  s    	zMIDIHeader.__init__c             C   sT   | j  |  j  | j  |  j  | j  |  j  | j  |  j  | j  |  j  d  S)N)r   r`   r   r   r   r   )r   r   r   r   r   	writeFileR  s
    zMIDIHeader.writeFileN)r   r   r   r   r   r   r   r   r   r   r   <  s   	r   c               @   sT  e  Z d  Z d Z d d d d d d d  Z d d d  Z d	 d
   Z d d d  Z d d   Z d d   Z	 d d d  Z
 d d   Z d d   Z d d   Z d d   Z d d d  Z d d  d!  Z d d" d#  Z d d$ d%  Z d& d d d' d(  Z d) d*   Z d& d d+ d,  Z d- d.   Z d d/ d0  Z d1 d2   Z d3 d4   Z d S)5r   a  
    A class that encapsulates a full, well-formed MIDI file object.

    This is a container object that contains a header (:class:`MIDIHeader`),
    one or more tracks (class:`MIDITrack`), and the data associated with a
    proper and well-formed MIDI file.
    r   TNc             C   s   t  | |  |  _ t   |  _ | d k r3 d } n d } | | |  _ d |  _ | d k rw d |  _ t j d t	  n	 | |  _ x3 t
 d |  j  D] } |  j j t | |   q Wd |  _ d S)a  

            Initialize the MIDIFile class

            :param numTracks: The number of tracks the file contains. Integer,
                one or greater
            :param removeDuplicates: If set to ``True`` remove duplicate events
                before writing to disk
            :param deinterleave: If set to ``True`` deinterleave the notes in
                the stream
            :param adjust_origin: If set to ``True`` (or left at the default of
                ``None``) shift all the events in the tracks so that the first
                event takes place at time t=0
            :param file_format: The format of the multi-track file. This should
                either be ``1`` (the default, and the most widely supported
                format) or ``2``.

            Note that the default for ``adjust_origin`` will change in a future
            release, so one should probably explicitly set it.

            Example:

            .. code::

                # Create a two-track MIDIFile

                from midiutil.MidiFile import MIDIFile
                midi_file = MIDIFile(2)

            A Note on File Formats
            ----------------------

            In previous versions of this code the file written was format 2
            (which can be thought of as a collection of independent tracks) but
            was identified as format 1. In this version one can specify either
            format 1 or 2.

            In format 1 files there is a separate tempo track which contains
            tempo and time signature data, but contains no note data. If one
            creates a single track format 1 file the actual file has two tracks
            -- one for tempo data and one for note data. In the track indexing
            the tempo track can be ignored. In other words track 0 is the note
            track (the second track in the file). However, tempo and time
            signature data will be written to the first, tempo track. This is
            done to try and preserve as much interoperability with previous
            versions as possible.

            In a format 2 file all tracks are indexed and the track parameter
            is interpreted literally.
        r   r   FNTzWPlease explicitly set adjust_origin. Default behaviour will change in a future version.)r   headerr   tracksr   rc   adjust_originwarningswarnFutureWarningr   ri   rW   event_counter)r   r   rh   rg   r   r   r   r   r   r   r   r   c  s    4				
	zMIDIFile.__init__c          
   C   s^   |  j  j d k r | d 7} |  j | j | | | | | d | d |  j |  j d 7_ d S)a_  

        Add notes to the MIDIFile object

        :param track: The track to which the note is added.
        :param channel: the MIDI channel to assign to the note. [Integer, 0-15]
        :param pitch: the MIDI pitch number [Integer, 0-127].
        :param time: the time (in beats) at which the note sounds [Float].
        :param duration: the duration of the note (in beats) [Float].
        :param volume: the volume (velocity) of the note. [Integer, 0-127].
        :param annotation: Arbitrary data to attach to the note.

        The ``annotation`` parameter attaches arbitrary data to the note. This
        is not used in the code, but can be useful anyway. As an example,
        I have created a project that uses MIDIFile to write
        `csound <http://csound.github.io/>`_ orchestra files directly from the
        class ``EventList``.
        r   r6   r   N)r   r   r   rj   r   )r   trackr&   r%   r   r4   r5   r6   r   r   r   addNote  s    

zMIDIFile.addNotec             C   sO   |  j  j d k r | d 7} |  j | j | | d |  j |  j d 7_ d S)aU  
        Name a track.

        :param track: The track to which the name is assigned.
        :param time: The time (in beats) at which the track name event is
            placed.  In general this should probably be time 0 (the beginning
            of the track).
        :param trackName: The name to assign to the track [String]
        r   r   N)r   r   r   rs   r   )r   r   r   r    r   r   r   rs     s
    


zMIDIFile.addTrackNamerx   c             C   sT   |  j  j d k r d } |  j | j | | | | | d |  j |  j d 7_ d S)a  
        Add a time signature event.

        :param track: The track to which the signature is assigned. Note that
            in a format 1 file this parameter is ignored and the event is
            written to the tempo track
        :param time: The time (in beats) at which the event is placed.
            In general this should probably be time 0 (the beginning of the
            track).
        :param numerator: The numerator of the time signature. [Int]
        :param denominator: The denominator of the time signature, expressed as
            a power of two (see below). [Int]
        :param clocks_per_tick: The number of MIDI clock ticks per metronome
            click (see below).
        :param notes_per_quarter: The number of annotated 32nd notes in a MIDI
            quarter note. This is almost always 8 (the default), but some
            sequencers allow this value to be changed. Unless you know that
            your sequencing software supports it, this should be left at its
            default value.

        The data format for this event is a little obscure.

        The ``denominator`` should be specified as a power of 2, with
        a half note being one, a quarter note being two, and eight note
        being three, etc. Thus, for example, a 4/4 time signature would
        have a ``numerator`` of 4 and a ``denominator`` of 2. A 7/8 time
        signature would be a ``numerator`` of 7 and a ``denominator``
        of 3.

        The ``clocks_per_tick`` argument specifies the number of clock
        ticks per metronome click. By definition there are 24 ticks in
        a quarter note, so a metronome click per quarter note would be
        24. A click every third eighth note would be 3 * 12 = 36.
        r   r   r   N)r   r   r   rt   r   )r   r   r   rS   rT   rU   rV   r   r   r   rt     s    $	
zMIDIFile.addTimeSignaturec             C   sK   |  j  j d k r d } |  j | j | | d |  j |  j d 7_ d S)a{  

        Add notes to the MIDIFile object

        :param track: The track to which the tempo event  is added. Note that
            in a format 1 file this parameter is ignored and the tempo is
            written to the tempo track
        :param time: The time (in beats) at which tempo event is placed
        :param tempo: The tempo, in Beats per Minute. [Integer]
        r   r   r   N)r   r   r   rm   r   )r   r   r   r   r   r   r   rm     s
    
zMIDIFile.addTempoc             C   sO   |  j  j d k r | d 7} |  j | j | | d |  j |  j d 7_ d S)a,  

        Add a copyright notice to the MIDIFile object

        :param track: The track to which the notice is added.
        :param time: The time (in beats) at which notice event is placed. In
            general this sould be time t=0
        :param notice: The copyright notice [String]
        r   r   N)r   r   r   ru   r   )r   r   r   r<   r   r   r   ru     s
    


zMIDIFile.addCopyrightr   c             C   sU   |  j  j d k r | d 7} |  j | j | | | | d |  j |  j d 7_ d S)a  
        Add a Key Signature to a track

        :param track: The track to which this should be added
        :param time: The time at which the signature should be placed
        :param accidentals: The number of accidentals in the key signature
        :param accidental_type: The type of accidental
        :param mode: The mode of the scale

        The easiest way to use this function is to make sure that the symbolic
        constants for accidental_type and mode are imported. By doing this:

        .. code::

            from midiutil.MidiFile import *

        one gets the following constants defined:

        * ``SHARPS``
        * ``FLATS``
        * ``MAJOR``
        * ``MINOR``

        So, for example, if one wanted to create a key signature for a minor
        scale with three sharps:

        .. code::

            MyMIDI.addKeySignature(0, 0, 3, SHARPS, MINOR)
        r   r   N)r   r   r   rv   r   )r   r   r   r@   rA   rB   r   r   r   r   rv   (  s     

zMIDIFile.addKeySignaturec             C   sO   |  j  j d k r | d 7} |  j | j | | d |  j |  j d 7_ d S)z

        Add a text event

        :param track: The track to which the notice is added.
        :param time: The time (in beats) at which text event is placed.
        :param text: The text to adde [ASCII String]
        r   r   N)r   r   r   rw   r   )r   r   r   r>   r   r   r   rw   O  s
    	

zMIDIFile.addTextc             C   s6   |  j  | j | | | d |  j |  j d 7_ d S)a  

        Add a MIDI program change event.

        :param track: The track to which program change event is added.
        :param channel: the MIDI channel to assign to the event.
            [Integer, 0-15]
        :param time: The time (in beats) at which the program change event is
            placed [Float].
        :param program: the program number. [Integer, 0-127].
        r   r   N)r   rr   r   )r   r   r&   r   rq   r   r   r   rr   ^  s    
zMIDIFile.addProgramChangec             C   sU   |  j  j d k r | d 7} |  j | j | | | | d |  j |  j d 7_ d S)a  

        Add a channel control event

        :param track: The track to which the event is added.
        :param channel: the MIDI channel to assign to the event.
            [Integer, 0-15]
        :param time: The time (in beats) at which the event is placed [Float].
        :param controller_number: The controller ID of the event.
        :param parameter: The event's parameter, the meaning of which varies by
            event type.
        r   r   N)r   r   r   rk   r   )r   r   r&   r   rN   rM   r   r   r   rk   n  s
    
zMIDIFile.addControllerEventc             C   sR   |  j  j d k r | d 7} |  j | j | | | d |  j |  j d 7_ d S)a]  

        Add a channel pitch wheel event

        :param track: The track to which the event is added.
        :param channel: the MIDI channel to assign to the event. [Integer, 0-15]
        :param time: The time (in beats) at which the event is placed [Float].
        :param pitchWheelValue: 0 for no pitch change. [Integer, -8192-8192]
        r   r   N)r   r   r   rl   r   )r   r   r&   r   ZpitchWheelValuer   r   r   rl     s    

#zMIDIFile.addPitchWheelEventFc	       
      C   s.  |  j  j d k r | d 7} | r0 d t d n d }	 |  j | j | | d | d |  j |  j d 7_ |  j | j | | |	 d | d |  j |  j d 7_ |  j | j | | d |	 d	 | d |  j |  j d 7_ | d
 k	 r*|  j | j | | d |	 d | d |  j |  j d 7_ d
 S)a  

        Perform a Registered Parameter Number Call

        :param track: The track to which this applies
        :param channel: The channel to which this applies
        :param time: The time of the event
        :param controller_msb: The Most significant byte of the controller. In
            common usage this will usually be 0
        :param controller_lsb: The Least significant Byte for the controller
            message. For example, for a fine-tuning change this would be 01.
        :param data_msb: The Most Significant Byte of the controller's
            parameter.
        :param data_lsb: The Least Significant Byte of the controller's
            parameter. If not needed this should be set to ``None``
        :param time_order: Order the control events in time (see below)

        As an example, if one were to change a channel's tuning program::

            makeRPNCall(track, channel, time, 0, 3, 0, program)

        (Note, however, that there is a convenience function,
        ``changeTuningProgram``, that does this for you.)

        The ``time_order`` parameter is something of a work-around for
        sequencers that do not preserve the order of events from the MIDI files
        they import. Within this code care is taken to preserve the order of
        events as specified, but some sequencers seem to transmit events
        occurring at the same time in an arbitrary order.  By setting this
        parameter to ``True`` something of a work-around is performed: each
        successive event (of which there are three or four for this event type)
        is placed in the time stream a small delta from the preceding one.
        Thus, for example, the controllers are set before the data bytes in
        this call.
        r   g      ?r   g        e   r   d   g       @r   Ng      @&   )r   r   r   r   rk   r   )
r   r   r&   r   controller_msbcontroller_lsbdata_msbdata_lsb
time_orderr   r   r   r   makeRPNCall  s     %
zMIDIFile.makeRPNCallc	       
      C   s.  |  j  j d k r | d 7} | r0 d t d n d }	 |  j | j | | d | d |  j |  j d 7_ |  j | j | | |	 d | d |  j |  j d 7_ |  j | j | | d |	 d	 | d |  j |  j d 7_ | d
 k	 r*|  j | j | | d |	 d | d |  j |  j d 7_ d
 S)a  

        Perform a Non-Registered Parameter Number Call

        :param track: The track to which this applies
        :param channel: The channel to which this applies
        :param time: The time of the event
        :param controller_msb: The Most significant byte of thecontroller. In
            common usage this will usually be 0
        :param controller_lsb: The least significant byte for the controller
            message. For example, for a fine-tunning change this would be 01.
        :param data_msb: The most significant byte of the controller's
            parameter.
        :param data_lsb: The least significant byte of the controller's
            parameter. If none is needed this should be set to ``None``
        :param time_order: Order the control events in time (see below)

        The ``time_order`` parameter is something of a work-around for
        sequencers that do not preserve the order of events from the MIDI files
        they import. Within this code care is taken to preserve the order of
        events as specified, but some sequencers seem to transmit events
        occurring at the same time in an arbitrary order.  By setting this
        parameter to ``True`` something of a work-around is performed: each
        successive event (of which there are three or four for this event type)
        is placed in the time stream a small delta from the preceding one.
        Thus, for example, the controllers are set before the data bytes in
        this call.

        r   g      ?r   g        c   r   b   ry   r   Nr.   r   )r   r   r   r   rk   r   )
r   r   r&   r   r   r   r   r   r   r   r   r   r   makeNRPNCall  s     
zMIDIFile.makeNRPNCallc          
   C   s)   |  j  | | | d d d | d | d S)a  

        Change the tuning bank for a selected track

        :param track: The track to which the data should be written
        :param channel: The channel for the event
        :param time: The time of the event
        :param bank: The tuning bank (0-127)
        :param time_order: Preserve the ordering of the component events by
            ordering in time. See ``makeRPNCall()`` for a discussion of when
            this may be necessary

        Note that this is a convenience function, as the same
        functionality is available from directly sequencing controller
        events.

        The specified tuning should already have been written to the
        stream with ``changeNoteTuning``.  r   r   r   N)r   )r   r   r&   r   Zbankr   r   r   r   changeTuningBank  s    zMIDIFile.changeTuningBankc          
   C   s)   |  j  | | | d d d | d | d S)a  

        Change the tuning program for a selected track

        :param track: The track to which the data should be written
        :param channel: The channel for the event
        :param time: The time of the event
        :param program: The tuning program number (0-127)
        :param time_order: Preserve the ordering of the component events by
            ordering in time. See ``makeRPNCall()`` for a discussion of when
            this may be necessary

        Note that this is a convenience function, as the same
        functionality is available from directly sequencing controller
        events.

        The specified tuning should already have been written to the
        stream with ``changeNoteTuning``.  r   r.   r   N)r   )r   r   r&   r   rq   r   r   r   r   changeTuningProgram  s    zMIDIFile.changeTuningProgramro   c             C   sU   |  j  j d k r | d 7} |  j | j | | | | d |  j |  j d 7_ d S)a>  
        Add a real-time MIDI tuning standard update to a track.

        :param track: The track to which the tuning is applied.
        :param tunings: A list to tuples representing the tuning. See below for
            an explanation.
        :param sysExChannel: The SysEx channel of the event. This is mapped to
            "manufacturer ID" in the event which is written. Unless there is a
            specific reason for changing it, it should be left at its default
            value.
        :param realTime: Speicifes if the Universal SysEx event should be
            flagged as real-time or non-real-time. As with the ``sysExChannel``
            argument, this should in general be left at it's default value.
        :param tuningProgram: The tuning program number.

        This function specifically implements the "real time single note tuning
        change" (although the name is misleading, as multiple notes can be
        included in each event). It should be noted that not all hardware or
        software implements the MIDI tuning standard, and that which does often
        does not implement it in its entirety.

        The ``tunings`` argument is a list of tuples, in (*note number*,
        *frequency*) format.  As an example, if one wanted to change the
        frequency on MIDI note 69 to 500 (it is normally 440 Hz), one could do
        it thus:

        .. code:: python

            from midiutil.MidiFile import MIDIFile
            MyMIDI = MIDIFile(1)
            tuning = [(69, 500)]
            MyMIDI.changeNoteTuning(0, tuning, tuningProgam=0)
        r   r   N)r   r   r   r   r   )r   r   r|   rI   rH   r}   r   r   r   r   $  s    #

zMIDIFile.changeNoteTuningc             C   sR   |  j  j d k r | d 7} |  j | j | | | d |  j |  j d 7_ d S)a  

        Add a System Exclusive event.

        :param track: The track to which the event should be written
        :param time: The time of the event.
        :param manID: The manufacturer ID for the event
        :param payload: The payload for the event. This should be a
            binary-packed value, and will vary for each type and function.

        **Note**: This is a low-level MIDI function, so care must be used in
        constructing the payload. It is recommended that higher-level helper
        functions be written to wrap this function and construct the payload if
        a developer finds him or herself using the function heavily.

        r   r   N)r   r   r   rn   r   )r   r   r   rE   rF   r   r   r   rn   N  s
    

zMIDIFile.addSysExc          	   C   s[   |  j  j d k r | d 7} |  j | j | | | | | | d |  j |  j d 7_ d S)a8  

        Add a Univeral System Exclusive event.

        :param track: The track to which the event should be written
        :param time: The time of the event, in beats.
        :param code: The event code. [Integer]
        :param subcode: The event sub-code [Integer]
        :param payload: The payload for the event. This should be a
            binary-packed value, and will vary for each type and function.
        :param sysExChannel: The SysEx channel.
        :param realTime: Sets the real-time flag. Defaults to non-real-time.
        :param manID: The manufacturer ID for the event


        **Note**: This is a low-level MIDI function, so care must be used in
        constructing the payload. It is recommended that higher-level helper
        functions be written to wrap this function and construct the payload if
        a developer finds him or herself using the function heavily. As an
        example of such a helper function, see the ``changeNoteTuning()``
        function, which uses the event to create a real-time note tuning
        update.

        r   r   N)r   r   r   rp   r   )r   r   r   rJ   rK   rF   rI   rH   r   r   r   rp   e  s    
	
zMIDIFile.addUniversalSysExc             C   sO   |  j  j |  |  j   x. t d |  j  D] } |  j | j |  q- Wd S)z
        Write the MIDI File.

        :param fileHandle: A file handle that has been opened for binary
            writing.
        r   N)r   r   closer   r   r   r   )r   r   r   r   r   r   r     s    
zMIDIFile.writeFilec             C   s   d } xR |  j  D]G } t | j  d k r x) | j D] } | j | k  r5 | j } q5 Wq Wx[ |  j  D]P } g  } x8 | j D]- } | j | } | | | _ | j |  q{ W| | _ qe Wd S)aS  Shift tracks to be zero-origined, or origined at offset.

        Note that the shifting of the time in the tracks uses the MIDIEventList
        -- in other words it is assumed to be called in the stage where the
        MIDIEventList has been created. This function, however, it meant to
        operate on the eventList itself.
        i@B r   N)r   rz   rd   r   ri   )r   offsetr   r   r   r   r   r   r   r   shiftTracks  s    zMIDIFile.shiftTracksc             C   s   |  j  r d SxE t d |  j  D]1 } |  j | j   |  j | j j d t  q  W|  j   } xE t d |  j  D]1 } |  j | j	 | |  j
  |  j | j   qt Wd |  _  d S)z
        Close the MIDIFile for further writing.

        To close the File for events, we must close the tracks, adjust the time
        to be zero-origined, and have the tracks write to their MIDI Stream
        data structure.
        Nr   r   T)rc   r   r   r   r   re   r   r   
findOriginr   r   r   )r   r   r   r   r   r   r     s    		zMIDIFile.closec             C   sY   d } xL |  j  D]A } t | j  d k r | j d j | k  r | j d j } q W| S)zE
        Find the earliest time in the file's tracks.append.
        i@B r   )r   rz   re   r   )r   r   r   r   r   r   r     s    zMIDIFile.findOrigin)r   r   r   r   r   r   rs   rt   rm   ru   rv   rw   rr   rk   rl   r   r   r   r   r   rn   rp   r   r   r   r   r   r   r   r   r   Z  s2   	M+&6/) c             C   s   t  |  d  } d d d d g } d d d d g } d } | d @} | | | <| d } | d ?} xB | d k r | d @} | d B} | | | <| d } | d ?} qe W| d | d <| d | d <| d | d <| d | d <| d	 | d	  S)
a_  
    Accept an input, and write a MIDI-compatible variable length stream

    The MIDI format is a little strange, and makes use of so-called variable
    length quantities. These quantities are a stream of bytes. If the most
    significant bit is 1, then more bytes follow. If it is zero, then the
    byte in question is the last in the stream
    g      ?r   ro   r   r   r   r.   ry   r   )r0   )r   inputoutputreversedcountresultr   r   r   r     s&    	







r   c             C   sy   |  } d } d } xZ | d >} t  j d | |  d } | d } | d } | | d @} | d @d k r Pq W| | f S)z
    A function to read a MIDI variable length variable.

    It returns a tuple of the value read and the number of bytes processed. The
    input is an offset into the buffer, and the buffer itself.
    r   r   z>Br   ro   r   )r^   unpack_from)r   bufferZtoffsetr   Z	bytesReadr   r   r   r   r     s    


r   c       	      C   s"  d } t  |   }  d d t j |  t  d  d  } t |  } d t d t  |  d d  } |  | k r d	 t j |  | d  n d
 } t | d |  } t t |  d ?d g  } | | d >} t | d g  } | d k r	| d k r	| d k r	d } t |  } | | | g S)z8
    Returns a three-byte transform of a frequency.
    i @  E   r*   i  ry   g       @g     @Q@g      (@i  r   r   r   ro   r   )floatmathlogr0   powroundmin)	Zfreq
resolutionZdollarsZ	firstByteZ	lowerFreqZcentDifZcentsZ
secondByteZ	thirdByter   r   r   r{     s    $!,$r{   c             C   sv   d } d t  d t |  d d  d  } t t |  d  d >t |  d	   d
 | } | t  d | d  } | S)zU
    The reverse of frequencyTransform. Given a byte stream, return a frequency.
    g      @i  g       @r   g     @Q@g      (@r   r   ry   g      Y@g     @)r   r   r0   )Z	freqBytesr   ZbaseFrequencyfracr~   r   r   r   returnFrequency/  s    %%r   c             C   s   |  j  |  j |  j f S)aR  
    .. py:function:: sort_events(event)

        The key function used to sort events (both MIDI and Generic)

        :param event: An object of type :class:`MIDIEvent` or (a derrivative)
            :class:`GenericEvent`

        This function should be provided as the ``key`` for both
        ``list.sort()`` and ``sorted()``. By using it sorting will be as
        follows:

        * Events are ordered in time. An event that takes place earlier will
          appear earlier
        * If two events happen at the same time, the secondary sort key is
          ``ord``. Thus a class of events can be processed earlier than
          another. One place this is used in the code is to make sure that note
          off events are processed before note on events.
        * If time and ordinality are the same, they are sorted in the order in
          which they were originally added to the list. Thus, for example, if
          one is making an RPN call one can specify the controller change
          events in the proper order and be sure that they will end up in the
          file that way.
    )r   r   r   )r   r   r   r   r   ;  s    r   )%
__future__r   r   r   r^   r   __version__r   ZcontrollerEventTypesr	   r
   r   r   __all__objectr   r   r3   r9   r:   r=   r?   rC   rD   rG   rL   rO   rQ   rR   rW   r   r   r   r   r{   r   r   r   r   r   r   <module>   sJ   H	
	  1  !