
    rhX                       S SK JrJr  S SKrS SKrS SKrSrSrSS0rS r	Sr
SrSr/ S	Qr " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S  S!\5      r " S" S#\5      r " S$ S%\5      r " S& S'\5      r " S( S)\5      r " S* S+\5      r " S, S-\5      r S. r!S/ r"S0 r#S1 r$S2 r%g)3    )divisionprint_functionNHEADi  pan
      )MIDIFileMAJORMINORSHARPSFLATSc                   2    \ rS rSrSrSrSrS rS rS r	Sr
g)	GenericEvent%   z8
The event class from which specific events are derived
Nr   c                     Xl         X l        g N)tickinsertion_order)selfr   r   s      K/home/james-whalen/.local/lib/python3.13/site-packages/midiutil/MidiFile.py__init__GenericEvent.__init__,   s    	.    c                 t    U R                   UR                   :H  =(       a    U R                  UR                  :H  $ )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   others     r   __eq__GenericEvent.__eq__0   s)     -I$))uzz2IJr   c                     [        U R                  5      nUS-   US-  -   nUS-  US-	  -  nUS-   US-  -   nUS-   US-  -  nUS	-   US
-  -   nUS-  US-	  -  nU$ )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   as     r   __hash__GenericEvent.__hash__>   s     		N^R(^R(^Q'^Q'^Q'^R(r   )r   r   )__name__
__module____qualname____firstlineno____doc__r   sec_sort_orderr   r   r*   __static_attributes__ r   r   r   r   %   s#     GN/Kr   r   c                   h   ^  \ rS rSrSrS rSrSr S
U 4S jjrS r	\
R                  rS rS rS	rU =r$ )NoteOnU   z"
A class that encapsulates a note
   r&   c                 h   > X l         X@l        XPl        Xl        X`l        [
        [        U ]  X75        g r   )pitchdurationvolumechannel
annotationsuperr5   r   )	r   r<   r9   r   r:   r;   r=   r   	__class__s	           r   r   NoteOn.__init__]   s+    
 $fd$T;r   c                     U R                   UR                   :H  =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r   r   r   r9   r<   r   s     r   r   NoteOn.__eq__f   Y    - L$))uzz2I L

ekk)L.2llemm.K	Mr   c                 z    SU R                   U R                  U R                  U R                  U R                  4-  $ )Nz-NoteOn %d at tick %d duration %d ch %d vel %d)r9   r   r:   r<   r;   r   s    r   __str__NoteOn.__str__n   s7    >JJ		4==$,,BM M 	Mr   c                 v   SnU R                   U R                  -  n[        U R                  U-
  5      nU H  nU[        R
                  " SU5      -  nM     U[        R
                  " SU5      -  nU[        R
                  " SU R                  5      -  nU[        R
                  " SU R                  5      -  nU$ oReturn a bytestring representation of the event, in the format required for
writing into a standard midi file.
r   >Bmidi_statusr<   writeVarLengthr   structpackr9   r;   r   previous_event_tick	midibytescodevarTimetimeBytes         r   	serializeNoteOn.serializer        	$,,. -@!@AHT844I  V[[t,,	V[[tzz22	V[[t{{33	r   )r=   r<   r:   r9   r;   Nr   r,   r-   r.   r/   r0   r   rN   r1   r   r   r   r*   rG   rX   r2   __classcell__r?   s   @r   r5   r5   U   sF     GKN 34<M $$HM r   r5   c                   h   ^  \ rS rSrSrS rSrSr S
U 4S jjrS r	\
R                  rS rS rS	rU =r$ )NoteOff   z,
A class that encapsulates a Note Off event
      c                 \   > X l         X@l        Xl        XPl        [        [
        U ]  X65        g r   )r9   r;   r<   r=   r>   r`   r   )r   r<   r9   r   r;   r=   r   r?   s          r   r   NoteOff.__init__   s&    
$gt%d<r   c                     U R                   UR                   :H  =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r   rB   r   s     r   r   NoteOff.__eq__   rD   r   c                 d    SU R                   U R                  U R                  U R                  4-  $ )Nz"NoteOff %d at tick %d ch %d vel %d)r9   r   r<   r;   rF   s    r   rG   NoteOff.__str__   s-    3JJ		4<<7> > 	>r   c                 v   SnU R                   U R                  -  n[        U R                  U-
  5      nU H  nU[        R
                  " SU5      -  nM     U[        R
                  " SU5      -  nU[        R
                  " SU R                  5      -  nU[        R
                  " SU R                  5      -  nU$ rJ   rM   rR   s         r   rX   NoteOff.serialize   rZ   r   )r=   r<   r9   r;   r[   r\   r^   s   @r   r`   r`      sE     GKN 34=M $$H> r   r`   c                   \   ^  \ rS rSrSrS rSrSU 4S jjrS r\	R                  r
S rSrU =r$ )	Tempo   z.
A class that encapsulates a tempo meta-event
r&   c                 R   > [        SU-  5      U l        [        [        U ]  X5        g )Ni )r(   tempor>   rm   r   )r   r   rp   r   r?   s       r   r   Tempo.__init__   s#    E)*
eT#D:r   c                     U R                   UR                   :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r   )r   r   rp   r   s     r   r   Tempo.__eq__   sA    - *		UZZ'*

