File: 04-data-control.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 (90 lines) | stat: -rw-r--r-- 3,135 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
04-data-control.py - Multicore midi synthesizer.
============================================================================================================================================


Need at least 4 cores to be really effective.

Usage:
    python3 -i 04-data-control.py

.. code-block:: python

    import sys, time, multiprocessing
    from random import uniform
    from pyo import *
    
    VOICES_PER_CORE = 4
    
    if sys.platform.startswith("linux"):
        audio = "jack"
    elif sys.platform.startswith("darwin"):
        audio = "portaudio"
    else:
        print("Multicore examples don't run under Windows... Sorry!")
        exit()
    
    
    class Proc(multiprocessing.Process):
        def __init__(self, pipe):
            super(Proc, self).__init__()
            self.daemon = True
            self.pipe = pipe
    
        def run(self):
            self.server = Server(audio=audio)
            self.server.deactivateMidi()
            self.server.boot().start()
    
            self.mid = Notein(poly=VOICES_PER_CORE, scale=1, first=0, last=127)
            self.amp = MidiAdsr(self.mid["velocity"], 0.005, 0.1, 0.7, 0.5, mul=0.01)
            self.pit = self.mid["pitch"] * [uniform(0.99, 1.01) for i in range(40)]
            self.rc1 = RCOsc(self.pit, sharp=0.8, mul=self.amp).mix(1)
            self.rc2 = RCOsc(self.pit * 0.99, sharp=0.8, mul=self.amp).mix(1)
            self.mix = Mix([self.rc1, self.rc2], voices=2)
            self.rev = STRev(Denorm(self.mix), [0.1, 0.9], 2, bal=0.30).out()
    
            while True:
                if self.pipe.poll():
                    data = self.pipe.recv()
                    self.server.addMidiEvent(*data)
                time.sleep(0.001)
    
            self.server.stop()
    
    
    if __name__ == "__main__":
        main1, child1 = multiprocessing.Pipe()
        main2, child2 = multiprocessing.Pipe()
        main3, child3 = multiprocessing.Pipe()
        main4, child4 = multiprocessing.Pipe()
        mains = [main1, main2, main3, main4]
        p1, p2, p3, p4 = Proc(child1), Proc(child2), Proc(child3), Proc(child4)
        p1.start()
        p2.start()
        p3.start()
        p4.start()
    
        playing = {0: [], 1: [], 2: [], 3: []}
        currentcore = 0
    
        def callback(status, data1, data2):
            global currentcore
            if status == 0x80 or status == 0x90 and data2 == 0:
                for i in range(4):
                    if data1 in playing[i]:
                        playing[i].remove(data1)
                        mains[i].send([status, data1, data2])
                        break
            elif status == 0x90:
                for i in range(4):
                    currentcore = (currentcore + 1) % 4
                    if len(playing[currentcore]) < VOICES_PER_CORE:
                        playing[currentcore].append(data1)
                        mains[currentcore].send([status, data1, data2])
                        break
    
        s = Server()
        s.setMidiInputDevice(99)  # Open all devices.
        s.boot().start()
        raw = RawMidi(callback)