annotate bin/inputquneo @ 2357:ccd04278e357

metrics cleanup
author drewp@bigasterisk.com
date Sat, 03 Jun 2023 17:15:40 -0700
parents ccdfdc8183ad
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
1 #!bin/python
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
2 """
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
3 read Quneo midi events, write to curvecalc and maybe to effects
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4 """
1859
f066d6e874db 2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents: 1858
diff changeset
5
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6 from run_local import log
1859
f066d6e874db 2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents: 1858
diff changeset
7 import logging, urllib.request, urllib.parse, urllib.error
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
8 import cyclone.web, cyclone.httpclient
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
9 from rdflib import URIRef
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
10 from twisted.internet import reactor, task
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
11 from light9.curvecalc.client import sendLiveInputPoint
1866
3c523c71da29 pyflakes cleanups and some refactors
Drew Perttula <drewp@bigasterisk.com>
parents: 1859
diff changeset
12 from light9.namespaces import L9, RDF
1692
6fa4288da8a6 rdfdb is its own package now
drewp@bigasterisk.com
parents: 1186
diff changeset
13 from rdfdb.syncedgraph import SyncedGraph
1182
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
14 from light9 import networking
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
15
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
16 import sys
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
17 sys.path.append('/usr/lib/python2.7/dist-packages') # For pygame
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
18 import pygame.midi
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
19
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
20 curves = {
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
21 23: URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-2'),
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
22 24: URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-3'),
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
23 25: URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-4'),
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
24 6: URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-5'),
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
25 18: URIRef('http://light9.bigasterisk.com/show/dance2014/song1/curve/c-6'),
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
26 }
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
27
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
28
2187
ccdfdc8183ad remove py2-style 'object' superclass
drewp@bigasterisk.com
parents: 1866
diff changeset
29 class WatchMidi:
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
30
1182
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
31 def __init__(self, graph):
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
32 self.graph = graph
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
33 pygame.midi.init()
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
34
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
35 dev = self.findQuneo()
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
36 self.inp = pygame.midi.Input(dev)
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
37 task.LoopingCall(self.step).start(.05)
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
38
1186
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
39 self.noteIsOn = {}
1182
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
40
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
41 self.effectMap = {} # note: effect class uri
1182
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
42 self.graph.addHandler(self.setupNotes)
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
43
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
44 def setupNotes(self):
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
45 for e in self.graph.subjects(RDF.type, L9['EffectClass']):
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
46 qn = self.graph.value(e, L9['quneoNote'])
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
47 if qn:
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
48 self.effectMap[int(qn)] = e
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
49 log.info("setup with %s effects", len(self.effectMap))
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
50
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
51 def findQuneo(self):
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
52 for dev in range(pygame.midi.get_count()):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
53 interf, name, isInput, isOutput, opened = pygame.midi.get_device_info(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
54 dev)
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
55 if 'QUNEO' in name and isInput:
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
56 return dev
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
57 raise ValueError("didn't find quneo input device")
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
58
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
59 def step(self):
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
60 if not self.inp.poll():
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
61 return
1186
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
62 NOTEON, NOTEOFF = 144, 128
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
63 for ev in self.inp.read(999):
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
64 (status, d1, d2, _), _ = ev
1186
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
65 if status in [NOTEON, NOTEOFF]:
1859
f066d6e874db 2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents: 1858
diff changeset
66 print(status, d1, d2)
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
67
1186
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
68 if status == NOTEON:
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
69 if not self.noteIsOn.get(d1):
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
70 self.noteIsOn[d1] = True
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
71 try:
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
72 e = self.effectMap[d1]
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
73 cyclone.httpclient.fetch(
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
74 url=networking.effectEval.path('songEffects'),
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
75 method='POST',
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
76 headers={
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
77 'Content-Type':
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
78 ['application/x-www-form-urlencoded']
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
79 },
1859
f066d6e874db 2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents: 1858
diff changeset
80 postdata=urllib.parse.urlencode([('drop', e)]),
1186
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
81 )
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
82 except KeyError:
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
83 pass
1182
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
84
1186
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
85 if status == NOTEOFF:
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
86 self.noteIsOn[d1] = False
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
87
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
88 if 0:
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
89 # curve editing mode, not done yet
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
90 for group in [(23, 24, 25), (6, 18)]:
1186
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
91 if d1 in group:
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
92 if not self.noteIsOn.get(group):
1859
f066d6e874db 2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents: 1858
diff changeset
93 print("start zero")
1186
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
94
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
95 for d in group:
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
96 sendLiveInputPoint(curves[d], 0)
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
97 self.noteIsOn[group] = True
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
98 else: # miss first update
1186
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
99 sendLiveInputPoint(curves[d1], d2 / 127)
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
100
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
101 if status == 128: #noteoff
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
102 for d in group:
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
103 sendLiveInputPoint(curves[d], 0)
1186
73e171c9d478 inputquneo sends effect creation on noteon
drewp@bigasterisk.com
parents: 1182
diff changeset
104 self.noteIsOn[group] = False
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
105
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
106
1182
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
107 def main():
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
108 log.setLevel(logging.DEBUG)
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
109 graph = SyncedGraph(networking.rdfdb.url, "inputQuneo")
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
110 wm = WatchMidi(graph)
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
111 reactor.run()
1866
3c523c71da29 pyflakes cleanups and some refactors
Drew Perttula <drewp@bigasterisk.com>
parents: 1859
diff changeset
112 del wm
1069
c756638275d6 quneo input demo. optimize curve display a little.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
113
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1692
diff changeset
114
1182
78973ed9e986 start to make inputquneo able to add effects to current song
Drew Perttula <drewp@bigasterisk.com>
parents: 1069
diff changeset
115 main()