| 1 |
""" |
| 2 |
04-data-control.py - Multicore midi synthesizer. |
| 3 |
|
| 4 |
Need at least 4 cores to be really effective. |
| 5 |
|
| 6 |
Usage: |
| 7 |
python3 -i 04-data-control.py |
| 8 |
|
| 9 |
""" |
| 10 |
import sys, time, multiprocessing |
| 11 |
from random import uniform |
| 12 |
from pyo import * |
| 13 |
|
| 14 |
VOICES_PER_CORE = 4 |
| 15 |
|
| 16 |
if sys.platform.startswith("linux"): |
| 17 |
audio = "jack" |
| 18 |
elif sys.platform.startswith("darwin"): |
| 19 |
audio = "portaudio" |
| 20 |
else: |
| 21 |
print("Multicore examples don't run under Windows... Sorry!") |
| 22 |
exit() |
| 23 |
|
| 24 |
|
| 25 |
class Proc(multiprocessing.Process): |
| 26 |
def __init__(self, pipe): |
| 27 |
super(Proc, self).__init__() |
| 28 |
self.daemon = True |
| 29 |
self.pipe = pipe |
| 30 |
|
| 31 |
def run(self): |
| 32 |
self.server = Server(audio=audio) |
| 33 |
self.server.deactivateMidi() |
| 34 |
self.server.boot().start() |
| 35 |
|
| 36 |
self.mid = Notein(poly=VOICES_PER_CORE, scale=1, first=0, last=127) |
| 37 |
self.amp = MidiAdsr(self.mid["velocity"], 0.005, 0.1, 0.7, 0.5, mul=0.01) |
| 38 |
self.pit = self.mid["pitch"] * [uniform(0.99, 1.01) for i in range(40)] |
| 39 |
self.rc1 = RCOsc(self.pit, sharp=0.8, mul=self.amp).mix(1) |
| 40 |
self.rc2 = RCOsc(self.pit * 0.99, sharp=0.8, mul=self.amp).mix(1) |
| 41 |
self.mix = Mix([self.rc1, self.rc2], voices=2) |
| 42 |
self.rev = STRev(Denorm(self.mix), [0.1, 0.9], 2, bal=0.30).out() |
| 43 |
|
| 44 |
while True: |
| 45 |
if self.pipe.poll(): |
| 46 |
data = self.pipe.recv() |
| 47 |
self.server.addMidiEvent(*data) |
| 48 |
time.sleep(0.001) |
| 49 |
|
| 50 |
self.server.stop() |
| 51 |
|
| 52 |
|
| 53 |
if __name__ == "__main__": |
| 54 |
main1, child1 = multiprocessing.Pipe() |
| 55 |
main2, child2 = multiprocessing.Pipe() |
| 56 |
main3, child3 = multiprocessing.Pipe() |
| 57 |
main4, child4 = multiprocessing.Pipe() |
| 58 |
mains = [main1, main2, main3, main4] |
| 59 |
p1, p2, p3, p4 = Proc(child1), Proc(child2), Proc(child3), Proc(child4) |
| 60 |
p1.start() |
| 61 |
p2.start() |
| 62 |
p3.start() |
| 63 |
p4.start() |
| 64 |
|
| 65 |
playing = {0: [], 1: [], 2: [], 3: []} |
| 66 |
currentcore = 0 |
| 67 |
|
| 68 |
def callback(status, data1, data2): |
| 69 |
global currentcore |
| 70 |
if status == 0x80 or status == 0x90 and data2 == 0: |
| 71 |
for i in range(4): |
| 72 |
if data1 in playing[i]: |
| 73 |
playing[i].remove(data1) |
| 74 |
mains[i].send([status, data1, data2]) |
| 75 |
break |
| 76 |
elif status == 0x90: |
| 77 |
for i in range(4): |
| 78 |
currentcore = (currentcore + 1) % 4 |
| 79 |
if len(playing[currentcore]) < VOICES_PER_CORE: |
| 80 |
playing[currentcore].append(data1) |
| 81 |
mains[currentcore].send([status, data1, data2]) |
| 82 |
break |
| 83 |
|
| 84 |
s = Server() |
| 85 |
s.setMidiInputDevice(99) |
| 86 |
s.boot().start() |
| 87 |
raw = RawMidi(callback) |