Skip to content
This repository was archived by the owner on Nov 23, 2023. It is now read-only.

Commit 6bbeda8

Browse files
convert notepredictor example to iipyper
1 parent 6d8783e commit 6bbeda8

2 files changed

Lines changed: 26 additions & 74 deletions

File tree

examples/notepredictor/midi-duet.scd

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ OSCdef(\return, {
2626
(Process.elapsedTime - t).postln;
2727
}, '/prediction', nil);
2828
t = Process.elapsedTime;
29-
b.sendMsg("/predictor/predict", 60+12.rand, 0);
29+
b.sendMsg("/predictor/predict", \pitch, 60+12.rand, \time, 0);
3030
)
3131

3232
// set the delay for more precise timing
@@ -51,7 +51,7 @@ MIDIdef.noteOn(\input, {
5151
SystemClock.clear;
5252

5353
//get a new prediction in light of current note
54-
b.sendMsg("/predictor/predict", num, dt);
54+
b.sendMsg("/predictor/predict", \pitch, num, \time, dt);
5555

5656
// release the previous note
5757
y.release(1.0);
@@ -99,7 +99,7 @@ OSCdef(\return, {
9999
// be if there was a lot of fast MIDI input)
100100
SystemClock.clear;
101101
// feed model its own prediction as input
102-
b.sendMsg("/predictor/predict", num, dt);
102+
b.sendMsg("/predictor/predict", \pitch, num, \time, dt);
103103
// release the previous note
104104
(dt<3e-2).if{
105105
// if the time delay is very small, slowly release to play a chord
@@ -122,10 +122,11 @@ OSCdef(\return, {
122122
}
123123
})};
124124

125-
}, '/prediction', nil);
125+
}, "/prediction", nil);
126126
)
127127

128-
// b.sendMsg("/predictor/predict", 70, 0.1);
128+
// send a note manually if you don't have a MIDI controller:
129+
b.sendMsg("/predictor/predict", \pitch, 70, \time, 0);
129130

130131
// load another model
131132
// b.sendMsg("/predictor/load", "/path/to/checkpoint");

examples/notepredictor/server.py

Lines changed: 20 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,51 @@
44
Jack Armitage
55
Intelligent Instruments Lab 2022
66
"""
7-
# TODO: convert to iipyper
87

9-
# import asyncio
10-
# import fire
118
from notepredictor import NotePredictor
12-
# from osc import OSC
139

1410
from iipyper import OSC, run
1511

16-
# def predictor_handler(address, *args):
17-
# """
18-
# Handle OSC messages to Predictor
19-
# """
20-
# address = address.split("/")
21-
22-
# if(address[2] == "load"):
23-
# print(f"/load {args}")
24-
# global predictor
25-
# predictor = NotePredictor.from_checkpoint(*args)
26-
# predictor.eval()
27-
28-
# elif(address[2] == "predict"):
29-
# print(f"/predict {args}")
30-
# r = predictor.predict(*args)
31-
# msg = (r['pitch'], r['time'])
32-
# # print(msg)
33-
# osc.send_message('/prediction', msg)
34-
35-
# elif(address[2] == "reset"):
36-
# print(f"/reset {args}")
37-
# predictor.reset(*args)
38-
39-
# else:
40-
# print(f"PitchPredictor: Unrecognised OSC {address} with {args}")
41-
42-
# async def loop():
43-
# """
44-
# Separate async loop.
45-
# """
46-
# i = 0
47-
# while True:
48-
# i += 1
49-
# # osc.send_message("/hello", i)
50-
# await asyncio.sleep(1)
51-
52-
# async def init_main():
53-
# await osc.create_server(asyncio.get_event_loop())
54-
# osc.add_handler("/predictor/*", predictor_handler)
55-
# osc.create_client()
56-
57-
# await loop()
58-
59-
# osc.close_server()
60-
61-
# def main(ip="127.0.0.1", send=57120, receive=9999, checkpoint=None):
6212
def main(host="127.0.0.1", port=9999, checkpoint=None):
63-
# global osc, predictor
64-
65-
# osc = OSC(ip, send, receive)
6613
osc = OSC(host, port)
6714

68-
predictor = None
15+
global predictor
6916
if checkpoint is not None:
7017
predictor = NotePredictor.from_checkpoint(checkpoint)
7118
predictor.eval()
72-
73-
# asyncio.run(init_main())
74-
75-
@osc.args('/predictor/*')
76-
def predictor_handler(address, *args):
19+
else:
20+
predictor = None
21+
22+
@osc.kwargs('/predictor/*')
23+
def _(address, **kw):
7724
"""
7825
Handle OSC messages to Predictor
7926
"""
27+
global predictor
28+
print(f"{address} {kw}")
29+
8030
address = address.split("/")
8131
cmd = address[2]
8232

8333
if cmd=="load":
84-
print(f"/load {args}")
85-
# global predictor
86-
predictor = NotePredictor.from_checkpoint(*args)
34+
predictor = NotePredictor.from_checkpoint(**kw)
8735
predictor.eval()
8836

8937
elif cmd=="predict":
90-
print(f"/predict {args}")
91-
r = predictor.predict(*args)
92-
return '/prediction', r['pitch'], r['time']
38+
if predictor is None:
39+
print('no model loaded')
40+
else:
41+
r = predictor.predict(**kw)
42+
return '/prediction', r['pitch'], r['time']
9343

9444
elif cmd=="reset":
95-
print(f"/reset {args}")
96-
predictor.reset(*args)
45+
if predictor is None:
46+
print('no model loaded')
47+
else:
48+
predictor.reset(**kw)
9749

9850
else:
99-
print(f"PitchPredictor: Unrecognised OSC {address} with {args}")
51+
print(f"PitchPredictor: Unrecognised OSC {address} with {kw}")
10052

10153
if __name__=='__main__':
102-
# fire.Fire(main)
10354
run(main)

0 commit comments

Comments
 (0)