3
ZX                @   s  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G dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd  d eZG d!d" d"eZG d#d$ d$eZG d%d& d&eZG d'd( d(eZG d)d* d*eZG d+d, d,eZG d-d. d.eZG d/d deZ d0d1 Z!d2d3 Z"d4d5 Z#d6d7 Z$d8d9 Z%dS );    )divisionprint_functionNHEADi  pan
      MIDIFileMAJORMINORSHARPSFLATSc               @   s0   e Zd Zd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
    Nr   c             C   s   || _ || _d S )N)tickinsertion_order)selfr   r    r   3/usr/really-local/MIDIUtil/src/midiutil/MidiFile.py__init__,   s    zGenericEvent.__init__c             C   s   | j |j ko| j|jkS )a  
        Equality operator.

        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).

        Some derived classes will need to override and consider their specific
        attributes in the comparison.
        )evtnamer   )r   otherr   r   r   __eq__0   s    zGenericEvent.__eq__c             C   sn   t | j}|d |d>  }|dA |d? A }|d |d>  }|d |d> A }|d	 |d
>  }|dA |d? A }|S )a  
        Return a hash code for the object.

        This is needed in order to allow GenericObject classes to be used
        as the key in a dict or set. duplicate objects are removed from
        the event list by storing all the objects in a set, and then
        reconstructing the list from the set.  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__>   s    
zGenericEvent.__hash__)	__name__
__module____qualname____doc__r   sec_sort_orderr   r   r   r   r   r   r   r   %   s   r   c                   sL   e Zd ZdZd ZdZdZd fdd	Zdd	 Ze	j
Z
d
d Zdd Z  ZS )NoteOnz*
    A class that encapsulates a note
       r   Nr   c                s4   || _ || _|| _|| _|| _tt| j|| d S )N)pitchdurationvolumechannel
annotationsuperr%   r   )r   r*   r'   r   r(   r)   r+   r   )	__class__r   r   r   ]   s    zNoteOn.__init__c             C   s0   | j |j ko.| j|jko.| j|jko.| j|jkS )N)r   r   r'   r*   )r   r   r   r   r   r   f   s    zNoteOn.__eq__c             C   s   d| j | j| j| j| jf S )Nz-NoteOn %d at tick %d duration %d ch %d vel %d)r'   r   r(   r*   r)   )r   r   r   r   __str__n   s    zNoteOn.__str__c             C   st   d}| j | jB }t| j| }x|D ]}|tjd|7 }q$W |tjd|7 }|tjd| j7 }|tjd| j7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
            z>B)midi_statusr*   writeVarLengthr   structpackr'   r)   )r   previous_event_tick	midibytescodevarTimetimeByter   r   r   	serializer   s    
zNoteOn.serialize)Nr   )r    r!   r"   r#   r   r0   r$   r   r   r   r   r.   r9   __classcell__r   r   )r-   r   r%   U   s   r%   c                   sL   e Zd ZdZd ZdZdZd fdd	Zdd	 Ze	j
Z
d
d Zdd Z  ZS )NoteOffz4
    A class that encapsulates a Note Off event
          Nr   c                s.   || _ || _|| _|| _tt| j|| d S )N)r'   r)   r*   r+   r,   r;   r   )r   r*   r'   r   r)   r+   r   )r-   r   r   r      s
    zNoteOff.__init__c             C   s0   | j |j ko.| j|jko.| j|jko.| j|jkS )N)r   r   r'   r*   )r   r   r   r   r   r      s    zNoteOff.__eq__c             C   s   d| j | j| j| jf S )Nz"NoteOff %d at tick %d ch %d vel %d)r'   r   r*   r)   )r   r   r   r   r.      s    zNoteOff.__str__c             C   st   d}| j | jB }t| j| }x|D ]}|tjd|7 }q$W |tjd|7 }|tjd| j7 }|tjd| j7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   z>B)r0   r*   r1   r   r2   r3   r'   r)   )r   r4   r5   r6   r7   r8   r   r   r   r9      s    
zNoteOff.serialize)Nr   )r    r!   r"   r#   r   r0   r$   r   r   r   r   r.   r9   r:   r   r   )r-   r   r;      s   r;   c                   s@   e Zd ZdZd ZdZd
 fdd	Zdd Zej	Z	dd	 Z
  ZS )Tempoz6
    A class that encapsulates a tempo meta-event
    r   r   c                s$   t d| | _tt| j|| d S )Ni )r   tempor,   r>   r   )r   r   r?   r   )r-   r   r   r      s    zTempo.__init__c             C   s$   | j |j ko"| j|jko"| j|jkS )N)r   r   r?   )r   r   r   r   r   r      s    zTempo.__eq__c       	      C   s   d}d}d}t jd| j}|dd }t| j| }x|D ]}|t jd|7 }q:W |t jd|7 }|t jd|7 }|t jdd7 }||7 }|S )	zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/      Q   z>Lr      z>Br   )r2   r3   r?   r1   r   )	r   r4   r5   r6   subcodefourbite	threebiter7   r8   r   r   r   r9      s    
