Skip to content

Allow using numpy arrays for mapping Grids and Arcs #11

@ambv

Description

@ambv

Thanks for the awesome asyncio-first library. Works really well.

I was able to get Grid + Arc consistently output at 30fps with ~90fps logic updates without stuttering. To achieve this, I had to switch from using GridBuffer and ArcBuffer which allocate the entire map every frame. Instead, I used a numpy array that I simply zeroed between frames, which is measurably faster.

To make this work, I had to make two changes:

  1. Add grid.led_level_map_raw and arc.ring_map_raw methods that don't unpack *data but simply pass data directly to OSC.

  2. In aiosc, I added support for numpy arrays in pack_message without importing numpy by doing this:

        elif type(arg).__module__ == 'numpy' and type(arg).__qualname__ == 'ndarray':
            result += arg.tobytes()
            dt = arg.dtype
            if dt == '>i4':
                tt = 'i' * arg.size
            elif dt == '>f4':
                tt = 'f' * arg.size
            else:
                raise NotImplementedError('Unsupported numpy ndarray dtype: ' + dt)
            typetag += tt

With those changes, numpy arrays passed to the new methods allow reusing the same memory buffer and are very efficiently translated to bytes entirely in C land (with arg.tobytes() above).

Would you be interested in pull requests to upstream my changes?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions