pygame.midi
pygame module for interacting with midi input and output.
initialize the midi module
uninitialize the midi module
returns True if the midi module is currently initialized
Input is used to get midi input from midi devices.
Output is used to send midi to an output device
gets the number of devices.
gets default input device number
gets default output device number
returns information about a midi device
converts midi events to pygame events
returns the current time in ms of the PortMidi timer
Converts a frequency into a MIDI note. Rounds to the closest midi note.
Converts a midi note to a frequency.
Returns the Ansi Note name for a midi number.
exception that pygame.midi functions and classes can raise

New in pygame 1.9.0.

MIDI模块可以将输出发送到MIDI设备,并从MIDI设备获取输入。它还可以列出系统上的MIDI设备。

MIDI模块支持真实和虚拟的MIDI设备。

它使用portmidi库。可移植到portmidi支持的任何平台(目前是Windows、Mac OS X和Linux)。

它目前使用的是pyportmidi,但将来可能会使用它自己的绑定。PYGAME中包含了pyportmidi绑定。


New in pygame 2.0.0.

这些都是骗局的事件 (pygame.eventpygame module for interacting with events and queues )预留供MIDI使用。这个 MIDIIN 事件由以下人员使用 pygame.midi.midis2events()converts midi events to pygame events 将MIDI事件转换为PYGAME事件时。

MIDIIN
MIDIOUT

pygame.midi.init()
initialize the midi module
init() -> None

初始化 pygame.midipygame module for interacting with midi input and output. 模块。必须在使用 pygame.midipygame module for interacting with midi input and output. 模块。

可以安全地不止一次地调用它。

pygame.midi.quit()
uninitialize the midi module
quit() -> None

取消初始化 pygame.midipygame module for interacting with midi input and output. 模块。如果 pygame.midi.init()initialize the midi module 被调用以初始化 pygame.midipygame module for interacting with midi input and output. 模块,则该函数将在程序退出时自动调用。

多次调用此函数是安全的。

pygame.midi.get_init()
returns True if the midi module is currently initialized
get_init() -> bool

对象的初始化状态。 pygame.midipygame module for interacting with midi input and output. 模块。

返回

True 如果 pygame.midipygame module for interacting with midi input and output. 模块当前已初始化。

返回类型

bool

New in pygame 1.9.5.

pygame.midi.Input
Input is used to get midi input from midi devices.
Input(device_id) -> None
Input(device_id, buffer_size) -> None
closes a midi stream, flushing any pending buffers.
returns True if there's data, or False if not.
reads num_events midi events from the buffer.
参数
  • device_id (int) -- MIDI设备ID

  • buffer_size (int) -- 要缓冲的输入事件数

close()
closes a midi stream, flushing any pending buffers.
close() -> None

当应用程序退出时,PortMidi会尝试关闭打开的流。

备注

在Windows下,这一点尤其困难。

poll()
returns True if there's data, or False if not.
poll() -> bool

用于指示是否存在任何数据。

返回

True 如果有数据, False 否则

返回类型

bool

引发

MidiException -- 发生错误时

read()
reads num_events midi events from the buffer.
read(num_events) -> midi_event_list

从输入缓冲区读取并返回MIDI事件。

参数

num_events (int) -- 要读取的输入事件数

返回

MIDI_EVENT_LIST格式为 [[[status, data1, data2, data3], timestamp], ...]

返回类型

list

pygame.midi.Output
Output is used to send midi to an output device
Output(device_id) -> None
Output(device_id, latency=0) -> None
Output(device_id, buffer_size=256) -> None
Output(device_id, latency, buffer_size) -> None
terminates outgoing messages immediately
closes a midi stream, flushing any pending buffers.
turns a midi note off (note must be on)
turns a midi note on (note must be off)
select an instrument, with a value between 0 and 127
modify the pitch of a channel.
writes a list of midi data to the Output
writes up to 3 bytes of midi data to the Output
writes a timestamped system-exclusive midi message.

这个 buffer_size 指定等待输出的要缓冲的输出事件数。在某些情况下(见下文),PortMidi根本不缓冲输出,而只是将数据传递给较低级别的API,在这种情况下,缓冲区大小将被忽略。

