Source

pygame / doc / src / midibase.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE module SYSTEM "api.dtd">

<module name="pygame2.midi.base">
  <show>0</show>
  <alias>pygame2.midi</alias>
  <short>MIDI input and output interaction module.</short>
  <desc>
    MIDI input and output interaction module.

    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 and
    virtual ones.

    It uses the portmidi library (using PyPortMidi) and is portable to all
    platforms portmidi supports (currently windows, OSX, and linux).
  </desc>
  
  <class name="Input">
    <constructor>Input (id, bufsize=4096) -> Input</constructor>
    <desc>Creates a new Input instance for a specific device.
      
      The Input class gives read access to a specific midi device, which allows
      input, with buffering support.
    
      Raises a ValueError, if the *id* is not within the range of available
      devices.
      Raises a pygame2.Error, if the midi module is not initialized.
    </desc>
    <method name="close">
      <call>close () -> None</call>
      <desc>Closes the Input device.
        
        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
    <method name="poll">
      <call>poll () -> bool</call>
      <desc>Gets, whether data is available on the buffer or not.

        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
    <method name="read">
      <call>read (amount) -> list</call>
      <desc>Reads a certain *amount* of midi events from the buffer.

        Reads from the Input buffer and gives back midi events in the form

            [ [[status,data1,data2,data3],timestamp],
              [[status,data1,data2,data3],timestamp], ...]

        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
  </class>

  <class name="Output">
    <constructor>Output (id, latency=0, bufsize=4096) -> Output</constructor>
    <desc>Creates a new Output instance for a specific device.
    
      The Output class gives write access to a specific midi device, which
      allows output.

      *latency* is the delay in milliseconds applied to timestamps to determine
      when the output should actually occur. (If *latency* is &lt; 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.
    
      Raises a ValueError, if the *id* is not within the range of available
      devices.
      Raises a pygame2.Error, if the midi module is not initialized.
    </desc>
    
    <method name="abort">
      <call>abort () -> None</call>
      <desc>Aborts outgoing messages immediately.

        The caller should immediately close the output port;
        this call may result in transmission of a partial midi message.

        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
    <method name="close">
      <call>close () -> None</call>
      <desc>Closes the Output device.

        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
    <method name="note_off">
      <call>note_off (note, velocity=None, channel = 0) -> None</call>
      <desc>Turn a note off in the output stream.

        Turn a note off in the output stream. The note must already
        be on for this to work correctly.

        Raises a ValueError, if *channel* is not in the range [0, 15].
        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
    <method name="note_on">
      <call>note_on (note, velocity=None, channel=0) -> None</call>
      <desc>Turn a note on in the output stream.

        Turn a note on in the output stream. The note must already be off for
        this to work correctly.

        Raises a ValueError, if *channel* is not in the range [0, 15].
        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
    <method name="set_instrument">
      <call>set_instrument (id, channel=0) -> None</call>
      <desc>Select an instrument, with a value between 0 and 127.

        Selects an instrument, where the *id* is in a range of [0, 127].

        Raises a ValueError, if *id* is not in the range [0, 127].
        Raises a ValueError, if *channel* is not in the range [0, 15].
        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
    <method name="write">
      <call>write (data) -> None</call>
      <desc>Writes midi data to the output device.

        Writes series of MIDI information in the form of a list:

        write([[[status &lt;,data1&gt;&lt;,data2&gt;&lt;,data3&gt;],timestamp],
               [[status &lt;,data1&gt;&lt;,data2&gt;&lt;,data3&gt;],timestamp],...])

        &lt;data&gt; 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]])

        This can send up to 1024 elements in your data list, otherwise an
        IndexError is raised.
        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
    <method name="write_short">
      <call>write_short (status, data1=None, data2=None) -> None</call>
      <desc>Writes MIDI information of 3 bytes or less.

        Writes a short MIDI information to the device. The data fields are
        optional and assumed 0, if omitted. The *status* byte could be:

            0xc0 = program change
            0x90 = note on
            ...

        Example: note 65 on with velocity 100

            write_short(0x90,65,100)

        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
    <method name="write_sys_ex">
      <call>write_sys_ex (timestamp, msg) -> None</call>
      <desc>Writes a timestamped, system-exclusive message.

        Writes a system-exclusive message *msg*, which can be either a byte
        buffer or string - or - list of bytes.

        Example:

            write_sys_ex (0, '\xF0\x7D\x10\x11\x12\x13\xF7')

        is equivalent to

            write_sys_ex (pygame2.midi.time (),
                          [0xF0,0x7D,0x10,0x11,0x12,0x13,0xF7])

        Raises a pygame2.Error, if the midi module is not initialized.
      </desc>
    </method>
  </class>

  <func name="get_count">
    <call>get_count () -> int</call>
    <desc>Gets the number of available midi devices.

      Raises a pygame2.Error, if the midi module is not initialized..
    </desc>
  </func>
  
  <func name="get_default_input_id">
    <call>get_default_input_id () -> int</call>
    <desc>Returns the default device ID or -1 if there are no devices.

      The result can be passed to the Input()/Ouput() 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).

      Raises a pygame2.Error, if the midi module is not initialized.
    </desc>
  </func>
  
  <func name="get_default_output_id">
    <call>get_default_output_id () -> int</call>
    <desc>Return the default device ID or -1 if there are no devices.

      The result can be passed to the Input()/Ouput() 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).

      Raises a pygame2.Error, if the midi module is not initialized.
    </desc>
  </func>
  
  <func name="get_device_info">
    <call>get_device_info (id) -> string, string, bool, bool, bool</call>
    <desc>Gets information about a midi device.

      Gets enhanced information about a midi device. The return values are

      * the name of the device, e.g. 'ALSA'
      * the enhanced description of the device, e.g. 'Midi Through Port-0'
      * a boolean indicating, whether the device is an input device
      * a boolean indicating, whether the device is an output device
      * a boolean indicating, whether the device is opened

      in this order.

      Raises a TypeError, if the *id* is not a integer value.
      Raises a ValueError, if the *id* is not within the range of available
      devices.
      Raises a pygame2.Error, if the midi module is not initialized.
    </desc>
  </func>
  
  <func name="init">
    <call>init () -> None</call>
    <desc>Initializes the midi module.

    Raises a pygame2.Error on failure.
    </desc>
  </func>
  
  <func name="quit">
    <call>quit () -> None</call>
    <desc>Uninitializes the midi module and releases all hold resources.</desc>
  </func>
  
  <func name="time">
    <call>time () -> int</call>
    <desc>Gets the time in milliseconds since the midi module was
    initialized.</desc>
  </func>

  <func name="was_init">
    <call>was_init () -> bool</call>
    <desc>Gets, whether the midi module was already initialized.</desc>
  </func>
</module>