-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
211 lines (177 loc) · 7.49 KB
/
state.py
File metadata and controls
211 lines (177 loc) · 7.49 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import json
import copy
from mido import Message
from constants import base_control_numbers, global_button_notes, get_note_for_button
# Initialize min and max volume variables
min_volume = float('inf')
max_volume = float('-inf')
# State tracking for channels and global buttons
channels = {
0: {'frequency': 110, 'volume': 1.0, 'mute': False, 'select': False, 'r': False, 'box': False},
1: {'frequency': 110, 'volume': 1.0, 'mute': True, 'select': False, 'r': False, 'box': False},
2: {'frequency': 110, 'volume': 1.0, 'mute': True, 'select': False, 'r': False, 'box': False},
3: {'frequency': 110, 'volume': 1.0, 'mute': True, 'select': False, 'r': False, 'box': False},
4: {'frequency': 110, 'volume': 1.0, 'mute': True, 'select': False, 'r': False, 'box': False},
5: {'frequency': 110, 'volume': 1.0, 'mute': True, 'select': False, 'r': False, 'box': False},
6: {'frequency': 110, 'volume': 1.0, 'mute': True, 'select': False, 'r': False, 'box': False},
7: {'frequency': 110, 'volume': 1.0, 'mute': True, 'select': False, 'r': False, 'box': False},
}
# New tube parameters state
tube_params = {
'speed_of_sound': 258, # m/s
'tube_length': 3.0, # m
'tube_diameter': 0.10, # m (10cm)
'damping_coefficient': 0.05, # m^-1
'hole_size': 0.001, # m (1mm)
'propane_pressure': 1.0, # normalized
'reflections': 5, # count
'q_factor': 5.0 # quality factor
}
channel_log = []
_current_log_index = -1 # Initialize the current log index
# Callbacks for updates - will be set by web_server.py
_update_callback = None
_tube_update_callback = None
def register_update_callback(callback_function):
"""Register a function to be called when channel state changes"""
global _update_callback
_update_callback = callback_function
def register_tube_update_callback(callback_function):
"""Register a function to be called when tube parameters change"""
global _tube_update_callback
_tube_update_callback = callback_function
def get_current_log_index():
return _current_log_index
def set_current_log_index(value):
global _current_log_index
_current_log_index = value
# Functions to update tube parameters
def update_tube_param(param_name, value):
"""Update a specific tube parameter"""
if param_name in tube_params:
tube_params[param_name] = value
notify_tube_updated(param_name)
return True
return False
def notify_tube_updated(param_name=None):
"""Notify listeners that tube parameters have changed"""
if _tube_update_callback:
_tube_update_callback(param_name)
# Function to get all tube parameters
def get_tube_params():
"""Get a copy of all tube parameters"""
return dict(tube_params)
# Function to handle frequency adjustment
def adjust_frequency(channel, value):
adjustment = 0.1 if channels[channel]['select'] else 1.0
channels[channel]['frequency'] += value * adjustment
# Notify listeners about the update
notify_channel_updated(channel)
return channel
# Function to handle volume adjustment
def adjust_volume(channel, value):
global min_volume, max_volume
# Update min and max volume values
if value < min_volume:
min_volume = value
if value > max_volume:
max_volume = value
# Calculate the volume ratio between 0.0 and 1.0
if max_volume != min_volume:
volume_ratio = (value - min_volume) / (max_volume - min_volume)
else:
volume_ratio = 0.0
channels[channel]['volume'] = volume_ratio
# Notify listeners about the update
notify_channel_updated(channel)
return channel
# Function to handle mute state changes
def set_mute(channel, mute_state):
channels[channel]['mute'] = mute_state
notify_channel_updated(channel)
return channel
# Function to mute all channels
def mute_all_channels():
for channel in range(8):
channels[channel]['mute'] = True
notify_all_channels_updated()
# Function to notify that a channel was updated
def notify_channel_updated(channel):
if _update_callback:
_update_callback(channel)
# Function to notify that all channels were updated
def notify_all_channels_updated():
if _update_callback:
for i in range(8):
_update_callback(i)
# Function to load the state log into an array
def load_channel_log():
try:
with open('channel_log.jsonl', 'r') as log_file:
state_log = [{int(k): v for k, v in json.loads(line).items()} for line in log_file]
return state_log
except FileNotFoundError:
print("Channel log file not found.")
return [channels]
# Function to record current state
def record_current_state():
# Append the current state to the channel_log
channel_log.append(copy.deepcopy(channels))
# Write the channel_log to a log file as multiple lines of JSON
with open('channel_log.jsonl', 'w') as log_file:
for entry in channel_log:
log_file.write(json.dumps({int(k): v for k, v in entry.items()}) + '\n')
print("Current state recorded")
# Navigate forward in channel log
def navigate_forward(midi_out=None):
global _current_log_index
if _current_log_index < len(channel_log) - 1:
_current_log_index += 1
channels.update(channel_log[_current_log_index])
if midi_out:
set_lights_to_current_state(midi_out)
notify_all_channels_updated()
print(f"Moved forward to log index {_current_log_index}")
# Navigate backward in channel log
def navigate_backward(midi_out=None):
global _current_log_index
if _current_log_index > 0:
_current_log_index -= 1
channels.update(channel_log[_current_log_index])
if midi_out:
set_lights_to_current_state(midi_out)
notify_all_channels_updated()
print(f"Moved back to log index {_current_log_index}")
# Function to handle global button actions
def handle_global_action(action_name, midi_out=None):
if action_name == 'play':
# Display all frequencies on one line
print(f"Frequencies: {', '.join([str(channels[i]['frequency']) for i in range(8)])}")
elif action_name == 'pause':
# Mute all channels
mute_all_channels()
if midi_out:
for channel in range(8):
mute_note = get_note_for_button(channel, 'mute')
midi_out.send(Message('note_on', note=mute_note, velocity=127))
print("All channels muted")
elif action_name == 'record':
record_current_state()
elif action_name == 'fast_forward':
navigate_forward(midi_out)
elif action_name == 'rewind':
navigate_backward(midi_out)
# Function to set all lights to the current state of the channels
def set_lights_to_current_state(midi_out):
for channel, states in channels.items():
for button, state in states.items():
if button in ['mute', 'select', 'r', 'box']:
note = get_note_for_button(channel, button)
velocity = 127 if state else 0
midi_out.send(Message('note_on', note=note, velocity=velocity))
log_length = len(channel_log)
current_log_index = get_current_log_index()
forward_velocity = 127 if current_log_index < len(channel_log) - 1 else 0
back_velocity = 127 if current_log_index > 0 else 0
midi_out.send(Message('note_on', note=global_button_notes['fast_forward'], velocity=forward_velocity))
midi_out.send(Message('note_on', note=global_button_notes['rewind'], velocity=back_velocity))