Created
June 29, 2018 12:42
-
-
Save jnory/ec208970cdacfcbc376c14ef4fae0740 to your computer and use it in GitHub Desktop.
pygameでmidiを操作する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import pygame | |
import pygame.midi | |
def main(args): | |
pygame.midi.init() | |
if args.list: | |
print("# of devices:", pygame.midi.get_count()) | |
for i in range(pygame.midi.get_count()): | |
print("DeviceID: ", i, "Info=", pygame.midi.get_device_info(i)) | |
else: | |
try: | |
out = pygame.midi.Output(args.device) | |
out.set_instrument(0) | |
out.note_on(60, 127) | |
time.sleep(1) | |
out.note_off(60, 0) | |
out.note_on(62, 127) | |
time.sleep(1) | |
out.note_off(62, 0) | |
out.note_on(64, 127) | |
time.sleep(1) | |
out.note_off(64, 0) | |
out.close() | |
except pygame.midi.MidiException: | |
print("Invalid device number:", args.device) | |
pygame.midi.quit() | |
def get_parser(): | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--list", action="store_true", default=False) | |
parser.add_argument("--device", type=int, default=0) | |
return parser | |
if __name__ == '__main__': | |
main(get_parser().parse_args()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment