
    ̺iH.                     ,   S r SSKrSSKrSSKrSSKJr  SSKJr  Sr\q	S r
\4S jrS	 rS
 rS 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 jrS S jrS rg)!z%
Useful tools for working with ports
    N   )Message)ParsergMbP?c                  8    [         R                  " [        5        g)zvSleep for N seconds.

This is used in ports when polling and waiting for messages. N can
be set with set_sleep_time().N)timesleep_sleep_time     I/var/www/piano.thomer.com/venv/lib/python3.13/site-packages/mido/ports.pyr   r      s    
 	JJ{r   c                     U q g)z-Set the number of seconds sleep() will sleep.Nr	   )secondss    r   set_sleep_timer      s	     Kr   c                      [         $ )z)Get number of seconds sleep() will sleep.r   r
   r   r   get_sleep_timer   #   s    r   c               #   d   #    Sn Sn[        S5       H  nX4 H  n[        SX#S9v   M     M     g7f)zBYield "All Notes Off" and "Reset All Controllers" for all channels{   y      control_changechannelcontrolNranger   )ALL_NOTES_OFFRESET_ALL_CONTROLLERSr   r   s       r   reset_messagesr   (   s9     M9%=G*GMM > s   .0c               #   L   #    Sn [        S5       H  n[        SXS9v   M     g7f)zYield "All Sounds Off" for all channels.

This will mute all sounding notes regardless of
envelopes. Useful when notes are hanging and nothing else
helps.
x   r   r   r   Nr   )ALL_SOUNDS_OFFr   s     r   panic_messagesr#   1   s-      N9&%? 	? s   "$c                        \ rS rSrS rS rSrg)	DummyLock>   c                     U $ Nr
   selfs    r   	__enter__DummyLock.__enter__?       r   c                     gNFr
   )r*   _s     r   __exit__DummyLock.__exit__B   s    r   r
   N)__name__
__module____qualname____firstlineno__r+   r1   __static_attributes__r
   r   r   r%   r%   >   s    r   r%   c                   X    \ rS rSrSrSrSrSrSS jrS r	S r
S	 rS
 rS rS rS rSrg)BasePortF   z1
