|
| 1 | +""" |
| 2 | +Authors: |
| 3 | + Victor Shepardson |
| 4 | + Jack Armitage |
| 5 | + Intelligent Instruments Lab 2022 |
| 6 | +""" |
| 7 | + |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from iipyper import OSC, run, repeat |
| 11 | + |
| 12 | +def main(port=5511, verbose=False): |
| 13 | + |
| 14 | + osc = OSC(port=port) |
| 15 | + osc.create_client("faust", port=5510) |
| 16 | + |
| 17 | + connected = False |
| 18 | + |
| 19 | + # Kisana parameters |
| 20 | + master = { "val": -12.0, "min": -60.0, "max": 0.0, "step": 2.0 } |
| 21 | + note = { "val": 5, "min": 0, "max": 11, "step": 1 } |
| 22 | + timbre = { "val": 0.5, "min": 0.0, "max": 1.0, "step": 0.25 } |
| 23 | + |
| 24 | + @osc.args("/*") |
| 25 | + def _(address, *args): |
| 26 | + """ |
| 27 | + Handle OSC messages from Faust |
| 28 | + """ |
| 29 | + |
| 30 | + if address=="/Kisana": |
| 31 | + # `osc("faust", "/*", "get")` response |
| 32 | + print(f"Kisana interface: {args}") |
| 33 | + |
| 34 | + else: |
| 35 | + address = address.split("/") |
| 36 | + cmd = address[2] |
| 37 | + if cmd=="level": |
| 38 | + print(f"Level updated to: {args}") |
| 39 | + |
| 40 | + @repeat(0.5) |
| 41 | + def _(): |
| 42 | + nonlocal connected |
| 43 | + if connected==False: |
| 44 | + connect() |
| 45 | + connected=True |
| 46 | + update() |
| 47 | + |
| 48 | + def connect(): |
| 49 | + nonlocal master |
| 50 | + osc("faust", "/*", "get") # discover OSC interface |
| 51 | + osc("faust", "/xmit", 1) # turn transmission on |
| 52 | + osc("faust", "/Kisana/master", master['val']) # set master gain |
| 53 | + print("Kisana connected!") |
| 54 | + |
| 55 | + def update(): |
| 56 | + nonlocal note, timbre |
| 57 | + coin_flip = np.random.choice(a=[True,False], size=2) |
| 58 | + |
| 59 | + if coin_flip[0]==True: |
| 60 | + note_step = np.random.randint(-note['step'], note['step']+1) |
| 61 | + note['val'] = constrain(note['val'] + note_step, note['min'], note['max']) |
| 62 | + osc("faust", "/Kisana/loop38/note", note['val']) |
| 63 | + |
| 64 | + elif coin_flip[1]==True: |
| 65 | + timbre_step = np.random.random() * (timbre['step'] * 2 - timbre['step']) |
| 66 | + timbre['val'] = constrain(timbre['val'] + timbre_step, timbre['min'], timbre['max']) |
| 67 | + osc("faust", "/Kisana/timbre", timbre['val']) |
| 68 | + |
| 69 | + def constrain(val, min_val, max_val): |
| 70 | + return min(max_val, max(min_val, val)) |
| 71 | + |
| 72 | +if __name__=='__main__': |
| 73 | + run(main) |
0 commit comments