ekk)	+r   c                 x   SnSnSn[         R                  " SU R                  5      nUSS n[        U R                  U-
  5      nU H  nU[         R                  " SU5      -  nM     U[         R                  " SU5      -  nU[         R                  " SU5      -  nU[         R                  " SS5      -  nX&-  nU$ )	rK   r      Q   >Lr      rL   r&   )rP   rQ   rp   rO   r   )	r   rS   rT   rU   subcodefourbite	threebiterV   rW   s	            r   rX   Tempo.serialize   s    . 	;;tTZZ0QqM	 -@!@AHT844I  V[[t,,	V[[w//	V[[t,,		r   )rp   r   r,   r-   r.   r/   r0   r   r1   r   r   r   r*   rX   r2   r]   r^   s   @r   rm   rm      s5     GN;+
 $$H# #r   rm   c                   >   ^  \ rS rSrSrS rSrSU 4S jjrS rSr	U =r
$ )	Copyright   z-
A class that encapsulates a copyright event
r   c                 X   > UR                  S5      U l        [        [        U ]  X5        g Nz
ISO-8859-1)encodenoticer>   r   r   )r   r   r   r   r?   s       r   r   Copyright.__init__   s"    mmL1i'>r   c                    SnSnSn[        U R                  U-
  5      nU H  nU[        R                  " SU5      -  nM     U[        R                  " SU5      -  nU[        R                  " SU5      -  n[	        U R
                  5      n[        U5      nU H  n	U[        R                  " SU	5      -  nM     X R
                  -  nU$ )rK   r   ru   rc   rL   b)rO   r   rP   rQ   lenr   
r   rS   rT   rU   ry   rV   rW   payloadLengthpayloadLengthVaris
             r   rX   Copyright.serialize   s     	 -@!@AHT844I  V[[t,,	V[[w//	DKK()-8!AS!,,I "[[ 	r   )r   r}   r,   r-   r.   r/   r0   r   r1   r   rX   r2   r]   r^   s   @r   r   r      s#     GN? r   r   c                   >   ^  \ rS rSrSrS rSrSU 4S jjrS rSr	U =r
$ )Texti  (
A class that encapsulates a text event
r   c                 X   > UR                  S5      U l        [        [        U ]  X5        g r   )r   textr>   r   r   )r   r   r   r   r?   s       r   r   Text.__init__  s"    KK-	dD"49r   c                    SnSnSn[        U R                  U-
  5      nU H  nU[        R                  " SU5      -  nM     U[        R                  " SU5      -  nU[        R                  " SU5      -  n[	        U R
                  5      n[        U5      nU H  n	U[        R                  " SU	5      -  nM     X R
                  -  nU$ )rK   r   ru   r   rL   B)rO   r   rP   rQ   r   r   r   s
             r   rX   Text.serialize  s     	 -@!@AHT844I  V[[t,,	V[[w//	DII)-8!AS!,,I "YY	r   )r   r}   r   r^   s   @r   r   r     s#     GN: r   r   c                   @   ^  \ rS rSrSrS rSr SU 4S jjrS rSr	U =r
$ )KeySignaturei-  r   r   c                 P   > X l         X0l        X@l        [        [        U ]  X5        g r   )accidentalsaccidental_typemoder>   r   r   )r   r   r   r   r   r   r?   s         r   r   KeySignature.__init__4  s#    &.	lD*4Ar   c                    SnSnSnSn[        U R                  U-
  5      nU H  nU[        R                  " SU5      -  nM     U[        R                  " SU5      -  nU[        R                  " SU5      -  nU[        R                  " SU5      -  nU[        R                  " SU R                  U R
                  -  5      -  nU[        R                  " SU R                  5      -  nU$ )rK   r   ru   Y   rc   rL   z>b)rO   r   rP   rQ   r   r   r   )r   rS   rT   rU   ry   event_subtyperV   rW   s           r   rX   KeySignature.serialize;  s     	 -@!@AHT844I  V[[t,,	V[[w//	V[[}55	V[[t'7'7$:N:N'NOO	V[[tyy11	r   )r   r   r   r}   r   r^   s   @r   r   r   -  s)     GN "#B r   r   c                   b   ^  \ rS rSrSrS rSrSr S	U 4S jjrS r	\
R                  rS rSrU =r$ )
ProgramChangeiN  3
A class that encapsulates a program change event.
   r   c                 D   > X0l         Xl        [        [        U ]  X$5        g r   )programNumberr<   r>   r   r   )r   r<   r   r   r   r?   s        r   r   ProgramChange.__init__V  s    *mT+DBr   c                     U R                   UR                   :H  =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r   )r   r   r   r<   r   s     r   r   ProgramChange.__eq__\  s\    - .		UZZ'.""e&9&99. -	/r   c                 (   SnU R                   U R                  -  n[        U R                  5      nU H  nU[        R
                  " SU5      -  nM     U[        R
                  " SU5      -  nU[        R
                  " SU R                  5      -  nU$ rJ   )rN   r<   rO   r   rP   rQ   r   rR   s         r   rX   ProgramChange.serialized  s     	$,,. +HT844I  V[[t,,	V[[t'9'9::	r   )r<   r   r}   r,   r-   r.   r/   r0   r   rN   r1   r   r   r   r*   rX   r2   r]   r^   s   @r   r   r   N  s@     GKN "#C/ $$H r   r   c                   \   ^  \ rS rSrSrSrSrS	U 4S jjrS r\	R                  r
S rSrU =r$ )

SysExEventir  z6
A class that encapsulates a System Exclusive  event.
SysExr   c                 D   > X l         X0l        [        [        U ]  X5        g r   )manIDpayloadr>   r   r   )r   r   r   r   r   r?   s        r   r   SysExEvent.__init__y  s    
j$(?r   c                     gNFr3   r   s     r   r   SysExEvent.__eq__~      r   c                    SnSn[        U R                  U-
  5      nU H  nU[        R                  " SU5      -  nM     U[        R                  " SU5      -  n[        [	        U R
                  5      S-   5      nU H  nU[        R                  " SU5      -  nM     U[        R                  " SU R                  5      -  nX R
                  -  nU[        R                  " SS5      -  nU$ )rK   r      rL   rc      )rO   r   rP   rQ   r   r   r   r   rS   rT   rU   rV   rW   r   lenBytes           r   rX   SysExEvent.serialize  s     	 -@!@AHT844I  V[[t,,	&s4<<'81'<=$GT733I % 	V[[tzz22	\\!	V[[t,,	r   )r   r   r}   r~   r^   s   @r   r   r   r  s6     GN@
 $$H r   r   c                   ^   ^  \ rS rSrSrSrSr S	U 4S jjrS r\	R                  r
S rSrU =r$ )
UniversalSysExEventi  z@
A class that encapsulates a Universal System Exclusive  event.
UniversalSysExr   c                 h   > X l         X0l        X@l        XPl        X`l        [
        [        U ]  X5        g r   )realTimesysExChannelrU   ry   r   r>   r   r   )	r   r   r   r   rU   ry   r   r   r?   s	           r   r   UniversalSysExEvent.__init__  s-     (	!41$Hr   c                     gr   r3   r   s     r   r   UniversalSysExEvent.__eq__  r   r   c                    SnSn[        U R                  U-
  5      nU H  nU[        R                  " SU5      -  nM     U[        R                  " SU5      -  n[        [	        U R
                  5      S-   5      nU H  nU[        R                  " SU5      -  nM     U R                  (       a  U[        R                  " SS5      -  nOU[        R                  " SS5      -  nU[        R                  " SU R                  5      -  nU[        R                  " SU R                  5      -  nU[        R                  " SU R                  5      -  nX R
                  -  nU[        R                  " SS5      -  nU$ )rK   r   r   rL   r$      ~   r   )
rO   r   rP   rQ   r   r   r   r   rU   ry   r   s           r   rX   UniversalSysExEvent.serialize  s6    	 -@!@AHT844I  V[[t,,	 's4<<'81'<=$GT733I % ==T400IT400IV[[t'8'899	V[[tyy11	V[[t||44	\\!	V[[t,,	r   )rU   r   r   ry   r   r}   r~   r^   s   @r   r   r     s;     GN +,I $$H r   r   c                   b   ^  \ rS rSrSrS rSrSr S	U 4S jjrS r	\
R                  rS rSrU =r$ )
ControllerEventi  r      r   c                 P   > X@l         Xl        X0l        [        [        U ]  X%5        g r   )	parameterr<   controller_numberr>   r   r   )r   r<   r   r   r   r   r?   s         r   r   ControllerEvent.__init__  s"    "!2ot-dDr   c                     gr   r3   r   s     r   r   ControllerEvent.__eq__  r   r   c                 v   SnU R                   U R                  -  n[        U R                  U-
  5      nU H  nU[        R
                  " SU5      -  nM     U[        R
                  " SU5      -  nU[        R
                  " SU R                  5      -  nU[        R
                  " SU R                  5      -  nU$ rJ   )rN   r<   rO   r   rP   rQ   r   r   rR   s         r   rX   ControllerEvent.serialize  s     	$,,. -@!@AHT844I  V[[t,,	V[[t'='=>>	V[[t~~66	r   )r<   r   r   r}   r   r^   s   @r   r   r     s@      GKN "#E $$H r   r   c                   `   ^  \ rS rSrSrSrSrSrS
U 4S jjrS r	\
R                  rS rS	rU =r$ )ChannelPressureEventi  zB
A class that encapsulates a Channel Pressure (Aftertouch) event.
ChannelPressure   r   c                 D   > Xl         X0l        [        [        U ]  X$5        g r   )r<   pressure_valuer>   r   r   )r   r<   r   r   r   r?   s        r   r   ChannelPressureEvent.__init__  s    ,"D24Ir   c                    U R                   R                  UR                   R                  :H  =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r   )r?   r,   r   r   r<   r   s     r   r   ChannelPressureEvent.__eq__  sh    ''5??+C+CC .		UZZ'.##u';';;. -	/r   c                 .   SnU R                   U R                  -  n[        U R                  U-
  5      nU H  nU[        R
                  " SU5      -  nM     U[        R
                  " SU5      -  nU[        R
                  " SU R                  5      -  nU$ rJ   )rN   r<   rO   r   rP   rQ   r   )r   rS   rT   rU   vartickxs         r   rX   ChannelPressureEvent.serialize  s     	$,,. -@!@AAT1--I V[[t,,	V[[t':':;;	r   )r<   r   r}   r   r^   s   @r   r   r     s;      GKNJ
/ $$H r   r   c                   `   ^  \ rS rSrSrS rSrSrS	U 4S jjrS r	\
R                  rS rSrU =r$ )
PitchWheelEventi  z7
A class that encapsulates a pitch wheel change event.
   r   c                 D   > Xl         X0l        [        [        U ]  X$5        g r   )r<   pitch_wheel_valuer>   r   r   )r   r<   r   r   r   r?   s        r   r   PitchWheelEvent.__init__  s    !2ot-dDr   c                     gr   r3   r   s     r   r   PitchWheelEvent.__eq__  r   r   c                    SnU R                   U R                  -  n[        U R                  U-
  5      nU H  nU[        R
                  " SU5      -   nM     U R                  S-   S-	  nU R                  S-   S-  nU[        R
                  " SU5      -   nU[        R
                  " SU5      -   nU[        R
                  " SU5      -   nU$ )rK   r   rL   i       r   )rN   r<   rO   r   rP   rQ   r   )r   rS   rT   rU   rV   rW   MSBLSBs           r   rX   PitchWheelEvent.serialize"  s     	$,,. -@!@AH!FKKh$??I  %%,2%%,4D$ 77	D# 66	D# 66	r   )r<   r   r}   r   r^   s   @r   r   r     s;      GKNE
 $$H r   r   c                   \   ^  \ rS rSrSrS rSrSU 4S jjrS r\	R                  r
S rSrU =r$ )		TrackNamei3  r   r   c                 X   > UR                  S5      U l        [        [        U ]  X5        g r   )r   	trackNamer>   r   r   )r   r   r   r   r?   s       r   r   TrackName.__init__:  s$    ")),7i'>r   c                     U R                   UR                   :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r   )r   r   r   r   s     r   r   TrackName.__eq__?  sA    - 2		UZZ'2%//1	3r   c                    Sn[        U R                  U-
  5      nU H  nU[        R                  " SU5      -  nM     U[        R                  " SS5      -  nU[        R                  " SS5      -  n[	        U R
                  5      n[        U5      nU H  nU[        R                  " SU5      -  nM     X R
                  -  nU$ )rK   r   rL   r   ru   r&   )rO   r   rP   rQ   r   r   )r   rS   rT   rV   rW   
dataLengthdataLengthVarr   s           r   rX   TrackName.serializeF  s     	 -@!@AHT844I  V[[d++	V[[d++	(
&z2AS!,,I ^^#	r   )r   r}   r~   r^   s   @r   r   r   3  s5     GN?
3
 $$H r   r   c                   @   ^  \ rS rSrSrS rSr SU 4S jjrS rSr	U =r
$ )TimeSignatureiX  z-
A class that encapsulates a time signature.
r   c                 \   > X l         X0l        X@l        XPl        [        [
        U ]  X5        g r   )	numeratordenominatorclocks_per_ticknotes_per_quarterr>   r   r   )r   r   r  r  r  r  r   r?   s          r   r   TimeSignature.__init___  s)    "&.!2mT+DBr   c                 D   SnSnSn[        U R                  U-
  5      nU H  nU[        R                  " SU5      -  nM     U[        R                  " SU5      -  nU[        R                  " SU5      -  nU[        R                  " SS5      -  nU[        R                  " SU R                  5      -  nU[        R                  " SU R
                  5      -  nU[        R                  " SU R                  5      -  nU[        R                  " SU R                  5      -  nU$ )rK   r   ru   X   rL   rx   )rO   r   rP   rQ   r  r  r  r  )r   rS   rT   rU   ry   rV   rW   s          r   rX   TimeSignature.serializeg  s     	 -@!@AHT844I  V[[t,,	V[[w//	V[[t,,	V[[t~~66	V[[t'7'788	V[[t';';<<	V[[t'='=>>	r   )r  r  r  r  r}   r   r^   s   @r   r   r   X  s)     GN 56C r   r   c                       \ rS rSrSrS r SS jr SS jrSS jrSS jr	SS	 jr
  SS
 jrSS jrSS jrSS jr SS jrSS jr SS jrSS jr  SS jrS rS rS rS rS rS rS rS rSrg) 	MIDITracki|  z(
A class that encapsulates a MIDI track
c                     [         R                  " SSSSS5      U l        SU l        SU l        SU l        / U l        / U l        Xl        X l	        g	)
z)Initialize the MIDITrack object.
        cccc   M   T   r   kr   r   FN)
rP   rQ   headerStringr   MIDIdataclosed	eventListMIDIEventListremdepdeinterleave)r   removeDuplicatesr  s      r   r   MIDITrack.__init__  sL     #KKdD$G&(r   Nc                     U R                   R                  [        XX4UUUS95        U R                   R                  [        XX4-   UUUS95        g)z%
Add a note by chromatic MIDI number
r=   r   N)r  appendr5   r`   )r   r<   r9   r   r:   r;   r=   r   s           r   addNoteByNumberMIDITrack.addNoteByNumber  sV    
 	fWTV0:5DF 	G 	ggdov1;6EG 	Hr   c           
      N    U R                   R                  [        XUUUS95        g)z
Add a controller event.
r   N)r  r  r   )r   r<   r   r   r   r   s         r   addControllerEventMIDITrack.addControllerEvent  s*     	og=N.7>MO 	Pr   c           	      J    U R                   R                  [        XX4S95        g)z
Add a pitch wheel event.
r   N)r  r  r   )r   r<   r   r   r   s        r   addPitchWheelEventMIDITrack.addPitchWheelEvent  s     	og=Npqr   c                 J    U R                   R                  [        XUS95        g)z$
Add a tempo change (or set) event.
r   N)r  r  rm   )r   r   rp   r   s       r   addTempoMIDITrack.addTempo  s$     	eD4CE 	Fr   c           	      L    U R                   R                  [        XUUS95        g)z
Add a SysEx event.
r   N)r  r  r   )r   r   r   r   r   s        r   addSysExMIDITrack.addSysEx  s&     	jg9HJ 	Kr   c                 P    U R                   R                  [        XUX#UUS95        g)z
Add a Universal SysEx event.
r   N)r  r  r   )r   r   rU   ry   r   r   r   r   s           r   addUniversalSysExMIDITrack.addUniversalSysEx  s+     	1$,"W.=? 	@r   c           	      L    U R                   R                  [        XUUS95        g)z
Add a program change event.
r   N)r  r  r   )r   r<   r   programr   s        r   addProgramChangeMIDITrack.addProgramChange  s&     	mG7<KM 	Nr   c           	      L    U R                   R                  [        XUUS95        g)z
Add a channel pressure event.
r   N)r  r  r   )r   r<   r   r   r   s        r   addChannelPressureMIDITrack.addChannelPressure  s'     	27.CRT 	Ur   c                 J    U R                   R                  [        XUS95        g)z
Add a track name event.
r   N)r  r  r   )r   r   r   r   s       r   addTrackNameMIDITrack.addTrackName  $     	i8GI 	Jr   c                 N    U R                   R                  [        XUXEUS95        g)z
Add a time signature.
r   N)r  r  r   )r   r   r  r  r  r  r   s          r   addTimeSignatureMIDITrack.addTimeSignature  s)    
 	mD[,;<KM 	Nr   c                 J    U R                   R                  [        XUS95        gz
Add a copyright notice
r   N)r  r  r   )r   r   r   r   s       r   addCopyrightMIDITrack.addCopyright  r9  r   c           
      N    U R                   R                  [        XUUUS95        gr>  )r  r  r   )r   r   r   r   r   r   s         r   addKeySignatureMIDITrack.addKeySignature  s)    
 	l4o+/;JL 	Mr   c                 J    U R                   R                  [        XUS95        g)z
