forked from nitrologic/drawline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvsynth.monkey2
1530 lines (1308 loc) · 32.9 KB
/
vsynth.monkey2
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
' synth vicious by [email protected]
'
' pitch wheel center not standard
' monkey2 release mode only
' no osc
' no modular ui
'
' osc reference https://hexler.net/docs/touchosc-controls
'#Import "audiopipe.monkey2"
#Import "<std>"
#Import "<mojo>"
#Import "<sdl2>"
#Import "<sdl2-mixer>"
#Import "<portmidi>"
#Import "<mojox>"
Using std..
Using mojo..
Using sdl2..
Using portmidi..
Using openal..
Public
Global DefaultWindowFlags:=WindowFlags.Resizable|WindowFlags.HighDPI
Global AppTitle:String="Synth Vicious 0.0.7"
Global Contact:="Latest Source=github.com/nitrologic/m2"
Global FragmentSize:=512
Global About:=AppTitle
Global Octave1:= "Sharps= W E T Y U "
Global Octave0:= "Notes=A S D F G H J K"
Global Controls:="Midi Panic=Escape,Quit=LongEscape"
Global EffectNames:=New String[]("None","Distorted")
Global RecordNames:=New String[]("Paused","Recording")
Global SustainNames:=New String[]("Up","Down")
Global OscillatorNames:=New String[]("Square","Sine","Sawtooth","Triangle","Noise","Rom","Live Mic","Sampler")
Global EnvelopeNames:=New String[]("None","Plain","Punchy","SlowOut","SlowIn")
Global ArpNames:=New String[]("None","Natural","Ascending","Descending","UpDown","Random1","Random2")
Global ProgNames:=New String[]("None","Recurse","Ascend","Descend")
Global SynthNames:=New String[]("Mono1","Poly64","MidiOut")
Global HoldNames:=New String[]("Off","On")
Global DivisorNames:=New String[]("Whole","Half","Third","Quarter","Fifth","Sixth","Seventh","Eighth")
Global DutyNames:=New String[]("1:2","3:4","1:4","7:8","1:8","5:8","3:8")
Global DutyCycle:=New Double[](0.5,0.75,0.25,0.875,0.125,0.625,0.375)
Global RepeatNames:=New String[]("1x","2x","3x","4x")
#Import "softsynth.monkey2"
Public
Global WriteAhead:=8192
Global instance:AppInstance
Global vsynth:VSynth
Class VSynth
Field audioSpec:SDL_AudioSpec
Field detune:V
Field fade:V=1.0
Field detune0:V
Field fade0:V=1.0
Field poly:Synth=New PolySynth()
Field mono:Synth=New MonoSynth()
Field root:Synth
Field effects:Chain
Field effecting:=False
Field recording:=False
Field arpeggiator:=New Arpeggiator()
Field midiSynth:MidiSynth
Field synthMode:Int
Field buffer:=New Double[FragmentSize*2]
Field detuneBuffer:=New V[FragmentSize]
Field fadeBuffer:=New V[FragmentSize]
Field overdrive:=New V[FragmentSize]
Field gain:=New V[FragmentSize]
Field wet:=New V[FragmentSize]
Field dry:=New V[FragmentSize]
Field falloff:=New V[FragmentSize]
Field scope:=New Scope()
Field position:Double
Field panSpeed:Double=22.0'/128'1024 'samples per pixel
Field panAmp:V
Field sampleBank:=New SampleBank()
Field bankPath:String
Field bankCount:int
Field samplers:=New Stack<Sampler>
Method New()
arpeggiator.SetSynth(mono)
arpeggiator.SetArpeggiation(1,0)
root=arpeggiator
effects=New Chain
effects.AddEffect(New Distortion,"Distortion")
effects.AddEffect(New Reverb,"Reverb")
For Local n:=Eachin effects.ControlNames()
Print n
Next
' samplers.Add(New LiveMicrophone)
End
Method FillAudioBuffer:Double[](samples:Int)
For Local i:=0 Until samples
buffer[i*2+0]=0
buffer[i*2+1]=0
detuneBuffer[i]=detune0+i*(detune-detune0)/samples
fadeBuffer[i]=fade0+i*(fade-fade0)/samples
overdrive[i]=20
gain[i]=1.0/20
wet[i]=1
dry[i]=1
falloff[i]=1.5
Next
detune0=detune
fade0=fade
Assert(root)
root.FillAudioBuffer(buffer,samples,detuneBuffer,fadeBuffer)
For Local sampler:=Eachin samplers
Local controls:V[][]
sampler.Mix(buffer.Data,samples,controls)
Next
If effecting
' Local overdriveGain:=New V[][](overdrive,gain)
' effect.EffectAudio(buffer.Data,samples,overdriveGain)
Local controls:=New V[][](overdrive,gain,wet,dry,falloff)
effects.EffectAudio(buffer.Data,samples,controls)
Endif
Duration+=samples
PlotScope(samples)
If recording
Record(buffer,samples)
Endif
Return buffer
End
Method ToggleEffect()
effecting=Not effecting
End
Method ToggleRecord()
recording=Not recording
End
Method PlotScope(samples:Int)
' green left channel
scope.Pen(LowSaturatedGreen)
For Local i:=0 Until samples
scope.Plot(panSpeed*i,buffer[i*2+0])
Next
' red right channel
scope.Pen(LowSaturatedRed)
For Local i:=0 Until samples
scope.Plot(panSpeed*i,panAmp*buffer[i*2+1])
Next
' advance head
position+=samples*panSpeed
' scope.Advance(Int(8*position))
scope.Advance(position) 'Int(8*position))
End
Method SetMidiOutput(portMidi:PortMidi,midiSend:Int)
midiSynth=New MidiSynth(portMidi,midiSend)
SetSynth(synthMode)
End
Method SetSustain(sustain:Bool)
root.SetSustain(sustain)
End
Method SetTone(oscillator:Int,envelope:Int)
root.SetTone(oscillator,envelope)
End
Method NoteOn(note:Note)
root.NoteOn(note)
End
Method NoteOff(note:Note)
root.NoteOff(note)
End
Method GetKeys:Keys()
Return root.GetKeys()
End
Method SetSynth(synth:Int)
synthMode=synth
Select synth
Case 0
arpeggiator.SetSynth(mono)
Case 1
arpeggiator.SetSynth(poly)
Case 2
If midiSynth arpeggiator.SetSynth(midiSynth)
End
End
Method Record(samples:V[],length:Int)
sampleBank.Record(samples,length,2)
End
Method SetBankPath(path:String,count:Int)
bankPath=path
bankCount=count
End
Method SetTempo(tempo:Tempo,divisor:Int,duty:V,rept:int)
arpeggiator.SetTempo(tempo,divisor,duty,rept)
End
' Method SetVolumeFader(vol:V)
' fade=vol
' End
Method SetState(state:JsonObject)
arpeggiator.SetState(state)
End
Method SetVolume(volume:V)
fade=volume*volume
' arpeggiator.SetVolume(volume)
End
Method SetArp(arpmode:Int,prog:Int)
arpeggiator.SetArpeggiation(arpmode,prog)
End
Method SetHold(hold:Bool)
arpeggiator.SetHold(hold)
End
Method Detune(bend:V)
detune=bend
End
Method PanScope(panx:V,pany:V)
panSpeed=(1+panx*panx)/2048.0
panAmp=1+pany*pany
End
Method ClearKeys()
root.Panic()
End
Method Command(command:SynthCommand,down:bool)
If Not down Return
' Select command
' Default
root.Command(command,down)
' End
End
End
Function FloatString:String(value:Float,dp:Int=2)
Local sign:String
Local integral:=Int(value*(Pow(10,dp)))
If integral<0
sign="-"
integral=-integral
Endif
Local a:String=integral
Local l:=dp+1-a.Length
If l>0 a="0000000000000".Slice(0,l)+a
Local r:=a.Length
Return sign+a.Slice(0,r-dp)+"."+a.Slice(r-dp,r)
End
Function Wrap:Int(value:Int,lower:Int,upper:Int)
If value<lower value=upper-1
If value>=upper value=lower
Return value
End
Class OSCHost
Const BufferSize:=1024
Field HostName:="0.0.0.0"
Field HostPort:Int
Field buffer:=New DataBuffer(BufferSize,ByteOrder.BigEndian)
Field clients:=New Map<SocketAddress,Int>
Field addr:=New SocketAddress
Field socket:Socket
Method Listen(port:Int)
HostPort=port
socket=Socket.Bind( HostName, HostPort )
If Not socket
Print "OSCHost Receiver failed to bind socket to " + HostName+":"+HostPort
Return
Endif
' socket.SetOption( "SO_REUSEADDR",1 )
Print "OSCHost receving at address "+socket.Address+" ready"
End
method PeekCString:String(offset:Int)
Local len:=0
While buffer.PeekByte(offset+len)
len+=1
Wend
Return buffer.PeekString(offset,len)
End
Method Poll()
While True
Local ready:=socket.CanReceive
If Not ready Return
Local bytes:=socket.ReceiveFrom(buffer.Data,ready,addr)
' Print "socket received "+bytes +" from client "+addr
If Not clients[addr]
Local id:=clients.Count()+1
clients[ New SocketAddress( addr ) ]=id
Print "New Client! id="+id
Endif
Local p:=0
While p<bytes
Local code:=buffer.PeekByte(p)
Select code
Case 47
Local a:=PeekCString(p+1)
Local n:=(1+a.Length+1+3)&-4
p+=n
Print "OSC Address "+a
Case 44
Local f:=PeekCString(p+1)
Local n:=(1+f.Length+1+3)&-4
p+=n
' Print "OSC Format "+f
For Local ff:=Eachin f
Select ff
Case "f"[0]
Local v:=buffer.PeekFloat(p)
p+=4
Print "f:"+v
Default
Print "OSC Format Error:"+ff
end
Next
Default
Print "OSC Parse Error code "+code
Return
End
Wend
wend
End
Method CloseAll()
socket.Close()
End
End
Class VSynthWindow Extends Window
Const MusicKeys:=New Key[]( Key.Q,Key.A,Key.W,Key.S,Key.E,Key.D, Key.F,Key.T,Key.G,Key.Y,Key.H,Key.U,Key.J, Key.K,Key.O,Key.L,Key.P,Key.Semicolon)',Key.Apostrophe )
Field keyNoteMapping:=New Map<Key,Int>
Field frame:Int
Field tick:Int
Field mousex:Int
Field mousey:Int
Field mouseMiddle:Bool
Field mouseLeft:Bool
Field mouseRight:bool
Field volume:V=0.1
Field mousebend:V
Field pitchbend:V=1.0
Field synth:Int
Field oscillator:Int=3
Field envelope:Int
Field octave:Int=4
Field sustain:Bool
Field expression:V
Field panx:Int ' scope speed
Field pany:Int ' scope zoom
Field arp:Int
Field prog:Int
Field hold:Bool
Field div:Int
Field duty:Int
Field rept:Int
Field arpstate:JsonObject
Field tempo:Tempo=92
Field pressure:Int
Field resetMidi:Int
field resetOSC:Int
Field oscHost:OSCHost
Field portMidi:PortMidi
Field midiInputs:Int
Field midiOutputs:Int
Field midiSend:Int
Field midiSendName:="None"
Field audioLatency:Int=2 '1 Shl (10+n) samples
Field applet:Applet
Field goFullscreen:Bool
Field bankPath:String
Field bankCount:int
Field oscPort:=8000
Field recording:=False
Method New(host:Applet, rect:Recti, fullscreen:bool, title:String)
Super.New(title,rect,DefaultWindowFlags)
applet=host
goFullscreen=fullscreen
Create()
End
Method New(host:Applet, title:String)
Super.New(title,800,600,DefaultWindowFlags)
applet=host
Create()
End
Method SelectBankPath()
Local path:=RequestDir("Select Bank Path",bankPath)
If path
bankPath=path
vsynth.SetBankPath(path,bankCount)
Endif
End
Method SelectOSCPort()
Local port:=mojox.RequestString("Select OSC Port Number","OSC UDP Port (1-65535)",""+oscPort)
If port.Length oscPort=Int(port)
End
Method Create()
volume=applet.DefaultNumber("synthVolume")
synth=applet.DefaultNumber("synthMode")
oscillator=applet.DefaultNumber("synthOscillator")
envelope=applet.DefaultNumber("synthEnvelope")
octave=applet.DefaultNumber("synthOctave")
tempo=applet.DefaultNumber("arpTempo")
arp=applet.DefaultNumber("arpMode")
prog=applet.DefaultNumber("arpProg")
hold=applet.DefaultNumber("arpHold")
div=applet.DefaultNumber("arpDivisor")
duty=applet.DefaultNumber("arpDuty")
rept=applet.DefaultNumber("arpRepeat")
arpstate=applet.DefaultObject("arpState")
audioLatency=applet.DefaultNumber("audioLatency",audioLatency)
oscPort=applet.DefaultNumber("oscPort",oscPort)
bankPath=Applet.prefsPath
bankPath=applet.DefaultString("bankPath",bankPath)
bankCount=applet.DefaultNumber("bankCount")
For Local i:=0 Until MusicKeys.Length
keyNoteMapping.Set(MusicKeys[i],i-1)
Next
vsynth=New VSynth()
vsynth.SetSynth(synth)
applet.OnFrame(Self)
OpenAudio()
#If __HOSTOS__<>"windows"
ResetMidi()
#Endif
ResetOSC()
ClearColor=new Color(1.0/16,1.0)
' midi mappings
MapKontrol()
MapAkai()
vsynth.SetState(arpstate)
vsynth.SetBankPath(bankPath,bankCount)
End
Method OnWindowEvent(event:WindowEvent) Override
Local host:=applet
Select event.Type
Case EventType.WindowClose
Print "OnClose"
host.OnClose()
Case EventType.WindowResized
host.OnFrame(Self)
' Print "OnSize "+Width+"x"+Height
Case EventType.WindowMoved
host.OnFrame(Self)
' Print "OnMove"
End
End
Field EscapeTime:Int
Method EscapeDown()
pitchbend=1.0
vsynth.ClearKeys()
EscapeTime=App.Millisecs
End
Method EscapeUp()
Local duration:=App.Millisecs-EscapeTime
If duration>500
applet.OnClose()
Endif
End
Field audioSpec:SDL_AudioSpec
Field buffer:=New Double[FragmentSize*2]
Field audioPipe:=AudioPipe.Create()
Method OpenAudio()
Local spec:SDL_AudioSpec
spec.freq=AudioFrequency
spec.format = AUDIO_S16
spec.channels = 2
spec.samples = FragmentSize
spec.callback = AudioPipe.Callback
spec.userdata = audioPipe.Handle()
Mix_CloseAudio()
Local error:Int = SDL_OpenAudio(Varptr spec,Varptr audioSpec)
If error
Print "error="+error+" "+String.FromCString(SDL_GetError())
Else
Print "Audio Open freq="+audioSpec.freq
AudioFrequency=audioSpec.freq
Endif
SDL_PauseAudio(0)
End
Method UpdateAudio()
While True
Local buffered:=audioPipe.writePointer-audioPipe.readPointer
If buffered>=WriteAhead Exit
Local samples:=FragmentSize
Local buffer:=vsynth.FillAudioBuffer(samples)
Local pointer:=Varptr buffer[0]
audioPipe.WriteSamples(pointer,samples*2)
Print "."
Wend
End
Method ResetOSC()
resetOSC=0
oscHost=New OSCHost()
oscHost.Listen(oscPort)
End
Method PollOSC()
If oscHost
oscHost.Poll()
Endif
end
Method ResetMidi()
resetMidi=0
if portMidi
portMidi.CloseAll()
Endif
portMidi=New PortMidi()
midiInputs=portMidi.inputDevices.Length
midiOutputs=portMidi.outputDevices.Length
Title="Found "+midiInputs+" MIDI in and "+midiOutputs+" MIDI out"
For Local i:=0 Until midiInputs
portMidi.OpenInput(i)
Next
For Local i:=0 Until midiOutputs
portMidi.OpenOutput(i)
SysEx(i)
Next
If midiSend<midiOutputs
midiSendName=portMidi.OutputName(midiSend)
Endif
End
Method SysEx(output:Int)
If volume<0 volume=0
If volume>1 volume=1
local value7:Int=127*volume
Local sysex:Int=(MidiSynth._SysEx)
' _ControllerChange)|(_VolumeController Shl 8)|(value7 Shl 16)
portMidi.SendMessage(output,sysex)
End
Method CycleMidiSend()
If midiOutputs
midiSend=(midiSend+1)Mod midiOutputs
midiSendName=portMidi.OutputName(midiSend)
vsynth.SetMidiOutput(portMidi,midiSend)
Endif
End
Method CycleAudioLatency()
audioLatency=(audioLatency+1)&3
End
method PollMidi()
Const NoteOn:=144
Const NoteOff:=128
Const Controller:=176
Const PitchWheel:=224
Const Clock:=248
Const Start:=250
Const Resume:=251
Const Fin:=252
Const Cry:=191
Const ProgramChange:=192
While portMidi and portMidi.HasEvent()
Local b:=portMidi.EventDataBytes()
Local t:=portMidi.EventTime()
Local note:=New Note(b[1],b[2])
Local word:Int=note.note+(note.velocity Shl 7)
Select b[0]
Case NoteOn
If note.velocity=0
vsynth.NoteOff(note)
Else
vsynth.NoteOn(note)
Endif
Case NoteOff
vsynth.NoteOff(note)
Case PitchWheel
pitchbend=1.0+(word-8452)/8452.0
Case Controller
OnControl(b[1],b[2])
Case ProgramChange
OnProgramChange(b[1])
Case Clock
Assert(b[1]=0 And b[2]=0)
OnMidiClock(t)
Case Start
Assert(b[1]=0 And b[2]=0)
OnMidiPlay(1,t)
Case Resume
Assert(b[1]=0 And b[2]=0)
OnMidiPlay(2,t)
Case Fin
Assert(b[1]=0 And b[2]=0)
OnMidiPlay(3,t)
' Case Cry
Default
Print b[0]+" "+b[1]+" "+b[2]+" "+b[3]
End
Wend
' portMidi.Sleep(1.0/60)
End
Field control:=New Int[128]
Field commands:=New Map<Int,SynthCommand>
Method MapAkai()
commands[83]=SynthCommand.Patch1
commands[80]=SynthCommand.Patch2
commands[81]=SynthCommand.Patch3
commands[82]=SynthCommand.Patch4
commands[84]=SynthCommand.NextProgram
commands[85]=SynthCommand.PreviousProgram
End
Method MapKontrol()
For Local i:=23 To 41
commands[i]=Cast<SynthCommand>(Int(SynthCommand.User)+i-23)
Next
commands[47]=SynthCommand.Rewind
commands[45]=SynthCommand.Play
commands[48]=SynthCommand.Forward
commands[49]=SynthCommand.Loop
commands[46]=SynthCommand.Stop
commands[44]=SynthCommand.Record
End
Method OnRecord()
recording=Not recording
If Not recording
Local path:String
Repeat
bankCount+=1
path=bankPath+"vsynth.rom"+bankCount+".wav"
Until GetFileType(path)=FileType.None
vsynth.sampleBank.Save(path)
Endif
vsynth.ToggleRecord()
End
Method OnProgramChange(index:Int)
' vsynth.Control(Patch+index)
End
Method OnControl(index:Int, value:Int)
Local f:=value/128.0
value-=64
If commands.Contains(index)
Local command:=commands[index]
Print "command="+int(command)+" value="+value
If value<0
vsynth.Command(command,False)
Else
vsynth.Command(command,True)
Endif
Return
endif
Select index
Case 2
pressure=f*256
Print "blow:"+pressure
Case 14
tempo=f*256
Case 16
ClearColor=New Color(f,ClearColor.G,ClearColor.B)
Case 17
ClearColor=New Color(ClearColor.R,f,ClearColor.B)
Case 18
ClearColor=New Color(ClearColor.R,ClearColor.G,f)
Case 3
' zoom=f/8
Case 121
Print "."
Case 123
Print "_"
Case 1
expression=4*f*f
volume=expression
Case 64
sustain=value>=0
Default
Print "OnControl:"+index+" "+value
End
End
Field midiTicks:Int
Field midiTime:Double
Field midiTempo:Double
Const ppqn:=24
Method OnMidiClock(t:Double)
If midiTicks>0
Local duration:=t-midiTime
If duration>0
Local bpm:=60.0/(duration*ppqn)
If bpm>666 bpm=666
midiTempo=(7*midiTempo+bpm)/8
' tempo=Int(midiTempo)
' Print "tempo="+Int($fffe&int(midiTempo))
tempo=$fffe&int(midiTempo)
Else
midiTempo=0
' tempo=0
Endif
Endif
midiTicks+=1
midiTime=t
End
Method OnMidiPlay(button:Int,t:Double)
Print "Play "+button
End
Field noteDownMap:=New IntMap<Bool>
Field KeyVelocity:=80
Method KeyDown(key:Key)
If keyNoteMapping.Contains(key)
KeyUp(key)
Local note:=keyNoteMapping[key]+octave*12
noteDownMap[note]=True
vsynth.NoteOn(New Note(note,KeyVelocity))
Else
Print "Unmapped keycode "+Int(key)
Endif
End
Method KeyUp(key:Key)
If keyNoteMapping.Contains(key)
For Local octave:=0 Until MaxOctave
Local note:=keyNoteMapping[key]+octave*12
If noteDownMap.Contains(note)
vsynth.NoteOff(New Note(note,0))
noteDownMap.Remove(note)
Endif
Next
Endif
End
Method UpdateSequence()
frame+=1
Local t:Int=(frame/20)
If t<>tick
Local note:=((t Shr 1)&15)*3+40
If t&1
vsynth.NoteOn(New Note(note,KeyVelocity))
Else
vsynth.NoteOff(New Note(note,0))
Endif
tick=t
Endif
' Print "tick d="+d
End
Function Limit:Int(value:Int, lo:Int, hi:Int)
If value<lo Return lo
If value>hi Return hi
Return value
End
Method OnKeyEvent( event:KeyEvent ) Override
Select event.Type
Case EventType.KeyDown
Select event.Key
Case Key.Slash
div=Wrap(div+1,0,DivisorNames.Length)
Case Key.Insert
duty=Wrap(duty+1,0,DutyNames.Length)
Case Key.Home
rept=Wrap(rept+1,0,RepeatNames.Length)
Case Key.KeyEnd
prog=Wrap(prog+1,0,ProgNames.Length)
Case Key.Minus
tempo-=1
Case Key.Equals
tempo+=1
Case Key.F1
Fullscreen = Not Fullscreen
applet.OnFrame(Self)
Case Key.F2
CycleAudioLatency()
Case Key.F3
New Fiber(SelectOSCPort)
Case Key.F4
SelectBankPath()
Case Key.F5
arp=0
Case Key.F6
arp=1
Case Key.F7
arp=2
Case Key.F8
arp=3
Case Key.F9
arp=4
Case Key.F10
If arp=5 arp=6 Else arp=5
Case Key.F11
OnRecord()
Case Key.Backspace
Title="Scanning Midi Bus, please wait."
resetMidi=1
resetOSC=1
Case Key.Tab
hold=Not hold
Case Key.Key1
oscillator=0
Case Key.Key2
oscillator=1
Case Key.Key3
oscillator=2
Case Key.Key4
oscillator=3
Case Key.Key5
oscillator=4
Case Key.Key6
oscillator=5
Case Key.Key7
oscillator=6
Case Key.Key8
oscillator=7
Case Key.Escape
EscapeDown()
Case Key.Space
vsynth.ToggleEffect()
Case Key.LeftBracket
envelope=Wrap(envelope-1,0,EnvelopeNames.Length)
Case Key.RightBracket
envelope=Wrap(envelope+1,0,EnvelopeNames.Length)
Case Key.Enter
synth=Wrap(synth+1,0,SynthNames.Length)
vsynth.SetSynth(synth)
Case Key.Comma
octave=Clamp(octave-1,0,MaxOctave)
Case Key.Period
octave=Clamp(octave+1,0,MaxOctave)
Case Key.LeftShift
sustain=True
Case Key.RightShift
sustain=Not sustain
Case Key.PageUp
volume=volume?1.20*volume Else 0.2
Case Key.PageDown
volume=0.92*volume
Case Key.Left
panx-=1
Case Key.Right
panx+=1
Case Key.Up
pany-=1
Case Key.Down
pany+=1
Default
KeyDown(event.Key)
End
Case EventType.KeyUp
Select event.Key
Case Key.LeftShift
sustain=False
Case Key.Escape
EscapeUp()
Default
KeyUp(event.Key)
End
End
End
Method OnMouseEvent( event:MouseEvent ) Override
mousex=event.Location.X
mousey=event.Location.Y
Select event.Type
Case EventType.MouseWheel
mousebend+=event.Wheel.Y/48.0
Case EventType.MouseDown
mouseLeft=event.Button&MouseButton.Left
mouseRight=event.Button&MouseButton.Right
mouseMiddle=event.Button&MouseButton.Middle
If mouseMiddle mousebend=0
' pixels.Click(mousex,mousey)
End
pitchbend=Pow(2,mousebend)
End
Method OnRender( display:Canvas ) Override
If goFullscreen
goFullscreen=False
Fullscreen=True
Endif
If resetMidi
ResetMidi()
Endif
PollMidi()
PollOSC()
App.RequestRender()
vsynth.PanScope(panx,pany)
' TODO: continous functions
vsynth.Detune(pitchbend)
vsynth.SetVolume(volume)
' vsynth.SetVolumeFader(volume)
vsynth.SetArp(arp,prog)
vsynth.SetHold(hold)
vsynth.SetSustain(sustain)
vsynth.SetTempo(tempo,1+div,DutyCycle[duty],rept)
vsynth.SetTone(oscillator,envelope)
' vsynth.UpdateAudio()
UpdateAudio()
Local text:String = About+",,"+Octave1+","+Octave0
text+=",,Octave=< >="+octave
text+=",Sustain=Shift="+SustainNames[sustain]
text+=",Volume=PageUpDown="+FloatString(volume)
text+=",PitchBend=Mouse Wheel="+FloatString(pitchbend)
text+=",,Oscillator=1-8="+OscillatorNames[oscillator]