Repository navigation
Expand file tree
/
Copy pathSfxLab.java
More file actions
5686 lines (5514 loc) · 350 KB
/
Copy pathSfxLab.java
File metadata and controls
5686 lines (5514 loc) · 350 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 javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.List;
import sfxlab.runtime.*;
import sfxlab.runtime.Clip; // over javax.sound.sampled.Clip
import static sfxlab.runtime.Sfx.*;
import static sfxlab.runtime.Samples.*;
import static sfxlab.runtime.Partials.*;
import static sfxlab.runtime.SfxFormat.*;
import static sfxlab.runtime.BenchMixer.*;
/**
* SfxLab — SynthLab reworked into a timeline sound-effect workbench.
*
* The spellcasting game is gone; what remains is the sound engine, pulled
* apart into orthogonal pieces and laid on a timeline:
*
* GENERATORS (a clip is one generator playing for a span of time)
* cloud the SynthLab voice cloud: N wavetable voices wandering around
* a chord, plus a sine sub. gather/drift/timbre are now
* independent knobs (timbre was derived from gather+stab+purity;
* drift is what stability used to hide).
* tones two clean tones at an interval (the old channel B), with
* optional detune pair, seek-shimmer, and an FM pair (fm ratio /
* fm depth) for bells, metal, and robot chirps. FM depth rides
* the envelope, so percussive clips get the classic bright
* strike that mellows into the ring.
* noise noise with a color knob: 0 white (hiss) → pink (wind) →
* 1 brown (rumble). No longer welded to "purity".
* sparkle the stochastic ping shower, with density/decay/spread and its
* own FM (each ping's brightness decays with its own ring-out).
* pluck a Karplus-Strong string: a noise burst circulating in a tuned
* delay line — guitars, harps, koto. damp = string warmth,
* sustain = ring length, pick = attack brightness, interval
* adds a second string (root+7 = a power chord that distorts
* as one, which is THE overdriven-chord sound).
* sample a recorded audio file (W imports it into samples/
* and drops a clip; anything ffmpeg can read is accepted —
* mp3/ogg/video files are decoded to .wav on the way in).
* Two pitch modes:
* tape pitch = playback speed (+12 = double speed and an
* octave up, like any classic sampler)
* keep len granular: pitch shifts while the length stays put
* (two overlapping 50 ms grains; each new grain is
* slid to line up with the one still sounding, so
* the crossfades don't flutter), and `speed` then
* time-stretches without touching pitch — 0 freezes
* the moment under the read head
* pitch sweeps/LFO bend it in either mode; the whole effect
* chain (drive, flange, filter, sends) applies as usual. Sample
* clips draw their waveform, so where the recording actually
* ends inside the clip is visible.
* choir a sample turned into a voice cloud: N copies of the recording,
* each pitched through its own keep-len grain shifter by a
* ratio that (like cloud) sits somewhere random when gather is
* 0 and on its chord slot when gather is 1 — so `gather swp`
* from 0 to 1 is a THX-style convergence of a real recording.
* wander bounds the random spread (±semitones: a shifted vocal
* stops sounding human past ~7), chord slots stay within an
* octave of the source, scatter spreads the voices' read
* positions across the recording so unison voices are different
* moments of it rather than one moment doubled, drift lets the
* ungathered voices keep wandering. Always loops. C converts a
* selected sample clip to choir (and back); the file, start and
* speed carry over.
*
* COMMON PER-CLIP PARAMS (every clip gets its own, nothing is global)
* level, attack, release envelope inside the clip
* pitch, pitch swp base pitch + linear sweep over the clip
* cutoff, cutoff swp, resonance per-clip state-variable filter
* filter LP / BP / HP mode
* pan, pan swp stereo placement + sweep
* echo send into the shared ping-pong delay
* reverb send into the shared room (freeverb-
* style combs+allpasses) — space & bloom
* drive waveshaping distortion ahead of the
* filter (the filter then acts as the
* cabinet) — overdrive, growl, grit
* flange, flange fb swept comb filter (a few ms of delay
* mixed back in, swept by the clip's LFO:
* sine = jet whoosh, random = arc jitter;
* feedback sharpens it metallic)
* phaser, phaser rate, ph stages, ph pos a chain of all-pass stages swept by their
* own slow LFO and mixed with the dry signal:
* a few moving notches — the swirl on
* magic, jets, and anything "alive" (the
* flanger is many evenly spaced notches;
* the phaser is few, unevenly spaced)
* duck, duck from sidechain: this clip's level dips by
* `duck` whenever track `duck from` gets
* loud (2 ms attack, 150 ms release) —
* a rumble that steps aside for the hit
* reads clearer and the recovery feels
* like a swell
* lfo rate/shape/pos, lfo>pitch/cut/amp per-clip LFO (sine / triangle /
* square / random S&H) — vibrato, sirens,
* tremolo, filter wobble. Depths default
* to 0, i.e. off.
* env curve 0 = linear ramps; positive = percussive
* (exponential-ish decay — coins, pings,
* impacts); negative = swelling
*
* The old one-shot gestures are now just palette presets built from these
* primitives — riser = tones + pitch sweep up, zap = short gritty tones with
* a -54 st sweep, woosh = noise through a swept bandpass with a pan sweep —
* so every one of them is editable after you drop it.
*
* TIMELINE: 6 tracks, drag clips to move (vertically to change track), drag
* the right edge to resize. Click a track number to mute it; drag the little
* bar under the number for that track's volume (saved with the project,
* honored by export). The playhead is the seeker: click/drag in the ruler or
* empty track space to scrub it.
*
* VIDEO REFERENCE: V attaches a screen recording (anything ffmpeg reads).
* Its frames are extracted once into video-cache/ and shown as a
* filmstrip lane under the ruler (ctrl-drag slides it in time, negative
* starts are fine) and in the monitor window (M), which follows the
* playhead. [ and ] step one frame. The video's own audio track lands on
* the last track as a reference sample clip LINKED to the picture: dragging
* either one moves both, so they never drift apart (the video menu links or
* unlinks any selected clip; linked clips get a blue left edge).
*
* CUTTING UP RECORDINGS: X splits the selected clip at the playhead — the
* right half resumes from the right spot in the recording and any sweeps
* stay continuous across the cut. Dragging a clip's LEFT edge trims it (the
* audio stays put, like a DAW). Clips and the video may start before 0:
* whatever hangs off the left is simply never heard, so a long recording can
* be shoved left until the moment you want sits at 0.
*
* PARTIALS: C cycles a sample clip -> choir -> partials -> sample. A partials
* clip is a sines + residual model of the recording: every stable sinusoid is
* tracked through the spectrogram and replayed by an oscillator bank, and what
* is left after notching those peaks out (noise, crackle, breath) plays as the
* residual through the keep-len grain shifter at ratio 1. `pitch` (and the
* global key) transposes ONLY the partials, so an arc, a hum or an ice ring
* sings a new note while its texture stays exactly as recorded. `sines` and
* `residual` mix the two halves; `floor` / `min len` are the analysis
* thresholds (re-analysed in the background when changed); `speed` 0 freezes.
* Same algorithm as tools/partials.py.
*
* ROOT & TUNING: a project has a root note (`root` line, default C2 =
* 65.41 Hz, the mod's crystal degree I; ctrl+R changes it). Key 0 means "as
* authored" and the convention is that every pitched layer is authored ON
* the root, so the mod can transpose by (scale degree + 12·octave + cents)
* with --key. Synth clips are absolute (pitch 0 = 110 Hz). Recordings are
* relative to whatever they were recorded at, so the panel shows each
* sample/choir/partials clip's estimated fundamental (from its partial
* analysis) after the clip's pitch offset, as Hz and a note name. R moves
* the selected clip's pitch to the nearest octave of the root; shift+R to a
* chosen degree above it (7 = a fifth) for chord-tone layers.
*
* SAMPLE BROWSER: A docks a panel on the right listing everything under
* samples with a filter box and tonal badges (share, note, length,
* computed in the background and cached), preview / stop / play-on-select,
* and one-click adds: as a sample clip at the playhead (double-click or
* ENTER) or as a partials clip already tuned to the root. ESC hands the
* keyboard back to the timeline. Its state is remembered in lab.cfg.
*
* HARMONIC CONTROLS (partials clips, and the tones `bank`): each tracked
* partial knows its ratio to the recording's fundamental and whether it sits
* on the harmonic series. `odd/even` fades one side of the series (hollow ↔
* odd-less), `tilt` brightens or darkens by ratio, `purity` scales the
* inharmonic partials (0 = a clean series, 2 = more alien), `stretch` warps
* the spacing toward bells and metal, `gather` pulls every partial to the
* nearest pitch class of `chord`, `shimmer` is a slow random detune per
* partial. `root shift` moves the classification root by semitones when the
* estimate picked the wrong octave or a harmonic (the value shows the note it
* lands on); `harm tol` is how far a partial may miss an integer ratio and
* still count (detuned unisons want 5-8 %). The residual is untouched by all
* of them, so they only bite on tonal material (see the browser badges). A tones clip gets the same knobs
* on an additive bank of 24 harmonics (`bank` level, 0 = the old tone), so
* a synth bed that has run out of range can be replaced by a tone with the
* same treatment. Everything needs the fundamental to be right: check the
* panel readout, R pins it.
*
* BENCH (H): the regulator palette view, for the Harmonic Regulator's
* sound (docs/HARMONIC-REGULATOR.md). The timeline gives way to a list of
* LAYERS: clips with no position that all sound at once while the bench
* plays, looping for ever; a layer marked one-shot instead fires on the
* lock or unlock event (right-click its row). SIGNALS from the machine
* (arm ratios / reach, radiance, consonance, tension, drive, coherence,
* score) drive layer params through BINDS: `bind tension synth1 drive` moves
* synth1's drive over its marked range as tension goes 0..1; `rel` binds add
* to the layer's own value (pitch). Right-click a slider to mark the RANGE
* that sounded good (with a note), or to bind a signal to it. A SPELL is a
* `spell <id>` section of the family file holding the same layer ids at
* their lock values (plus one-shots): as its score rises past 0.55 every
* shared param blends from its searching value to the spell's, and layers
* only the spell has fade in. Bound params are smoothed (~30 ms) so signals
* never zipper. The regulator panel (J) docks the signal sliders (the
* scrubber), the family picker, a score slider per spell with lock / unlock
* buttons, and the bind tables: a table per bench (the palette's, then one
* per spell) with an `on` box per bind and one per signal, so a bind or a
* whole signal can be switched off and compared live instead of deleted
* (`off` on the bind line); a filter box, sortable columns, and ctrl+C /
* ctrl+V to carry bind lines between tables, layers and families. The
* signal sliders and the spell rows fold. A FAMILY is one file, regulator/<family>.sfx:
* the palette, then one section per spell. Edits to the loaded family
* autosave to its working copy, regulator/.working/<family>.sfx, which
* reopens with the family until S saves it into the family file (shift+S
* saves as a new family, shift+O reverts to the saved file); a scratch
* bench autosaves to bench.sfx and S saves it as a family. The `new spell` button adds a spell.
* shift+H sends a timeline clip over as a layer; a layer's menu copies it back.
*
* MARKERS: K drops a named marker at the playhead (shift+K removes the
* nearest; right-click one in the ruler to delete). Clip drags snap to
* markers, so: step to the frame where the cue happens, K, drag the clip.
*
* LIBRARY: bank any clip you've dialed in for reuse across projects.
* B (or the "+ lib" button) save the selected clip to the library under a
* chosen name (library.sfx — same
* format as projects; clip lines can even be
* hand-copied between the two)
* Q (or the "library" button) browse the library: click an entry to drop
* a copy at the playhead; "manage" renames or
* deletes entries
*
* KEYS
* 1..9, 0 add palette clip at the playhead on the selected track
* SPACE play/pause ENTER rewind L loop
* P preview the selected clip solo
* E export the mix as .wav or .ogg, with mono (Minecraft
* positional sounds must be mono), normalize and trim-tail
* options. The folder and options are remembered in
* lab.cfg — point it at the mod's sounds dir once
* V / M attach a video / toggle the monitor window
* shift+M hear the mix in mono, as the game plays it (one positional source)
* [ ] step the playhead one video frame (50 ms without a video)
* K add a marker at the playhead (shift+K removes the nearest)
* W import a sample file (wav/aiff, or mp3/ogg/video via ffmpeg)
* C convert the selected sample clip to a choir clip, or back
* S save: stamp the timeline into a named .sfx. The working
* timeline itself always autosaves to project.sfx, so you name
* a sound once it exists — not up front. Re-stamping the same
* name doesn't re-confirm; a new name asks before overwriting.
* O open a .sfx into the workspace (ctrl+Z brings back what was
* on the timeline before)
* N clear the workspace (undoable)
* H bench view (the regulator palette); shift+H sends the selected clip there
* J regulator panel: signal sliders, signature, lock / unlock, binds, ranges, notes
* U the machine: conduct the regulator as the game does (hold left = crescendo, right = diminuendo,
* middle or T = toggle, on the stage) and hear the bench answer
*
* FILE FORMATS: .sfx is the lab's editable source (the workspace autosaves
* to project.sfx; S stamps named copies); exported .wav is what the mod
* consumes. Family files (regulator/<family>.sfx) use the same line style
* with `layer`, `range`, `bind`, `note` and `spell` lines, which the
* timeline parser skips. parseProject/renderWav are static and headless, so the mod's
* build can also batch-render .sfx files via --render, or embed this class
* and synthesize at runtime.
* DEL delete clip D duplicate arrows nudge / change track
* X split the selected clip at the playhead
* ctrl+Z undo ctrl+shift+Z / ctrl+Y redo
* G toggle snap-to-grid (shift-drag inverts it while held)
* , / . browse combos.txt entries I insert combo as clips
* ctrl+wheel zoom wheel scroll
*
* Run: ./sfxlab from the checkout (it compiles runtime/ into .build/runtime.jar when a source
* there changed, then runs this file against it). The folder holding SfxLab.java
* is the workspace (samples/, projects/, forge/, renders/, lab.cfg live in it).
* $SFXLAB_DIR overrides that; ~/synthlab is the fallback when run elsewhere.
* Headless render: ./sfxlab --render [project.sfx] [out.wav|out.ogg] [--mono] [--normalize] [--no-trim] [--key N]
* Headless check: ./sfxlab --check [family ...] format problems in the family files (exit 1 if any)
* Headless bake: ./sfxlab --bake [family ...] [--out dir] [--quality 0-10] what the mod ships: forge/baked/
* Headless forge: ./sfxlab --forge <sound.ogg|project.sfx> [--name n] [--root C2] [--register nearest|0|1|2] [--keys 0,2,4,...] [--wav] [--stereo]
*/
public class SfxLab extends JPanel {
/** The workspace: $SFXLAB_DIR if set; else the current directory when it is
* a checkout (SfxLab.java or lab.cfg beside it); else ~/synthlab. samples/,
* projects/, forge/, renders/ and lab.cfg all live inside it. */
static final Path DIR = workspaceDir();
static Path workspaceDir() {
String env = System.getenv("SFXLAB_DIR");
if (env != null && !env.isBlank()) return Paths.get(env).toAbsolutePath().normalize();
Path cwd = Paths.get("").toAbsolutePath();
if (Files.exists(cwd.resolve("SfxLab.java")) || Files.exists(cwd.resolve("lab.cfg"))) return cwd;
return Paths.get(System.getProperty("user.home"), "synthlab");
}
static final Path PROJECT_FILE = DIR.resolve("project.sfx");
static final Path PROJECTS_DIR = DIR.resolve("projects"); // named .sfx stamps (S) and the open dialog (O)
static final Path COMBO_FILE = DIR.resolve("combos.txt");
static final Path LIB_FILE = DIR.resolve("library.sfx");
/** Per-file analysis badges {f0, sines share, seconds} for a folder, computed
* on one low-priority thread (analysis only — nothing is kept in PARTS, so
* a big library doesn't pin its residuals in memory) and cached on disk. */
static class BadgeCache {
static final java.util.concurrent.ExecutorService SCAN = java.util.concurrent.Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r, "badge-scan"); t.setDaemon(true); t.setPriority(Thread.MIN_PRIORITY); return t; });
final Path root;
final Map<String, double[]> map = new java.util.concurrent.ConcurrentHashMap<>();
volatile int gen;
BadgeCache(Path root) { this.root = root; load(); }
Path file() { return FORGE_DIR.resolve(".badges-" + Integer.toHexString(root.toString().hashCode()) + ".txt"); }
void load() {
try {
if (!Files.exists(file())) return;
for (String l : Files.readAllLines(file())) {
String[] t = l.split("\\t");
if (t.length < 4) continue;
Path f = root.resolve(t[0]);
if (Files.exists(f) && Files.size(f) == Long.parseLong(t[1]))
map.put(t[0], new double[]{Double.parseDouble(t[2]), Double.parseDouble(t[3]), t.length > 4 ? Double.parseDouble(t[4]) : 0});
}
} catch (Exception e) { /* badges are a convenience */ }
}
void save() {
try {
Files.createDirectories(FORGE_DIR);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, double[]> e : map.entrySet()) {
Path f = root.resolve(e.getKey());
if (Files.exists(f)) sb.append(String.format(Locale.ROOT, "%s\t%d\t%.3f\t%.4f\t%.3f%n", e.getKey(), Files.size(f), e.getValue()[0], e.getValue()[1], e.getValue()[2]));
}
Files.writeString(file(), sb.toString());
} catch (IOException e) { /* ditto */ }
}
/** Analyses whatever isn't cached yet; onProgress runs on the EDT after each file. */
void scan(List<String> files, Runnable onProgress) {
int g = ++gen;
SCAN.submit(() -> {
for (String f : files) {
if (g != gen) return; // a rescan superseded us
if (map.containsKey(f)) continue;
try {
Partials pa = analyzePartials(root.resolve(f).toString(), 14, 46);
map.put(f, new double[]{pa.f0, pa.share, pa.res[0].length / (double) SR});
} catch (Exception e) { map.put(f, new double[]{0, 0, 0}); }
SwingUtilities.invokeLater(onProgress);
}
save();
});
}
/** One-off analysis for a file (cached afterwards). */
double[] get(String f) {
double[] b = map.get(f);
if (b == null) {
try { Partials pa = analyzePartials(root.resolve(f).toString(), 14, 46); b = new double[]{pa.f0, pa.share, pa.res[0].length / (double) SR}; }
catch (Exception e) { b = new double[]{0, 0, 0}; }
map.put(f, b);
}
return b;
}
}
/** List cell: "share% note dur path", coloured by how tonal the file is. */
static Component badgeCell(String v, double[] b, Font font, boolean sel) {
String tag = b == null ? "" : String.format(Locale.ROOT, "%3.0f%% %-4s %4.1fs", 100 * b[1], b[0] > 0 ? noteName(b[0]).split(" ")[0] : "-", b[2]);
JLabel l = new JLabel(String.format("%-16s %s", tag, v));
l.setFont(font); l.setOpaque(true);
l.setBackground(sel ? new Color(60, 70, 90) : Color.BLACK);
l.setForeground(b == null ? Color.GRAY : b[1] >= 0.5 ? new Color(90, 255, 190) : b[1] >= 0.2 ? new Color(255, 215, 120) : new Color(170, 170, 170));
return l;
}
/** Plays a wav through javax.sound, stopping `prev` first; returns the new clip. */
static javax.sound.sampled.Clip playWav(Path wav, javax.sound.sampled.Clip prev) throws Exception {
if (prev != null) { try { prev.stop(); prev.close(); } catch (Exception ignored) {} }
AudioInputStream in = AudioSystem.getAudioInputStream(wav.toFile());
javax.sound.sampled.Clip c = AudioSystem.getClip();
c.open(in);
c.start();
return c;
}
/** The sample browser, docked on the right of the workbench (A toggles it):
* a filtered list of everything under samples with tonal badges,
* preview, and one-click adds to the timeline. */
static class SampleBrowser extends JPanel {
final SfxLab lab;
final DefaultListModel<String> model = new DefaultListModel<>();
final List<String> all = new ArrayList<>();
final JList<String> list = new JList<>(model);
final JTextField filter = new JTextField();
final JLabel countL = new JLabel();
final JCheckBox autoB = new JCheckBox("play on select", false);
BadgeCache badges = new BadgeCache(SAMPLE_DIR);
javax.sound.sampled.Clip player;
final java.util.concurrent.ExecutorService bg = java.util.concurrent.Executors.newSingleThreadExecutor(r -> { Thread t = new Thread(r, "browser-bg"); t.setDaemon(true); return t; });
SampleBrowser(SfxLab lab) {
this.lab = lab;
setLayout(new BorderLayout(4, 4));
setPreferredSize(new Dimension(400, 100));
setBackground(Color.BLACK);
JPanel top = new JPanel(new BorderLayout(4, 2));
top.add(new JLabel("samples/ "), BorderLayout.WEST);
top.add(filter, BorderLayout.CENTER);
top.add(countL, BorderLayout.EAST);
add(top, BorderLayout.NORTH);
list.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
list.setBackground(Color.BLACK);
list.setCellRenderer((jl, v, i, sel, foc) -> badgeCell(v, badges.map.get(v), list.getFont(), sel));
list.addListSelectionListener(e -> { if (!e.getValueIsAdjusting() && autoB.isSelected()) preview(); });
list.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) addSample(); } });
list.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "back");
list.getActionMap().put("back", new AbstractAction() { public void actionPerformed(ActionEvent e) { if (lab.swapTarget != null) { lab.swapTarget = null; lab.toast("swap cancelled"); } lab.requestFocusInWindow(); } });
list.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "add");
list.getActionMap().put("add", new AbstractAction() { public void actionPerformed(ActionEvent e) { addSample(); } });
list.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "play");
list.getActionMap().put("play", new AbstractAction() { public void actionPerformed(ActionEvent e) { preview(); } });
add(new JScrollPane(list), BorderLayout.CENTER);
filter.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
public void insertUpdate(javax.swing.event.DocumentEvent e) { refilter(); }
public void removeUpdate(javax.swing.event.DocumentEvent e) { refilter(); }
public void changedUpdate(javax.swing.event.DocumentEvent e) { refilter(); }
});
filter.addActionListener(e -> { list.requestFocusInWindow(); if (model.size() > 0 && list.getSelectedIndex() < 0) list.setSelectedIndex(0); });
JPanel bottom = new JPanel(new GridLayout(0, 1, 2, 2));
JPanel row1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 0)), row2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 0));
for (Object[] b : new Object[][]{{"▶ preview", (Runnable) this::preview}, {"stop", (Runnable) this::stop}, {"add at playhead", (Runnable) this::addSample},
{"add as partials", (Runnable) this::addPartials}}) {
JButton jb = new JButton((String) b[0]);
jb.addActionListener(e -> ((Runnable) b[1]).run());
(b[0].equals("▶ preview") || b[0].equals("stop") ? row1 : row2).add(jb);
}
row1.add(autoB);
JButton imp = new JButton("import file… (W)"), rescan = new JButton("rescan");
imp.addActionListener(e -> { lab.importSample(); rescan(); });
rescan.addActionListener(e -> rescan());
row2.add(imp); row1.add(rescan);
bottom.add(row1); bottom.add(row2);
bottom.add(new JLabel(" dbl-click / ENTER: add · SPACE: play · ESC: back to timeline"));
add(bottom, BorderLayout.SOUTH);
rescan();
}
void rescan() {
all.clear();
try (var st = Files.walk(SAMPLE_DIR)) {
st.filter(Files::isRegularFile).map(f -> SAMPLE_DIR.relativize(f).toString().replace('\\', '/'))
.filter(f -> !f.startsWith(".") && !f.contains("/.") && f.matches("(?i).*\\.(wav|ogg|mp3|flac|aiff?|m4a|au)$"))
.sorted(Comparator.comparing((String f) -> f.startsWith("_")).thenComparing(f -> f)).forEach(all::add);
} catch (IOException e) { lab.toast("samples scan failed: " + e); }
badges = new BadgeCache(SAMPLE_DIR);
refilter();
}
void refilter() {
String q = filter.getText().trim().toLowerCase(Locale.ROOT);
model.clear();
for (String f : all) if (q.isEmpty() || f.toLowerCase(Locale.ROOT).contains(q)) model.addElement(f);
countL.setText(model.size() + (q.isEmpty() ? " files" : " / " + all.size()));
// badge the files on screen first, then the rest of the library
List<String> order = new ArrayList<>();
for (int i = 0; i < model.size(); i++) order.add(model.get(i));
for (String f : all) if (!order.contains(f)) order.add(f);
badges.scan(order, list::repaint);
}
String sel() { return list.getSelectedValue(); }
void preview() {
String f = sel();
if (f == null) return;
bg.submit(() -> { try { player = playWav(decodedPath(samplePath(f)), player); } catch (Exception e) { lab.toast("preview failed: " + e); } });
}
void stop() { if (player != null) { try { player.stop(); player.close(); } catch (Exception ignored) {} player = null; } }
void addSample() {
String f = sel();
if (f == null) return;
Clip t = lab.swapTarget;
if (t != null) { // armed from a bench row's menu: this pick replaces that layer's recording
lab.swapTarget = null;
if (lab.benchOn && lab.bench.layers.contains(t)) { lab.swapSource(t, f); lab.requestFocusInWindow(); return; }
lab.toast("the layer to swap is gone; " + f + " added instead");
}
lab.pushUndo("");
Clip c = lab.addSampleClip(f, lab.selTrack, lab.playPos);
if (c != null) { lab.toast(f + " on track " + (lab.selTrack + 1)); lab.requestFocusInWindow(); }
}
/** Adds the file as a partials clip tuned to the nearest octave of the root. */
void addPartials() {
String f = sel();
if (f == null) return;
bg.submit(() -> {
double[] b = badges.get(f);
double tune = 0;
if (b[0] > 0) {
double target = lab.rootHz * Math.pow(2, Math.round(Math.log(b[0] / lab.rootHz) / Math.log(2)));
tune = 12 * Math.log(target / b[0]) / Math.log(2);
}
double t = tune;
SwingUtilities.invokeLater(() -> {
try {
double dur = sample(f)[0].length / (double) SR;
if (!lab.benchOn) lab.pushUndo("");
Clip c = new Clip(forgeName(Paths.get(f).getFileName().toString()), PARTIALS, lab.selTrack, lab.playPos, dur, lab.uiRng.nextLong());
c.file = f; c.p[P_PITCH] = t; c.p[P_ATT] = 0.005; c.p[P_REL] = 0.05;
if (lab.benchOn) c.p[NCOMMON + PA_LOOP] = 1;
lab.place(c);
partials(c, false);
lab.toast(String.format(Locale.ROOT, "%s as partials %s, tuned %+.2f st%s", f, lab.benchOn ? "layer " + c.id : "on track " + (lab.selTrack + 1), t, b[0] > 0 ? " -> " + noteName(b[0] * Math.pow(2, t / 12)) : " (no clear pitch)"));
lab.requestFocusInWindow();
} catch (Exception e) { lab.toast("add failed: " + e); }
});
});
}
}
/** The forge window: a read-only mirror of the mod's sounds folder on the
* left, the selected sound's analysis in the middle, the promotion form
* on the right. Everything it does goes through forge() and is staged in
* forge/<name>/. */
static class Forge extends JPanel {
final SfxLab lab;
JFrame frame;
final DefaultListModel<String> model = new DefaultListModel<>();
final List<String> all = new ArrayList<>();
final JList<String> list = new JList<>(model);
final JTextField filter = new JTextField(), rootF, keysF, nsF, epF, ppF;
final JLabel folderL = new JLabel();
final JComboBox<String> regBox = new JComboBox<>(new String[]{"nearest octave", "root (C2)", "+1 oct (C3)", "+2 oct (C4)", "+3 oct (C5)"});
final JCheckBox oggB = new JCheckBox("ogg", true), monoB = new JCheckBox("mono", true);
final JSpinner keySpin = new JSpinner(new SpinnerNumberModel(7, -24, 36, 1));
final JTextArea log = new JTextArea(8, 30);
final JButton runB = new JButton("Run promotion"), openB = new JButton("open in workbench"), tlB = new JButton("promote current timeline…");
BadgeCache badges;
final java.util.concurrent.ExecutorService bg = java.util.concurrent.Executors.newSingleThreadExecutor(r -> { Thread t = new Thread(r, "forge-bg"); t.setDaemon(true); return t; });
String selected; // rel path in the mirror
Partials selPa; BufferedImage selImg; double selDur;
javax.sound.sampled.Clip player;
final JPanel card = new JPanel() {
@Override protected void paintComponent(Graphics g0) {
super.paintComponent(g0);
Graphics2D g = (Graphics2D) g0;
g.setColor(Color.BLACK); g.fillRect(0, 0, getWidth(), getHeight());
g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 13));
if (selected == null) { g.setColor(Color.GRAY); g.drawString("select a sound on the left, or promote the current timeline", 14, 24); return; }
g.setColor(new Color(245, 235, 215));
g.drawString(selected, 14, 24);
g.setColor(Color.GRAY);
if (selPa == null) g.drawString("analysing…", 14, 44);
else {
double f0 = selPa.f0;
g.drawString(String.format(Locale.ROOT, "%.2f s %d partials %.0f%% sines fundamental %s%s", selDur, selPa.nTracks, 100 * selPa.share,
f0 > 0 ? String.format(Locale.ROOT, "~%.1f Hz = %s", f0, noteName(f0)) : "none",
f0 > 0 && selPa.share < 0.2 ? " (faint)" : ""), 14, 44);
double tune = tuneFor(selPa.f0);
if (f0 > 0) g.drawString(String.format(Locale.ROOT, "promotion tunes it %+.2f st -> %s, then bakes %d keys", tune, noteName(f0 * Math.pow(2, tune / 12)), keys().length), 14, 62);
}
if (selImg != null) {
int w = getWidth() - 28, h = Math.max(60, getHeight() - 90);
g.drawImage(selImg, 14, 76, w, h, null);
g.setColor(new Color(255, 255, 255, 90));
g.drawString("0–6 kHz, cyan = tracked partials", 18, 76 + h - 6);
}
}
};
Forge(SfxLab lab) {
this.lab = lab;
badges = new BadgeCache(lab.mirrorRoot());
setLayout(new BorderLayout(6, 6));
setBackground(Color.BLACK);
// left: mirror
JPanel left = new JPanel(new BorderLayout(4, 4));
JPanel top = new JPanel(new BorderLayout(4, 4));
JButton folderB = new JButton("folder…");
folderB.addActionListener(e -> pickFolder());
top.add(folderL, BorderLayout.CENTER); top.add(folderB, BorderLayout.EAST);
top.add(filter, BorderLayout.SOUTH);
left.add(top, BorderLayout.NORTH);
list.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
list.setCellRenderer((jl, v, i, sel, foc) -> badgeCell(v, badges.map.get(v), list.getFont(), sel));
list.setBackground(Color.BLACK);
list.addListSelectionListener(e -> { if (!e.getValueIsAdjusting()) select(list.getSelectedValue()); });
left.add(new JScrollPane(list), BorderLayout.CENTER);
left.add(tlB, BorderLayout.SOUTH);
left.setPreferredSize(new Dimension(360, 100));
filter.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
public void insertUpdate(javax.swing.event.DocumentEvent e) { refilter(); }
public void removeUpdate(javax.swing.event.DocumentEvent e) { refilter(); }
public void changedUpdate(javax.swing.event.DocumentEvent e) { refilter(); }
});
add(left, BorderLayout.WEST);
// middle: card + preview buttons
JPanel mid = new JPanel(new BorderLayout(4, 4));
mid.add(card, BorderLayout.CENTER);
JPanel play = new JPanel(new FlowLayout(FlowLayout.LEFT, 6, 4));
for (String[] b : new String[][]{{"play original", "orig"}, {"sines", "sines"}, {"residual", "resid"}, {"stop", "stop"}}) {
JButton jb = new JButton(b[0]);
jb.addActionListener(e -> preview(b[1]));
play.add(jb);
}
JButton atKey = new JButton("at key");
atKey.addActionListener(e -> preview("key"));
play.add(atKey); play.add(keySpin); play.add(new JLabel("st"));
openB.addActionListener(e -> openInWorkbench());
mid.add(play, BorderLayout.SOUTH);
add(mid, BorderLayout.CENTER);
// right: form + log
JPanel form = new JPanel(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.insets = new Insets(2, 4, 2, 4); gc.anchor = GridBagConstraints.WEST; gc.fill = GridBagConstraints.HORIZONTAL;
rootF = new JTextField(noteName(lab.rootHz).split(" ")[0], 8);
keysF = new JTextField(Arrays.toString(FORGE_KEYS).replaceAll("[\\[\\] ]", ""), 22);
nsF = new JTextField("bubbys_world", 12); epF = new JTextField("spell_", 8); ppF = new JTextField("spells/", 8);
int row = 0;
for (Object[] r : new Object[][]{{"root", rootF}, {"register", regBox}, {"keys (st above root)", keysF},
{"namespace", nsF}, {"event prefix", epF}, {"path prefix", ppF}}) {
gc.gridx = 0; gc.gridy = row; gc.weightx = 0; form.add(new JLabel((String) r[0]), gc);
gc.gridx = 1; gc.weightx = 1; form.add((Component) r[1], gc);
row++;
}
JPanel fmt = new JPanel(new FlowLayout(FlowLayout.LEFT, 4, 0)); fmt.add(oggB); fmt.add(monoB);
gc.gridx = 0; gc.gridy = row; form.add(new JLabel("format"), gc); gc.gridx = 1; form.add(fmt, gc); row++;
gc.gridx = 0; gc.gridy = row; gc.gridwidth = 2; form.add(runB, gc); row++;
gc.gridx = 0; gc.gridy = row; gc.gridwidth = 2; form.add(openB, gc); row++;
runB.addActionListener(e -> run());
JPanel right = new JPanel(new BorderLayout(4, 4));
right.add(form, BorderLayout.NORTH);
log.setEditable(false); log.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 11));
log.setBackground(Color.BLACK); log.setForeground(new Color(180, 180, 180));
right.add(new JScrollPane(log), BorderLayout.CENTER);
right.setPreferredSize(new Dimension(400, 100));
add(right, BorderLayout.EAST);
tlB.addActionListener(e -> promoteTimeline());
rootF.addActionListener(e -> card.repaint());
regBox.addActionListener(e -> card.repaint());
rescan();
}
void open() {
if (frame == null) {
frame = new JFrame("SfxLab forge — promote sounds for the mod");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.add(this);
frame.setSize(1400, 720);
frame.setLocationRelativeTo(lab);
}
frame.setVisible(true);
frame.toFront();
}
void logLine(String s) { SwingUtilities.invokeLater(() -> { log.append(s + "\n"); log.setCaretPosition(log.getDocument().getLength()); }); }
double rootHz() { double r = parseNote(rootF.getText()); return Double.isNaN(r) || r <= 0 ? lab.rootHz : r; }
Integer register() { int i = regBox.getSelectedIndex(); return i == 0 ? null : i - 1; }
double tuneFor(double f0) {
if (f0 <= 0) return 0;
double root = rootHz();
Integer reg = register();
double target = reg == null ? root * Math.pow(2, Math.round(Math.log(f0 / root) / Math.log(2))) : root * Math.pow(2, reg);
return 12 * Math.log(target / f0) / Math.log(2);
}
int[] keys() {
try { return Arrays.stream(keysF.getText().split("[,\\s]+")).filter(x -> !x.isEmpty()).mapToInt(x -> Integer.parseInt(x.trim())).toArray(); }
catch (NumberFormatException e) { logLine("bad key list, using the default pentatonic set"); return FORGE_KEYS; }
}
void pickFolder() {
JFileChooser fc = new JFileChooser(lab.mirrorRoot().toFile());
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { lab.forgeMirror = fc.getSelectedFile().toString(); lab.saveCfg(); rescan(); }
}
void rescan() {
all.clear();
if (lab.forgeMirror.isBlank()) {
badges = new BadgeCache(FORGE_DIR);
folderL.setText("(no folder yet)");
folderL.setToolTipText("folder… picks the mod's sounds directory; remembered in lab.cfg as forge_mirror");
logLine("no mirror folder set — folder… to pick the mod's sounds directory");
refilter();
return;
}
Path root = lab.mirrorRoot();
badges = new BadgeCache(root);
folderL.setText(root.getFileName() == null ? root.toString() : ".../" + root.getParent().getFileName() + "/" + root.getFileName());
folderL.setToolTipText(root.toString());
if (Files.isDirectory(root)) {
try (var st = Files.walk(root)) {
st.filter(Files::isRegularFile).map(f -> root.relativize(f).toString().replace('\\', '/'))
.filter(f -> f.matches("(?i).*\\.(wav|ogg|mp3|flac|aiff?|m4a)$"))
.sorted(Comparator.comparing((String f) -> f.startsWith("_")).thenComparing(f -> f)).forEach(all::add); // _originals & co. last
} catch (IOException e) { logLine("scan failed: " + e); }
} else logLine("mirror folder not found: " + root + " (folder… to pick one)");
refilter();
badges.scan(new ArrayList<>(all), list::repaint);
}
void refilter() {
String q = filter.getText().trim().toLowerCase(Locale.ROOT);
model.clear();
for (String f : all) if (q.isEmpty() || f.toLowerCase(Locale.ROOT).contains(q)) model.addElement(f);
}
void select(String rel) {
selected = rel; selPa = null; selImg = null;
card.repaint();
if (rel == null) return;
Path f = lab.mirrorRoot().resolve(rel);
bg.submit(() -> {
try {
Partials pa = partials(f.toString(), 14, 46, true);
BufferedImage img = spectrogram(f.toString(), pa, 900, 300);
SwingUtilities.invokeLater(() -> { if (rel.equals(selected)) { selPa = pa; selImg = img; selDur = pa.res[0].length / (double) SR; card.repaint(); } });
} catch (Exception e) { logLine("analysis failed: " + e); }
});
}
void stopPlayer() { if (player != null) { try { player.stop(); player.close(); } catch (Exception ignored) {} player = null; } }
void playFile(Path wav) throws Exception { player = playWav(wav, player); }
/** Renders the selected sound's model (or the file itself) to a temp wav and plays it. */
void preview(String what) {
if (what.equals("stop")) { stopPlayer(); return; }
if (selected == null || (selPa == null && !what.equals("orig"))) return;
Path f = lab.mirrorRoot().resolve(selected);
bg.submit(() -> {
try {
if (what.equals("orig")) { playFile(decodedPath(f)); return; }
double tune = tuneFor(selPa.f0), key = what.equals("key") ? ((Number) keySpin.getValue()).doubleValue() : 0;
Clip c = forgeClip("preview", f.toString(), selDur, tune, what.equals("resid") ? 0 : 1, what.equals("sines") ? 0 : 1);
double[] tv = new double[TRACKS]; Arrays.fill(tv, 1.0);
Path tmp = Files.createTempFile("forge-preview-", ".wav");
tmp.toFile().deleteOnExit();
renderFile(List.of(c), tv, new boolean[TRACKS], tmp, false, false, false, true, key);
playFile(tmp);
logLine(String.format(Locale.ROOT, "preview %s%s", what, what.equals("key") ? String.format(Locale.ROOT, " %+.0f st (%s)", key, noteName(rootHz() * Math.pow(2, key / 12))) : ""));
} catch (Exception e) { logLine("preview failed: " + e); }
});
}
void run() {
if (selected == null) { logLine("select a sound first"); return; }
Path f = lab.mirrorRoot().resolve(selected);
runOn(f, forgeName(f.getFileName().toString()));
}
void runOn(Path src, String name) {
runB.setEnabled(false);
double root = rootHz(); Integer reg = register(); int[] keys = keys();
boolean ogg = oggB.isSelected(), mono = monoB.isSelected();
String ns = nsF.getText().trim(), ep = epF.getText().trim(), pp = ppF.getText().trim();
logLine("== promoting " + src.getFileName() + " as \"" + name + "\" (root " + noteName(root) + ")");
bg.submit(() -> {
try { forge(src, name, root, reg, keys, ogg, mono, ns, ep, pp, this::logLine); }
catch (Exception e) { logLine("FAILED: " + e); }
finally { SwingUtilities.invokeLater(() -> runB.setEnabled(true)); }
});
}
/** Renders what is on the workbench right now and promotes that. */
void promoteTimeline() {
String def = lab.lastStampName != null ? forgeName(lab.lastStampName) : "";
String name = (String) JOptionPane.showInputDialog(this, "Promote the current timeline as (folder name under forge/):",
"Promote timeline", JOptionPane.PLAIN_MESSAGE, null, null, def);
if (name == null || name.trim().isEmpty()) return;
try {
Files.createDirectories(FORGE_DIR);
Path tmp = FORGE_DIR.resolve(forgeName(name) + ".timeline.sfx");
Files.writeString(tmp, lab.projectText());
runOn(tmp, forgeName(name));
} catch (IOException e) { logLine("couldn't stage the timeline: " + e); }
}
void openInWorkbench() {
if (selected == null) return;
Path f = lab.mirrorRoot().resolve(selected);
double tune = selPa != null ? tuneFor(selPa.f0) : 0;
lab.importAsPartials(f, forgeName(f.getFileName().toString()), tune);
Window w = SwingUtilities.getWindowAncestor(lab);
if (w != null) w.toFront();
lab.requestFocusInWindow();
}
}
// ---- palette: presets built from the primitives above. The old gestures
// live here now — as starting points, not hardcoded behavior.
record Pal(String label, int type, double dur, int[] pi, double[] pv) {}
static final Pal[] PALETTE = {
new Pal("cloud", CLOUD, 4.0, new int[]{P_PITCH}, new double[]{-12}),
new Pal("tones", TONE, 2.0, new int[]{P_PITCH}, new double[]{12}),
new Pal("sub", TONE, 2.0, new int[]{P_PITCH, NCOMMON, NCOMMON + 1}, new double[]{-12, 0, 2}),
new Pal("noise", NOISE, 1.5, new int[]{P_LEVEL, P_CUT}, new double[]{0.5, 0.7}),
new Pal("riser", TONE, 1.8, new int[]{P_ATT, P_REL, P_PSWP, NCOMMON + 1, NCOMMON + 2},
new double[]{1.4, 0.3, 36, 1, 0.6}),
new Pal("downer", TONE, 1.4, new int[]{P_REL, P_PITCH, P_PSWP, NCOMMON, NCOMMON + 1, tailIdx(TONE, 5)},
new double[]{0.8, 32, -30, -12, 1.3, 0.3}),
new Pal("zap", TONE, 0.3, new int[]{P_LEVEL, P_ATT, P_REL, P_PITCH, P_PSWP, NCOMMON + 1, tailIdx(TONE, 5)},
new double[]{0.9, 0.005, 0.25, 52, -54, 0.25, 0.5}),
new Pal("woosh", NOISE, 1.2, new int[]{P_LEVEL, P_ATT, P_REL, P_CUT, P_CSWP, P_RES, P_MODE, P_PAN, P_PANSWP},
new double[]{0.7, 0.45, 0.5, 0.28, 0.5, 0.6, 1, -0.7, 1.4}),
new Pal("sparkle", SPARKLE, 1.6, new int[]{P_PITCH, P_REL}, new double[]{39, 0.5}),
new Pal("pluck", PLUCK, 1.2, new int[]{P_PITCH, P_REL}, new double[]{-5, 0.15}),
new Pal("arc", ARC, 1.5, new int[]{P_LEVEL, P_PITCH, P_REL}, new double[]{0.5, 15, 0.2}),
};
Clip fromPal(Pal pal, int track, double start) {
Clip c = new Clip(pal.label(), pal.type(), track, start, pal.dur(), uiRng.nextLong());
for (int k = 0; k < pal.pi().length; k++) c.p[pal.pi()[k]] = pal.pv()[k];
return c;
}
// ---- sample store: files from samples/, decoded once to
// stereo floats at engine rate. A failed load caches as silence.
static final Path SAMPLE_DIR = DIR.resolve("samples");
static final java.util.concurrent.ConcurrentHashMap<String, Path> SAMPLE_PATHS = new java.util.concurrent.ConcurrentHashMap<>();
/** samples/<name> — or, when that file has since been sorted into another
* subfolder, the first file under samples/ with the same basename, so old
* projects keep resolving after the library is reorganised. */
static Path samplePath(String name) {
Path p = SAMPLE_DIR.resolve(name);
if (Files.exists(p)) return p;
return SAMPLE_PATHS.computeIfAbsent(name, nm -> {
String base = Paths.get(nm).getFileName().toString();
try (var st = Files.walk(SAMPLE_DIR, FileVisitOption.FOLLOW_LINKS)) { // samples/ may itself be a link (the test workspace)
Optional<Path> hit = st.filter(f -> Files.isRegularFile(f) && f.getFileName().toString().equals(base)
&& !SAMPLE_DIR.relativize(f).toString().startsWith(".decoded")).sorted().findFirst();
if (hit.isPresent()) {
System.err.println("sample " + nm + " not found; using " + SAMPLE_DIR.relativize(hit.get()));
return hit.get();
}
} catch (IOException e) { /* no samples/ at all: fall through to the plain path */ }
return p;
});
}
/** The GUI's sample loader (installed into sfxlab.runtime.Samples): samples/<name>, decoded by javax.sound
* (ffmpeg first for anything but PCM), linearly resampled to the engine rate. */
static float[][] decodeSample(String nm) throws Exception {
AudioInputStream in = AudioSystem.getAudioInputStream(decodedPath(samplePath(nm)).toFile());
AudioFormat f = in.getFormat();
AudioFormat target = new AudioFormat(f.getSampleRate(), 16, 2, true, false);
byte[] data = AudioSystem.getAudioInputStream(target, in).readAllBytes();
int frames = data.length / 4;
double ratio = f.getSampleRate() / (double) SR;
int outN = Math.max(1, (int) (frames / ratio));
float[][] out = new float[2][outN];
for (int i = 0; i < outN; i++) {
double sp = i * ratio;
int j = Math.min(frames - 1, (int) sp);
int j2 = Math.min(frames - 1, j + 1);
double fr = sp - (int) sp;
for (int ch = 0; ch < 2; ch++) {
double a = ((short) ((data[j * 4 + ch * 2] & 0xff) | (data[j * 4 + ch * 2 + 1] << 8))) / 32768.0;
double b = ((short) ((data[j2 * 4 + ch * 2] & 0xff) | (data[j2 * 4 + ch * 2 + 1] << 8))) / 32768.0;
out[ch][i] = (float) (a * (1 - fr) + b * fr);
}
}
return out;
}
static {
Samples.loader = SfxLab::decodeSample;
Partials.source = SfxLab::analyzeCached;
}
static boolean isPcmName(String nm) {
String l = nm.toLowerCase(Locale.ROOT);
return l.endsWith(".wav") || l.endsWith(".aif") || l.endsWith(".aiff") || l.endsWith(".au");
}
/** Anything javax.sound can't read (mp3, ogg, a video's audio track…) is
* decoded by ffmpeg into samples/.decoded/, one wav per source file: named by its folder as well as its name
* (two folders may hold a file of the same name), and decoded again whenever the source is newer than the
* copy. (Until 2026-10-01 the copy was named by the file name alone and never refreshed: a recording replaced
* under its old name went on playing as the old one.) */
static Path decodedPath(Path f) throws IOException, InterruptedException {
if (isPcmName(f.getFileName().toString())) return f;
Path abs = f.toAbsolutePath();
String tag = abs.getParent() == null ? ""
: abs.startsWith(SAMPLE_DIR) ? SAMPLE_DIR.relativize(abs.getParent()).toString().replaceAll("[^A-Za-z0-9_.-]+", "__") + "__"
: Integer.toHexString(abs.getParent().toString().hashCode()) + "-"; // outside samples/
Path out = SAMPLE_DIR.resolve(".decoded").resolve(tag + f.getFileName() + ".wav");
if (!Files.exists(out) || Files.getLastModifiedTime(out).compareTo(Files.getLastModifiedTime(f)) < 0) {
Files.createDirectories(out.getParent());
run("ffmpeg", "-v", "error", "-y", "-i", f.toString(), "-vn", "-ac", "2", "-ar", String.valueOf(SR),
"-c:a", "pcm_s16le", out.toString());
}
return out;
}
// ---- ffmpeg/ffprobe: the lab shells out for anything beyond PCM wav
static Boolean ffmpegOk;
static boolean haveFfmpeg() {
if (ffmpegOk == null) { try { run("ffmpeg", "-version"); ffmpegOk = true; } catch (Exception e) { ffmpegOk = false; } }
return ffmpegOk;
}
static String run(String... cmd) throws IOException, InterruptedException {
Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
String out = new String(p.getInputStream().readAllBytes());
int rc = p.waitFor();
if (rc != 0) throw new IOException(cmd[0] + " failed (" + rc + "): " + out.trim());
return out;
}
/** Peak envelope per PBIN samples (both channels), for drawing clip waveforms. */
static final int PBIN = 128;
static final java.util.concurrent.ConcurrentHashMap<String, float[]> PEAKS = new java.util.concurrent.ConcurrentHashMap<>();
static float[] peaks(String name) {
return PEAKS.computeIfAbsent(name, nm -> {
float[][] s = sample(nm);
float[] pk = new float[(s[0].length + PBIN - 1) / PBIN];
for (int i = 0; i < s[0].length; i++) {
float a = Math.max(Math.abs(s[0][i]), Math.abs(s[1][i]));
if (a > pk[i / PBIN]) pk[i / PBIN] = a;
}
return pk;
});
}
// Analyses are kept on disk (forge/.parts/, git-ignored), keyed by file, thresholds, size and mtime:
// a 20 s recording takes ~2 s to analyse and ~7 MB to store, and a palette has half a dozen of them.
static final Path PARTS_DIR = DIR.resolve("forge").resolve(".parts");
static Path partsCachePath(String file, double fl, double ml) {
try {
Path f = samplePath(file);
// "|2": analyses made before the decoded copies were kept fresh (see decodedPath) may be of the wrong audio
String key = file + "|" + Math.round(fl) + "|" + Math.round(ml) + "|" + Files.size(f) + "|" + Files.getLastModifiedTime(f).toMillis() + "|2";
String base = f.getFileName().toString().replaceAll("[^A-Za-z0-9_.-]", "_");
return PARTS_DIR.resolve(String.format("%08x-%s.parts", key.hashCode(), base));
} catch (Exception e) { return null; }
}
static Partials analyzeCached(String file, double fl, double ml) {
Path cp = partsCachePath(file, fl, ml);
if (cp != null && Files.exists(cp)) { try { return loadPartials(cp); } catch (Exception e) { System.err.println("partials cache read failed: " + e); } }
Partials pa = analyzePartials(file, fl, ml);
if (cp != null) { try { savePartials(cp, pa); } catch (Exception e) { System.err.println("partials cache write failed: " + e); } }
return pa;
}
static void savePartials(Path cp, Partials pa) throws IOException {
Files.createDirectories(cp.getParent());
Path tmp = cp.resolveSibling(cp.getFileName() + ".tmp");
try (DataOutputStream o = new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(tmp), 1 << 16))) { Partials.write(o, pa); }
Files.move(tmp, cp, StandardCopyOption.REPLACE_EXISTING);
}
static Partials loadPartials(Path cp) throws IOException {
try (DataInputStream in = new DataInputStream(new BufferedInputStream(Files.newInputStream(cp), 1 << 16))) { return Partials.read(in); }
}
// ---- FORGE: promotes a finished sound (a recording, or a workbench project
// rendered first) into a root-tuned, keyed package for the mod, staged in
// forge/<name>/ and never touching the source. Output:
// <name>_residual.ogg, <name>_sines.ogg the two halves of the model
// <name>_k00.ogg … one per key (semitones above the root; kn05 = -5)
// <name>.partials.json the partial tracks + tuning, for a runtime resynth
// <name>.sounds.json a sounds.json fragment, ready to paste
// <name>.png spectrogram with the tracked partials
static final Path FORGE_DIR = DIR.resolve("forge");
static final int[] FORGE_KEYS = {0, 2, 4, 7, 9, 12, 14, 16, 19, 21, 24, 26, 28, 31, 33}; // major pentatonic, 3 octaves
static String forgeName(String s) {
String n = s.replaceFirst("\\.[^.]+$", "").toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9_]", "_");
return n.isEmpty() ? "sound" : n;
}
static String keyTag(int k) { return (k < 0 ? "kn" : "k") + String.format(Locale.ROOT, "%02d", Math.abs(k)); }
record ForgeOut(Path dir, double f0, double share, double tune, int nTracks, double dur, List<String> files) {}
/** The whole promotion. register: null = nearest octave of the root, else octaves above the root. */
static ForgeOut forge(Path src, String name, double rootHz, Integer register, int[] keys, boolean ogg, boolean mono,
String ns, String eventPrefix, String pathPrefix, java.util.function.Consumer<String> log) throws Exception {
Path dir = FORGE_DIR.resolve(name);
Files.createDirectories(dir);
Path audio = src.toAbsolutePath();
if (src.toString().toLowerCase(Locale.ROOT).endsWith(".sfx")) {
audio = dir.resolve(name + "_source.wav");
double[] tv = new double[TRACKS]; Arrays.fill(tv, 1.0);
boolean[] mu = new boolean[TRACKS];
List<Clip> cs = parseProject(src, new boolean[1], tv, mu);
renderFile(cs, tv, mu, audio, false, false, false, true, 0);
log.accept("rendered project " + src.getFileName() + " -> " + audio.getFileName());
}
String af = audio.toString();
Partials pa = partials(af, 14, 46, true);
double dur = pa.res[0].length / (double) SR;
log.accept(String.format(Locale.ROOT, "analysed: %d partials, %.0f%% sines, %.2f s", pa.nTracks, 100 * pa.share, dur));
double tune = 0;
if (pa.f0 > 0) {
double target = register == null ? rootHz * Math.pow(2, Math.round(Math.log(pa.f0 / rootHz) / Math.log(2)))
: rootHz * Math.pow(2, register);
tune = Math.max(COMMON[P_PITCH].min(), Math.min(COMMON[P_PITCH].max(), 12 * Math.log(target / pa.f0) / Math.log(2)));
log.accept(String.format(Locale.ROOT, "fundamental ~%.1f Hz (%s) -> %s: tune %+.2f st%s", pa.f0, noteName(pa.f0),
noteName(pa.f0 * Math.pow(2, tune / 12)), tune,
pa.share < 0.2 ? " (faint pitch: " + Math.round(100 * pa.share) + "% sines)" : ""));
} else log.accept("no partials found: the keyed files carry only the residual");
String ext = ogg ? ".ogg" : ".wav";
double[] tv = new double[TRACKS]; Arrays.fill(tv, 1.0);
boolean[] mu = new boolean[TRACKS];
List<String> files = new ArrayList<>();
Map<String, String> keyFiles = new LinkedHashMap<>();
for (String part : new String[]{"residual", "sines"}) {
Clip c = forgeClip(name, af, dur, tune, part.equals("sines") ? 1 : 0, part.equals("residual") ? 1 : 0);
String fn = name + "_" + part + ext;
renderFile(List.of(c), tv, mu, dir.resolve(fn), ogg, mono, false, true, 0);
files.add(fn);
}
for (int k : keys) {
String fn = name + "_" + keyTag(k) + ext;
renderFile(List.of(forgeClip(name, af, dur, tune, 1, 1)), tv, mu, dir.resolve(fn), ogg, mono, false, true, k);
files.add(fn); keyFiles.put(keyTag(k), fn);
log.accept(" " + fn + " " + noteName(rootHz * Math.pow(2, k / 12.0)));
}
writeModel(dir.resolve(name + ".partials.json"), name, af, pa, rootHz, tune, dur, keyFiles, files.get(0), files.get(1));
files.add(name + ".partials.json");
writeSoundsFragment(dir.resolve(name + ".sounds.json"), ns, eventPrefix, pathPrefix, name, keyFiles);
files.add(name + ".sounds.json");
ImageIO.write(spectrogram(af, pa, 900, 300), "png", dir.resolve(name + ".png").toFile());
files.add(name + ".png");
log.accept("done -> " + dir);
return new ForgeOut(dir, pa.f0, pa.share, tune, pa.nTracks, dur, files);
}
/** A unity-gain partials clip over the whole recording, pitch-keyed only. */