| 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 |
|
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
def getDataMessage(address, *args): |
| 33 |
if address == "/data/play": |
| 34 |
|
| 35 |
|
| 36 |
source.path = args[0] |
| 37 |
source.speed = args[1] |
| 38 |
source.mul = args[2] |
| 39 |
source.play() |
| 40 |
elif address == "/data/rezo": |
| 41 |
|
| 42 |
freqs.value = list(args) |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
rec = OscDataReceive(port=9900, address="/data/*", function=getDataMessage) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 60 |
|
| 61 |
sender1.send([soundfile, speed, gain]) |
| 62 |
|
| 63 |
pat.time = sndinfo(soundfile)[1] / speed |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
pat = Pattern(choose).play() |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 81 |
pat2 = Pattern(change, time=2).play() |
| 82 |
|
| 83 |
s.gui(locals()) |