-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlk3d_musisynth.lua
1381 lines (1079 loc) · 31.7 KB
/
lk3d_musisynth.lua
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
--[[
LK3D MusiSynth
coded by lokachop
procedural music via software synth & bytebeat
]]--
LK3D = LK3D or {}
LK3D.MusiSynth = LK3D.MusiSynth or {}
function LK3D.MusiSynth.NewInstrument(len, rate)
return {
len = len or 1,
sprate = rate or 11025,
elems = {
}
}
end
function LK3D.MusiSynth.NewWaveList()
return {}
end
function LK3D.MusiSynth.NewWave(typ_e, props)
return {
type = typ_e,
offset = props.offset or 0,
rate = props.rate or 440,
vol = props.vol or 1,
lowpass = props.lowpass,
highpass = props.highpass,
multiplicators = {},
mixers = {},
adsrs = {},
}
end
function LK3D.MusiSynth.AddADSR(to, adsr, type)
if not to.adsrs then
to.adsrs = {}
end
adsr["type"] = type or "amp"
to.adsrs[#to.adsrs + 1] = adsr
end
function LK3D.MusiSynth.AddMultiplicator(to, mult)
if not to.multiplicators then
return
end
to.multiplicators[#to.multiplicators + 1] = mult
end
function LK3D.MusiSynth.AddMixer(to, mixer)
if not to.mixers then
return
end
to.mixers[#to.mixers + 1] = mixer
end
function LK3D.MusiSynth.AddWave(to, wave)
if not to.elems then
return
end
to.elems[#to.elems + 1] = wave
end
-- http://www.vttoth.com/CMS/index.php/technical-notes/68
-- (a + b) - a * b * sign(a + b) -- found on some random reddit post
-- assumes samples are -1 to 1
local function sample_mix(vals)
local final = vals[1]
for i = 2, #vals do
local cv = vals[i]
final = (final + cv) - final * cv * (((final + cv) >= 0) and 1 or -1) -- sign
end
return final
end
local function tbl_avrg(tbl)
local sum = 0
for i = 1, #tbl do
sum = sum + tbl[i]
end
return sum / #tbl
end
local calc_rand = {}
for i = 1, 32768 do -- 32k cached random
calc_rand[i] = math.Rand(-1, 1)
end
local t_call_tbl = {
["sine"] = function(t, e, vol, dat, rate)
return math.sin((((t / dat.sprate) + e.offset) * math.pi) * rate) * vol
end,
["square"] = function(t, e, vol, dat, rate)
return math.sin((((t / dat.sprate) + e.offset) * math.pi) * (rate * 2)) > 0 and vol or -vol
end,
["triangle"] = function(t, e, vol, dat, rate)
return math.tan(math.sin((((t / dat.sprate) + e.offset) * math.pi) * (rate * 2))) * vol
end,
["saw"] = function(t, e, vol, dat, rate)
return ((((((t * rate) % dat.sprate) / (dat.sprate / 2)) + e.offset) % 2) - 1) * vol
end,
["noise"] = function(t, e, vol, dat, rate)
return calc_rand[math.floor((((t / 440) * rate) % dat.sprate) % 32768) + 1] * vol
end,
["smoothnoise"] = function(t, e, vol, dat, rate)
local curr = math.floor((((t / 440) * rate) % dat.sprate) % 32768) + 1
local vals = {}
for i = 1, 6 do
vals[#vals + 1] = calc_rand[((curr + i) % 32768) + 1]
end
return tbl_avrg(vals) * vol
end
}
function LK3D.MusiSynth.AddWaveFunc(name, call)
t_call_tbl[name] = call
end
local function calc_adsr(t, e, adsrdat)
local stage = 1 -- start attack
local adsr_dat = adsrdat
local t_ret = t
for i = 1, #adsr_dat do
local lencurr = adsr_dat[i].len
if t_ret > lencurr then
t_ret = t_ret - lencurr
stage = stage + 1
continue
else
break
end
end
local prev_adsr_info = adsr_dat[stage - 1] or (adsrdat["type"] == "amp" and {["val"] = e.vol} or {["val"] = 1})
local curr_adsr_info = adsr_dat[stage]
local adsr_delta = (t_ret / curr_adsr_info.len)
local target = curr_adsr_info["val"]
local prev = prev_adsr_info["val"]
return Lerp(adsr_delta, prev, target)
end
local function samp_full_calc(t, data, e)
local sec = t / data.sprate
local vc = e.vol
local rm = 1
if e.adsrs then
vc = 1
for _, v in ipairs(e.adsrs) do
if v["type"] == "amp" then
vc = vc * calc_adsr(sec, e, v)
end
if v["type"] == "rate" then
rm = rm * calc_adsr(sec, e, v)
end
end
end
return t_call_tbl[e.type](t, e, vc, data, e.rate * rm)
end
LK3D.MusiSynth.ExistingDatas = {}
function LK3D.MusiSynth.GenerateSoundData(data, name)
local len = data.len
local elements = data.elems
local vals = {}
local t_v = math.floor(CurTime())
local prev_samples = {}
sound.Generate("lk3d_ms_" .. name .. t_v, data.sprate, len, function(t)
vals = {}
for k, v in ipairs(elements) do
if t_call_tbl[v.type] then
local real_calc = samp_full_calc(t, data, v)
for _, v2 in ipairs(v.multiplicators) do
real_calc = real_calc * samp_full_calc(t, data, v2)
end
local tomix = {}
for _, v2 in ipairs(v.mixers) do
tomix[#tomix + 1] = samp_full_calc(t, data, v2)
end
if #tomix ~= 0 then
real_calc = sample_mix(tomix)
end
local old_real = real_calc
if (v.lowpass ~= nil) then
local lpv = v.lowpass
real_calc = (lpv * real_calc) + ((prev_samples[k] or 0) * (1 - lpv))
end
if (v.highpass ~= nil) then
local hpv = v.highpass
real_calc = (hpv * real_calc) - ((prev_samples[k] or 0) * (1 - hpv))
end
prev_samples[k] = old_real
vals[#vals + 1] = real_calc
end
end
local sm = sample_mix(vals)
return sm
end)
LK3D.MusiSynth.ExistingDatas[name] = "lk3d_ms_" .. name .. t_v
end
LK3D.MusiSynth.ExistingPitches = {}
function LK3D.MusiSynth.GenerateSoundScript(data, name, pitch)
if not LK3D.MusiSynth.ExistingDatas[name] then
LK3D.MusiSynth.GenerateSoundData(data, name)
end
pitch = math.floor(pitch * 10) / 10
LK3D.MusiSynth.ExistingPitches[name] = LK3D.MusiSynth.ExistingPitches[name] or {}
sound.Add({
name = "lk3d_musisynth_" .. name .. "_p_" .. pitch,
channel = CHAN_AUTO,
volume = 1,
pitch = pitch,
sound = LK3D.MusiSynth.ExistingDatas[name],
})
LK3D.MusiSynth.ExistingPitches[name][pitch] = true
end
LK3D.MusiSynth.ValidInstruments = {}
-- 0-255 autogen
function LK3D.MusiSynth.DeclareInstrument(data, name)
for i = 1, 255 do
LK3D.MusiSynth.GenerateSoundScript(data, name, i)
end
LK3D.New_D_Print("Declared instrument \"" .. name .. "\"", LK3D_SEVERITY_INFO, "MusiSynth")
LK3D.MusiSynth.ValidInstruments[name] = true
end
function LK3D.MusiSynth.PlayInstrument(name, pitch)
local c_pitch = math.max(math.min(math.floor(pitch * 10) / 10, 254), 1)
local id = "lk3d_musisynth_" .. name .. "_p_" .. c_pitch
--surface.PlaySound(id)
LocalPlayer():EmitSound(id, 0, 100, 1, CHAN_STATIC, SND_NOFLAGS, 0)
--sound.Play(id, Vector(0, 0, 0), 75, 100, 1)
end
local instr_digisnare = LK3D.MusiSynth.NewInstrument(.3)
local wavedsnare = LK3D.MusiSynth.NewWave("smoothnoise", {
rate = 220,
vol = 2,
--lowpass = .5,
highpass = .5
})
-- quick adsr reminder for whoever reads this
-- https://kronoslang.io/resources/code-examples/unit-generators/adsr-envelope
--
--\/attack
-- /\ <- delay
-- / \ \/sustain
-- / \___________________
-- / \ release
-- / \
LK3D.MusiSynth.AddADSR(wavedsnare, {
{len = .3, val = 0}, -- attack
{len = 0, val = 0}, -- delay
{len = 0, val = 0}, -- sustain
{len = 0, val = 0}, -- release
})
LK3D.MusiSynth.AddWave(instr_digisnare, wavedsnare)
local instr_flute = LK3D.MusiSynth.NewInstrument(1.5)
local waveflute = LK3D.MusiSynth.NewWave("sine", {
rate = 1100,
vol = 0,
--lowpass = .5,
--highpass = .5
})
LK3D.MusiSynth.AddADSR(waveflute, {
{len = .125, val = .35}, -- attack
{len = 1.375, val = 0}, -- delay
{len = 5.25, val = 0}, -- sustain
{len = .5, val = 0}, -- release
})
local wave_flute_add = LK3D.MusiSynth.NewWave("sine", {
rate = 120,
vol = 1,
--lowpass = .5,
--highpass = .5
})
LK3D.MusiSynth.AddMultiplicator(waveflute, wave_flute_add)
LK3D.MusiSynth.AddWave(instr_flute, waveflute)
local instr_4 = LK3D.MusiSynth.NewInstrument(.35)
local wave4 = LK3D.MusiSynth.NewWave("smoothnoise", {
rate = 1100,
vol = 0,
--lowpass = .5,
--highpass = .5
})
LK3D.MusiSynth.AddADSR(wave4, {
{len = .35, val = .35}, -- attack
{len = .35, val = 0}, -- delay
{len = .25, val = 0}, -- sustain
{len = .5, val = 0}, -- release
})
LK3D.MusiSynth.AddADSR(wave4, {
{len = .35, val = 1.25}, -- attack
{len = .35, val = 1}, -- delay
{len = .25, val = 1}, -- sustain
{len = .5, val = 1}, -- release
}, "rate")
LK3D.MusiSynth.AddWave(instr_4, wave4)
LK3D.MusiSynth.DeclareInstrument(instr_digisnare, "digisnare")
LK3D.MusiSynth.DeclareInstrument(instr_flute, "flute")
LK3D.MusiSynth.DeclareInstrument(instr_4, "weird")
local instr_drumsnare = LK3D.MusiSynth.NewInstrument(0.5)
local wave_ns = LK3D.MusiSynth.NewWave("smoothnoise", {
rate = 1200,
vol = 0,
lowpass = .5,
--highpass = .5
})
LK3D.MusiSynth.AddADSR(wave_ns, {
{len = .01, val = 0}, -- attack
{len = .0, val = 1}, -- delay
{len = .15, val = 0}, -- sustain
{len = .5, val = 0}, -- release
})
LK3D.MusiSynth.AddWave(instr_drumsnare, wave_ns)
LK3D.MusiSynth.DeclareInstrument(instr_drumsnare, "snare")
LK3D.MusiSynth.AddWaveFunc("hihat_long", function(t, e, vol, dat, rate)
local time = t / dat.sprate
local curr = math.floor((((t / 440) * rate) % dat.sprate) % 32768) + 1
local vals = {}
for i = 1, 6 do
vals[#vals + 1] = calc_rand[((curr + i) % 32768) + 1]
end
return tbl_avrg(vals) * math.pow(1 - ((time * 4) % 1), 4)
end)
local instr_hihat = LK3D.MusiSynth.NewInstrument(12.3, 44100)
local wave_hihat = LK3D.MusiSynth.NewWave("hihat_long", {
rate = 440,
vol = 1,
--lowpass = .5,
--highpass = .5
})
LK3D.MusiSynth.AddWave(instr_hihat, wave_hihat)
LK3D.MusiSynth.DeclareInstrument(instr_hihat, "hihat_long")
--timer.Simple(1, function()
-- LK3D.MusiSynth.PlayInstrument("sonar_test", 100)
--end)
-- gui section
LK3D.MusiSynth.Editor = {}
LK3D.MusiSynth.Editor.Bcol = Color(34, 38, 40)
LK3D.MusiSynth.Editor.BDcol = Color(28, 31, 32)
LK3D.MusiSynth.Editor.BDDcol = Color(21, 23, 24)
LK3D.MusiSynth.Editor.BElmcol = Color(30, 34, 36)
LK3D.MusiSynth.Editor.Wcol = Color(25, 27, 29)
LK3D.MusiSynth.Editor.BGcol = Color(65, 75, 85)
LK3D.MusiSynth.Editor.BGDarkcol = Color(38, 44, 49)
LK3D.MusiSynth.Editor.TBcol = Color(35, 34, 40)
LK3D.MusiSynth.Editor.CurrFile = "None"
LK3D.MusiSynth.Editor.ValidElements = {}
LK3D.MusiSynth.Editor.Elements = {}
LK3D.MusiSynth.Editor.RenderOffset = Vector(0, 0)
local iconMats = {}
local function buttonPaint(s, w, h)
local ogc = s:GetTextColor()
local col = Color(ogc.r, ogc.g, ogc.b, 255)
if s:IsDown() then
col.r = col.r * 1.5
col.g = col.g * 1.5
col.b = col.b * 1.5
end
surface.SetDrawColor(col)
surface.DrawRect(0, 0, w, h)
local icon = s:GetImage()
if not iconMats[icon] then
iconMats[icon] = Material(icon, "ignorez nocull")
end
surface.SetMaterial(iconMats[icon])
surface.SetDrawColor(255, 255, 255)
surface.DrawTexturedRect(0, 0, h, h)
col.r = col.r * 2.2
col.g = col.g * 2.2
col.b = col.b * 2.2
draw.SimpleText(s:GetText(), "BudgetLabel", h, h / 2, col, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
end
local function btn_place(e)
e:DockMargin(4, 4, 0, 4)
e:Dock(LEFT)
e:SetImageVisible(false)
e.Paint = buttonPaint
end
function LK3D.MusiSynth.Editor.MakeTopbar(tb)
function tb:Paint(w, h)
surface.SetDrawColor(LK3D.MusiSynth.Editor.TBcol)
surface.DrawRect(0, 0, w, h)
end
local btnnew = vgui.Create("DImageButton", tb)
btnnew:SetWidth(48)
btnnew:SetText("New")
btnnew:SetTextColor(Color(78, 141, 69, 0))
btnnew:SetIcon("icon16/page_add.png")
btn_place(btnnew)
function btnnew:DoClick()
LK3D.MusiSynth.Editor.CurrFile = "NewFile"
LK3D.MusiSynth.Editor.Elements = {}
LK3D.MusiSynth.Editor.RenderOffset = Vector(0, 0)
LK3D.MusiSynth.Editor.Selected = nil
end
local btnopen = vgui.Create("DImageButton", tb)
btnopen:SetWidth(48)
btnopen:SetText("Open")
btnopen:SetTextColor(Color(69, 76, 141, 0))
btnopen:SetIcon("icon16/folder_page.png")
btn_place(btnopen)
local btnsave = vgui.Create("DImageButton", tb)
btnsave:SetWidth(48)
btnsave:SetText("Save")
btnsave:SetTextColor(Color(69, 124, 141, 0))
btnsave:SetIcon("icon16/folder_add.png")
btn_place(btnsave)
end
function LK3D.MusiSynth.Editor.MakeWavePrev(wprev)
function wprev:Paint(w, h)
surface.SetDrawColor(LK3D.MusiSynth.Editor.Wcol)
surface.DrawRect(0, 0, w, h)
draw.SimpleText("Waveform Preview", "BudgetLabel", 0, 0, Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
end
end
local icon_genned = {}
local function iconGen(nm, func)
local rt = GetRenderTarget("lk3dmusi_" .. nm, 512, 256)
local ma_t = CreateMaterial("lk3dmusi_m_" .. nm, "UnlitGeneric", {
["$basetexture"] = rt:GetName(),
["$ignorez"] = 1,
["$nocull"] = 1,
["$vertexcolor"] = 1,
["$alphatest"] = 1
})
local ow, oh = ScrW(), ScrH()
render.SetViewPort(0, 0, rt:Width(), rt:Height())
cam.Start2D()
render.PushRenderTarget(rt)
render.Clear(0, 0, 0, 0)
render.OverrideAlphaWriteEnable(true, true)
func()
render.OverrideAlphaWriteEnable(false)
render.PopRenderTarget(rt)
cam.End2D()
render.SetViewPort(0, 0, ow, oh)
icon_genned[nm] = {
rt = rt,
mat = ma_t
}
end
iconGen("squrwave", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 0, 255)
local itr = ScrW() / 1
for i = 1, itr do
local d = i / itr
local mv = (i * 4) % itr
if (mv > 0) and (mv < (itr / 2)) then
surface.DrawRect(ScrW() * d, ScrH() - 32, 16, 16)
elseif mv <= 0 or (mv == (itr / 2)) then
surface.DrawRect(ScrW() * d, (ScrH() / 2) - 80, 16, 192)
else
surface.DrawRect(ScrW() * d, 32, 16, 16)
end
end
end)
iconGen("sinewave", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 0, 255)
local itr = ScrW() / 1
for i = 1, itr do
local d = i / itr
local mv = (d * 24) + 1
surface.DrawRect(ScrW() * d, (ScrH() / 2) + (math.sin(mv) * (ScrH() * .25)), 16, 16)
end
end)
iconGen("triwave", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 0, 255)
local itr = ScrW() / 1
for i = 1, itr do
local d = i / itr
local mv = (d * 24) + 1
surface.DrawRect(ScrW() * d, (ScrH() / 2) + (math.tan(math.sin(mv)) * (ScrH() * .15)), 16, 16)
end
end)
iconGen("sawwave", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 0, 255)
local itr = ScrW() / 1
for i = 1, itr do
local d = i / itr
local mv = (d * 32) + 3
surface.DrawRect(ScrW() * d, (ScrH() / 2) + (((mv % 8) - 4) * 16), 16, (mv % 8) == 0 and 128 or 16)
end
end)
iconGen("noisewave", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 0, 255)
local itr = ScrW() / 1
for i = 1, itr do
local d = i / itr
local nsd = (i / 8) % 64
local fnsd = math.floor(nsd)
local fract = nsd - fnsd
local ns_v = Lerp(fract, calc_rand[fnsd + 1], calc_rand[fnsd + 2])
surface.DrawRect(ScrW() * d, (ScrH() / 2) + (ns_v * (ScrH() * .25)), 16, 16)
end
end)
iconGen("smoothnoisewave", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 0, 255)
local itr = ScrW() / 1
for i = 1, itr do
local d = i / itr
local nsd = (i / 32) % 64
local fnsd = math.floor(nsd)
local fract = nsd - fnsd
local ns_v = Lerp(fract, calc_rand[fnsd + 1], calc_rand[fnsd + 2])
surface.DrawRect(ScrW() * d, (ScrH() / 2) + (ns_v * (ScrH() * .25)), 16, 16)
end
end)
iconGen("mod_amplify", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 100, 255)
local m = Matrix()
m:Rotate(Angle(0, 45, 0))
m:SetTranslation(Vector(ScrW() / 2, ScrH() / 2))
cam.PushModelMatrix(m)
surface.DrawRect(-16, -98, 32, 196)
surface.DrawRect(-98, -16, 196, 32)
cam.PopModelMatrix()
end)
iconGen("mod_adsr", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 100, 255)
local adsr_icon = {
{len = .125, val = .75},
{len = .25, val = .35},
{len = .5, val = .35},
{len = .125, val = 0},
}
local itr = ScrW() / 1
for i = 1, itr do
local d = i / itr
local dc = d
local adsr_st = 1
for i2 = 1, #adsr_icon do
local cu = adsr_icon[i2]
if dc > cu.len then
dc = dc - cu.len
adsr_st = adsr_st + 1
else
break
end
end
local adsr_curr = adsr_icon[adsr_st]
local adsr_prev = adsr_icon[math.max(adsr_st - 1, 1)]
local delta_curr = (dc / adsr_curr.len)
local v_curr = (Lerp(delta_curr, adsr_prev.val, adsr_curr.val) - .5)
surface.DrawRect(ScrW() * d, (ScrH() / 2) - (v_curr * 256), 16, 16)
end
end)
iconGen("mod_highpass", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 100, 255)
local itr = ScrW() / 1
for i = 1, itr do
local d = i / itr
local vc = math.log(d) * 32
surface.DrawRect(ScrW() * d, (ScrH() / 2) - vc, 16, 16)
end
end)
iconGen("mod_lowpass", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 100, 255)
local itr = ScrW() / 1
for i = 1, itr do
local d = i / itr
local vc = math.log(math.abs(1 - d)) * 32
surface.DrawRect(ScrW() * d, (ScrH() / 2) - vc, 16, 16)
end
end)
-- there's a 1 pixel imperfection, sorry
iconGen("output", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(0, 50, 100, 255)
surface.DrawRect(ScrW() / 2, 48, 16, ScrH() - 86)
local m = Matrix()
m:SetAngles(Angle(0, 45, 0))
m:SetTranslation(Vector((ScrW() / 2) - 48, (ScrH() / 2) + 16))
cam.PushModelMatrix(m)
surface.DrawRect(0, 0, 96, 16)
cam.PopModelMatrix()
m:SetAngles(Angle(0, -45, 0))
m:SetTranslation(Vector((ScrW() / 2) - 62, (ScrH() / 2) - 16))
cam.PushModelMatrix(m)
surface.DrawRect(0, 0, 96, 16)
cam.PopModelMatrix()
surface.DrawRect(172, ScrH() - 110, 32, 16)
surface.DrawRect(172, ScrH() - 150, 32, 16)
surface.DrawRect(162, ScrH() - 150, 16, 52)
local hc = ScrH() * .55
surface.DrawRect((ScrW() / 2) + 32, (ScrH() / 2) - (hc / 2) + 5, 16, hc)
hc = ScrH() * .7
surface.DrawRect((ScrW() / 2) + 64, (ScrH() / 2) - (hc / 2) + 5, 16, hc)
hc = ScrH() * .85
surface.DrawRect((ScrW() / 2) + 96, (ScrH() / 2) - (hc / 2) + 5, 16, hc)
end)
iconGen("mod_modulator", function()
render.Clear(0, 0, 0, 0)
surface.SetDrawColor(100, 50, 100, 255)
local itr = ScrW() / 1
for i = 1, itr do
local d = i / itr
local mv = (d * 8) + 1
local s1 = math.sin(mv)
local s2 = math.sin(mv * 8) * s1
surface.DrawRect(ScrW() * d, (ScrH() / 2) + (s2 * (ScrH() * .25)), 16, 16)
end
end)
local t_sort = {
["output"] = 1,
["wave"] = 2,
["mod"] = 3,
}
local typeicons = {
output = "icon16/database_go.png",
wave = "icon16/page_white_world.png",
mod = "icon16/database_edit.png"
}
function LK3D.MusiSynth.Editor.DeclareElement(name, type, data)
LK3D.MusiSynth.Editor.ValidElements[name] = data
LK3D.MusiSynth.Editor.ValidElements[name].type = type
LK3D.MusiSynth.Editor.ValidElements[name].renderOrd = t_sort[type]
table.sort(LK3D.MusiSynth.Editor.ValidElements, function(a, b)
return t_sort[a.type] > t_sort[b.type]
end)
end
LK3D.MusiSynth.Editor.DeclareElement("output", "output", {
fancyName = "Output",
icon = "output",
col = Color(0, 100, 200),
props = {
khz = 11025
},
inputs = 1,
outputs = 0,
})
--[[
LK3D.MusiSynth.Editor.DeclareElement("amp", "mod", {
fancyName = "Amplify",
icon = "mod_amplify",
col = Color(200, 100, 200),
props = {
rate = 440,
amplitude = 1,
offset = 0,
},
inputs = 1,
outputs = 1,
})
]]--
LK3D.MusiSynth.Editor.DeclareElement("modulator", "mod", {
fancyName = "Modulator",
icon = "mod_modulator",
props = {},
col = Color(200, 100, 200),
inputs = 2,
outputs = 1,
})
LK3D.MusiSynth.Editor.DeclareElement("adsr", "mod", {
fancyName = "ADSR",
icon = "mod_adsr",
props = {
attack = {len = .125, val = .75},
decay = {len = .25, val = .35},
sustain = {len = .5, val = .35},
release = {len = .125, val = 0},
},
col = Color(200, 100, 200),
inputs = 1,
outputs = 1,
})
LK3D.MusiSynth.Editor.DeclareElement("highpass", "mod", {
fancyName = "Highpass",
icon = "mod_highpass",
col = Color(200, 100, 200),
props = {
mult = .5,
},
inputs = 1,
outputs = 1,
})
LK3D.MusiSynth.Editor.DeclareElement("lowpass", "mod", {
fancyName = "Lowpass",
icon = "mod_lowpass",
col = Color(200, 100, 200),
props = {
mult = .5
},
inputs = 1,
outputs = 1,
})
LK3D.MusiSynth.Editor.DeclareElement("square", "wave", {
fancyName = "Square Wave",
icon = "squrwave",
col = Color(200, 100, 0),
props = {
rate = 440,
amplitude = 1,
offset = 0,
},
inputs = 0,
outputs = 1,
})
LK3D.MusiSynth.Editor.DeclareElement("sine", "wave", {
fancyName = "Sine Wave",
icon = "sinewave",
col = Color(200, 100, 0),
props = {
rate = 440,
amplitude = 1,
offset = 0,
},
inputs = 0,
outputs = 1,
})
LK3D.MusiSynth.Editor.DeclareElement("tri", "wave", {
fancyName = "Triangle Wave",
icon = "triwave",
col = Color(200, 100, 0),
props = {
rate = 440,
amplitude = 1,
offset = 0,
},
inputs = 0,
outputs = 1,
})
LK3D.MusiSynth.Editor.DeclareElement("saw", "wave", {
fancyName = "Saw Wave",
icon = "sawwave",
col = Color(200, 100, 0),
props = {
rate = 440,
amplitude = 1,
offset = 0,
},
inputs = 0,
outputs = 1,
})
LK3D.MusiSynth.Editor.DeclareElement("noise", "wave", {
fancyName = "Noise",
icon = "noisewave",
col = Color(200, 100, 0),
props = {
rate = 440,
amplitude = 1,
offset = 0,
},
inputs = 0,
outputs = 1,
})
LK3D.MusiSynth.Editor.DeclareElement("smoothnoise", "wave", {
fancyName = "Smooth Noise",
icon = "smoothnoisewave",
col = Color(200, 100, 0),
props = {
rate = 440,
amplitude = 1,
offset = 0,
},
inputs = 0,
outputs = 1,
})
local function r_tbl_copy(tbl)
local ntbl = {}
for k, v in pairs(tbl) do
if type(v) ~= "table" then
ntbl[k] = v
else
ntbl[k] = r_tbl_copy(v)
end
end
return ntbl
end
local function addElementToEditPanel(data, x, y)
LK3D.MusiSynth.Editor.Elements[#LK3D.MusiSynth.Editor.Elements + 1] = {
pos = Vector(x, y),
data = r_tbl_copy(data),
}
end
local corner_mat = Material("gui/corner16", "ignorez nocull")
local function drawCircleBad(x, y, w, h)
surface.SetMaterial(corner_mat)
surface.DrawTexturedRectUV(x - w, y - h, w, h, 0, 0, 1, 1)
surface.DrawTexturedRectUV(x, y - h, w, h, 1, 0, 0, 1)
surface.DrawTexturedRectUV(x - w, y, w, h, 0, 1, 1, 0)
surface.DrawTexturedRectUV(x, y, w, h, 1, 1, 0, 0)
end
local function drawPanelInCanvas(x, y, data)
local w, h = 96, 64
local col = data.col
local col_c = Color(col.r * .75, col.g * .75, col.b * .75)
surface.SetDrawColor(col_c)
surface.DrawRect(x, y, w, h)
surface.SetDrawColor(col)
surface.DrawRect(x + 2, y + 2, w - 4, h - 4)
draw.SimpleText(data.type, "BudgetLabel", x + 2, y + h - 14, col_c, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
surface.SetDrawColor(255, 255, 255, 255)
surface.SetMaterial(icon_genned[data.icon].mat)
local sm = w < h and w or h
local wc = sm * 1.5
local hc = sm * .75
surface.DrawTexturedRect(x + (w / 2) - (wc / 2), y + (h / 2) - (hc / 2), wc, hc)
local t_icon = typeicons[data.type]
if not iconMats[t_icon] then
iconMats[t_icon] = Material(t_icon, "ignorez nocull")
end
surface.SetMaterial(iconMats[t_icon])
surface.DrawTexturedRect(x, y, 16, 16)
draw.SimpleText(data.fancyName, "BudgetLabel", x + w / 2, y + 0, Color(255, 255, 255), TEXT_ALIGN_CENTER)