Add a text event
r   N)r  r  r   )r   r   r   r   s       r   addTextMIDITrack.addText  s#     	d4.=? 	@r   c                 j   [         R                  " SU5      nU[         R                  " S[        U5      5      -   nU HM  u  pxU[         R                  " SU5      -   n[        U5      n	U	 H  n
U[         R                  " SU
5      -   nM     MO     U R                  R                  [        SX2SSXeS95        g)z!
Change the tuning of MIDI notes
rL   r      rc   r   N)rP   rQ   r   frequencyTransformr  r  r   )r   tuningsr   r   tuningProgamr   r   
noteNumber	frequencyMIDIFreqencybytes              r   changeNoteTuningMIDITrack.changeNoteTuning  s    
 ++dL1FKKc'l;;'.#ZD* ==G-i8L$!FKKd$;; % (/ 	1!XGN 	Or   c                     U R                    Vs/ s H  oPM     snU l        U R                  R                  [        S9  U R                  (       a  U R                  5         ggs  snf )zt
Process the event list, creating a MIDIEventList,
which is then sorted to be in chronological order by start tick.
keyN)r  r  sortsort_eventsr  deInterleaveNotes)r   evts     r   processEventListMIDITrack.processEventList
  sU     .2^^<^cc^<K0""$ 	 =s   A"c                     [        U R                  5      n[        U5      U l        U R                  R                  [        S9  g)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.
