
    &h_                     2   S r SSKrSSKrSSKrSSKrSSKJr  \R                  R                  r	\R                  R                  r
/ SQrSS/rSS jrS rS rS	 rS
 rS rS rS rS r " S S5      r " S S5      rS rS r " S S\5      rS rS rS rg)a  pygame.midi
pygame module for interacting with midi input and output.

The midi module can send output to midi devices, and get input
from midi devices.  It can also list midi devices on the system.

Including real midi devices, and virtual ones.

It uses the portmidi library.  Is portable to which ever platforms
portmidi supports (currently windows, OSX, and linux).

This uses pyportmidi for now, but may use its own bindings at some
point in the future.  The pyportmidi bindings are included with pygame.

New in pygame 1.9.0.
    N)InputMIDIINMIDIOUTMidiExceptionOutput	get_countget_default_input_idget_default_output_idget_device_infoinitmidis2eventsquitget_inittimefrequency_to_midimidi_to_frequencymidi_to_ansi_noter   r   c                     U b  U [         l        U $  [         R                    [         R                  $ ! [         a     gf = f)NF)_module_initvalueAttributeError)states    C/var/www/auris/envauris/lib/python3.13/site-packages/pygame/midi.pyr   r   <   sG     "   s   2 
??c                      [        5       (       d;  [        R                  " 5         [        S5        [        R                  " [
        5        gg)zinitialize the midi module
pygame.midi.init(): return None

Call the initialisation function before using the midi module.

It is safe to call this more than once.
TN)r   _pypm
Initializeatexitregisterr        r   r   r   J   s0     >>T r    c                  d    [        5       (       a!  [        R                  " 5         [        S5        gg)zuninitialize the midi module
pygame.midi.quit(): return None


Called automatically atexit if you don't call it.

It is safe to call this function more than once.
FN)r   r   	Terminater   r    r   r   r   X   s"     ~~U r    c                      [        5       $ )zreturns True if the midi module is currently initialized
pygame.midi.get_init(): return bool

Returns True if the pygame.midi module is currently initialized.

New in pygame 1.9.5.
)r   r   r    r   r   r   g   s     >r    c                  8    [        5       (       d  [        S5      eg )Nzpygame.midi not initialised.)r   RuntimeErrorr   r    r   _check_initr&   r   s    >>9:: r    c                  @    [        5         [        R                  " 5       $ )ztgets the number of devices.
pygame.midi.get_count(): return num_devices


Device ids range from 0 to get_count() -1
)r&   r   CountDevicesr   r    r   r   r   w   s     Mr    c                  @    [        5         [        R                  " 5       $ )aW  gets default input device number
pygame.midi.get_default_input_id(): return default_id


Return the default device ID or -1 if there are no devices.
The result can be passed to the Input()/Output() class.

On the PC, the user can specify a default device by
setting an environment variable. For example, to use device #1.

    set PM_RECOMMENDED_INPUT_DEVICE=1

The user should first determine the available device ID by using
the supplied application "testin" or "testout".

In general, the registry is a better place for this kind of info,
and with USB devices that can come and go, using integers is not
very reliable for device identification. Under Windows, if
PM_RECOMMENDED_OUTPUT_DEVICE (or PM_RECOMMENDED_INPUT_DEVICE) is
*NOT* found in the environment, then the default device is obtained
by looking for a string in the registry under:
    HKEY_LOCAL_MACHINE/SOFTWARE/PortMidi/Recommended_Input_Device
and HKEY_LOCAL_MACHINE/SOFTWARE/PortMidi/Recommended_Output_Device
for a string. The number of the first device with a substring that
matches the string exactly is returned. For example, if the string
in the registry is "USB", and device 1 is named
"In USB MidiSport 1x1", then that will be the default
input because it contains the string "USB".

In addition to the name, get_device_info() returns "interf", which
is the interface name. (The "interface" is the underlying software
system or API used by PortMidi to access devices. Examples are
MMSystem, DirectX (not implemented), ALSA, OSS (not implemented), etc.)
At present, the only Win32 interface is "MMSystem", the only Linux
interface is "ALSA", and the only Max OS X interface is "CoreMIDI".
To specify both the interface and the device name in the registry,
separate the two with a comma and a space, e.g.:
    MMSystem, In USB MidiSport 1x1
In this case, the string before the comma must be a substring of
the "interf" string, and the string after the space must be a
substring of the "name" name string in order to match the device.

Note: in the current release, the default is simply the first device
(the input or output device with the lowest PmDeviceID).
)r&   r   GetDefaultInputDeviceIDr   r    r   r	   r	      s    \ M((**r    c                  @    [        5         [        R                  " 5       $ )aZ  gets default output device number
pygame.midi.get_default_output_id(): return default_id


Return the default device ID or -1 if there are no devices.
The result can be passed to the Input()/Output() class.

On the PC, the user can specify a default device by
setting an environment variable. For example, to use device #1.

    set PM_RECOMMENDED_OUTPUT_DEVICE=1

The user should first determine the available device ID by using
the supplied application "testin" or "testout".

In general, the registry is a better place for this kind of info,
and with USB devices that can come and go, using integers is not
very reliable for device identification. Under Windows, if
PM_RECOMMENDED_OUTPUT_DEVICE (or PM_RECOMMENDED_INPUT_DEVICE) is
*NOT* found in the environment, then the default device is obtained
by looking for a string in the registry under:
    HKEY_LOCAL_MACHINE/SOFTWARE/PortMidi/Recommended_Input_Device
and HKEY_LOCAL_MACHINE/SOFTWARE/PortMidi/Recommended_Output_Device
for a string. The number of the first device with a substring that
matches the string exactly is returned. For example, if the string
in the registry is "USB", and device 1 is named
"In USB MidiSport 1x1", then that will be the default
input because it contains the string "USB".

In addition to the name, get_device_info() returns "interf", which
is the interface name. (The "interface" is the underlying software
system or API used by PortMidi to access devices. Examples are
MMSystem, DirectX (not implemented), ALSA, OSS (not implemented), etc.)
At present, the only Win32 interface is "MMSystem", the only Linux
interface is "ALSA", and the only Max OS X interface is "CoreMIDI".
To specify both the interface and the device name in the registry,
separate the two with a comma and a space, e.g.:
    MMSystem, In USB MidiSport 1x1
In this case, the string before the comma must be a substring of
the "interf" string, and the string after the space must be a
substring of the "name" name string in order to match the device.

Note: in the current release, the default is simply the first device
(the input or output device with the lowest PmDeviceID).
)r&   r   GetDefaultOutputDeviceIDr   r    r   r
   r
      s    \ M))++r    c                 B    [        5         [        R                  " U 5      $ )a(  returns information about a midi device
pygame.midi.get_device_info(an_id): return (interf, name,
                                            input, output,
                                            opened)

interf - a byte string describing the device interface, eg b'ALSA'.
name - a byte string for the name of the device, eg b'Midi Through Port-0'
input - 0, or 1 if the device is an input device.
output - 0, or 1 if the device is an output device.
opened - 0, or 1 if the device is opened.

If the id is out of range, the function returns None.
)r&   r   GetDeviceInfo)an_ids    r   r   r      s     Mu%%r    c                   :    \ rS rSrSrS
S jrS rS rS rS r	Sr
g	)r      zInput is used to get midi input from midi devices.
Input(device_id)
Input(device_id, buffer_size)

buffer_size - the number of input events to be buffered waiting to
  be read using Input.read()
c                    [        5         US:X  a  [        S5      e [        U5      nU(       aM  Uu    pEpdU(       a#   [
        R                  " X5      U l        Xl        gU(       a  [        S5      e[        S5      e[        S5      e! [         a    [        S5      e[         a    [	        S5      ef = f! [         a    [        S5      ef = f)	zl
The buffer_size specifies the number of input events to be buffered
waiting to be read using Input.read().
_Device id is -1, not a valid output id.  -1 usually means there were no default Output devices.an integer is required$long int too large to convert to intz<Device id given is not a valid input id, it is an output id.z(Device id given is not a valid input id. Device id invalid, out of range.N)	r&   r   r   	TypeErrorOverflowErrorr   r   _input	device_id)selfr;   buffer_sizeresult_is_input	is_outputs          r   __init__Input.__init__  s    
 	?" 	H$Y/F +1(AqI>"'++i"EDK "+#R  $$NOO BCC/  	6455 	H FGG	H ! >#$<==>s   B B4 *B14C
c                 4    U R                   c  [        S5      eg )Nmidi not open.)r:   r   r<   s    r   _check_openInput._check_open*  s    ;; 011 r    c                 t    [        5         U R                  b  U R                  R                  5         SU l        g)zcloses a midi stream, flushing any pending buffers.
Input.close(): return None

PortMidi attempts to close open streams when the application
exits -- this is particularly difficult under Windows.
N)r&   r:   CloserF   s    r   closeInput.close.  s*     	;;"KKr    c                 l    [        5         U R                  5         U R                  R                  U5      $ )zreads num_events midi events from the buffer.
Input.read(num_events): return midi_event_list

Reads from the Input buffer and gives back midi events.
[[[status,data1,data2,data3],timestamp],
 [[status,data1,data2,data3],timestamp],...]
)r&   rG   r:   Read)r<   
num_eventss     r   read
Input.read:  s*     	{{
++r    c                    [        5         U R                  5         U R                  R                  5       nU[        R
                  :X  a  gU[        R                  :X  a  g[        R                  " U5      n[        X45      e)zkreturns true if there's data, or false if not.
Input.poll(): return Bool

raises a MidiException on error.
TF)	r&   rG   r:   Pollr   TRUEFALSEGetErrorTextr   )r<   r>   err_texts      r   poll
Input.pollF  sc     	!!#UZZU[[ %%f-V.//r    )r:   r;   N)i   )__name__
__module____qualname____firstlineno____doc__rB   rG   rK   rP   rX   __static_attributes__r   r    r   r   r      s"    'DR2

,0r    c                   r    \ rS rSrSrSS jrS rS rS rS r	SS jr
S	 rSS
 jrSS jrSS jrSS jrSrg)r   iZ  ao  Output is used to send midi to an output device
Output(device_id)
Output(device_id, latency = 0)
Output(device_id, buffer_size = 4096)
Output(device_id, latency, buffer_size)

The buffer_size specifies the number of output events to be
buffered waiting for output.  (In some cases -- see below --
PortMidi does not buffer output at all and merely passes data
to a lower-level API, in which case buffersize is ignored.)

latency is the delay in milliseconds applied to timestamps to determine
when the output should actually occur. (If latency is < 0, 0 is
assumed.)

If latency is zero, timestamps are ignored and all output is delivered
immediately. If latency is greater than zero, output is delayed until
the message timestamp plus the latency. (NOTE: time is measured
relative to the time source indicated by time_proc. Timestamps are
absolute, not relative delays or offsets.) In some cases, PortMidi
can obtain better timing than your application by passing timestamps
along to the device driver or hardware. Latency may also help you
to synchronize midi data to audio data by matching midi latency to
the audio buffer latency.

c                    [        5         SU l        US:X  a  [        S5      e [        U5      nU(       aN  Uu    pVpuU(       a$   [        R                  " XU5      U l        Xl	        g	U(       a  [        S5      e[        S5      e[        S5      e! [         a    [	        S5      e[
         a    [        S5      ef = f! [         a    [	        S5      ef = f)
a>  Output(device_id)
Output(device_id, latency = 0)
Output(device_id, buffer_size = 4096)
Output(device_id, latency, buffer_size)

The buffer_size specifies the number of output events to be
buffered waiting for output.  (In some cases -- see below --
PortMidi does not buffer output at all and merely passes data
to a lower-level API, in which case buffersize is ignored.)

latency is the delay in milliseconds applied to timestamps to determine
when the output should actually occur. (If latency is < 0, 0 is
assumed.)

If latency is zero, timestamps are ignored and all output is delivered
immediately. If latency is greater than zero, output is delayed until
the message timestamp plus the latency. (NOTE: time is measured
relative to the time source indicated by time_proc. Timestamps are
absolute, not relative delays or offsets.) In some cases, PortMidi
can obtain better timing than your application by passing timestamps
along to the device driver or hardware. Latency may also help you
to synchronize midi data to audio data by matching midi latency to
the audio buffer latency.
r   r3   r4   r5   r6   z<Device id given is not a valid output id, it is an input id.z)Device id given is not a valid output id.r7   N)
r&   _abortedr   r   r8   r9   r   r   _outputr;   )r<   r;   latencyr=   r>   r?   r@   rA   s           r   rB   Output.__init__v  s    4 	?" 	H$Y/F +1(AqI>#(<<	K#PDL "+#R  $$OPP BCC/  	6455 	H FGG	H ! >#$<==>s   B B< *B9<Cc                 l    U R                   c  [        S5      eU R                  (       a  [        S5      eg )NrE   zmidi aborted.)rc   r   rb   rF   s    r   rG   Output._check_open  s/    << 011==00 r    c                 t    [        5         U R                  b  U R                  R                  5         SU l        g)zcloses a midi stream, flushing any pending buffers.
Output.close(): return None

PortMidi attempts to close open streams when the application
exits -- this is particularly difficult under Windows.
N)r&   rc   rJ   rF   s    r   rK   Output.close  s*     	<<#LL r    c                 |    [        5         U R                  (       a  U R                  R                  5         SU l        g)a=  terminates outgoing messages immediately
Output.abort(): return None

The caller should immediately close the output port;
this call may result in transmission of a partial midi message.
There is no abort for Midi input because the user can simply
ignore messages in the buffer and close an input device at
any time.
   N)r&   rc   Abortrb   rF   s    r   abortOutput.abort  s'     	<<LL r    c                 n    [        5         U R                  5         U R                  R                  U5        g)a  writes a list of midi data to the Output
Output.write(data)

writes series of MIDI information in the form of a list:
     write([[[status <,data1><,data2><,data3>],timestamp],
            [[status <,data1><,data2><,data3>],timestamp],...])
<data> fields are optional
example: choose program change 1 at time 20000 and
send note 65 with velocity 100 500 ms later.
     write([[[0xc0,0,0],20000],[[0x90,60,100],20500]])
notes:
  1. timestamps will be ignored if latency = 0.
  2. To get a note to play immediately, send MIDI info with
     timestamp read from function Time.
  3. understanding optional data fields:
       write([[[0xc0,0,0],20000]]) is equivalent to
       write([[[0xc0],20000]])

Can send up to 1024 elements in your data list, otherwise an
 IndexError exception is raised.
N)r&   rG   rc   Write)r<   datas     r   writeOutput.write  s'    , 	4 r    c                 p    [        5         U R                  5         U R                  R                  XU5        g)a  write_short(status <, data1><, data2>)
Output.write_short(status)
Output.write_short(status, data1 = 0, data2 = 0)

output MIDI information of 3 bytes or less.
data fields are optional
status byte could be:
     0xc0 = program change
     0x90 = note on
     etc.
     data bytes are optional and assumed 0 if omitted
example: note 65 on with velocity 100
     write_short(0x90,65,100)
N)r&   rG   rc   
WriteShort)r<   statusdata1data2s       r   write_shortOutput.write_short  s)     	u5r    c                 n    [        5         U R                  5         U R                  R                  X5        g)as  writes a timestamped system-exclusive midi message.
Output.write_sys_ex(when, msg)

msg - can be a *list* or a *string*
when - a timestamp in milliseconds
example:
  (assuming o is an onput MIDI stream)
    o.write_sys_ex(0,'\xF0\x7D\x10\x11\x12\x13\xF7')
  is equivalent to
    o.write_sys_ex(pygame.midi.time(),
                   [0xF0,0x7D,0x10,0x11,0x12,0x13,0xF7])
N)r&   rG   rc   
WriteSysEx)r<   whenmsgs      r   write_sys_exOutput.write_sys_ex  s'     	*r    c                 b    SUs=::  a  S::  d  O  [        S5      eU R                  SU-   X5        g)a  turns a midi note on.  Note must be off.
Output.note_on(note, velocity, channel=0)

note is an integer from 0 to 127
velocity is an integer from 0 to 127
channel is an integer from 0 to 15

Turn a note on in the output stream.  The note must already
be off for this to work correctly.
r      Channel not between 0 and 15.   N
ValueErrorry   r<   notevelocitychannels       r   note_onOutput.note_on  1     G!r!<==8r    c                 b    SUs=::  a  S::  d  O  [        S5      eU R                  SU-   X5        g)a3  turns a midi note off.  Note must be on.
Output.note_off(note, velocity=0, channel=0)

note is an integer from 0 to 127
velocity is an integer from 0 to 127 (release velocity)
channel is an integer from 0 to 15

Turn a note off in the output stream.  The note must already
be on for this to work correctly.
r   r   r      Nr   r   s       r   note_offOutput.note_off'  r   r    c                     SUs=::  a  S::  d  O  [        SU 35      eSUs=::  a  S::  d  O  [        S5      eU R                  SU-   U5        g)zselect an instrument for a channel, with a value between 0 and 127
Output.set_instrument(instrument_id, channel=0)

Also called "patch change" or "program change".
r      zUndefined instrument id: r   r      Nr   )r<   instrument_idr   s      r   set_instrumentOutput.set_instrument7  sQ     M(S(8HIIG!r!<==7r    c                     SUs=::  a  S::  d  O  [        S5      eSUs=::  a  S::  d  O  [        SU S35      eUS-   nUS	-  nUS
-	  nU R                  SU-   X45        g)a  modify the pitch of a channel.
Output.pitch_bend(value=0, channel=0)

Adjust the pitch of a channel.  The value is a signed integer
from -8192 to +8191.  For example, 0 means "no change", +4096 is
typically a semitone higher, and -8192 is 1 whole tone lower (though
the musical range corresponding to the pitch bend range can also be
changed in some synthesizers).

If no value is given, the pitch bend is returned to "no change".
r   r   r   i i  z6Pitch bend value must be between -8192 and +8191, not .i    r         Nr   )r<   r   r   lsbmsbs        r   
pitch_bendOutput.pitch_bendE  st     G!r!<==%%HqQ  dlqj2r    )rb   rc   r;   N)r      )r   r   )r   )rZ   r[   r\   r]   r^   rB   rG   rK   rm   rr   ry   r   r   r   r   r   r_   r   r    r   r   r   Z  s@    6=D~1
 !66&+"9 9 83r    c                  @    [        5         [        R                  " 5       $ )zreturns the current time in ms of the PortMidi timer
pygame.midi.time(): return time

The time is reset to 0, when the module is inited.
)r&   r   Timer   r    r   r   r   n  s     M::<r    c                     / nU  HC  nUu  u  pEpgn[         R                  R                  [        UUUUUUS9n	UR	                  U	5        ME     U$ )zconverts midi events to pygame events
pygame.midi.midis2events(midis, device_id): return [Event, ...]

Takes a sequence of midi events and returns list of pygame events.
)rv   rw   rx   data3	timestampvice_id)pygameeventEventr   append)
midisr;   evsmidirv   rw   rx   r   r   r   s
             r   r   r   x  se     C592	&&	"" # 
 	

