File: 04-buffer-interface.rst.txt

package info (click to toggle)
python-pyo 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,332 kB
  • sloc: python: 135,133; ansic: 127,822; javascript: 16,116; sh: 395; makefile: 388; cpp: 242
file content (49 lines) | stat: -rw-r--r-- 1,496 bytes parent folder | download | duplicates (2)
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
47
48
49
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.

.. code-block:: python

    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())