1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
"""
Sharing a table with a numpy array using the buffer protocol
**04-buffer-interface.py**
This example shows two things:
1. How to share memory from a PyoTableObject to a numpy array with the
`getBuffer()` method of the PyoTableObject. Numpy functions can then
be used to modify the table's content without copying every samples.
2. How to register a `callback` function, with the `setCallback()` method
of the Server object, that the server will call at the beginning of
every processing loop (each `buffersize` samples).
.. note::
The numpy module **must** be installed for this example to work.
"""
from pyo import *
import numpy as np
s = Server().boot()
# Get the length of an audio block.
bs = s.getBufferSize()
# Create a table of length `buffer size` and read it in loop.
t = DataTable(size=bs)
osc = TableRead(t, freq=t.getRate(), loop=True, mul=0.1).out()
# Share the table's memory with a numpy array.
arr = np.asarray(t.getBuffer())
def process():
"Fill the array (so the table) with white noise."
arr[:] = np.random.normal(0.0, 0.5, size=bs)
# Register the `process` function to be called at the beginning
# of every processing loop.
s.setCallback(process)
s.gui(locals())
|