rS  N)setr  listrU  rV  )r   ss     r   r  MIDITrack.removeDuplicates  s2     a,r   c                     U R                   (       a  gSU l         U R                  (       a  U R                  5         U R                  5         g)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  rY  rF   s    r   
closeTrackMIDITrack.closeTrack(  s4     ;;;;!!#r   c           	          U R                  5         U =R                  [        R                  " SSSSS5      -  sl        [        R                  " S[	        U R                  5      5      U l        g)z>
Write the meta data and note data to the packed MIDI stream.
BBBBr   ru   /   rw   N)writeEventsToStreamr  rP   rQ   r   r   rF   s    r   writeMIDIStreamMIDITrack.writeMIDIStream<  sN     	  " 	VT4tDD !++dC,>?r   c                 v    SnU R                    H'  nU =R                  UR                  U5      -  sl        M)     g)z{
Write the events in MIDIEvents to the MIDI stream.
MIDIEventList is presumed to be already sorted in chronological order.
r   N)r  r  rX   )r   rS   events      r   rf  MIDITrack.writeEventsToStreamM  s2    
  ''EMMU__-@AAM (r   c                    / n0 nU R                    GH  nUR                  S;   a  [        UR                  5      [        UR                  5      -   nUR                  S:X  aE  XB;   a  X$   R                  UR                  5        OUR                  /X$'   UR                  U5        M  UR                  S:X  aa  [        X$   5      S:  a*  X$   R                  5       Ul        UR                  U5        M  X$   R                  5         UR                  U5        GM  GM  UR                  U5        GM     Xl         U R                   R                  [        S9  g)a  
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.
)r5   r`   r5   r`   r   rS  N)r  r   strr9   r<   r  r   r   poprU  rV  )r   tempEventListstackrj  noteeventkeys        r   rW  MIDITrack.deInterleaveNotesZ  s    ''E}} 55
  #5;;/#emm2DD==H,#,+225::>/4zzl+!((/]]i/5./!3%*%8%<%<%>