Abstract base class for Input and Output ports.
FTNc                     [        U S5      (       a  g Xl        U R                  (       a  [        R                  " 5       U l        O[        5       U l        SU l        U R                  " S0 UD6  SU l        g )NclosedTFr
   )	hasattrname_locking	threadingRLock_lockr%   r<   _openr*   r>   kwargss      r   __init__BasePort.__init__N   sT    4"" 	=="*DJ"DJ

Vr   c                     g r(   r
   )r*   rE   s     r   rC   BasePort._open]       r   c                     g r(   r
   r)   s    r   _closeBasePort._close`   rJ   r   c                 &   U R                      U R                  (       dJ  [        U S5      (       a"  U R                  (       a   U R	                  5         U R                  5         SU l        SSS5        g! [
         a     N,f = f! , (       d  f       g= f)zClose the port.

If the port is already closed, nothing will happen.  The port
is automatically closed when the object goes out of scope or
is garbage collected.
	autoresetTN)rB   r<   r=   rO   resetOSErrorrL   r)   s    r   closeBasePort.closec   sh     ZZ;;4--$..

 " Z
 #  Zs/   4BA2B2
A?<B>A??B
Bc                 $    U R                  5         g r(   rR   r)   s    r   __del__BasePort.__del__u   s    

r   c                     U $ r(   r
   r)   s    r   r+   BasePort.__enter__x   r-   r   c                 $    U R                  5         gr/   rU   )r*   typevalue	tracebacks       r   r1   BasePort.__exit__{   s    

r   c                 &   U R                   (       a  SnOSnU R                  U R                  4nSSSSS.U   nU R                  =(       d    Sn U R                  nS	R                  XXE5      $ ! [
         a    U R                  R                  n N4f = f)
Nr<   openinputoutputzI/O portz	mute port))TFFT)TT)FF z<{} {} {!r} ({})>)	r<   is_input	is_outputr>   _device_typeAttributeError	__class__r3   format)r*   statecapabilities	port_typer>   device_types         r   __repr__BasePort.__repr__   s    ;;EE}}dnn4$+$,#-%0 $	%	 yyB	2++K #))d1 	1  	2..11K	2s   A- - BB)rB   r<   r>   r(   )r3   r4   r5   r6   __doc__re   rf   r?   rF   rC   rL   rR   rV   r+   r1   ro   r7   r
   r   r   r9   r9   F   s@     HIH#$1r   r9   c                   R    \ rS rSrSrSrSS jrS rSS jrS r	SS jr
S	 rS
 rSrg)	BaseInput   zBase class for input port.

Subclass and override _receive() to create a new input port type.
(See portmidi.py for an example of how to do this.)
Tc                     [         R                  " X40 UD6  [        5       U l        U R                  R                  U l        g)zCreate an input port.

name is the port name, as returned by input_names(). If
name is not passed, the default input is used instead.
N)r9   rF   r   _parsermessages	_messagesrD   s      r   rF   BaseInput.__init__   s1     	$//x..r   c                 X    [        U S5      (       a  U R                  b  [        S5      eg g )Ncallbackza callback is set for this port)r=   r{   
ValueErrorr)   s    r   _check_callbackBaseInput._check_callback   s-    4$$)B>?? *C$r   c                     g r(   r
   r*   blocks     r   _receiveBaseInput._receive   rJ   r   c              #   @   #     U R                  5       nUc  gUv   M  7f)z!Iterate through pending messages.N)pollr*   msgs     r   iter_pendingBaseInput.iter_pending   s%     ))+C{	 s   c                    U R                   (       d  [        S5      eU R                  5         U R                     U R                  (       a#  U R                  R                  5       sSSS5        $  SSS5        U R                  (       a  U(       a  [        S5      eg U R                     U R                  US9nU(       a  UsSSS5        $ U R                  (       a#  U R                  R                  5       sSSS5        $ U(       d
   SSS5        gU R                  (       a  [        S5      e SSS5        [        5         M  ! , (       d  f       N= f! , (       d  f       N+= f)a  Return the next message.

This will block until a message arrives.

If you pass block=False it will not block and instead return
None if there is no available message.

If the port is closed and there are no pending messages IOError
will be raised. If the port closes while waiting inside receive(),
IOError will be raised. TODO: this seems a bit inconsistent. Should
different errors be raised? What's most useful here?
zNot an input portNzreceive() called on closed portr   zport closed during receive())
re   r|   r}   rB   rx   popleftr<   r   rQ   r   )r*   r   r   s      r   receiveBaseInput.receive   s     }}011 ZZ~~~~--/ Z  ;; !BCCmm%m0 
 >>>>113    [[!"@AA !  G  Z s)   +E (E
*E>EE 
E
Ec                      U R                  SS9$ )z]Receive the next pending message or None

This is the same as calling `receive(block=False)`.Fr   )r   r)   s    r   r   BaseInput.poll   s     ||%|((r   c              #      #    U R                  5           U R                  5       v   M  ! [         a    U R                  (       a   ge f = f7f)z/Iterate through messages until the port closes.N)r}   r   rQ   r<   r)   s    r   __iter__BaseInput.__iter__   sK     
 		lln$   ;; s%   A) AAAAA)rx   rv   Nrd   T)r3   r4   r5   r6   rq   re   rF   r}   r   r   r   r   r   r7   r
   r   r   rs   rs      s3    
 H/@*X)r   rs   c                   >    \ rS rSrSrSrSS jrS rS rS r	S r
S	rg
)
BaseOutput   z~
Base class for output port.

Subclass and override _send() to create a new port type.  (See
portmidi.py for how to do this.)
Tc                 >    [         R                  " X40 UD6  X l        g)zCreate an output port

name is the port name, as returned by output_names(). If
name is not passed, the default output is used instead.
N)r9   rF   rO   )r*   r>   rO   rE   s       r   rF   BaseOutput.__init__  s     	$//"r   c                     g r(   r
   r   s     r   _sendBaseOutput._send  rJ   r   c                 >   U R                   (       d  [        S5      e[        U[        5      (       d  [	        S5      eU R
                  (       a  [        S5      eU R                     U R                  UR                  5       5        SSS5        g! , (       d  f       g= f)zSend a message on the port.

A copy of the message will be sent, so you can safely modify
the original message without any unexpected consequences.
zNot an output portz$argument to send() must be a Messagezsend() called on closed portN)	rf   r|   
isinstancer   	TypeErrorr<   rB   r   copyr   s     r   sendBaseOutput.send  sf     ~~122C))BCC[[;<<ZZJJsxxz" ZZs   % B
Bc                 l    U R                   (       a  g[        5        H  nU R                  U5        M     g)z@Send "All Notes Off" and "Reset All Controllers" on all channelsN)r<   r   r   r   s     r   rP   BaseOutput.reset#  s$    ;;!#CIIcN $r   c                 l    U R                   (       a  g[        5        H  nU R                  U5        M     g)zSend "All Sounds Off" on all channels.

This will mute all sounding notes regardless of
envelopes. Useful when notes are hanging and nothing else
helps.
N)r<   r#   r   r   s     r   panicBaseOutput.panic+  s&     ;;!#CIIcN $r   )rO   N)rd   F)r3   r4   r5   r6   rq   rf   rF   r   r   rP   r   r7   r
   r   r   r   r      s'     I## r   r   c                       \ rS rSrSS jrSrg)
BaseIOPorti9  c                 `    [         R                  " X40 UD6  [        R                  " X40 UD6  g)zJCreate an IO port.

name is the port name, as returned by ioport_names().
N)rs   rF   r   rD   s      r   rF   BaseIOPort.__init__:  s*    
 	400D1&1r   r
   Nr   )r3   r4   r5   r6   rF   r7   r
   r   r   r   r   9  s    2r   r   c                   8    \ rS rSrSrSrS rS rS rS
