you can use aiosc for sending OSC in asyncio event loops. pymonome is built on top of aiosc, so it should already be installed on your system.

to send osc using aiosc, modify your grid key handler like this:

import aiosc
...
class GridStudies(monome.GridApp):
...
    def on_grid_key(self, x, y, s):
        print("key:", x, y, s)
        asyncio.ensure_future(aiosc.send(('127.0.0.1', 4560), '/flag1', 'text', 672, 8.871))
        self.grid.led_level_set(x, y, s*15)

or create a wrapper for the Sonic Pi client as follows:

class GridStudies(monome.GridApp):
    def __init__(self, sonic_pi):
        super().__init__()
        self.sonic_pi = sonic_pi

    def on_grid_key(self, x, y, s):
        self.sonic_pi.flag1(...)

class SonicPi(aiosc.OSCProtocol):
    def flag1(self, *args):
        self.send('/flag1', *args)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()

    coro = loop.create_datagram_endpoint(SonicPi, local_addr=('127.0.0.1', 0), remote_addr=('127.0.0.1', 4560))
    transport, sonic_pi = loop.run_until_complete(coro)

    grid_studies = GridStudies(sonic_pi)
    ....
2 Likes