latency 应用于时间戳以确定输出实际发生时间的延迟(以毫秒为单位)。如果 latency <<0,则假定为0。

如果 latency 为零,则忽略时间戳并立即传递所有输出。如果 latency 大于零,则输出将延迟,直到消息时间戳加上 latency 。在某些情况下,通过将时间戳传递给设备驱动程序或硬件,PortMidi可以获得比您的应用程序更好的计时。延迟还可以通过将MIDI延迟与音频缓冲延迟相匹配来帮助您将MIDI数据同步到音频数据。

备注

时间是相对于time_proc指示的时间源来测量的。时间戳是绝对的,而不是相对延迟或偏移量。

abort()
terminates outgoing messages immediately
abort() -> None

调用者应立即关闭输出端口;此调用可能导致传输部分MIDI消息。不会中止MIDI输入,因为用户只需忽略缓冲区中的消息并随时关闭输入设备。

close()
closes a midi stream, flushing any pending buffers.
close() -> None

当应用程序退出时,PortMidi会尝试关闭打开的流。

备注

在Windows下,这一点尤其困难。

note_off()
turns a midi note off (note must be on)
note_off(note, velocity=None, channel=0) -> None

在输出流中关闭笔记。笔记必须已打开,才能正常工作。

note_on()
turns a midi note on (note must be off)
note_on(note, velocity=None, channel=0) -> None

在输出流中打开便笺。笔记必须已关闭,才能正常工作。

set_instrument()
select an instrument, with a value between 0 and 127
set_instrument(instrument_id, channel=0) -> None

选择一种乐器。

pitch_bend()
modify the pitch of a channel.
set_instrument(value=0, channel=0) -> None

调整频道的音调。该值是一个从-8192到+8191的带符号整数。例如,0表示“不变”,+4096通常是半音高,-8192表示低1个全音(尽管在某些合成器中,与音高弯曲范围相对应的音乐范围也可以改变)。

如果未给出值,则节距折弯将返回到“不变”。

New in pygame 1.9.4.

write()
writes a list of midi data to the Output
write(data) -> None

以列表的形式写入一系列MIDI信息。

参数

data (list) -- 要写入的数据,预期格式为 [[[status, data1=0, data2=0, ...], timestamp], ...] 使用 data# 可选字段

引发

IndexError -- 如果数据列表中的元素超过1024个

示例:

# Program change at time 20000 and 500ms later send note 65 with
# velocity 100.
write([[[0xc0, 0, 0], 20000], [[0x90, 60, 100], 20500]])

备注

  • 如果延迟=0,时间戳将被忽略

  • 要让音符立即播放,请发送带有从函数时间读取的时间戳的MIDI信息

  • 可选数据字段: write([[[0xc0, 0, 0], 20000]]) 相当于 write([[[0xc0], 20000]])

write_short()
writes up to 3 bytes of midi data to the Output
write_short(status) -> None
write_short(status, data1=0, data2=0) -> None

输出3字节或更少的MIDI信息。这个 data 字段是可选的,如果省略,则假定为0。

状态字节值的示例:

0xc0  # program change
0x90  # note on
# etc.

示例:

# note 65 on with velocity 100
write_short(0x90, 65, 100)
write_sys_ex()
writes a timestamped system-exclusive midi message.
write_sys_ex(when, msg) -> None

写入带有时间戳的系统独占MIDI消息。

参数
  • msg (list[int] or str) -- MIDI消息

  • when -- 时间戳(毫秒)

示例:

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

# is equivalent to

midi_output.write_sys_ex(pygame.midi.time(),
                         [0xF0, 0x7D, 0x10, 0x11, 0x12, 0x13, 0xF7])
pygame.midi.get_count()
gets the number of devices.
get_count() -> num_devices

设备ID的范围从0到 get_count() - 1

pygame.midi.get_default_input_id()
gets default input device number
get_default_input_id() -> default_id

下面描述了此函数和 get_default_output_id() 功能。

返回默认设备ID或 -1 如果没有设备的话。可以将结果传递给 Input/Output 班级。