zTempo.serialize)r   )r    r!   r"   r#   r   r$   r   r   r   r   r9   r:   r   r   )r-   r   r>      s   r>   c                   s2   e Zd ZdZd ZdZd fdd	Zdd Z  ZS )		Copyrightz5
    A class that encapsulates a copyright event
    r   r   c                s"   |j d| _tt| j|| d S )Nz
ISO-8859-1)encodenoticer,   rF   r   )r   r   rH   r   )r-   r   r   r      s    zCopyright.__init__c       
      C   s   d}d}d}t | j| }x|D ]}|tjd|7 }q W |tjd|7 }|tjd|7 }t| j}t |}x|D ]}	|tjd|	7 }qpW || j7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   r@   r=   z>Bb)r1   r   r2   r3   lenrH   )
r   r4   r5   r6   rC   r7   r8   payloadLengthpayloadLengthVarir   r   r   r9      s    



zCopyright.serialize)r   )	r    r!   r"   r#   r   r$   r   r9   r:   r   r   )r-   r   rF      s
   rF   c                   s2   e Zd ZdZd ZdZd fdd	Zdd Z  ZS )	Textz0
    A class that encapsulates a text event
    r   r   c                s"   |j d| _tt| j|| d S )Nz
ISO-8859-1)rG   textr,   rN   r   )r   r   rO   r   )r-   r   r   r     s    zText.__init__c       
      C   s   d}d}d}t | j| }x|D ]}|tjd|7 }q W |tjd|7 }|tjd|7 }t| j}t |}x|D ]}	|tjd|	7 }qpW || j7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   r@   r   z>BB)r1   r   r2   r3   rJ   rO   )
r   r4   r5   r6   rC   r7   r8   rK   rL   rM   r   r   r   r9     s    



zText.serialize)r   )	r    r!   r"   r#   r   r$   r   r9   r:   r   r   )r-   r   rN     s
   rN   c                   s2   e Zd ZdZd ZdZd fdd	Zdd Z  ZS )	KeySignaturez0
    A class that encapsulates a text event
    r   r   c                s(   || _ || _|| _tt| j|| d S )N)accidentalsaccidental_typemoder,   rQ   r   )r   r   rR   rS   rT   r   )r-   r   r   r   4  s    zKeySignature.__init__c             C   s   d}d}d}d}t | j| }x|D ]}|tjd|7 }q$W |tjd|7 }|tjd|7 }|tjd|7 }|tjd| j| j 7 }|tjd| j7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   r@   Y   r=   z>Bz>b)r1   r   r2   r3   rR   rS   rT   )r   r4   r5   r6   rC   event_subtyper7   r8   r   r   r   r9   ;  s    
zKeySignature.serialize)r   )	r    r!   r"   r#   r   r$   r   r9   r:   r   r   )r-   r   rQ   -  s
   rQ   c                   sD   e Zd ZdZd ZdZdZd fdd	Zdd Ze	j
Z
d	d
 Z  ZS )ProgramChangez;
    A class that encapsulates a program change event.
       r   r   c                s"   || _ || _tt| j|| d S )N)programNumberr*   r,   rW   r   )r   r*   r   rY   r   )r-   r   r   r   V  s    zProgramChange.__init__c             C   s0   | j |j ko.| j|jko.| j|jko.| j|jkS )N)r   r   rY   r*   )r   r   r   r   r   r   \  s    zProgramChange.__eq__c             C   s^   d}| j | jB }t| j}x|D ]}|tjd|7 }q W |tjd|7 }|tjd| j7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   z>B)r0   r*   r1   r   r2   r3   rY   )r   r4   r5   r6   r7   r8   r   r   r   r9   d  s    

zProgramChange.serialize)r   )r    r!   r"   r#   r   r0   r$   r   r   r   r   r9   r:   r   r   )r-   r   rW   N  s   rW   c                   s@   e Zd ZdZdZdZd fdd	Zdd Zej	Z	d	d
 Z
  ZS )
