File: 08-multiple-threads.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 (76 lines) | stat: -rw-r--r-- 2,480 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Launching the audio processing in a separated thread
============================================================================================================================================


**08-multiple-threads.py**

This program launches an audio process in a separated thread with the
subprocess module and sends parameters to its standard input to control
the sound playback.

.. code-block:: python

    import os
    import sys
    import time
    import random
    import subprocess
    
    # Get the python command according to the current operating system.
    if sys.platform.startswith("win"):
        PYTHON_EXE = "py -%d.%d" % (sys.version_info[0], sys.version_info[1])
    else:
        PYTHON_EXE = "python%d.%d" % (sys.version_info[0], sys.version_info[1])
    
    # Path of the python file to run in the subprocess.
    script_path = os.path.join(os.path.expanduser("~"), "08_multiple_threads_process.py")
    
    # Thread's processing... Soundfile player + delay + reverb.
    script = """
    from pyo import *
    
    s = Server(duplex=0).boot()
    
    sf = SfPlayer('../snds/snd_1.aif', mul=0.7)
    dd = SmoothDelay(sf, delay=0.25, feedback=0.5)
    rv = STRev(sf+dd, inpos=0.50, revtime=1.5, cutoff=5000, bal=0.25).out()
    
    s.start()
    """
    
    # Create the python file to run in the subprocess.
    f = open(script_path, "w")
    f.write(script)
    f.close()
    
    # Launch an interactive python (-i flag) in a subprocess and store a
    # reference to the standard input to pass message to the running process.
    pipe = subprocess.Popen(
        ["%s -i %s" % (PYTHON_EXE, script_path)], shell=True, universal_newlines=True, stdin=subprocess.PIPE,
    ).stdin
    
    # Wait for the audio server to be ready.
    time.sleep(2)
    
    # Send events to the subprocess.
    for i in range(20):
        # Randomly choose a sound (snd_1.aif to snd_6.aif).
        snd = "../snds/snd_%d.aif" % random.randrange(1, 7)
    
        # Replace the sound and start the playback.
        pipe.write("sf.path = '%s'\ndump = sf.play()\n" % snd)
        pipe.write("dd.delay = %f\n" % random.uniform(0.1, 0.5))
        pipe.flush()
    
        # Wait some time before the next event.
        time.sleep(random.uniform(0.5, 1))
    
    # Stop the audio Server before exiting.
    pipe.write("s.stop()\ntime.sleep(0.25)\n")
    
    # Close the subprocess.
    pipe.close()
    
    # Delete the python file.
    os.remove(script_path)