5  Jr    c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )r   i  zPexception that pygame.midi functions and classes can raise
MidiException(errno)
c                 0   > [         TU ]  U5        Xl        g N)superrB   	parameter)r<   r   	__class__s     r   rB   MidiException.__init__  s    r    c                 ,    [        U R                  5      $ r   )reprr   rF   s    r   __str__MidiException.__str__  s    DNN##r    )r   )	rZ   r[   r\   r]   r^   rB   r   r_   __classcell__)r   s   @r   r   r     s    $ $r    r   c           
          [        [        SS[        R                  " U S-  5      -  [        R                  " S5      -  -   5      5      $ )zconverts a frequency into a MIDI note.

Rounds to the closest midi note.

::Examples::

>>> frequency_to_midi(27.5)
21
>>> frequency_to_midi(36.7)
26
>>> frequency_to_midi(4186.0)
108
E           {@   )introundmathlog)	frequencys    r   r   r     s9     uR2U): ;;txx{JJKLLr    c                 2    [        SSU S-
  S-  -  -  S5      $ )zConverts a midi note to a frequency.

::Examples::

>>> midi_to_frequency(21)
27.5
>>> midi_to_frequency(26)
36.7
>>> midi_to_frequency(108)
4186.0
r   r   r   gUUUUUU?rk   )r   )	midi_notes    r   r   r     s$     	B:>??CCr    c                 R    / SQnSnU[        U S-
  U-  5         nU S-
  U-  nU U 3$ )zreturns the Ansi Note name for a midi number.

::Examples::

>>> midi_to_ansi_note(21)
'A0'
>>> midi_to_ansi_note(102)
'F#7'
>>> midi_to_ansi_note(108)
'C8'
)AzA#BCzC#DzD#EFzF#GzG#r      )r   )r   notes	num_notes	note_namenote_numbers        r   r   r     sD     NEIc9r>Y678Ir>i/K[&&r    r   )r^   r   r   r   pygame.localspygame.pypmpypmr   localsr   r   __all____theclasses__r   r   r   r   r&   r   r	   r
   r   r   r   r   r   	Exceptionr   r   r   r   r   r    r   <module>r      s   0      
		
--

( 8$;
 /+d/,d&$_0 _0DE3 E3h0
$I 
$M"D'r    