SysExEventz>
    A class that encapsulates a System Exclusive  event.
    SysExr   r   c                s"   || _ || _tt| j|| d S )N)manIDpayloadr,   rZ   r   )r   r   r\   r]   r   )r-   r   r   r   y  s    zSysExEvent.__init__c             C   s   dS )NFr   )r   r   r   r   r   r   ~  s    zSysExEvent.__eq__c             C   s   d}d}t | j| }x|D ]}|tjd|7 }qW |tjd|7 }t t| jd }x|D ]}|tjd|7 }q\W |tjd| j7 }|| j7 }|tjdd7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/      z>Br=      )r1   r   r2   r3   rJ   r]   r\   )r   r4   r5   r6   r7   r8   rK   lenByter   r   r   r9     s    


zSysExEvent.serialize)r   )r    r!   r"   r#   r   r$   r   r   r   r   r9   r:   r   r   )r-   r   rZ   r  s   rZ   c                   s@   e Zd ZdZdZdZd fdd	Zdd Zej	Z	d	d
 Z
  ZS )UniversalSysExEventzH
    A class that encapsulates a Universal System Exclusive  event.
    UniversalSysExr   r   c                s4   || _ || _|| _|| _|| _tt| j|| d S )N)realTimesysExChannelr6   rC   r]   r,   ra   r   )r   r   rc   rd   r6   rC   r]   r   )r-   r   r   r     s    zUniversalSysExEvent.__init__c             C   s   dS )NFr   )r   r   r   r   r   r     s    zUniversalSysExEvent.__eq__c             C   s   d}d}t | j| }x|D ]}|tjd|7 }qW |tjd|7 }t t| jd }x|D ]}|tjd|7 }q\W | jr|tjdd7 }n|tjdd7 }|tjd| j7 }|tjd| j7 }|tjd| j	7 }|| j7 }|tjdd7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   r^   z>Br      ~   r_   )