%,,U3+//1%,,U3 0 $$U++ (. + 	K0r   c                     [        U R                  5      S:X  a  g/ nU(       a  UOSnSnU R                   H.  nUR                  U-
  nXu-
  Ul        UnUR                  U5        M0     X0l        g)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)r   r  r   r  )r   originadjustro  internal_originrunningTickrj  adjustedTicks           r   adjustTimeAndOriginMIDITrack.adjustTimeAndOrigin  sp     t!!"a'$*&''E ::7L%3EJ&K  '	 ( +r   c                     UR                  U R                  5        UR                  U R                  5        UR                  U R                  5        g)z
Write track to disk.
N)writer  r   r  r   
fileHandles     r   
writeTrackMIDITrack.writeTrack  s<    
 	**+)'r   )r  r  r  r   r  r  r  r  r[   r}   )r   Fr   )r   Tr   r   )r,   r-   r.   r/   r0   r   r  r!  r$  r'  r*  r-  r1  r4  r7  r;  r?  rB  rE  rP  rY  r  ra  rg  rf  rW  ry  r  r2   r3   r   r   r
  r
  |  s    
) :;H" ,-PrFK 7<*+@NUJ =>NJ )*M@ EI9:O %-" (@"B*1X+,(r   r
  c                   $    \ rS rSrSrS rS rSrg)
