-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi_connector.py
185 lines (121 loc) · 5.15 KB
/
midi_connector.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# this is to connect with the MIDI intervace
import numpy as np
from voltage import voltage
import time
import sys
import os
import pygame
import pygame.midi
from pygame.locals import *
import progressbar
class midi_connector(object):
def __init__(self,WINDOW,CHANNELS):
'''
:param WINDOW: Fenster des Sliding mean
:param CHANNELS: Anzahl der Midi Kanäle die Basti hat
'''
self.WINDOW = WINDOW
#self.SAMPLE_RATE = SAMPLE_RATE
self.CHANNELS = CHANNELS
# list of channels: obacht! startet mit 1!
self.channel_list = [int(c + 1) for c in range(self.CHANNELS)]
# setup the volotmeter
self.voltometer = voltage(WINDOW = self.WINDOW)
# set up midi interface
self.mymidi = pygame.midi
self.init_midi()
self.port = 2 # war beim letzten mal richtig...
self.midi_out = self.mymidi.Output(self.port, 0)
bar = progressbar.ProgressBar(maxval=100,widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
bar.start()
print('Initial Data collection ...')
for i in range(100):
self.voltometer.update_shunt_voltage()
bar.update(i + 1)
time.sleep(0.1)
bar.finish()
def init_midi(self):
self.mymidi.init()
def quit_midi(self):
self.mymidi.quit()
def get_normed_signal(self):
# update the voltage array
self.voltometer.update_shunt_voltage()
# get the current shunt voltage
this_shunt_voltage = self.voltometer.get_shunt_voltage()
print('Shunt Voltage: ',this_shunt_voltage)
this_voltage_array = self.voltometer.get_voltage_array()
# get MAX and MIN values of the voltage array for normalization
this_max_volt = np.max(this_voltage_array)
this_min_volt = np.min(this_voltage_array)
normed_voltage_array = (this_voltage_array - this_min_volt) / (this_max_volt - this_min_volt)
return normed_voltage_array
def get_channel_number(self):
# get the normalized signal
normed_voltage_array = self.get_normed_signal()
# latest shunt voltage
this_normed_voltage = normed_voltage_array[0]
print('Normed voltage is: ', this_normed_voltage)
this_channel_number = np.digitize(this_normed_voltage, np.linspace(0,1,self.CHANNELS))
this_channel_number = this_channel_number-1
print('Channel number: ',this_channel_number)
return int(this_channel_number)
def send_signal(self):
this_channel_number = self.get_channel_number()
self.midi_out.note_on(127,1,this_channel_number)
def send_channel(self,channel):
self.midi_out.note_on(1, 127, channel)
class midi_connector_test(object):
def __init__(self,WINDOW,CHANNELS):
'''
Test Version: signal is generated with numpy.random.rand
:param WINDOW: Fenster des Sliding mean
:param CHANNELS: Anzahl der Midi Kanäle die Basti hat
'''
self.WINDOW = WINDOW
#self.SAMPLE_RATE = SAMPLE_RATE
self.CHANNELS = CHANNELS
# list of channels: obacht! startet mit 1!
self.channel_list = [int(c + 1) for c in range(self.CHANNELS)]
# setup the volotmeter
#self.voltometer = voltage(WINDOW = self.WINDOW)
# set up midi interface
self.mymidi = pygame.midi
self.init_midi()
self.port = 2 # war beim letzten mal richtig...
self.midi_out = self.mymidi.Output(self.port, 0)
# bar = progressbar.ProgressBar(maxval=100,widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
# bar.start()
# print('Initial Data collection ...')
# for i in range(100):
# self.voltometer.update_shunt_voltage()
# bar.update(i + 1)
# time.sleep(0.1)
# bar.finish()
def init_midi(self):
self.mymidi.init()
def quit_midi(self):
self.mymidi.quit()
def get_normed_signal(self):
# update the voltage array
#self.voltometer.update_shunt_voltage()
this_voltage_array = np.random.rand(self.WINDOW) #self.voltometer.get_voltage_array()
# get MAX and MIN values of the voltage array for normalization
this_max_volt = np.max(this_voltage_array)
this_min_volt = np.min(this_voltage_array)
normed_voltage_array = (this_voltage_array - this_min_volt) / (this_max_volt - this_min_volt)
return normed_voltage_array
def get_channel_number(self):
# get the normalized signal
normed_voltage_array = self.get_normed_signal()
# latest shunt voltage
this_normed_voltage = normed_voltage_array[0]
print('Voltage is: ', this_normed_voltage)
this_channel_number = np.digitize(this_normed_voltage, np.linspace(0,1,self.CHANNELS))
print('Channel number: ',this_channel_number)
return int(this_channel_number)
def send_signal(self):
this_channel_number = self.get_channel_number()
self.midi_out.note_on(127,1,this_channel_number)
def test_send(self,channel):
self.midi_out.note_on(1, 127, channel)