S jr	Sr
g	)IOPortiC  zInput / output port.

This is a convenient wrapper around an input port and an output
port which provides the functionality of both. Every method call
is forwarded to the appropriate port.
Fc                     Xl         X l        [        UR                  5       S[        UR                  5       3U l        U R                   R                  U l        SU l        [        5       U l        g )Nz + F)ra   rb   strr>   rx   r<   r%   rB   )r*   ra   rb   s      r   rF   IOPort.__init__M  sT    
 5::'s3v{{+;*<=	--[
r   c                 l    U R                   R                  5         U R                  R                  5         g r(   )ra   rR   rb   r)   s    r   rL   IOPort._closeW  s"    

r   c                 :    U R                   R                  U5        g r(   )rb   r   r*   messages     r   r   IOPort._send[  s    !r   c                 4    U R                   R                  US9$ )Nr   )ra   r   r   s     r   r   IOPort._receive^  s    zz!!!..r   )rB   rx   r<   ra   r>   rb   Nr   )r3   r4   r5   r6   rq   r?   rF   rL   r   r   r7   r
   r   r   r   r   C  s"     H!"/r   r   c                   2    \ rS rSrS r\R                  rSrg)EchoPortib  c                 :    U R                   R                  U5        g r(   )rx   appendr   s     r   r   EchoPort._sendc  s    g&r   r
   N)	r3   r4   r5   r6   r   r   r   r   r7   r
   r   r   r   r   b  s    ' &&Hr   r   c                   .    \ rS rSrSS jrS rSS jrSrg)		MultiPortii  c                 \    [         R                  U S5        [        U5      U l        X l        g )Nmulti)r   rF   listportsyield_ports)r*   r   r   s      r   rF   MultiPort.__init__j  s#    D'*%[
&r   c                 r    U R                    H'  nUR                  (       a  M  UR                  U5        M)     g r(   )r   r<   r   )r*   r   ports      r   r   MultiPort._sendo  s%    JJD;;;		'" r   c                 t    U R                   R                  [        U R                  U R                  US95        g )Nr   r   )rx   extendmulti_receiver   r   r   s     r   r   MultiPort._receiveu  s-    mDJJ8<8H8H279 	:r   r   r   NFr   )r3   r4   r5   r6   rF   r   r   r7   r
   r   r   r   r   i  s    '
#:r   r   c              #     #    [        U 5      n  [        R                  " U 5        U  H?  nUR                  (       a  M  UR	                  5        H  nU(       a  X44v   M  Uv   M     MA     U(       a  [        5         OgMp  7f)aW  Receive messages from multiple ports.

Generates messages from ever input port. The ports are polled in
random order for fairness, and all messages from each port are
yielded before moving on to the next port.

If yield_ports=True, (port, message) is yielded instead of just
the message.

If block=False only pending messages will be yielded.
N)r   randomshuffler<   r   r   )r   r   r   r   r   s        r   r   r   {  sg      KE
uD;;;#002G""m+%	  3  G s   7A?AA?c                     [        XSS9$ )zIterate through all pending messages in ports.

This is the same as calling multi_receive(ports, block=False).
The function is kept around for backwards compatability.
Fr   )r   r   s     r   multi_iter_pendingr     s     uEEr   c                 8    U  H  nUR                  U5        M     g)zSend message on all ports.N)r   )r   r   r   s      r   
multi_sendr     s    		# r   rc   r   )rq   r   r@   r   rw   r   parserr   _DEFAULT_SLEEP_TIMEr	   r   r   r   r   r#   r%   r9   rs   r   r   r   r   r   r   r   r   r
   r   r   <module>r      s   
       ! / 
N
? N1 N1bd dN8 8v2J 2/Z />'z ':
 :$>Fr   