MIDIHeaderi  a2  
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                 .   [         R                  " SSSSS5      U l        [         R                  " SS5      U l        [         R                  " SU5      U l        X l        [         R                  " SU5      U l        [         R                  " SU5      U l        g	)
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   drw      z>HN)rP   rQ   r  
headerSize	formatnumnumeric_format	numTracksticks_per_quarternote)r   r  file_formatr  s       r   r   MIDIHeader.__init__  sm     #KKdD$G ++dA.T;7)T95%+[[7L%M"r   c                    UR                  U R                  5        UR                  U R                  5        UR                  U R                  5        UR                  U R                  5        UR                  U R
                  5        g r   )r|  r  r  r  r  r  r}  s     r   	writeFileMIDIHeader.writeFile  s`    **+)((334r   )r  r  r  r  r  r  N)r,   r-   r.   r/   r0   r   r  r2   r3   r   r   r  r    s    N.5r   r  c                       \ rS rSrSrSSSSS\S4S jrS rS r S!S
 jr	S r
 S"S jrS rS r S#S jrS rS rS rS rS r S$S jr S$S jrS$S jr S$S jr  S%S jrS r S&S jrS rS#S jrS rS rS rg	)'r
   i  z
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                    [        5       U l        US:X  a  US-   U l        OXl        [        U R                  XV5      U l        X@l        SU l        X`l        Xpl        U R                  (       a	  S U l	        OU R                  U l	        [        SU R                  5       H'  nU R                  R                  [        X#5      5        M)     SU l        g)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                     U $ r   r3   )r   s    r   <lambda>#MIDIFile.__init__.<locals>.<lambda>%  s    1r   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  r   s	            r   r   MIDIFile.__init__  s    @ f!&]DN&N T*%:""4""!,D!%!5!5Dq$..)AKKy)9HI * r   c                 0    [        XR                  -  5      $ r   )r(   r  )r   quarternote_times     r   r  MIDIFile.quarter_to_tick1  s    #&@&@@AAr   c                 2    [        U5      U R                  -  $ r   )floatr  )r   ticknums     r   tick_to_quarterMIDIFile.tick_to_quarter4  s    W~ : :::r   Nc           
          U R                   R                  S:X  a  US-  nU R                  U   R                  X#U R	                  U5      U R	                  U5      XgU R
                  S9  U =R
                  S-  sl        g)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  N)r  r  r  r  r  r  )r   trackr<   r9   timer:   r;   r=   s           r   addNoteMIDIFile.addNote7  sy    0 ;;%%*QJEE**7+/+=+=d+CTEWEWX`Ea+1;?;M;M 	+ 	O 	ar   c                     U R                   R                  S:X  a  US-  nU R                  U   R                  U R	                  U5      UU R
                  S9  U =R
                  S-  sl        g)a  
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  r7  r  r  )r   r  r  r   s       r   r7  MIDIFile.addTrackNameW  se     ;;%%*QJEE''(:(:4(@)8<8J8J 	( 	Lar   c           	          U R                   R                  S:X  a  SnU R                  U   R                  U R	                  U5      X4XVU R
                  S9  U =R
                  S-  sl        g)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  s          r   r;  MIDIFile.addTimeSignatureg  sg    f ;;%%*EE++D,>,>t,Di,;<@<N<N 	, 	P 	ar   c                     U R                   R                  S:X  a  SnU R                  U   R                  U R	                  U5      UU R
                  S9  U =R
                  S-  sl        g)aC  

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  rp   s       r   r'  MIDIFile.addTempo  sa     ;;%%*EE##D$6$6t$<e484F4F 	$ 	Har   c                     U R                   R                  S:X  a  US-  nU R                  U   R                  U R	                  U5      UU R
                  S9  U =R
                  S-  sl        g)z

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  r   s       r   r?  MIDIFile.addCopyright  se     ;;%%*QJEE''(:(:4(@&8<8J8J 	( 	Lar   c                     U R                   R                  S:X  a  SnU R                  U   R                  U R	                  U5      X4XPR
                  S9  U =R
                  S-  sl        g)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  rB  r  r  )r   r  r  r   r   r   r   s          r   rB  MIDIFile.addKeySignature  sb    @ ;;%%*EE**4+=+=d+C[+/ASAS 	+ 	Uar   c                     U R                   R                  S:X  a  US-  nU R                  U   R                  U R	                  U5      UU R
                  S9  U =R
                  S-  sl        g)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  rE  r  r  )r   r  r  r   s       r   rE  MIDIFile.addText  se     ;;%%*QJEE""4#5#5d#;T373E3E 	# 	Gar   c                     U R                   R                  S:X  a  US-  nU R                  U   R                  X R	                  U5      UU R
                  S9  U =R
                  S-  sl        g)aV  

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  r1  r  r  )r   tracknumr<   r  r0  s        r   r1  MIDIFile.addProgramChange  sf     ;;%%*MHH..w8J8J48PRY?C?Q?Q 	/ 	Sar   c                     U R                   R                  S:X  a  US-  nU R                  U   nUR                  X R	                  U5      UU R
                  S9  U =R
                  S-  sl        g)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  r4  r  r  )r   r  r<   r  r   r  s         r   r4  MIDIFile.addChannelPressure
  sj     ;;%%*MHH%  *<*<T*BN151C1C 	! 	Ear   c                     U R                   R                  S:X  a  US-  nU R                  U   R                  X R	                  U5      UXPR
                  S9  U =R
                  S-  sl        g)az  

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  r   r   s         r   r!  MIDIFile.addControllerEvent  sf     ;;%%*QJEE--g7I7I$7OQb.7I[I[ 	. 	]ar   c                     U R                   R                  S:X  a  US-  nU R                  U   R                  X R	                  U5      UU R
                  S9  U =R
                  S-  sl        g)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  pitchWheelValues        r   r$  MIDIFile.addPitchWheelEvent0  sf     ;;%%*QJEE--g7I7I$7OQ`>B>P>P 	. 	Rar   c	                 8   U R                  U5      n	U R                  R                  S:X  a  US-  nU R                  U   nU(       a  SOSn
UR	                  X)SX@R
                  S9  U =R
                  S-  sl        X-  n	UR	                  X)SXPR
                  S9  U =R
                  S-  sl        X-  n	UR	                  X)SX`R
                  S9  U =R
                  S-  sl        X-  n	Ub1  UR	                  X)SXpR
                  S9  U =R
                  S-  sl        gg)	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.)

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_incrs              r   makeRPNCallMIDIFile.makeRPNCall@  s2   r !!$';;%%*QJEE"#A	  !/ASAS 	! 	Ua  !/ASAS 	! 	Ua  !);M;M 	! 	Oa$$WB%-?Q?Q % S!#  r   c	                 8   U R                  U5      n	U R                  R                  S:X  a  US-  nU R                  U   nU(       a  SOSn
UR	                  X)SX@R
                  S9  U =R
                  S-  sl        X-  n	UR	                  X)SXPR
                  S9  U =R
                  S-  sl        X-  n	UR	                  X)SX`R
                  S9  U =R
                  S-  sl        X-  n	Ub1  UR	                  X)SXpR
                  S9  U =R
                  S-  sl        gg)	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  s              r   makeNRPNCallMIDIFile.makeNRPNCall  s1   > !!$';;%%*QJEE"#A	  !/ASAS 	! 	Ua  !/ASAS 	! 	Ua  !);M;M 	! 	Oa$$WB%-?Q?Q % S!#  r   c                 .    U R                  XUSSSUUS9  g)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   rx   r  Nr  )r   r  r<   r  bankr  s         r   changeTuningBankMIDIFile.changeTuningBank  s&    & 	q!Q$. 	 	0r   c                 .    U R                  XUSSSUUS9  g)an  

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  Nr  )r   r  r<   r  r0  r  s         r   changeTuningProgramMIDIFile.changeTuningProgram  s&    * 	q!Q$. 	 	0r   c                     U R                   R                  S:X  a  US-  nU R                  U   R                  X#UUU R                  S9  U =R                  S-  sl        g)af  
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  rP  r  )r   r  rJ  r   r   rK  s         r   rP  MIDIFile.changeNoteTuning  s`    F ;;%%*QJEE++G8,8<@<N<N 	, 	P 	ar   c                     U R                  U5      nU R                  R                  S:X  a  US-  nU R                  U   R	                  XSUU R
                  S9  U =R
                  S-  sl        g)aB  

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   s         r   r*  MIDIFile.addSysEx   sj    " !!$';;%%*QJEE##D484F4F 	$ 	Har   c           
          U R                  U5      nU R                  R                  S:X  a  US-  nU R                  U   R	                  XXEXgU R
                  S9  U =R
                  S-  sl        g)a  

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  rU   ry   r   r   r   r   s	            r   r-  MIDIFile.addUniversalSysEx8  so    4 !!$';;%%*QJEE,,T-9=A=O=O 	- 	Q 	ar   c                     U R                   R                  U5        U R                  5         [        SU R                  5       H!  nU R
                  U   R                  U5        M#     g)ze
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   s      r   r  MIDIFile.writeFileZ  sN     	j) 	

 q$..)AKKN%%j1 *r   c                    SnU R                  U5      nU R                   HO  n[        UR                  5      S:  d  M  UR                   H!  nUR                  U:  d  M  UR                  nM#     MQ     U R                   HG  n/ nUR                   H,  nUR                  U-
  nXs-   Ul        UR                  U5        M.     Xdl        MI     g)a+  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.
 r   N)r  r  r   r  r   r  )r   offsetrt  tick_offsetr  rj  ro  rx  s           r   shiftTracksMIDIFile.shiftTracksk  s     ((0[[E5??#a'"__EzzF*!& - ! [[EM $zzF2)7
$$U+ ) ,O !r   c                    U R                   (       a  g[        SU R                  5       HJ  nU R                  U   R	                  5         U R                  U   R
                  R                  [        S9  ML     U R                  5       n[        SU R                  5       HH  nU R                  U   R                  X R                  5        U R                  U   R                  5         MJ     SU l         g)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   rS  T)r  r  r  r  ra  r  rU  rV  
findOriginry  r  rg  )r   r   rt  s      r   r  MIDIFile.close  s     ;;q$..)AKKN%%'
 KKN((--+-> * "q$..)AKKN..v7I7IJKKN**, * r   c                     SnU R                    HV  n[        UR                  5      S:  d  M  UR                  S   R                  U:  d  M=  UR                  S   R                  nMX     U$ )z5
Find the earliest time in the file's tracks.append.
r  r   )r  r   r  r   )r   rt  r  s      r   r  MIDIFile.findOrigin  sb      [[E5&&'!+&&q)..7"00388F !
 r   )	r  r  r  r  r  r  r  r  r  r   )rH  r}   )F)r   Tr   )r   F) r,   r-   r.   r/   r0   TICKSPERQUARTERNOTEr   r  r  r  r7  r;  r'  r?  rB  rE  r1  r4  r!  r$  r  r  r  r  rP  r*  r-  r  r  r  r  r2   r3   r   r   r
   r
     s     "#T$!':uTrB;   @ " =>9 v " " )*$ L  $ $ ( " 49O$d EJ5$n0. (-00 =A56( T 2 7<  D2",@8r   r
   c                     U S:X  a  S/$ / nSnU S:  a)  UR                  U S-  U-  S-  5        U S-  n SnU S:  a  M)  UR                  5         U$ )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   r   ru   r   rb   )r  reverse)r   vlbyteshibits      r   rO   rO     se    ( 	Avs
GE
a%TU*d23	a a% OONr   c                     U nSnSn US-  n[         R                  " SX5      S   nUS-   nUS-   nX5S-  -   nUS-  S:X  a   X44$ M?  )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   rL   r   r   rb   )rP   unpack_from)r  buffertoffsetoutput	bytesReadrO  s         r   readVarLengthr    sq     GFI
1!!$8;A+M	#:&3J1 r   c                    Sn[        U 5      n SS[        R                  " U [        S5      -  S5      -  -   n[        U5      nS[	        S[        U5      S-
  S-  5      -  nX:w  a  S	[        R                  " X-  S5      -  OS
n[        US-  U-  5      n[        [        U5      S-	  S/5      nXgS-  -
  n[        US/5      nUS:X  a  US:X  a  US:X  a  Sn[        U5      nX7U/$ )z0
Returns a three-byte transform of a frequency.
i @  E   r"     rc          @     @Q@      (@i  r   r  r   r   r   )r  mathlogr(   powroundmin)	freq
resolutiondollars	firstByte	lowerFreqcentDifcents
secondByte	thirdBytes	            r   rI  rI    s     J;D2s!4a888GGIc#y!1D!8D @BBI8<8IdTXXt/!44qG'C-*,-Ec%jAot,-Jq)IY%&IDZ4/I4E	II9--r   c                     SnS[        S[        U S   S-
  5      S-  5      -  n[        [        U S   5      S-  [        U S	   5      -   5      S
-  U-  nU[        SUS-  5      -  nU$ )zM
The reverse of frequencyTransform. Given a byte stream, return a frequency.
g      @r   r  r   r  r  r   r   rc   g      Y@g     @)r  r  r(   )	freqBytesr
  baseFrequencyfracrM  s        r   returnFrequencyr    sx     J#cE)A,*=$>$EGGM3y|$)S1->>?%G:UDC 77Ir   c                 H    U R                   U R                  U R                  4$ )a  
.. 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   r1   r   )rj  s    r   rV  rV    s!    4 JJ,,e.C.CDDr   )&
__future__r   r   r  rP   warnings__version__r  controllerEventTypesr   r   r   r   __all__objectr   r5   r`   rm   r   r   r   r   r   r   r   r   r   r   r   r
  r  r
   rO   r  rI  r  rV  r3   r   r   <module>r     sM   0   
  t}  	
		


;-6 -`)\ )X*| *Z5L 5p% %P< >< B!L !H# #L/, /d l  F <  F l  F" "J!L !Hg( g(T	&5 &5Riv iXF*.(Er   