在PC上,用户可以通过设置环境变量来指定默认设备。要使用1号设备,例如:

set PM_RECOMMENDED_INPUT_DEVICE=1
or
set PM_RECOMMENDED_OUTPUT_DEVICE=1

用户应该首先通过使用提供的应用程序“Testn”或“Testout”来确定可用的设备ID。

总体而言,登记处是存储这类信息的更好场所。对于可以来来去去的USB设备,使用整数进行设备识别并不是很可靠。在Windows下,如果 PM_RECOMMENDED_INPUT_DEVICE (或 PM_RECOMMENDED_OUTPUT_DEVICE ),则通过在注册表中查找位于:下的字符串来获取默认设备

HKEY_LOCAL_MACHINE/SOFTWARE/PortMidi/Recommended_Input_Device
or
HKEY_LOCAL_MACHINE/SOFTWARE/PortMidi/Recommended_Output_Device

返回具有与字符串完全匹配的子字符串的第一个设备的编号。例如,如果注册表中的字符串为“USB”,而设备1的名称为“In USB MideSports 1x1”,则这将是默认输入,因为它包含字符串“USB”。

除了名字之外, get_device_info() 返回接口名称“interf”。“接口”是PortMidi用来访问设备的底层软件系统或API。支持的接口:

MMSystem   # the only Win32 interface currently supported
ALSA       # the only Linux interface currently supported
CoreMIDI   # the only Mac OS X interface currently supported
# DirectX - not implemented
# OSS     - not implemented

要在注册表中同时指定接口和设备名称,请用逗号和空格将两者分开。逗号之前的字符串必须是“interf”字符串的子字符串,空格之后的字符串必须是“name”名称字符串的子字符串,才能与设备匹配。例如:

MMSystem, In USB MidiSport 1x1

备注

在当前版本中,默认设置只是第一个设备(具有最低PmDeviceID的输入或输出设备)。

pygame.midi.get_default_output_id()
gets default output device number
get_default_output_id() -> default_id

看见 get_default_input_id() 有关用法详细信息,请参阅。

pygame.midi.get_device_info()
returns information about a midi device
get_device_info(an_id) -> (interf, name, input, output, opened)
get_device_info(an_id) -> None

获取给定ID的设备信息。

参数

an_id (int) -- 被查询的MIDI设备的ID

返回

如果ID超出范围 None 返回,否则返回(interf,名称,输入,输出,打开)的元组。-interf:描述设备接口的字符串(例如‘ALSA’)-name:设备的字符串名称(例如‘MIDI到Port-0’)-如果设备是输入设备,则输入:1;否则,0-输出:1;如果设备是输出设备,则输出:1;否则,0-打开:如果设备已打开,则为1;否则,为0

返回类型

tuple or None

pygame.midi.midis2events()
converts midi events to pygame events
midis2events(midi_events, device_id) -> [Event, ...]

获取一系列MIDI事件并返回PYGAME事件列表。

这个 midi_events 数据预计将是一系列 ((status, data1, data2, data3), timestamp) MIDI事件(需要所有值)。

返回

事件类型的pyGame事件列表 MIDIIN

返回类型

list

pygame.midi.time()
returns the current time in ms of the PortMidi timer
time() -> time

时间被重置为0,当 pygame.midipygame module for interacting with midi input and output. 模块已初始化。

pygame.midi.frequency_to_midi()
Converts a frequency into a MIDI note. Rounds to the closest midi note.
frequency_to_midi(midi_note) -> midi_note

示例:

frequency_to_midi(27.5) == 21

New in pygame 1.9.5.

pygame.midi.midi_to_frequency()
Converts a midi note to a frequency.
midi_to_frequency(midi_note) -> frequency

示例:

midi_to_frequency(21) == 27.5

New in pygame 1.9.5.

pygame.midi.midi_to_ansi_note()
Returns the Ansi Note name for a midi number.
midi_to_ansi_note(midi_note) -> ansi_note

示例:

midi_to_ansi_note(21) == 'A0'

New in pygame 1.9.5.

exception pygame.midi.MidiException
exception that pygame.midi functions and classes can raise
MidiException(errno) -> None



Edit on GitHub