-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSillySampler.py
More file actions
1275 lines (1065 loc) · 48.3 KB
/
SillySampler.py
File metadata and controls
1275 lines (1065 loc) · 48.3 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys, os, logging, re, traceback, multiprocessing
from pathlib import Path
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
from concurrent.futures import ThreadPoolExecutor
import numpy as np
from numba import njit
import soundfile as sf
import GOOFER as gf
from SillyEditor import interactive_voicing, edit_goofy_files, write_back_voicing_to_goofy
n_fft = 1024
hop_length = n_fft // 4
def _src_tag_from_feat(feat_path: str) -> str:
stem = Path(feat_path).name
if stem.endswith("_features.goofy"):
return stem[:-len("_features.goofy")]
return Path(feat_path).stem
def _invalidate_render_cache(out_path: str, feat_path: str):
try:
out_dir = Path(out_path).parent
tag = _src_tag_from_feat(feat_path)
# Nukes typical cached renders for this source
for p in out_dir.glob(f"{tag}*.wav"):
try:
p.unlink()
logging.info(f"[SE] Invalidated cache: {p.name}")
except Exception as e:
logging.warning(f"[SE] Could not delete {p}: {e}")
# Optional: remove small side-assets OU sometimes drops
for p in out_dir.glob(f"{tag}*.{{json,txt,lock}}"):
try:
p.unlink()
except Exception:
pass
except Exception as e:
logging.warning(f"[SE] Cache invalidate failed: {e}")
logging.basicConfig(format='%(message)s', level=logging.INFO)
# --- UTAU pitch & flags parsing --------------------------------------------
notes = {'C':0,'C#':1,'D':2,'D#':3,'E':4,'F':5,'F#':6,'G':7,'G#':8,'A':9,'A#':10,'B':11}
note_re = re.compile(r'([A-G]#?)(-?\d+)')
flag_re = re.compile(r'([A-Za-z]{1,4})([+-]?\d+)?')
def parse_flags(flag_string):
flags = {}
for key, val in flag_re.findall(flag_string.replace('/', '')):
flags[key] = int(val) if val else None
return flags
def to_uint6(c):
o=ord(c)
if o>=97: return o-71
if o>=65: return o-65
if o>=48: return o+4
if o==43: return 62
if o==47: return 63
raise ValueError(f"Bad b64 '{c}'")
def to_int12(p):
v=(to_uint6(p[0])<<6)|to_uint6(p[1])
return v-4096 if (v&0x800) else v
def to_int12_stream(s):
return [to_int12(s[i:i+2]) for i in range(0,len(s),2)]
def pitch_string_to_cents(x):
parts = x.split('#')
out = []
for i in range(0, len(parts), 2):
chunk = parts[i:i+2]
if len(chunk) == 2:
ps, run = chunk
out += to_int12_stream(ps)
out += [out[-1]] * int(run)
else:
out += to_int12_stream(chunk[0])
arr = np.array(out, dtype=np.float32)
return arr if arr.size else np.array([0.0], dtype=np.float32) # nuke auto-zeroing
def note_to_midi(n):
m = note_re.match(n)
if not m: raise ValueError(f"Bad note '{n}'")
nm, octv = m.groups()
return (int(octv)+1)*12 + notes[nm]
def midi_to_hz(m):
return 440.0 * 2**((m-69)/12)
def dynamic_butter_filter(signal, f0, sr, cutoff_factor, order=4, btype='lowpass'):
x = np.asarray(signal, dtype=np.float32)
n = len(x)
if n == 0:
return x
f0 = np.asarray(f0, dtype=np.float32)
if f0.size != n:
idx_old = np.linspace(0, n - 1, num=f0.size, dtype=np.float64)
f = gf.interp1d(idx_old, f0.astype(np.float64), kind='linear', fill_value='extrapolate')
f0 = f(np.arange(n, dtype=np.float64)).astype(np.float32)
if np.any(f0 > 0):
k = 5
pad = k // 2
padv = np.pad(f0, (pad, pad), mode='edge')
f0_s = np.convolve(padv, np.ones(k, dtype=np.float32) / k, mode='valid')
else:
f0_s = f0
return _dynamic_butter_filter_core(x, f0_s, sr, cutoff_factor, order, btype)
@njit(fastmath=True)
def _dynamic_butter_filter_core(x, f0_s, sr, cutoff_factor, order, btype):
n = len(x)
if n == 0:
return x
floor_lp = 60.0
floor_hp = 20.0
ceil_fc = 0.45 * sr
fc = np.empty(n, dtype=np.float32)
for i in range(n):
if f0_s[i] > 0.0:
fc[i] = f0_s[i] * cutoff_factor
else:
fc[i] = cutoff_factor
if btype == 'lowpass':
for i in range(n):
fc[i] = max(fc[i], floor_lp)
else:
for i in range(n):
fc[i] = max(fc[i], floor_hp)
for i in range(n):
fc[i] = min(fc[i], ceil_fc)
two_pi = 2.0 * np.pi
alpha = np.empty(n, dtype=np.float32)
if btype == 'lowpass':
for i in range(n):
alpha[i] = (two_pi * fc[i]) / (two_pi * fc[i] + sr)
else: # highpass
for i in range(n):
alpha[i] = sr / (two_pi * fc[i] + sr)
y = x.copy()
if btype == 'lowpass':
for _ in range(max(1, int(order))):
yp = np.float32(0.0)
for i in range(n):
a = alpha[i]
xp = y[i]
yp = yp + a * (xp - yp)
y[i] = yp
else:
for _ in range(max(1, int(order))):
yp = np.float32(0.0)
prev_x = y[0] if n > 0 else np.float32(0.0)
for i in range(n):
a = alpha[i]
xp = y[i]
yp = a * (yp + xp - prev_x)
y[i] = yp
prev_x = xp
return y
def stretch_prefix_1d(x, pre_len, factor):
n = len(x)
if pre_len <= 1 or n <= 1 or abs(factor - 1.0) < 1e-6:
return x
pre_new = max(1, int(round(pre_len * factor)))
n_new = pre_new + (n - pre_len)
idx_new = np.arange(n_new, dtype=np.float64)
old_pos = np.where(idx_new < pre_new,
idx_new / factor,
(idx_new - pre_new) + pre_len)
f = gf.interp1d(np.arange(n, dtype=np.float64), x, kind='linear', fill_value='extrapolate')
return f(old_pos)
def stretch_prefix_2d_frames(M, pre_len, factor):
n = M.shape[1]
if pre_len <= 1 or n <= 1 or abs(factor - 1.0) < 1e-6:
return M
pre_new = max(1, int(round(pre_len * factor)))
n_new = pre_new + (n - pre_len)
idx_old = np.arange(n, dtype=np.float64)
idx_new = np.arange(n_new, dtype=np.float64)
old_pos = np.where(idx_new < pre_new,
idx_new / factor,
(idx_new - pre_new) + pre_len)
rows = []
for row in M:
f = gf.interp1d(idx_old, row, kind='linear', fill_value='extrapolate')
rows.append(f(old_pos))
return np.stack(rows, axis=0)
def stretch_prefix_formant_track(track, pre_len, factor):
arr = np.asarray(track, dtype=np.float64)
arr_w = stretch_prefix_1d(arr, pre_len, factor)
return arr_w
def is_audio_file(file):
return file.suffix.lower() in ['.wav', '.flac', '.aiff', '.aif', '.mp3']
def process_file(audio_file):
feat_file = audio_file.with_name(f"{audio_file.stem}_features.goofy")
if feat_file.exists():
logging.info(f"[SKIP] {feat_file.name} already exists")
return
try:
logging.info(f"[EXTRACT] {audio_file}")
y, sr = sf.read(audio_file)
if y.ndim > 1:
y = y.mean(axis=1)
env, f0i, vmask, forms, env_knots = gf.extract_features(y, sr)
ylen = len(y)
gf.save_features(feat_file, env_knots, f0i, vmask, forms, sr, ylen)
except Exception as e:
logging.error(f"[ERROR] Failed to extract {audio_file.name}: {str(e)}")
def extract_features_recursive(input_path):
input_path = Path(input_path)
all_files = input_path.rglob('*') if input_path.is_dir() else [input_path]
audio_files = [f for f in all_files if f.is_file() and is_audio_file(f)]
num_threads = multiprocessing.cpu_count()
with ThreadPoolExecutor(max_workers=num_threads) as executor:
executor.map(process_file, audio_files)
logging.info(f"[DONE] Extracted features from {len(audio_files)} files using {num_threads} threads.")
def canon_formants(d, n_frames):
# remapping
out = {}
for k, v in d.items():
if isinstance(k, int):
name = f'F{k}'
elif isinstance(k, str) and k.upper().startswith('F'):
name = k.upper()
else:
try:
name = f'F{int(k)}'
except Exception:
continue
arr = np.asarray(v, dtype=np.float32)
if len(arr) < n_frames:
arr = np.pad(arr, (0, n_frames - len(arr)), mode='edge')
elif len(arr) > n_frames:
arr = arr[:n_frames]
out[name] = arr
return out
def sanitize_smooth_formant(track, T, sr, min_hz=120.0, max_hz=None, sigma_frames=3):
max_hz = max_hz or (sr * 0.48)
x = np.asarray(track, dtype=np.float32)
if len(x) < T: # in case
x = np.pad(x, (0, T - len(x)), mode='edge')
elif len(x) > T:
x = x[:T]
bad = (~np.isfinite(x)) | (x < min_hz) | (x > max_hz)
if np.any(bad):
good_idx = np.where(~bad)[0]
if good_idx.size:
f = gf.interp1d(good_idx.astype(np.float32), x[~bad], kind='linear', fill_value='extrapolate')
x[bad] = f(np.where(bad)[0].astype(np.float32))
else:
x = np.full_like(x, 300.0)
if sigma_frames > 0:
x = gf.gaussian_filter1d(x, sigma=sigma_frames)
return x.astype(np.float32)
class GooferResampler:
def __init__(
self,
in_file, out_file,
pitch, velocity,
flags='',
offset=0, length=1000, consonant=0, cutoff=0,
volume=100, modulation=0, tempo='!120', pitch_string='AA'
):
self.in_file = Path(in_file)
self.out_file = Path(out_file)
self.pitch_m = note_to_midi(pitch)
self.velocity = float(velocity)
self.flags = parse_flags(flags)
self.offset = float(offset) / 1000.0
self.length = float(length) / 1000.0
self.consonant = float(consonant) / 1000.0
self.cutoff = float(cutoff) / 1000.0
self.volume = float(volume) / 100.0
self.modulation = float(modulation) / 100.0
self.tempo = float(tempo.lstrip('!'))
self.bend = pitch_string_to_cents(pitch_string)
# SillyEditor for v-uv
se_val = next((v for k, v in self.flags.items() if k.lower() == 'se'), 0)
self.use_editor = (se_val == 1)
# gender flag
self.formant_shift = 1.0 + (self.flags.get('g', 0) / 200.0)
# brightness flag
self.brightness_env = (self.flags.get('br', 0) + 100) / 100.0
# formants band flag
self.F1_shift = 1.0 + (self.flags.get('fa', 0) / 100.0)
self.F2_shift = 1.0 + (self.flags.get('fb', 0) / 100.0)
self.F3_shift = 1.0 + (self.flags.get('fc', 0) / 100.0)
self.F4_shift = 1.0 + (self.flags.get('fd', 0) / 100.0)
# roughness/harshness flag
sh_val = self.flags.get('sh', None)
self.f0_jitter = sh_val is not None and sh_val > 0
self.f0_jitter_strength = (sh_val or 0) / 50.0
sr_val = self.flags.get('sr', None)
self.volume_jitter = sr_val is not None and sr_val > 0
self.volume_jitter_strength = (sr_val or 0) / 50.0
# dryness flag
sd_val = self.flags.get('sd', None)
self.sd_strength = float(sd_val or 0)
# breathiness flag
self.breathiness_mix = (self.flags.get('B', 0) + 100) / 100.0
# unvoiced flag
self.unvoiced_mix = (self.flags.get('U', 0) + 100) / 100.0
# voicing flag
self.harmonic_mix = np.clip(self.flags.get('V', 100), 0, 100) / 100.0
# stretch flag
loop_flag = next((k for k in self.flags if k.lower() == 'l'), None)
if loop_flag:
lval = self.flags[loop_flag]
if lval == 0:
self.loop_mode = 'concat'
elif lval == 1:
self.loop_mode = 'avg'
elif lval == 2:
self.loop_mode = 'stretch'
else:
self.loop_mode = 'concat' # default slay
else:
self.loop_mode = 'concat' # default if no L flag (bad lmao)
# tension flag
self.tension = self.flags.get('st', 0) / 100.0
# growl flag
sg_val = self.flags.get('sg', 0)
self.subharm_weight = (sg_val / 100.0) * 1.5
self.add_subharm = sg_val > 0
# reverse flag (R0 = off, R1 = on)
self.reverse = self.flags.get('R', 0) == 1
# growl mix like straycat so its different, ill call it jitter
self.growl_mix = np.clip(self.flags.get('sj', 0) or 0, 0, 100) / 100.0
# aperiodic mix
self.aperiodic_mix = np.clip(self.flags.get('sa', 0) or 0, 0, 100) / 100.0
# subharmonics mix. HP at original F0
self.subharm_gain = np.clip(self.flags.get('su', 0) or 0, 0, 100) / 100.0
# normalization flag
self.normalize = (np.clip(self.flags['P'], 0, 100) / 100.0) if 'P' in self.flags else 1.0
# env smoothing/sharping flag
es_raw = next((v for k, v in self.flags.items() if k.lower() == 'es'), 0) or 0
self.env_shape = float(np.clip(es_raw, -100, 100)) / 100.0
# force voiced flag (funny)
self.force_voiced = (self.flags.get('FV', 0) == 1)
# dynamics based on pitch flag
pd_raw = next((v for k, v in self.flags.items() if k.lower() == 'pd'), 0) or 0
pd_raw = int(np.clip(pd_raw, -100, 100))
self.pitch_dyn = float(pd_raw) / 100.0
# formant width expansion flag
self.formant_width = ((self.flags.get('fw', 0) or 0) / 100.0) * 0.1
# formant strength flags
fst_val = next((v for k, v in self.flags.items() if k.lower() == 'fst'), 0) or 0
self.formant_strength_global = float(np.clip(fst_val, -100, 100)) / 100.0
fsta_val = next((v for k, v in self.flags.items() if k.lower() == 'fsta'), 0) or 0
fstb_val = next((v for k, v in self.flags.items() if k.lower() == 'fstb'), 0) or 0
fstc_val = next((v for k, v in self.flags.items() if k.lower() == 'fstc'), 0) or 0
fstd_val = next((v for k, v in self.flags.items() if k.lower() == 'fstd'), 0) or 0
self.formant_strength_f1 = float(np.clip(self.formant_strength_global + (fsta_val / 100.0), -1.0, 1.0))
self.formant_strength_f2 = float(np.clip(self.formant_strength_global + (fstb_val / 100.0), -1.0, 1.0))
self.formant_strength_f3 = float(np.clip(self.formant_strength_global + (fstc_val / 100.0), -1.0, 1.0))
self.formant_strength_f4 = float(np.clip(self.formant_strength_global + (fstd_val / 100.0), -1.0, 1.0))
self.render()
def render(self):
feat = self.in_file.with_name(f'{self.in_file.stem}_features.goofy')
if feat.exists():
logging.info('Loading cached features')
env, f0i, vmask, forms, sr, ylen = gf.load_features(feat)
y, _ = sf.read(self.in_file)
if y.ndim > 1:
y = y.mean(axis=1)
if isinstance(env, dict) and env.get("mode") == "knots":
env = gf.decode_env_from_knots(env)
else:
logging.info('Extracting features')
y, sr = sf.read(self.in_file)
if y.ndim > 1:
y = y.mean(axis=1)
env, f0i, vmask, forms, env_knots = gf.extract_features(y, sr, n_fft=n_fft, hop_length=hop_length)
ylen = len(y)
gf.save_features(feat, env_knots, f0i, vmask, forms, sr, ylen)
self._raw_y = y
self._sr = sr
# Reverse features if flag R is set
if self.reverse:
logging.info('Reversing features (R flag)')
env = env[:, ::-1]
f0i = f0i[::-1]
vmask = vmask[::-1]
forms = {k: list(forms[k])[::-1] for k in forms}
self._raw_y = self._raw_y[::-1]
features = (env, f0i, vmask, forms, sr, ylen)
self.resample(features)
def resample(self, features):
env_spec, f0_interp, voicing_mask, forms, sr, ylen = features
if isinstance(env_spec, dict) and env_spec.get("mode") == "knots":
env_spec = gf.decode_env_from_knots(env_spec)
sample_length_sec = ylen / sr
start_sec_base = self.offset
if self.cutoff < 0:
end_sec_base = self.offset - self.cutoff # your current logic
else:
end_sec_base = sample_length_sec - self.cutoff
# reverse flag
if self.reverse:
L = end_sec_base - start_sec_base
offset_used = sample_length_sec - end_sec_base
cutoff_used = sample_length_sec - (offset_used + L)
else:
offset_used = self.offset
cutoff_used = self.cutoff
start_sample = int(offset_used * sr)
consonant_sample = start_sample + int(self.consonant * sr)
if cutoff_used < 0:
end_sec = offset_used - cutoff_used
else:
end_sec = sample_length_sec - cutoff_used
end_sample = int(end_sec * sr)
#debug
#logging.info(f"Sample length: {sample_length_sec:.3f}s")
#logging.info(f"Offset: {self.offset:.3f}s: Start sample: {start_sample}")
#logging.info(f"Cutoff: {self.cutoff:.3f}s: End sample: {end_sample}")
logging.info('Interpolating features')
# cut frame indices
start_frame = start_sample // hop_length
consonant_frame = consonant_sample // hop_length
end_frame = end_sample // hop_length
#debug
#logging.info(f"Start frame: {start_frame} ({start_sample / sr:.3f}s)")
#logging.info(f"Consonant frame: {consonant_frame} ({consonant_sample / sr:.3f}s)")
#logging.info(f"End frame: {end_frame} ({end_sample / sr:.3f}s)")
env_pre = env_spec[:, start_frame:consonant_frame]
f0_pre = f0_interp[start_sample:consonant_sample]
mask_pre = voicing_mask[start_sample:consonant_sample]
env_tail = env_spec[:, consonant_frame:end_frame]
f0_tail = f0_interp[consonant_sample:end_sample]
mask_tail= voicing_mask[consonant_sample:end_sample]
# brightness flag
if self.brightness_env != 1.0:
if env_pre.size or env_tail.size:
n_bins = (env_pre if env_pre.size else env_tail).shape[0]
freqs = np.linspace(1e-6, sr * 0.5, n_bins, dtype=np.float32)
norm_f = np.clip(freqs / (sr * 0.5), 0.02, 1.0)
alpha = np.clip(self.brightness_env - 1.0, -0.9, 1.0)
tilt = norm_f ** alpha
tilt /= (tilt.mean() + 1e-12)
if env_pre.size:
env_pre *= tilt[:, None].astype(env_pre.dtype)
if env_tail.size:
env_tail *= tilt[:, None].astype(env_tail.dtype)
# envelope smoothing/sharping stuff
if self.env_shape != 0.0 and (env_pre.size or env_tail.size):
def match_frame_means(orig, mod):
m0 = np.mean(orig, axis=0, keepdims=True)
m1 = np.mean(mod, axis=0, keepdims=True)
return (mod * (m0 / (m1 + 1e-12))).astype(orig.dtype)
s = abs(self.env_shape)
sigma_sm = 1.0 + 6.0 * s
sigma_sh = 0.8 + 4.0 * s
amount = 5 * s
def smooth(block):
if not block.size: return block
src = block
blur = gf.gaussian_filter1d(block, sigma=sigma_sm, axis=0)
out = match_frame_means(src, blur)
return np.maximum(0.0, out)
def sharpen(block):
if not block.size: return block
src = block
blur = gf.gaussian_filter1d(block, sigma=sigma_sh, axis=0)
out = src + amount * (src - blur)
out = np.maximum(0.0, out)
out = match_frame_means(src, out)
return out
if self.env_shape < 0.0:
if env_pre.size: env_pre = smooth(env_pre)
if env_tail.size: env_tail = smooth(env_tail)
else:
if env_pre.size: env_pre = sharpen(env_pre)
if env_tail.size: env_tail = sharpen(env_tail)
# formant width expansion flag
if self.formant_width != 0.0 and env_spec.size:
def warp_envelope_bandwidth(env, amount):
n_bins, n_frames = env.shape
bins = np.arange(n_bins, dtype=np.float64)
center = n_bins / 2.0
# expansion curve stretch away from center if positive
warped = (bins - center) * (1.0 + amount) + center
warped = np.clip(warped, 0, n_bins - 1)
lo = np.floor(warped).astype(int)
hi = np.minimum(lo + 1, n_bins - 1)
frac = warped - lo
out = np.empty_like(env)
for i in range(n_frames):
col = env[:, i]
out[:, i] = (1 - frac) * col[lo] + frac * col[hi]
return out
if env_pre.size:
env_pre = warp_envelope_bandwidth(env_pre, self.formant_width)
if env_tail.size:
env_tail = warp_envelope_bandwidth(env_tail, self.formant_width)
### voicing editor stuff (SE1 flag)
feat_path = str(self.in_file.with_name(f'{self.in_file.stem}_features.goofy'))
base_mask = np.concatenate([mask_pre, mask_tail]).astype(np.float32)
if self.use_editor:
# GUI always opens when SE1 is on
y_src = self._raw_y # mostlikely going to change
y_snip_raw = y_src[start_sample:end_sample].astype(np.float32)
ui_result = interactive_voicing(
y_snip_raw,
sr,
init_mask=base_mask,
title=f"Voicing: {self.in_file.name}"
)
if ui_result is not None and len(ui_result) == len(base_mask):
edited = ui_result.astype(np.float32)
write_back_voicing_to_goofy(
feat_path,
edited,
start_sample,
end_sample,
self.reverse,
ylen,
)
# use the edited mask for this render
e_pre = edited[:len(mask_pre)]
e_tail = edited[len(mask_pre):]
mask_pre = e_pre.astype(np.float32)
mask_tail = e_tail.astype(np.float32)
_invalidate_render_cache(str(self.out_file), feat_path)
else:
pass
else:
pass
### End of SE1
# force voiced flag
if self.force_voiced:
if mask_pre.size:
mask_pre[:] = 1.0
if mask_tail.size:
mask_tail[:] = 1.0
desired_tail_samples = int(self.length * sr)
# Loop (tile) envelope frames for sustain...
tail_frames = env_tail.shape[1]
desired_tail_frames = int(np.ceil(self.length * sr / hop_length))
if tail_frames >= desired_tail_frames:
env_tail_looped = env_tail[:, :desired_tail_frames]
else:
reps = desired_tail_frames // tail_frames
rem = desired_tail_frames % tail_frames
if self.loop_mode == 'stretch':
if tail_frames == 0:
env_tail_looped = np.zeros((env_spec.shape[0], desired_tail_frames), dtype=np.float32)
else:
env_tail_looped = gf.stretch_feature(env_tail, desired_tail_frames / tail_frames)
elif tail_frames >= desired_tail_frames:
env_tail_looped = env_tail[:, :desired_tail_frames]
else:
if self.loop_mode == 'avg':
loop_tile = (env_tail + env_tail[:, ::-1]) / 2.0
parts = [loop_tile] * reps
if rem:
parts.append(loop_tile[:, :rem])
env_tail_looped = np.concatenate(parts, axis=1)
else: # "concat" mode
full_loop = [env_tail.copy()]
for _ in range(reps - 1):
prev = full_loop[-1]
max_fade = min(8, tail_frames // 2)
fade_in = np.linspace(0, 1, max_fade)[None, :]
fade_out = np.linspace(1, 0, max_fade)[None, :]
A = prev[:, -max_fade:]
B = env_tail[:, :max_fade]
crossfaded = A * fade_out + B * fade_in
chunk = np.concatenate([
prev[:, :-max_fade],
crossfaded,
env_tail[:, max_fade:]
], axis=1)
full_loop[-1] = chunk
full_loop.append(env_tail.copy())
if rem:
last_chunk = env_tail[:, :rem]
prev = full_loop[-1]
max_fade = min(8, rem // 2)
if max_fade > 0:
fade_in = np.linspace(0, 1, max_fade)[None, :]
fade_out = np.linspace(1, 0, max_fade)[None, :]
A = prev[:, -max_fade:]
B = last_chunk[:, :max_fade]
crossfaded = A * fade_out + B * fade_in
chunk = np.concatenate([
prev[:, :-max_fade],
crossfaded,
last_chunk[:, max_fade:]
], axis=1)
else:
chunk = np.concatenate([prev, last_chunk], axis=1)
full_loop[-1] = chunk
env_tail_looped = np.concatenate(full_loop, axis=1)
# loop f0 and voicing mask
tail_len = len(f0_tail)
if tail_len >= desired_tail_samples:
f0_tail_looped = f0_tail[:desired_tail_samples]
mask_tail_looped = mask_tail[:desired_tail_samples]
else:
reps_samp = desired_tail_samples // tail_len
rem_samp = desired_tail_samples % tail_len
parts_f0 = [f0_tail] * reps_samp
parts_mask = [mask_tail] * reps_samp
if rem_samp:
parts_f0.append(f0_tail[:rem_samp])
parts_mask.append(mask_tail[:rem_samp])
f0_tail_looped = np.concatenate(parts_f0)
mask_tail_looped = np.concatenate(parts_mask)
formants_pre = {k: v[start_frame:consonant_frame] for k, v in forms.items()}
formants_tail = {k: v[consonant_frame:end_frame] for k, v in forms.items()}
formants_tail_looped = {}
for k in forms:
track = np.asarray(formants_tail[k], dtype=np.float32)
if self.loop_mode == 'stretch':
if track.size == 0:
formants_tail_looped[k] = np.zeros(desired_tail_frames, dtype=np.float32)
else:
factor = desired_tail_frames / float(track.size)
formants_tail_looped[k] = gf.stretch_feature(track, factor, kind='linear').astype(np.float32)
else:
if track.size == 0:
formants_tail_looped[k] = np.zeros(desired_tail_frames, dtype=np.float32)
else:
reps = desired_tail_frames // track.size
rem = desired_tail_frames % track.size
if self.loop_mode == 'avg':
loop_tile = (track + track[::-1]) * 0.5
base = np.tile(loop_tile, reps)
if rem > 0:
base = np.concatenate([base, loop_tile[:rem]])
formants_tail_looped[k] = base.astype(np.float32)
else:
base = np.tile(track, reps)
if rem > 0:
base = np.concatenate([base, track[:rem]])
formants_tail_looped[k] = base.astype(np.float32)
formants_new = {
k: np.concatenate([formants_pre[k], formants_tail_looped[k]])
for k in forms
}
# concatenate pre and looped tail
env_new = np.concatenate([env_pre, env_tail_looped], axis=1)
f0_new = np.concatenate([f0_pre, f0_tail_looped])
mask_new = np.concatenate([mask_pre, mask_tail_looped])
target_frames = env_new.shape[1]
for k in formants_new:
f = formants_new[k]
if len(f) < target_frames:
pad = target_frames - len(f)
formants_new[k] = np.pad(f, (0, pad), mode='edge')
elif len(f) > target_frames:
formants_new[k] = f[:target_frames]
# convel shits
vel_factor = float(2.0 ** (1.0 - (self.velocity / 100.0)))
#vel_factor = float(np.clip(vel_factor, 0.33, 3.0)) this was for a test lmao idt ppl will need this
pre_frames = env_pre.shape[1]
pre_samples = len(f0_pre)
if abs(vel_factor - 1.0) > 1e-6 and pre_frames > 1 and pre_samples > 1:
env_new = stretch_prefix_2d_frames(env_new, pre_frames, vel_factor)
new_target_frames = env_new.shape[1]
formants_new_warped = {}
for k, track in formants_new.items():
formants_new_warped[k] = stretch_prefix_formant_track(track, pre_frames, vel_factor)
f = formants_new_warped[k]
if len(f) < new_target_frames:
f = np.pad(f, (0, new_target_frames - len(f)), mode='edge')
elif len(f) > new_target_frames:
f = f[:new_target_frames]
formants_new_warped[k] = f
formants_new = formants_new_warped
f0_new = stretch_prefix_1d(f0_new, pre_samples, vel_factor)
mask_new = stretch_prefix_1d(mask_new, pre_samples, vel_factor)
### formant strength stuff
formants_new = canon_formants(formants_new, target_frames)
strength_vals = [
self.formant_strength_f1,
self.formant_strength_f2,
self.formant_strength_f3,
self.formant_strength_f4,
]
# pull tracks ,fall back to zeros if missing
T = env_new.shape[1]
F1 = sanitize_smooth_formant(formants_new.get('F1', np.zeros(T)), T, sr, min_hz=120.0, sigma_frames=4)
F2 = sanitize_smooth_formant(formants_new.get('F2', np.zeros(T)), T, sr, min_hz=300.0, sigma_frames=4)
F3 = sanitize_smooth_formant(formants_new.get('F3', np.zeros(T)), T, sr, min_hz=1500.0, sigma_frames=4)
F4 = sanitize_smooth_formant(formants_new.get('F4', np.zeros(T)), T, sr, min_hz=2000.0, sigma_frames=4)
Fs = [F1, F2, F3, F4]
n_bins = env_new.shape[0]
freqs = np.linspace(0.0, sr / 2.0, n_bins, dtype=np.float32)
gain_env = np.ones_like(env_new, dtype=np.float32)
# how wide each formant’s influence bell is in hz
sigma_hz_list = [100.0, 200.0, 350.0, 500.0]
# probably needs vectorize
for t in range(T):
for k, (Ftrack, s_val) in enumerate(zip(Fs, strength_vals)):
if abs(s_val) < 1e-6:
continue
f0 = float(Ftrack[t])
if not np.isfinite(f0) or f0 <= 50.0 or f0 >= (sr * 0.5):
continue
sigma_hz = sigma_hz_list[k]
weight = np.exp(-0.5 * ((freqs - f0) / sigma_hz) ** 2).astype(np.float32)
gain = 1.0 + s_val
gain_env[:, t] *= 1.0 + (gain - 1.0) * weight
env_new *= gain_env
### end formant strength stuff
# thank you straycat
n_total = len(f0_new)
t_samples = np.arange(n_total) / sr
# self.bend is cents; /100 = semitones; + self.pitch = absolute MIDI curve
pitch_semi = self.bend.astype(np.float64) / 100.0 + self.pitch_m
# pitch offset flag
t_cents = self.flags.get('t', 0)
if t_cents:
pitch_semi = pitch_semi + (t_cents / 100.0)
# tick times: one tick = 1/96 quarter‐note; quarter-note = 60/tempo seconds
# so tick_dt = 60/(tempo*96)
tick_dt = 60.0 / (self.tempo * 96.0)
t_pitch = np.arange(len(pitch_semi)) * tick_dt
pitch_interp = gf.interp1d(t_pitch, pitch_semi, kind='linear', fill_value='extrapolate')
t_clamped = np.clip(t_samples, t_pitch[0], t_pitch[-1])
midi_curve = pitch_interp(t_clamped)
f0_new = mask_new * midi_to_hz(midi_curve)
# pitch to dynamic flag thingy
self.dyn_gain = None
if self.pitch_dyn != 0.0:
t_cents = self.flags.get('t', 0) or 0
baseline_midi = self.pitch_m + (t_cents / 100.0)
bend_semi = (midi_curve - baseline_midi).astype(np.float32)
sigma_samp = max(1, int(0.010 * sr))
bend_s = gf.gaussian_filter1d(bend_semi, sigma=sigma_samp)
ref = float(np.percentile(np.abs(bend_s), 95)) + 1e-8
v = np.clip(bend_s / ref, -1.0, 1.0)
signed = v if self.pitch_dyn > 0 else -v
max_db = 12.0
amt_db = max_db * abs(self.pitch_dyn)
gain_db = amt_db * signed
self.dyn_gain = np.power(10.0, gain_db / 20.0).astype(np.float32)
self.dyn_gain = np.clip(self.dyn_gain, 1e-3, 1e3)
# only work on voiced
vmask_s = gf.gaussian_filter1d(mask_new.astype(np.float32), sigma=int(0.01 * sr))
self.dyn_gain = 1.0 + (self.dyn_gain - 1.0) * vmask_s
### FRY STUFF 1
vf = float(self.flags.get('vf', 0)) # amount & direction
vh_val = float(self.flags.get('vh', 50)) # fry base Hz
vh_val = max(1.0, vh_val)
vl = float(self.flags.get('vl', 15)) # glide % [0..100]
vl = np.clip(vl, 0.0, 100.0)
if vf != 0:
vf = float(np.clip(vf, -100.0, 100.0))
n = len(f0_new)
if vf > 0:
# fry from start
L = int(round(n * (vf / 100.0)))
if L > 0:
glide_len = int(round(L * (vl / 100.0)))
glide_len = np.clip(glide_len, 0, L)
const_len = L - glide_len
if const_len > 0:
s_const = slice(0, const_len)
f0_new[s_const] = vh_val * (mask_new[s_const] > 0)
if glide_len > 0:
s_glide = slice(const_len, L)
w = np.linspace(0.0, 1.0, glide_len, endpoint=True)
target = f0_new[s_glide]
base = vh_val * (mask_new[s_glide] > 0)
f0_new[s_glide] = (1.0 - w) * base + w * target
elif vf < 0:
# fry from end
L = int(round(n * (abs(vf) / 100.0)))
if L > 0:
glide_len = int(round(L * (vl / 100.0)))
glide_len = np.clip(glide_len, 0, L)
const_len = L - glide_len
start = n - L
if glide_len > 0:
s_glide = slice(start, start + glide_len)
w = np.linspace(1.0, 0.0, glide_len, endpoint=True)
target = f0_new[s_glide]
base = vh_val * (mask_new[s_glide] > 0)
f0_new[s_glide] = (1.0 - w) * base + w * target
if const_len > 0:
s_const = slice(start + glide_len, n)
f0_new[s_const] = vh_val * (mask_new[s_const] > 0)
# fry mask
self._fry_mask = None
if vf != 0:
n = len(f0_new)
mid = n // 2
if vf > 0:
L = int(round(mid * (vf / 100.0)))
start_i, end_i = 0, max(0, min(n, L))
else:
L = int(round((n - mid) * (abs(vf) / 100.0)))
start_i, end_i = max(0, n - L), n
if end_i > start_i:
fry_mask = np.zeros(n, dtype=np.float32)
fry_mask[start_i:end_i] = 1.0
fade = int(0.01 * sr)
if fade > 0:
a0 = start_i
a1 = min(end_i, start_i + fade)
if a1 > a0:
fry_mask[a0:a1] *= np.linspace(0.0, 1.0, a1 - a0, endpoint=True)
b0 = max(start_i, end_i - fade)
b1 = end_i
if b1 > b0:
fry_mask[b0:b1] *= np.linspace(1.0, 0.0, b1 - b0, endpoint=True)
self._fry_mask = fry_mask
else:
self._fry_mask = None
# fry formant shift
fry_env_shift = 0.92
if getattr(self, "_fry_mask", None) is not None and env_new.size:
fry_mask_samples = self._fry_mask
n_bins, n_frames = env_new.shape
frame_centers = np.minimum(len(fry_mask_samples) - 1,
(np.arange(n_frames) * hop_length + hop_length // 2)).astype(int)
fry_mask_frames = fry_mask_samples[frame_centers] # (n_frames,)
frames_to_warp = np.nonzero(fry_mask_frames > 1e-6)[0]
if frames_to_warp.size:
bin_idx = np.arange(n_bins, dtype=np.float64)
for j in frames_to_warp:
w = float(fry_mask_frames[j])
s = 1.0 - w * (1.0 - fry_env_shift)
if abs(s - 1.0) < 1e-6:
continue
src = bin_idx / s
src = np.clip(src, 0.0, n_bins - 1.0)
lo = np.floor(src).astype(np.int32)
hi = np.minimum(lo + 1, n_bins - 1)
frac = src - lo
col = env_new[:, j]
env_new[:, j] = (1.0 - frac) * col[lo] + frac * col[hi]
### END OF FRY STUFF 1
# dang i hate this
# dummy y-length (goofer doesnt care)