Debian Codegraph @

pyo/examples/x17-osc/05-send-receive-data.py

raw symbols SCIP
1 """
2 Sending and receiving data with OSC protocol
3
4 **05-send-receive-data.py**
5
6 OscDataSend and OscDataReceive are objects that should be used to
7 send and receive Open Sound Control messages when message type is
8 not simple float or when the user want to process values before
9 sending using them to control an audio process.
10
11 **OscDataSend** sends messages in the form of a list containing elements
12 according to its `types` argument. See the documentation of the object
13 for the various available types.
14
15 **OscDataReceive** calls a function with the address as the first argument,
16 followed by a tuple containing every values of the data message.
17
18 """
19 import random
20 from pyo import *
21
22 s = Server().boot()
23
24 # Little audio process to control with OSC messages.
25 source = SfPlayer("../snds/snd_1.aif")
26 freqs = SigTo([100, 200, 300, 400], time=2)
27 rezon = Resonx(source.mix(2), freq=freqs, q=2, stages=2).out()
28
29 # ----- Receiver -----
30
31 # Handle all messages coming from addresses starting with "/data/"
32 def getDataMessage(address, *args):
33 if address == "/data/play":
34 # Arguments are the sound to play as a string,
35 # speed and gain as floats.
36 source.path = args[0]
37 source.speed = args[1]
38 source.mul = args[2]
39 source.play()
40 elif address == "/data/rezo":
41 # Arguments are 4 frequencies as floats.
42 freqs.value = list(args)
43
44
45 # OSC data receiver.
46 rec = OscDataReceive(port=9900, address="/data/*", function=getDataMessage)
47
48 # ----- Sender 1 -----
49
50 # Builds and sends messages to load an play a new sound.
51 sender1 = OscDataSend(types="sff", port=9900, address="/data/play", host="127.0.0.1")
52
53
54 def choose():
55 "Chooses new soundfile values and starts playback."
56 soundfile = "../snds/snd_%i.aif" % random.randint(1, 6)
57 speed = random.choice([0.5, 0.75, 1.0, 1.25])
58 gain = random.uniform(0.5, 0.7)
59 # Sends the message (message must be a list of values whose
60 # types respect the `types` argument of the OscDataSend object.
61 sender1.send([soundfile, speed, gain])
62 # Adjusts the timing of the Pattern according to the playback duration.
63 pat.time = sndinfo(soundfile)[1] / speed
64
65
66 # Call the choose() function to set a new sound playback.
67 pat = Pattern(choose).play()
68
69 # ----- Sender 2 -----
70
71 # Sends new frequencies to the resonator filters.
72 sender2 = OscDataSend(types="ffff", port=9900, address="/data/rezo", host="127.0.0.1")
73
74
75 def change():
76 "Randomly chooses new frequencies and sends them to the filters."
77 sender2.send([random.uniform(250, 4000) for i in range(4)])
78
79
80 # Call the change() function every 2 seconds.
81 pat2 = Pattern(change, time=2).play()
82
83 s.gui(locals())