Debian Codegraph @

docs/_sources/examples/17-osc/05-send-receive-data.rst.txt

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