Skip to content

Instantly share code, notes, and snippets.

@trueroad
Last active September 20, 2023 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trueroad/4185ae60bcd8cb038321f849e9053159 to your computer and use it in GitHub Desktop.
Save trueroad/4185ae60bcd8cb038321f849e9053159 to your computer and use it in GitHub Desktop.
WinRT MIDI Devices with python winrt
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
WinRT MIDI Devices with python winrt.
https://gist.github.com/trueroad/4185ae60bcd8cb038321f849e9053159
Copyright (C) 2023 Masamichi Hosoda.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
"""
import asyncio
from typing import List, Tuple, Union
from winrt.windows.devices.enumeration import ( # type: ignore[import]
DeviceInformation, DeviceInformationCollection
)
from winrt.windows.devices.midi import ( # type: ignore[import]
MidiInPort, MidiOutPort
)
class winrt_midi_device_show():
"""WinRT MIDI Device Show class."""
def __init__(self) -> None:
"""__init__."""
# MIDI IN port list
self.in_ports: List[Tuple[str, str]] = []
# MIDI OUT port list
self.out_ports: List[Tuple[str, str]] = []
async def list_ports(self,
kind: Union[MidiInPort, MidiOutPort] = MidiInPort
) -> List[Tuple[str, str]]:
"""
Get MIDI ports list.
Args:
kind (Union[MidiInPort, MidiOutPort]): MIDI port kind
Returns:
List:
Tuple:
str: Name
str: ID
"""
devs: DeviceInformationCollection = \
await DeviceInformation.find_all_async(
kind.get_device_selector(), [])
retval: List[Tuple[str, str]] = []
d: DeviceInformation
for d in devs:
retval.append((d.name, d.id))
return retval
async def show_midi_in_ports(self) -> None:
"""Show MIDI IN ports."""
self.in_ports = await self.list_ports()
i: int
print('\nMIDI IN ports')
for i in range(len(self.in_ports)):
print(f'\n***** IN {i} *****\n'
'*** Name ***\n'
f'{self.in_ports[i][0]}\n'
f'{str(self.in_ports[i][0].encode())}\n'
'*** ID ***\n'
f'{self.in_ports[i][1]}\n'
f'{str(self.in_ports[i][1].encode())}\n'
'**********')
async def show_midi_out_ports(self) -> None:
"""Show MIDI OUT ports."""
self.out_ports = await self.list_ports(MidiOutPort)
i: int
print('\nMIDI OUT ports')
for i in range(len(self.out_ports)):
print(f'\n***** OUT {i} *****\n'
'*** Name ***\n'
f'{self.out_ports[i][0]}\n'
f'{str(self.out_ports[i][0].encode())}\n'
'*** ID ***\n'
f'{self.out_ports[i][1]}\n'
f'{str(self.out_ports[i][1].encode())}\n'
'**********')
async def show(self) -> None:
"""Show MIDI IN/OUT ports."""
await self.show_midi_in_ports()
await self.show_midi_out_ports()
def main() -> None:
"""Test main."""
print('WinRT MIDI Devices with python winrt\n\n'
'https://gist.github.com/trueroad/'
'4185ae60bcd8cb038321f849e9053159\n\n'
'Copyright (C) 2023 Masamichi Hosoda.\n'
'All rights reserved.\n')
wmds: winrt_midi_device_show = winrt_midi_device_show()
loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()
try:
loop.run_until_complete(wmds.show())
except KeyboardInterrupt:
print('Interrupted')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment