-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlearning_server.py
More file actions
93 lines (82 loc) · 3.29 KB
/
learning_server.py
File metadata and controls
93 lines (82 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
If executing this script returns an 'Address already in use' error
make sure there are no processes running on the ports already.
To do that run 'sudo lsof -i:9997' 'sudo lsof -i:9998'
(9997 and 9998 are the default ports used here, so adjust accordingly
if using different ports) This commands brings up list of processes using these ports,
and gives their PID. For each process type, 'kill XXXX' where XXXX is PID.
Also, make sure the pure data patch is disconnected when you do this, otherwise
it will show up in the list and killing the process will quit the patch.
"""
import argparse
import sys
import pickle
import numpy as np
from pythonosc import dispatcher
from pythonosc import osc_server
from pythonosc import osc_message_builder
from pythonosc import udp_client
from utilities_globals import *
cnote = -1
data_per_note = None
def monophonic_handler(*args):
global cnote
m = int(np.round(float(args[1])))
m -= min_note_midi
print(m)
cnote = m
def fft_handler(*args):
global data_per_note
if cnote >= 0 and cnote < len(data_per_note):
print("updating data for note:", pitches_per_index[cnote])
fft = args[1].split()
fft = np.array([float(i) for i in fft])
fft = normalize_vector(fft.reshape(1,-1))
data_per_note[cnote] += [fft]
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--ip",
default="127.0.0.1", help="The ip to listen on")
parser.add_argument("--serverport",
type=int, default=9997, help="The port for server listen on")
parser.add_argument("--clientport",
type=int, default=9996, help="The client port")
parser.add_argument("--datafile", default="fretdata.p",
help="File to write data to (extension should be '.p')")
parser.add_argument("--minnote", default="E2")
parser.add_argument("--maxnote", default="C#6")
args = parser.parse_args()
pitches_per_index = create_notebins(min_note=args.minnote,
max_note=args.maxnote)
min_note_midi = note_to_midi(args.minnote)
max_note_midi = note_to_midi(args.maxnote)
data_per_note = [[] for i in pitches_per_index]
datafilename = args.datafile
dispatcher = dispatcher.Dispatcher()
dispatcher.map("/fftmag", fft_handler)
dispatcher.map("/monophonic_signal", monophonic_handler)
client = udp_client.SimpleUDPClient(args.ip, args.clientport)
server = osc_server.ThreadingOSCUDPServer(
(args.ip, args.serverport), dispatcher)
print("Serving on {}".format(server.server_address))
print("Ctrl+C to quit")
try:
server.serve_forever()
except KeyboardInterrupt:
save = input("Save? (y/n)")
overwrite = None
if save != 'y':
sys.exit()
try:
old_data = pickle.load(open(datafilename, "rb"))
except:
pickle.dump(data_per_note, open(datafilename, "wb"))
sys.exit()
overwrite = input("Data file with same name already exists."
+ "Overwrite data file or merge it? (o/m)")
if overwrite == 'm':
print("Merging data")
data_per_note = old_data+data_per_note
else:
print("Overwriting previous data")
pickle.dump(data_per_note, open(datafilename, "wb"))