[Numpy-discussion] An alternative to vectorize that lets you access the array?

Daniele Nicolodi daniele at grinta.net
Wed Jul 15 13:45:53 EDT 2020


On 12/07/2020 07:00, Ram Rachum wrote:
> The reason I want the second version is that I can then have sounddevice
> start playing `output_array` in a separate thread, while it's being
> calculated. (Yes, I know about the GIL, I believe that sounddevice
> releases it.)

I don't think this is a sound design.

I don't know sounddevice, but in similar situations the standard pattern
is to allocate a buffer (in this case it can be a numpy array) and pass
that to the consumer (soundevice in your case). The consumer then tells
the producer (your music synth) when it has to produce more data.

At a quick read, it seems that the sounddevice.Stream class allows to
apply this pattern
https://python-sounddevice.readthedocs.io/en/0.3.15/usage.html#callback-streams

This also easily allows your produces function to operate on arrays and
not on single elements. Using numpy functions to operate on arrays is
going to be more efficient than iterating on the elements in Python.

Cheers,
Dan


More information about the NumPy-Discussion mailing list