Table of Contents
- About pyo
- Installing pyo with pip
- Compiling pyo from sources
- Structure of the library
- Getting started
- Configuring the audio output (Windows)
- Improve performance of pyo programs
- API documentation
- Examples
- First steps
- Parameter control
- Synthesis generators
- Playing with soundfiles
- Amplitude envelopes
- Filtering
- Creating sound effects
- Dynamic range of audio signals
- Calling python functions from audio objects
- Using tables
- How to use MIDI with pyo
- How to use OSC with pyo
- Multirate audio processing
- Multicore audio programming
- Utilities
- Events framework
- Evaluating prefix expression
- Advanced tutorials
Search
Sharing a table with a numpy array using the buffer protocol¶
04-buffer-interface.py
This example shows two things:
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.
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())