-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_wav_multispeaker.py
More file actions
383 lines (298 loc) · 14.9 KB
/
Copy pathgenerate_wav_multispeaker.py
File metadata and controls
383 lines (298 loc) · 14.9 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python3
"""
Helper script to convert a tactile spatialiser script file into a multi-channel WAV audio file.
Usage:
python generate_wav_multispeaker.py path/to/script.txt output.wav
python generate_wav_multispeaker.py path/to/script.txt output.wav --config config_4x4_grid.txt
If arguments are omitted, you'll be prompted.
"""
import os
import numpy as np
import argparse
import soundfile as sf
import multispeaker_main as main
def script_to_buffer(script_path, config_path=None):
"""
Parse a script file and convert its actions into a multi-channel buffer.
"""
# Initialize the spatialiser
spatialiser = main.MultiSpeakerSpatialiser(config_path)
# Read and parse actions
with open(script_path, 'r') as f:
lines = f.read().splitlines()
actions = main.parse_script(lines)
# Set up the spatialiser for the script
main.generate_tactile_tone.spatialiser = spatialiser
buffers = []
sr = spatialiser.audio_engine.sample_rate
current_pos = np.array([0.0, 0.0]) # Track current position
print(f"Processing {len(actions)} actions...")
print(f"Output format: {spatialiser.audio_engine.num_channels} channels at {sr}Hz")
for i, act in enumerate(actions):
if i % 10 == 0:
print(f"Processing action {i+1}/{len(actions)}: {act[0]}")
cmd = act[0]
if cmd == 'WAIT':
duration = act[1]
n = int(duration * sr)
silence = np.zeros((n, spatialiser.audio_engine.num_channels), dtype=np.float32)
buffers.append(silence)
elif cmd == 'JUMP':
# No sound, just reposition
current_pos = act[1]
continue
elif cmd == 'SOUND':
_, pos, freq, amp = act
current_pos = pos
buf = spatialiser.audio_engine.generate_tone(pos, freq, amp)
buffers.append(buf.astype(np.float32))
elif cmd == 'ARC':
_, points, duration, steps, freq, amp, mode = act
if steps <= 0:
print("Warning: ARC with steps <= 0, skipping")
continue
# Calculate the time for each step
step_time = duration / steps
# Handle step-by-step interpolation
for j in range(steps):
t = j / (steps - 1) if steps > 1 else 0
if mode == 'CURVED' and len(points) == 3:
# Quadratic Bézier via midpoint
P0, Pm, P1 = points[0], points[1], points[2]
pos = (1 - t) ** 2 * P0 + 2 * (1 - t) * t * Pm + t ** 2 * P1
else:
# Linear interpolation between first and last points
P0, P1 = points[0], points[-1]
pos = P0 * (1 - t) + P1 * t
current_pos = pos
buf = spatialiser.audio_engine.generate_tone(pos, freq, amp)
buffers.append(buf.astype(np.float32))
# Add silence between bursts if not the last step
if j < steps - 1:
wait_samples = int((step_time - spatialiser.audio_engine.tone_duration) * sr)
if wait_samples > 0:
silence = np.zeros((wait_samples, spatialiser.audio_engine.num_channels), dtype=np.float32)
buffers.append(silence)
elif cmd == 'CIRCLE_SMOOTH':
_, radius, duration, steps, freq, amp = act
# Generate continuous circular sweep
N = int(duration * sr)
t = np.arange(N) / sr
ang = 2 * np.pi * (t / duration)
# Pre-allocate buffer
buf = np.zeros((N, spatialiser.audio_engine.num_channels), dtype=np.float32)
# Generate sound for each time step
for j in range(0, N, int(sr * 0.01)): # Update every 10ms
end_idx = min(j + int(sr * 0.01), N)
# Calculate position for this time segment
t_seg = j / sr
angle = 2 * np.pi * (t_seg / duration)
x = radius * np.sin(angle)
y = radius * np.cos(angle)
pos = np.array([x, y])
# Generate short segment
segment_length = end_idx - j
t_segment = np.arange(segment_length) / sr
# Calculate gains for this position
gains, delays = spatialiser.audio_engine.spat_engine.calculate_gains_delays(pos)
# Apply window
window = np.ones(segment_length)
if segment_length > 100: # Only apply fade if segment is long enough
fade_len = min(50, segment_length // 4)
ramp = 0.5 * (1 - np.cos(np.pi * np.arange(fade_len) / fade_len))
window[:fade_len] = ramp
window[-fade_len:] = ramp[::-1]
# Generate tone for each channel
for k, speaker in enumerate(spatialiser.speaker_config.speakers):
channel = speaker['channel']
gain = gains[k]
delay = delays[k]
if gain > 0.001 and channel < spatialiser.audio_engine.num_channels:
# Generate tone with delay
if delay != 0:
tone = amp * gain * np.sin(2 * np.pi * freq * (t_segment + j/sr - delay))
else:
tone = amp * gain * np.sin(2 * np.pi * freq * (t_segment + j/sr))
# Apply window
tone *= window
# Add to buffer
buf[j:end_idx, channel] += tone
buffers.append(buf)
elif cmd == 'FREQ_RAMP':
_, pos, start_freq, end_freq, duration, steps, amp = act
if steps <= 0:
print("Warning: FREQ_RAMP with steps <= 0, skipping")
continue
# Calculate step-by-step frequencies
step_time = duration / steps
for j in range(steps):
t = j / (steps - 1) if steps > 1 else 0
# Linear interpolation of frequency
freq = start_freq * (1 - t) + end_freq * t
current_pos = pos
buf = spatialiser.audio_engine.generate_tone(pos, freq, amp)
buffers.append(buf.astype(np.float32))
# Add silence between bursts if not the last step
if j < steps - 1:
wait_samples = int((step_time - spatialiser.audio_engine.tone_duration) * sr)
if wait_samples > 0:
silence = np.zeros((wait_samples, spatialiser.audio_engine.num_channels), dtype=np.float32)
buffers.append(silence)
elif cmd == 'FREQ_RAMP_SMOOTH':
_, pos, start_freq, end_freq, duration, amp = act
# Generate continuous frequency ramp
N = int(duration * sr)
t = np.arange(N) / sr
# Linear frequency ramp
freq_t = start_freq + (end_freq - start_freq) * (t / duration)
# Calculate phase by integrating frequency
phase = 2 * np.pi * np.cumsum(freq_t) / sr
# Calculate gains and delays for fixed position
gains, delays = spatialiser.audio_engine.spat_engine.calculate_gains_delays(pos)
# Create multi-channel buffer
buf = np.zeros((N, spatialiser.audio_engine.num_channels), dtype=np.float32)
# Apply fade in/out
window = np.ones(N)
if N > spatialiser.audio_engine.fade_len * 2:
fade_len = spatialiser.audio_engine.fade_len
ramp = 0.5 * (1 - np.cos(np.pi * np.arange(fade_len) / fade_len))
window[:fade_len] = ramp
window[-fade_len:] = ramp[::-1]
# Generate tone for each channel
for k, speaker in enumerate(spatialiser.speaker_config.speakers):
channel = speaker['channel']
gain = gains[k]
delay = delays[k]
if gain > 0.001 and channel < spatialiser.audio_engine.num_channels:
# Generate tone with delay
if delay != 0:
tone = amp * gain * np.sin(phase - 2 * np.pi * freq_t * delay)
else:
tone = amp * gain * np.sin(phase)
# Apply window
tone *= window
# Add to buffer
buf[:, channel] = tone
buffers.append(buf)
elif cmd == 'PATH_FREQ_RAMP':
_, points, start_freq, end_freq, duration, steps, amp, mode = act
if steps <= 0:
print("Warning: PATH_FREQ_RAMP with steps <= 0, skipping")
continue
# Generate continuous buffer with both position and frequency changes
N = int(duration * sr)
t = np.arange(N) / sr
normalized_t = t / duration
# Calculate positions along the path
positions = np.zeros((N, 2))
if mode == 'CURVED' and len(points) == 3:
# Quadratic Bézier via midpoint
P0, Pm, P1 = points[0], points[1], points[2]
for j, nt in enumerate(normalized_t):
positions[j] = (1 - nt) ** 2 * P0 + 2 * (1 - nt) * nt * Pm + nt ** 2 * P1
else:
# Linear interpolation between first and last points
P0, P1 = points[0], points[-1]
for j, nt in enumerate(normalized_t):
positions[j] = P0 * (1 - nt) + P1 * nt
# Linear frequency ramp
freq_t = start_freq + (end_freq - start_freq) * normalized_t
# Calculate phase by integrating frequency
phase = 2 * np.pi * np.cumsum(freq_t) / sr
# Pre-allocate buffer
buf = np.zeros((N, spatialiser.audio_engine.num_channels), dtype=np.float32)
# Apply fade in/out
window = np.ones(N)
if N > spatialiser.audio_engine.fade_len * 2:
fade_len = spatialiser.audio_engine.fade_len
ramp = 0.5 * (1 - np.cos(np.pi * np.arange(fade_len) / fade_len))
window[:fade_len] = ramp
window[-fade_len:] = ramp[::-1]
# Generate sound for segments to handle changing position
segment_size = int(sr * 0.01) # 10ms segments
for j in range(0, N, segment_size):
end_idx = min(j + segment_size, N)
segment_length = end_idx - j
# Use position at middle of segment
mid_idx = j + segment_length // 2
pos = positions[mid_idx]
# Calculate gains for this position
gains, delays = spatialiser.audio_engine.spat_engine.calculate_gains_delays(pos)
# Generate tone for each channel
for k, speaker in enumerate(spatialiser.speaker_config.speakers):
channel = speaker['channel']
gain = gains[k]
delay = delays[k]
if gain > 0.001 and channel < spatialiser.audio_engine.num_channels:
# Generate tone segment with delay
if delay != 0:
tone_segment = amp * gain * np.sin(phase[j:end_idx] - 2 * np.pi * freq_t[j:end_idx] * delay)
else:
tone_segment = amp * gain * np.sin(phase[j:end_idx])
# Apply window
tone_segment *= window[j:end_idx]
# Add to buffer
buf[j:end_idx, channel] += tone_segment
buffers.append(buf)
else:
# Unknown command
print(f"Warning: Unsupported command: {cmd}")
# Concatenate all buffers
print("Concatenating buffers...")
if buffers:
final_buffer = np.concatenate(buffers, axis=0)
print(f"Final buffer shape: {final_buffer.shape}")
return final_buffer
else:
print("No audio generated")
return np.zeros((0, spatialiser.audio_engine.num_channels), dtype=np.float32)
def main_cli():
parser = argparse.ArgumentParser(description='Convert tactile script to multi-channel WAV')
parser.add_argument('script', nargs='?', help='Path to input script file')
parser.add_argument('output', nargs='?', help='Path to output WAV file')
parser.add_argument('--config', help='Speaker configuration file')
args = parser.parse_args()
# Prompt if missing
script_path = args.script
while not script_path:
script_path = input("Enter path to script file: ").strip()
while not os.path.isfile(script_path):
script_path = input(f"File '{script_path}' not found. Enter path to script file: ").strip()
output_path = args.output
while not output_path:
output_path = input("Enter path for output WAV file: ").strip()
config_path = args.config
if config_path and not os.path.isfile(config_path):
print(f"Config file '{config_path}' not found. Using default configuration.")
config_path = None
print(f"Input script: {script_path}")
print(f"Output WAV: {output_path}")
if config_path:
print(f"Speaker config: {config_path}")
else:
print("Speaker config: Default 4x4 grid")
try:
buf = script_to_buffer(script_path, config_path)
# Get sample rate from the spatialiser
spatialiser = main.MultiSpeakerSpatialiser(config_path)
sr = spatialiser.audio_engine.sample_rate
# Write the file
sf.write(output_path, buf, sr)
duration = buf.shape[0] / sr
channels = buf.shape[1]
print(f"\nSuccess!")
print(f"Wrote {buf.shape[0]} samples ({duration:.2f} seconds)")
print(f"Channels: {channels}")
print(f"Sample rate: {sr} Hz")
print(f"Output file: {output_path}")
# Show some basic statistics
max_amplitude = np.max(np.abs(buf))
print(f"Peak amplitude: {max_amplitude:.3f}")
if max_amplitude > 0.95:
print("Warning: Audio may be clipping (peak > 0.95)")
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
if __name__ == '__main__':
main_cli()