r1   r   r2   r3   rJ   r]   rc   rd   r6   rC   )r   r4   r5   r6   r7   r8   rK   r`   r   r   r   r9     s$    


zUniversalSysExEvent.serialize)r   )r    r!   r"   r#   r   r$   r   r   r   r   r9   r:   r   r   )r-   r   ra     s   ra   c                   sD   e Zd ZdZd ZdZdZd fdd	Zdd Ze	j
Z
d	d
 Z  ZS )ControllerEventz;
    A class that encapsulates a program change event.
       r   r   c                s(   || _ || _|| _tt| j|| d S )N)	parameterr*   controller_numberr,   rg   r   )r   r*   r   rj   ri   r   )r-   r   r   r     s    zControllerEvent.__init__c             C   s   dS )NFr   )r   r   r   r   r   r     s    zControllerEvent.__eq__c             C   st   d}| j | jB }t| j| }x|D ]}|tjd|7 }q$W |tjd|7 }|tjd| j7 }|tjd| j7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   z>B)r0   r*   r1   r   r2   r3   rj   ri   )r   r4   r5   r6   r7   r8   r   r   r   r9     s    
zControllerEvent.serialize)r   )r    r!   r"   r#   r   r0   r$   r   r   r   r   r9   r:   r   r   )r-   r   rg     s   rg   c                   sD   e Zd ZdZdZdZdZd fdd	Zdd	 Ze	j
Z
d
d Z  ZS )ChannelPressureEventzJ
    A class that encapsulates a Channel Pressure (Aftertouch) event.
    ChannelPressure   r   r   c                s"   || _ || _tt| j|| d S )N)r*   pressure_valuer,   rk   r   )r   r*   r   rn   r   )r-   r   r   r     s    zChannelPressureEvent.__init__c             C   s4   | j j|j jko2| j|jko2| j|jko2| j|jkS )N)r-   r    r   rn   r*   )r   r   r   r   r   r     s    zChannelPressureEvent.__eq__c             C   sb   d}| j | jB }t| j| }x|D ]}|tjd|7 }q$W |tjd|7 }|tjd| j7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   z>B)r0   r*   r1   r   r2   r3   rn   )r   r4   r5   r6   vartickxr   r   r   r9     s    
zChannelPressureEvent.serialize)r   )r    r!   r"   r#   r   r0   r$   r   r   r   r   r9   r:   r   r   )r-   r   rk     s   rk   c                   sD   e Zd ZdZd ZdZdZd fdd	Zdd Ze	j
Z
d	d
 Z  ZS )PitchWheelEventz?
    A class that encapsulates a pitch wheel change event.
       r   r   c                s"   || _ || _tt| j|| d S )N)r*   pitch_wheel_valuer,   rq   r   )r   r*   r   rs   r   )r-   r   r   r     s    zPitchWheelEvent.__init__c             C   s   dS )NFr   )r   r   r   r   r   r     s    zPitchWheelEvent.__eq__c             C   s   d}| j | jB }t| j| }x|D ]}|tjd| }q$W | jd d? }| jd d@ }|tjd| }|tjd| }|tjd| }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   z>Bi       re   )r0   r*   r1   r   r2   r3   rs   )r   r4   r5   r6   r7   r8   MSBLSBr   r   r   r9   "  s    
zPitchWheelEvent.serialize)r   )r    r!   r"   r#   r   r0   r$   r   r   r   r   r9   r:   r   r   )r-   r   rq     s   rq   c                   s@   e Zd ZdZd ZdZd	 fdd	Zdd Zej	Z	dd Z
  ZS )
	TrackNamez;
    A class that encapsulates a program change event.
    r   c                s"   |j d| _tt| j|| d S )Nz
ISO-8859-1)rG   	trackNamer,   rw   r   )r   r   rx   r   )r-   r   r   r   :  s    zTrackName.__init__c             C   s$   | j |j ko"| j|jko"| j|jkS )N)r   r   rx   )r   r   r   r   r   r   ?  s    zTrackName.__eq__c             C   s   d}t | j| }x|D ]}|tjd|7 }qW |tjdd7 }|tjdd7 }t| j}t |}x|D ]}|tjd|7 }qhW || j7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   z>BrP   r@   r   )r1   r   r2   r3   rJ   rx   )r   r4   r5   r7   r8   
dataLengthdataLengthVarrM   r   r   r   r9   F  s    



zTrackName.serialize)r   )r    r!   r"   r#   r   r$   r   r   r   r   r9   r:   r   r   )r-   r   rw   3  s   rw   c                   s2   e Zd ZdZd ZdZd fdd	Zdd Z  ZS )TimeSignaturez5
    A class that encapsulates a time signature.
    r   c                s.   || _ || _|| _|| _tt| j|| d S )N)	numeratordenominatorclocks_per_ticknotes_per_quarterr,   r{   r   )r   r   r|   r}   r~   r   r   )r-   r   r   r   _  s
    zTimeSignature.__init__c             C   s   d}d}d}t | j| }x|D ]}|tjd|7 }q W |tjd|7 }|tjd|7 }|tjdd7 }|tjd| j7 }|tjd| j7 }|tjd| j7 }|tjd| j7 }|S )zReturn a bytestring representation of the event, in the format required for
        writing into a standard midi file.
        r/   r@   X   z>BrB   )r1   r   r2   r3   r|   r}   r~   r   )r   r4   r5   r6   rC   r7   r8   r   r   r   r9   g  s    
zTimeSignature.serialize)r   )	r    r!   r"   r#   r   r$   r   r9   r:   r   r   )r-   r   r{   X  s
   r{   c               @   s   e Zd ZdZdd Zd5ddZd6dd	Zd7d
dZd8ddZd9d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ZdAd d!ZdBd#d$Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 ZdS )C	MIDITrackz0
    A class that encapsulates a MIDI track
    c             C   sB   t jddddd| _d| _d| _d| _g | _g | _|| _|| _	d	S )
z)Initialize the MIDITrack object.
        cccc   M   T   r   kr   r/   FN)
r2   r3   headerStringry   MIDIdataclosed	eventListMIDIEventListremdepdeinterleave)r   removeDuplicatesr   r   r   r   r     s    zMIDITrack.__init__Nr   c          
   C   sB   | j jt|||||||d | j jt|||| |||d dS )z5
        Add a note by chromatic MIDI number
        )r+   r   N)r   appendr%   r;   )r   r*   r'   r   r(   r)   r+   r   r   r   r   addNoteByNumber  s    
zMIDITrack.addNoteByNumberc             C   s   | j jt|||||d dS )z)
        Add a controller event.
        )r   N)r   r   rg   )r   r*   r   rj   ri   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)r   r   rq   )r   r*   r   rs   r   r   r   r   addPitchWheelEvent  s    zMIDITrack.addPitchWheelEventc             C   s   | j jt|||d dS )z4
        Add a tempo change (or set) event.
        )r   N)r   r   r>   )r   r   r?   r   r   r   r   addTempo  s    zMIDITrack.addTempoc             C   s   | j jt||||d dS )z$
        Add a SysEx event.
        )r   N)r   r   rZ   )r   r   r\   r]   r   r   r   r   addSysEx  s    zMIDITrack.addSysExre   Fc          
   C   s"   | j jt|||||||d dS )z.
        Add a Universal SysEx event.
        )r   N)r   r   ra   )r   r   r6   rC   r]   rd   rc   r   r   r   r   addUniversalSysEx  s    zMIDITrack.addUniversalSysExc             C   s   | j jt||||d dS )z-
        Add a program change event.
        )r   N)r   r   rW   )r   r*   r   programr   r   r   r   addProgramChange  s    zMIDITrack.addProgramChangec             C   s   | j jt||||d dS )z/
        Add a channel pressure event.
        )r   N)r   r   rk   )r   r*   r   rn   r   r   r   r   addChannelPressure  s    zMIDITrack.addChannelPressurec             C   s   | j jt|||d dS )z)
        Add a track name event.
        )r   N)r   r   rw   )r   r   rx   r   r   r   r   addTrackName  s    zMIDITrack.addTrackNamec          	   C   s    | j jt||||||d dS )z'
        Add a time signature.
        )r   N)r   r   r{   )r   r   r|   r}   r~   r   r   r   r   r   addTimeSignature  s    zMIDITrack.addTimeSignaturec             C   s   | j jt|||d dS )z(
        Add a copyright notice
        )r   N)r   r   rF   )r   r   rH   r   r   r   r   addCopyright  s    zMIDITrack.addCopyrightc             C   s   | j jt|||||d dS )z(
        Add a copyright notice
        )r   N)r   r   rQ   )r   r   rR   rS   rT   r   r   r   r   addKeySignature  s    zMIDITrack.addKeySignaturec             C   s   | j jt|||d dS )z"
        Add a text event
        )r   N)r   r   rN   )r   r   rO   r   r   r   r   addText  s    zMIDITrack.addTextTc          
   C   s   t jd|}|t jdt| }xF|D ]>\}}|t jd| }t|}	x|	D ]}
|t jd|
 }qLW q&W | jjtd||dd||d dS )z1
        Change the tuning of MIDI notes
        z>Br      r=   )r   N)r2   r3   rJ   frequencyTransformr   r   ra   )r   tuningsrd   rc   tuningProgamr   r]   
noteNumber	frequencyMIDIFreqencybyter   r   r   changeNoteTuning  s    
zMIDITrack.changeNoteTuningc             C   s2   dd | j D | _| jjtd | jr.| j  dS )z
        Process the event list, creating a MIDIEventList,
        which is then sorted to be in chronological order by start tick.
        c             S   s   g | ]}|qS r   r   ).0evtr   r   r   
<listcomp>  s    z.MIDITrack.processEventList.<locals>.<listcomp>)keyN)r   r   sortsort_eventsr   deInterleaveNotes)r   r   r   r   processEventList
  s    zMIDITrack.processEventListc             C   s&   t | j}t|| _| jjtd 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.
        )r   N)setr   listr   r   )r   sr   r   r   r     s    

zMIDITrack.removeDuplicatesc             C   s*   | j r
dS d| _ | 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)r   r   r   r   )r   r   r   r   
closeTrack(  s    zMIDITrack.closeTrackc             C   s<   | 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.
        BBBBr   r@   /   z>LN)writeEventsToStreamr   r2   r3   rJ   ry   )r   r   r   r   writeMIDIStream<  s    zMIDITrack.writeMIDIStreamc             C   s,   d}x"| j D ]}|  j|j|7  _qW dS )z
        Write the events in MIDIEvents to the MIDI stream.
        MIDIEventList is presumed to be already sorted in chronological order.
        r   N)r   r   r9   )r   r4   eventr   r   r   r   M  s    zMIDITrack.writeEventsToStreamc             C   s   g }i }x| j D ]}|jdkrt|jt|j }|jdkrn||krV|| j|j n|jg||< |j| q|jdkrt|| dkr|| j |_|j| q|| j  |j| q|j| qW || _ | j j	t
d 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)r%   r;   )r   r   strr'   r*   r   r   rJ   popr   r   )r   tempEventListstackr   noteeventkeyr   r   r   r   Z  s&    



zMIDITrack.deInterleaveNotesc             C   sb   t | jdkrdS g }|r|nd}d}x0| jD ]&}|j| }|| |_|}|j| q.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   N)rJ   r   r   r   )r   originadjustr   internal_originrunningTickr   adjustedTickr   r   r   adjustTimeAndOrigin  s    

zMIDITrack.adjustTimeAndOriginc             C   s(   |j | j |j | j |j | j dS )z&
        Write track to disk.
        N)writer   ry   r   )r   
fileHandler   r   r   
writeTrack  s    zMIDITrack.writeTrack)Nr   )r   )r   )r   )r   )re   Fr   )r   )r   )r   )r   )r   )r   )r   )re   Tr   r   )r    r!   r"   r#   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   |  s4   

	


 







 
,r   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   sV   t jddddd| _t jdd| _t jd|| _|| _t jd|| _t jd|| _d	S )
a   Initialize the data structures

        :param numTracks: The number of tracks the file contains. Integer,
            one or greater
        :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``.
        :param ticks_per_quarternote: The number of ticks per quarter
            note is what the Standard MIDI File Format Specification calls
            "division".  Ticks are the integer unit of time in the SMF, and in
            every MIDI sequencer I am aware of.  Common values are 120, 240,
            384, 480, 960. Note that all these numbers are evenly divisible by
            2,3,4,6,8,12,16, and 24, except 120 does not have 16 as a divisor.
        r   r   r      h   dz>L   z>HN)r2   r3   r   
headerSize	formatnumnumeric_format	numTracksticks_per_quarternote)r   r   file_formatr   r   r   r   r     s    zMIDIHeader.__init__c             C   s@   |j | j |j | j |j | j |j | j |j | j d S )N)r   r   r   r   r   r   )r   r   r   r   r   	writeFile  s
    zMIDIHeader.writeFileN)r    r!   r"   r#   r   r   r   r   r   r   r     s   	r   c               @   s   e Zd ZdZdddddedfddZdd Z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# Zd>d$d%Zd?d&d'Zd@d(d)ZdAd*d+ZdBd-d.Zd/d0 ZdCd1d2Zd3d4 ZdDd5d6Zd7d8 Zd9d: ZdS )Er   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   TFc       	      C   s   t  | _|dkr|d | _n|| _t| j||| _|| _d| _|| _|| _| jr\dd | _	n| j
| _	x&td| jD ]}| jjt|| qrW d| _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`` shift all the events in the tracks 
            so that the first event takes place at time t=0. Default is ``False``
        :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``.
        :param ticks_per_quarternote: The number of ticks per quarter note is
            what the Standard MIDI File Format Specification calls "division".
            Ticks are the integer unit of time in the SMF, and in most if
            not all MIDI sequencers.  Common values are 120, 240, 384,
            480, 960. Note that all these numbers are evenly divisible by
            2,3,4,6,8,12,16, and 24, except 120 does not have 16 as a divisor.

        :param eventtime_is_ticks: If set True means event time and duration
            argument values are integer ticks instead of fractional quarter
            notes.

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

        In a format 1 file, it would be a rare cirumstance where adjusting the
        origin of each track to the track's first note makes any sense.

        Example:

        .. code::

            # Create a two-track MIDIFile

            from midiutil.MidiFile import MIDIFile
            midi_file = MIDIFile(1, adjust_origin=False)

        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   Fc             S   s   | S )Nr   )rp   r   r   r   <lambda>%  s    z#MIDIFile.__init__.<locals>.<lambda>r   N)r   tracksr   r   headeradjust_originr   r   eventtime_is_tickstime_to_ticksquarter_to_tickranger   r   event_counter)	r   r   r   r   r   r   r   r   rM   r   r   r   r     s    @zMIDIFile.__init__c             C   s   t || j S )N)r   r   )r   quarternote_timer   r   r   r   1  s    zMIDIFile.quarter_to_tickc             C   s   t || j S )N)floatr   )r   ticknumr   r   r   tick_to_quarter4  s    zMIDIFile.tick_to_quarterNc          	   C   sR   | j jdkr|d7 }| j| j||| j|| j|||| jd |  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 at which the note sounds. The value can be either
            quarter notes [Float], or ticks [Integer]. Ticks may be specified by
            passing eventtime_is_ticks=True to the MIDIFile constructor.
            The default is quarter notes.
        :param duration: the duration of the note. Like the time argument, the
            value can be either quarter notes [Float], or ticks [Integer].
        :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   )r+   r   N)r   r   r   r   r   r   )r   trackr*   r'   timer(   r)   r+   r   r   r   addNote7  s    
zMIDIFile.addNotec             C   sD   | j jdkr|d7 }| j| j| j||| jd |  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   r   r   r   )r   r   r   rx   r   r   r   r   W  s
    

zMIDIFile.addTrackNamer   c             C   sF   | j jdkrd}| j| j| j|||||| jd |  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.

        The ``notes_per_quarter`` value is also a little confusing. It
        specifies the number of 32nd notes in a MIDI quarter note. Usually
        there are 8 32nd notes in a quarter note (8/32 = 1/4), so
        the default value is 8. However, one can change this value if
        needed. Setting it to 16, for example, would cause the music to
        play at double speed, as there would be 16/32 (or what could be
        considered *two* quarter notes for every one MIDI quarter note.

        Note that both the ``clocks_per_tick`` and the
        ``notes_per_quarter`` are specified in terms of quarter notes,
        even is the score is not a quarter-note based score (i.e.,
        even if the denominator is not ``4``). So if you're working with a
        time signature of, say, 6/8, one still needs to specify the clocks
        per quarter note.
        r   r   )r   N)r   r   r   r   r   r   )r   r   r   r|   r}   r~   r   r   r   r   r   g  s    3
zMIDIFile.addTimeSignaturec             C   s@   | j jdkrd}| j| j| j||| jd |  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   r   r   r   )r   r   r   r?   r   r   r   r     s
    
zMIDIFile.addTempoc             C   sD   | j jdkr|d7 }| j| j| j||| jd |  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   r   r   r   )r   r   r   rH   r   r   r   r     s
    

zMIDIFile.addCopyrightr   c             C   sD   | j jdkrd}| j| j| j||||| jd |  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   )r   N)r   r   r   r   r   r   )r   r   r   rR   rS   rT   r   r   r   r   r     s
     zMIDIFile.addKeySignaturec             C   sD   | j jdkr|d7 }| j| j| j||| jd |  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   r   r   r   )r   r   r   rO   r   r   r   r     s
    	
zMIDIFile.addTextc             C   sF   | j jdkr|d7 }| j| j|| j||| jd |  jd7  _dS )a  

        Add a MIDI program change event.

        :param tracknum: The zero-based track number 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   r   r   r   r   r   )r   tracknumr*   r   r   r   r   r   r     s
    
zMIDIFile.addProgramChangec             C   sJ   | j jdkr|d7 }| j| }|j|| j||| jd |  jd7  _dS )a  
        Add a Channel Pressure event.

        :param tracknum: The zero-based track number to which channel pressure 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 channel pressure event is
            placed [Float].
        :param pressure_value: the pressure value. [Integer, 0-127].
        r   )r   N)r   r   r   r   r   r   )r   r   r*   r   rn   r   r   r   r   r   
  s    

zMIDIFile.addChannelPressurec             C   sH   | j jdkr|d7 }| j| j|| j|||| jd |  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   r   r   r   )r   r   r*   r   rj   ri   r   r   r   r     s
    zMIDIFile.addControllerEventc             C   sF   | j jdkr|d7 }| j| j|| j||| jd |  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   r   r   r   )r   r   r*   r   pitchWheelValuer   r   r   r   0  s
    

zMIDIFile.addPitchWheelEventc	             C   s   | j |}	| jjdkr|d7 }| j| }|r0dnd}
|j||	d|| jd |  jd7  _|	|
7 }	|j||	d|| jd |  jd7  _|	|
7 }	|j||	d|| jd |  jd7  _|	|
7 }	|dk	r|j||	d|| jd |  jd7  _dS )	aB  

        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.)

        Registered/Non-Registered Parameter Number (RPN / NRPN)
        -------------------------------------------------------
        Controller number 6 (Data Entry), in conjunction with Controller numbers 96
        (Data Increment), 97 (Data Decrement), 98 (Registered Parameter Number LSB),
        99 (Registered Parameter Number MSB), 100 (Non-Registered Parameter Number
        LSB), and 101 (Non-Registered Parameter Number MSB), extend the number of
        controllers available via MIDI. Parameter data is transferred by first
        selecting the parameter number to be edited using controllers 98 and 99 or
        100 and 101, and then adjusting the data value for that parameter using
        controller number 6, 96, or 97.

        RPN and NRPN are typically used to send parameter data to a synthesizer in
        order to edit sound patches or other data. Registered parameters are those
        which have been assigned some particular function by the MIDI Manufacturers
        Association (MMA) and the Japan MIDI Standards Committee (JMSC). For
        example, there are Registered Parameter numbers assigned to control pitch
        bend sensitivity and master tuning for a synthesizer. Non-Registered
        parameters have not been assigned specific functions, and may be used for
        different functions by different manufacturers.

        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   r   e   )r   d   r   N&   )r   r   r   r   r   r   )r   r   r*   r   controller_msbcontroller_lsbdata_msbdata_lsb
time_orderr   	tick_incrr   r   r   makeRPNCall@  s*    9





zMIDIFile.makeRPNCallc	             C   s   | j |}	| jjdkr|d7 }| j| }|r0dnd}
|j||	d|| jd |  jd7  _|	|
7 }	|j||	d|| jd |  jd7  _|	|
7 }	|j||	d|| jd |  jd7  _|	|
7 }	|dk	r|j||	d|| jd |  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   r   c   )r   b   r   Nr   )r   r   r   r   r   r   )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   rB   )r   N)r   )r   r   r*   r   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   r   r   r   r   r   changeTuningProgram  s    zMIDIFile.changeTuningProgramre   c             C   sB   | j jdkr|d7 }| j| j||||| jd |  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   rd   rc   r   r   r   r   r     s    #
zMIDIFile.changeNoteTuningc             C   sJ   | j |}| jjdkr|d7 }| j| j|||| jd |  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   r   r   r   )r   r   r   r\   r]   r   r   r   r   r      s    

zMIDIFile.addSysExc       	   	   C   sP   | j |}| jjdkr|d7 }| j| j||||||| jd |  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   r   r   r   )	r   r   r   r6   rC   r]   rd   rc   r   r   r   r   r   8  s    

zMIDIFile.addUniversalSysExc             C   s>   | j j| | j  x$td| jD ]}| j| j| q"W d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   rM   r   r   r   r   Z  s    zMIDIFile.writeFilec             C   s   d}| j |}x<| jD ]2}t|jdkrx|jD ]}|j|k r0|j}q0W qW xF| jD ]<}g }x,|jD ]"}|j| }|| |_|j| qdW ||_qTW d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 r   N)r   r   rJ   r   r   r   )r   offsetr   tick_offsetr   r   r   r   r   r   r   shiftTracksk  s    



zMIDIFile.shiftTracksc             C   s   | j r
dS x6td| jD ]&}| j| j  | j| jjtd qW | j }x6td| jD ]&}| j| j	|| j
 | j| j  qXW d| _ 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)r   r   r   r   r   r   r   r   
findOriginr   r   r   )r   rM   r   r   r   r   r     s    	zMIDIFile.closec             C   sB   d}x8| j D ].}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 r   )r   rJ   r   r   )r   r   r   r   r   r   r     s    zMIDIFile.findOrigin)N)r   )r   )F)F)F)F)re   Tr   )re   F)r   )r    r!   r"   r#   TICKSPERQUARTERNOTEr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s:   W

:
%
Q
6

 
)
!
 c             C   sR   | dkrdgS g }d}x.| dkrD|j | d@ |B d@  | dL } d}qW |j  |S )a  
    Accept an integer, and serialize it as a MIDI file variable length quantity

    Some numbers in MTrk chunks are represented in a form called a variable-
    length quantity.  These numbers are represented in a sequence of bytes,
    each byte holding seven bits of the number, and ordered most significant
    bits first. All bytes in the sequence except the last have bit 7 set,
    and the last byte has bit 7 clear.  This form allows smaller numbers to
    be stored in fewer bytes.  For example, if the number is between 0 and
    127, it is thus represented exactly as one byte.  A number between 128
    and 16383 uses two bytes, and so on.

    Examples:
    Number  VLQ
    128     81 00
    8192    C0 00
    16383   FF 7F
    16384   81 80 00
    r   re   r@   rt   r<   )r   reverse)rM   vlbyteshibitr   r   r   r1     s    
r1   c             C   s^   | }d}d}xH|d> }t jd||d }|d }|d }||d@  }|d@ dkrP q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   rt   z>Br   re   r<   )r2   unpack_from)r   buffertoffsetoutput	bytesReadr   r   r   r   readVarLength  s    r  c       	      C   s   d}t | } ddtj| t d d  }t|}dtdt |d d  }| |krfd	tj| | d nd
}t|d | }tt|d? dg}||d>  }t|dg}|dkr|dkr|dkrd}t|}|||gS )z8
    Returns a three-byte transform of a frequency.
    i @  E   r   i  r=   g       @g     @Q@g      (@i  r   r   rt   re   rf   )r   mathlogr   powroundmin)	freq
resolutiondollars	firstByte	lowerFreqcentDifcents
secondByte	thirdByter   r   r   r     s     r   c             C   s`   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   rt   r=   g      Y@g     @)r  r   r   )	freqBytesr	  baseFrequencyfracr   r   r   r   returnFrequency  s
    (r  c             C   s   | j | j| jfS )ai  
    .. 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
          ``sec_sort_order``. 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 event time and event 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  r2   warnings__version__r   controllerEventTypesr	   r
   r   r   __all__objectr   r%   r;   r>   rF   rN   rQ   rW   rZ   ra   rg   rk   rq   rw   r{   r   r   r   r1   r  r   r  r   r   r   r   r   <module>   sR   0,-8(!$&2###%$  ,)     q#