forked from ChadMcKinney/Lich.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.html
793 lines (553 loc) · 29.3 KB
/
Demo.html
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
<!--
Lich.js - JavaScript a networked audio/visual live coding language
Copyright (C) 2012-2013 Chad McKinney
http://chadmckinneyaudio.com/
LICENSE
=======
Licensed under the Simplified BSD License:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
-->
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lich.js</title>
<meta name="keywords" content="Experimental, Computer Music, Multimedia, Live Coding" />
<meta name="description" content="Lich.js is a very simple live coding audio and visual language implemented in javascript." />
<!--<link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono' rel='stylesheet' type='text/css'>-->
<link rel="stylesheet" type="text/css" href="WP-Content/LichStyle.css" media="all" />
<script type="text/javascript" src="third-party/glMatrix-0.9.5.min.js"></script>
<script type="text/javascript" src="third-party/webgl-utils.js"></script>
<script type="text/javascript" src="third-party/three.js/three.min.js"></script>
<script type="text/javascript" src="third-party/three.js/three.shaders.min.js"></script>
<script type="text/javascript" src="third-party/ace/ace.js"></script>
<!-- <script type="text/javascript" src="third-party/cycle.js"></script> -->
<!--
<script type="text/javascript" src="Compiler/Objects.js"></script>
<script type="text/javascript" src="Compiler/VM.js"></script>
<script type="text/javascript" src="Compiler/Compiler.js"></script>
<script type="text/javascript" src="third-party/AudioContextMonkeyPatch.js"></script>
<script type="text/javascript" src="Soliton.js/Soliton.js"></script>
<script type="text/javascript" src="CloudChamber.js/CloudChamber.js"></script>
<script type="text/javascript" src="CloudChamber.js/MarchingCubes.js"></script>
<script type="text/javascript" src="Parser/ParseUtility.js"></script>
<script type="text/javascript" src="Parser/Types.js"></script>
<script type="text/javascript" src="Parser/Lexeme.js"></script>
<script type="text/javascript" src="Parser/preL.js"></script>
<script type="text/javascript" src="Parser/iterL.js"></script>
<script type="text/javascript" src="Parser/parse.js"></script>
<script type="text/javascript" src="Parser/LichParser.js"></script>
<script type="text/javascript" src="Parser/LichLibraryParser.js"></script>
<script type="text/javascript" src="Library/Prelude.js"></script>
<script type="text/javascript" src="Lich.js"></script>
<script type="text/javascript" src="Networking/LichClient.js"></script>
<script type="text/javascript" src="Networking/LichChat.js"></script>
<script type="text/javascript" src="Networking/LichTerminal.js"></script>
<script type="text/javascript" src="Pieces/MountainsOfMadnessPanel.js"></script>-->
<script type="text/javascript" src="lich.min.js"></script>
<script src="socket.io/socket.io.js"></script>
<!--<script type="text/javascript" src="Pieces/MountainsOfMadness.js"></script>-->
</head>
<body>
<canvas id="canvas">
</canvas>
<div id="pageDiv">
<textarea id="chatInput" rows="2" spellcheck="true" readonly="false">Chat...</textarea>
<textarea id="connectionStatus" rows="1" spellcheck="false" readonly="true">Connected</textarea>
<div id="textdiv">
</div>
<textarea id="post" rows="6" spellcheck="false" readonly="true">Lich.js </textarea>
</div>
<script LANGUAGE="JavaScript" type="text/javascript">
compileLich();
</script>
<textarea id="demoCode" style="display:none">
{-
Welcome to Lich.js!
**** Lich.js is designed for collaboration. If nobody is in the server with you, invite a friend! ****
If you want to chat with any other players you can use alt-c to enter chat messages.
Lich.js is a new language for live coding music and graphics directly in a web browser.
This is a little demo page to give you an idea of what's possible.
The language isn't quite done yet (beta!), and there's still some bugs, but there is much fun to be had.
The syntax is similar to Haskell, but if you haven't used it, don't be scared.
If you want to get the source code or learn how to run your own server go to https://github.com/ChadMcKinney/Lich.js
When you're done playing with Lich.js please go to http://www.chadmckinneyaudio.com/lich/ and fill out the questionnaire.
thanks,
Chad McKinney
-}
-- Hacker Tip #1: Comments begin with --
-- Lets warm up with some math. Put your cursor on the next line and hold shift, then press enter or return. You should see 4 post.
2 + 2
-- Lich.js uses normal order of operations. For example, this will return 7:
1 + 2 * 3
-- The keyword let is used to declare variables at global scope. The next line defines monster as the string Cthulhu.
let monster = "Cthulhu"
-- Functions are evaluated simply by supplying arguments with spaces.
reverse monster
-- New functions can be composed by using argument names after variable declarations, like so:
let addFive x = x + 5
addFive 1
addFive 2
addFive 1337
-- One last thing before we make some noise. Let's talk about partial application.
-- Partial application is used heavily in Lich.js so it will be good to get used to it.
-- Normally a language requires you to supply all the arguments for a function for it to return a result. For example:
add 2 3 -- Evaluates to 5
-- In Haskell (and Lich) we can actually provide less than the expected number of arguments.
-- What will return is a new function expecting the rest of the arguments. For example:
add 2 -- Evaulates to (\r ->)
add -- Evaulates to (\l r ->)
-- We can assign this to variable and use it like any other function
let addTwo = add 2
addTwo 5 -- returns 7
-- This can be done with any function at all. Math operations are so common we have shortcuts
(/2) 4 -- 4 / 2 = 2
(*4) 3 -- 3 * 4 = 12
(3/) 6 -- 3 / 6 = 0.5
(4-) 2 -- 4 - 2 = 2
-- because of the haskell like syntax (-4) just means negative 4. We have to use subtract to get what we want.
(subtract 4) 2 -- 2 - 4 = -2
-- If this is a bit confusing, don't worry, as you go through the examples you'll see how they're used and how useful they are!
-------------------------------------------------------------
-- OK. You came here for music right? Let's make some noise.
let sineWave = play (sin 440)
stop sineWave -- when you've heard enough
-- Hacker Tip #2: Ctrl-Period will make all the sound stop.
-- Sometimes you accidently make zombies. Ctrl-. is your nuclear option.
-- Lich.js makes it easy to get sound going and there's a large collection of custom functions to do just that.
-- Evaluate the next line to see the collection of audio functions.
ugenList
-- Let's try another, how about something a bit more interesting?
let sawz = saw 40 >> lowpass 792 15 >> play
stop sawz
-- What's going on here? We're using the >> operator to chain different functions together.
-- The >> operator just passes the left hand side to the function on the right.
-- This is useful for audio because it makes function chains look like a signal path, like guitar pedals.
-- In the previous example we have a saw wave at 40hz fed into a lowpass filter with a cutoff of 792 hz and 15 for the res.
-- If you want to know what arguments a particular function has, just evaluate it with no arguments. For example:
lowpass
-- If you evaluate lowpass you will see this print: (\freq q input ->)
-- Ok This is fine and all but how about we make a melody?
-- First we need to make a synth definition, we'll use the operator =>
let sawSynth freq => saw freq >> lowpass (freq * 16) 1 >> perc 0 0.3 0.7
-- Synth definitions are like functions, they can take arguments and can be evaluated like this:
play (sawSynth 440) -- You can evaluate this multiple times, it will free itself because of the perc above
sawSynth 440 >> play -- These are equivelent.
-- Now lets make a melody using a Solo Stream
sawMelody ~> sawSynth 110 220 330 440 550 660 770 880
stop sawMelody
-- We can get a frequency as a degree from a scale by using the degree2Freq function
degree2Freq major 6
degree2Freq minor -2
d2f bartok 10 -- d2f is shorthand for degree2Freq.
-- Choose a scale degree in the bartok scale and play the sawSynth with it. in the Execute this several times.
sawSynth (d2f bartok (choose [-12..12])) >> play
-- to see a full list of scales evaulate the next line
scaleList
-- We can add modifiers to a sequence by using the pipe character |
-- After the pipe | we need to provide a function.
-- Here we're creating a new function from d2f minor. Partial application FTW!
sawMelody ~> sawSynth 0 1 2 3 4 5 6 7 | (d2f minor)
-- **** Reminder: Lich.js is designed for collaboration. If nobody is in the server with you, invite a friend! ****
-- This isn't a terribly interesting melody. We can change it just be redefining it
-- Evaluate the next line and you will hear the melody sequence change.
sawMelody ~> sawSynth 5 5 5 4 4 7 7 1 11 1 12 2 4 0 | (d2f minor)
stop sawMelody -- When you've heard enough
-- The ~> operator creates a new Solo Stream. The first argument is a synth definition (sawSynth from above)
-- After that we get a list of numbers. The | pipe indicates that we want to use a modifier on these values.
-- The d2f function simply translates the numbers as scale degrees into frequencies in hz.
-- If that's a bit too much, don't worry, just make your pattern look like above and you're in business.
-- This melody is a little fast, wouldn't it be nice if we could use rests in our melody?
sawMelody ~> sawSynth 5 5 5 _ 4 4 _ 7 7 _ 1 11 1 12 2 _ 4 0 _ | (d2f minor)
-- The _ is a rest, and no synth will be played on those beats. We can use [] to make faster notes:
sawMelody ~> sawSynth 0 1 2 3 [4 5] [6 7] | (d2f minor)
-- Note that there's no commas in these []. That's because we're using a special syntax, this is unique to these pattern streams.
-- How about we mix _ and []
sawMelody ~> sawSynth 0 _ 1 2 3 [_ 4] [_ 5] [6 7] | (d2f minor)
stop sawMelody -- when you've heard enough
-- How about some drums?
-- We can make some very simple drums using the => again.
-- They don't need arguments for our purpose as you'll see.
-- Evaluate these lines one at a time.
let bd => square 39 >> lowpass 1000 0 >> perc 0 1 0.2
let sn => pink 1 >> perc 0 1 0.2
let hh => violet 1 >> perc 0 0.3 (wchoose [0.8,0.2] [0.1,0.5])
-- Now we can create an Impulse Stream using the +> operator
drums +> bd hh sn hh
-- just like before we can edit them on the fly. Evaluating each line in turn will change the pattern
drums +> bd hh sn hh bd [hh hh] sn [hh sn]
drums +> [_ hh] hh
drums +> [bd bd]
-- We can use modifiers to change the durations.
-- The | (*2) makes all the durations twice as long
drums +> bd hh sn hh bd [hh hh] sn [hh sn] | (*2)
drums +> bd hh sn hh bd [hh hh] sn [hh sn] -- back to normal
-- Hey, we can bring that melody back in
sawMelody ~> sawSynth 5 5 5 _ 4 4 _ 7 7 _ [1 11] [1 12] 4 0 _ 4 0 _ | (d2f minor)
-- OK what about some cool generative melody?
sawMelody ~> sawSynth (psin 0.1 >> prange 0 10) | (d2f minor)
-- Lets change it up more
sawMelody ~> sawSynth (psin 0.2 >> prange 0 10 >> pwarp (psin 0.7)) | (d2f minor)
-- how about some bass?
-- HackerTip #3: Head phones or nicer speakers will make everything sound better
sawBass ~> sawSynth (psin 0.2 >> prange -2 -16 >> pstutter 14) | (d2f minor)
-- And some harmony?
sawHarmony ~> sawSynth (psaw 0.15 >> prange -2 5 >> pstutter 6) | (d2f minor)
-- Break it down
drums +> bd hh sn hh [hh bd] [hh hh] sn [hh [sn sn]] | (*2)
-- When you've heard enough, compile these in turn
stop drums
stop sawMelody
stop sawBass
stop sawHarmony
{-
Pattern functions
Lich.js contains several functions that begin with the letter p, such as pseq, pstutter, and pwarp.
These functions are pattern functions. These functions return lambdas that when given a beat will return a value.
Look over the code below to get a feel for what the different pattern functions can do.
Don't worry if you don't completely grok them yet. You'll get a feel for how to use them as you go.
-}
-- There are a plethora of pattern functions in Lich. Evaluate the next line to see a list
patternList
-- Each pattern function takes some number of arguments and returns a Pattern data type object.
pseq [1,2,3] -- evaluates to: Pattern { p = (\t ->) }
-- A pattern object is simply a type wrapper for a function that takes a time argument and returns a value
-- For example we can assign the previous pattern to a value and then invoke that contained function to get a value.
let ps = pseq [0,1,2]
ps::p 0 -- 0
ps::p 1 -- 1
ps::p 2 -- 2
ps::p 3 -- 0, pseq will wrap around to the beginning
ps::p 4 -- 1
-- Lets look at another pattern function, pseries
let ps = pseries 1 2 -- this pseries begins with 1 and steps by 2
ps::p 0 -- 1
ps::p 1 -- 3
ps::p 2 -- 5
ps::p 3 -- 7
-- Some pattern functions expect another pattern function as one of their arguments.
-- We call these pattern filters. For example pstutter
let ps = pseries 1 2 >> pstutter 2
ps::p 0 -- 1
ps::p 1 -- 1
ps::p 2 -- 3
ps::p 3 -- 3
ps::p 4 -- 5
ps::p 5 -- 5
-- Here's some examples of using pattern functions to generate lists of values.
-- Normally we'd use pattern functions in solo streams or impulse streams like above
-- You'll see this soon, but the next several examples are just to give you some idea of what they can do.
-- Evaulate a sequence of values
let s = pseq [pseq [0,pseq [0.5, 666]], pseq [1,10], pseq [2,20]]
foldl (\acc t -> acc ++ (s::p t)) [] [0..12]
-- Random selections from array
let sh = prand [pseq [555,666,777], 1, 2]
foldl (\acc t -> acc ++ (sh::p t)) [] [0..12]
-- weighted random selections
let ws = pwrand [0.5, 0.25, 0.25] [pseq [555,666,777], 1, 2]
foldl (\acc t -> acc ++ (ws::p t)) [] [0..12]
-- shuffle array and return a pattern that sequences it
let ps = pshuf [0..4]
foldl (\acc t -> acc ++ (ps::p t)) [] [0..12]
-- a pattern filter. Stutters the output of a pattern
let st = pstutter 3 $ pseq [0..5]
foldl (\acc t -> acc ++ (st::p t)) [] [0..12]
-- Another pattern filter. Wraps a filter within a range
let pw = pwrap 3 6 (pseq [0..7])
foldl (\acc t -> acc ++ (pw::p t)) [] [0..12]
-- Generates a range of values from an initial value and step size
let ps = pseries 2 5
foldl (\acc t -> acc ++ (ps::p t)) [] [0..12]
let ps = pwrap 0 13 $ pseries 0 2
foldl (\acc t -> acc ++ (ps::p t)) [] [0..12]
-- Generates a range of values using multiplcation, from an initial value and step size
let pg = pgeom 1 2
foldl (\acc t -> acc ++ (pg::p t)) [] [0..12]
let pg = pwrap 0 13 $ pgeom 1 2
foldl (\acc t -> acc ++ (pg::p t)) [] [0..12]
let pr = preverse $ pseq [0..4]
foldl (\acc t -> acc ++ (pr::p t)) [] [0..12]
-- Now normally you don't have to worry about using the ::p and supplying an argument
-- When using the patter functions in a pattern stream such as with ~> and +> functions this will be done auto-magically
-- The next few example use patterns to generate values in ~> streams
let si freq => sin freq >> perc 0 0.3 0.15
siner ~> si (pseq [444,555,666,777])
siner ~> si (pseq [444,555,666,777] >> pstutter 3)
siner ~> si (pseq [444,555,666,777] >> pwarp (pseq [13.5, pseq [-5.3, 1]]))
stop siner
-- We can also use patterns with +> streams
-- Here we're using pattern function to select which synth definition to use
let s => sin 444 >> pan 0 >> perc 0 0.5 0.3
let s2 => sin 666 >> pan -1 >> perc 0 0.5 0.3
let s3 => sin 777 >> pan 1 >> perc 0 0.5 0.3
siner2 +> (pseq [s2,s3,s] >> pstutter 2)
siner2 +> (pseq [s,s3,s2] >> pwarp (pseq [(1/3),(1/4)])) | (/2)
stop siner2
{-
beatPattern
You can't use a pattern function such as pseq or pstutter inside a synth definition normally.
However by using the beatPattern function we can embed a pattern function in a synth definition,
and the value according to the current beat will be returned.
For speed we can use bp instead of beatPattern.
-}
-- Every time this synth is invoked bp will evaluate pseq according to the current global beat value
-- d2f pelog will then turn this value (which is between 1 and 10) into a frequency used by he sin oscillator.
-- Hacker Tip #4: Multi-line blocks of code will be evaluated if you cursor is anywhere inside the block when you press shift-return.
let bSyn => sin freq >> pan -1 >> perc 0 1 0.25
where freq = pseq [1..10] >> bp >> d2f pelog -- Note the use of the bp function!!
beatSynthesizer +> bSyn
-- create a new synth with a different sequence
let bSyn2 => sin freq >> pan 1 >> perc 0 1 0.25
where freq = pseq [1..8] >> bp >> d2f pelog
beatSynthesizer +> bSyn bSyn bSyn bSyn2 bSyn2 bSyn2
stop beatSynthesizer
-- The beat based pattern functions using bp will update independant of the speed of the sequence.
-- This is because their values are based on the current beat number, this value is found by executing: currentBeat 0
-- If you evaluate currentBeat 0 several times you'll see the number it returns always rises according to the current tempo.
currentBeat 0
-- by using this current beat value with a pattern function we can get updated values
-- Evaluate this several times
(pseq [0..5])::p (currentBeat 0)
-- All beatPattern (bp) does is exactly that. The next line is effectivly the same as the last
pseq [0..5] >> bp
-- Evaulate the next two lines to see how this separation effects the sampling of values
beatSynthesizer +> bSyn bSyn bSyn bSyn2 bSyn2 bSyn2 | (*2) -- Slower
beatSynthesizer +> bSyn bSyn bSyn bSyn2 bSyn2 bSyn2 | (/2) -- Faster
-- More beatPattern fun
let bSyn => sin freq >> pan -1 >> perc 0 1 0.25
where
freq = psin 0.1 >> prange 0 6 >> pmul (pseq [1,2]) >> pwarp (psquare 0.2) >> bp >> d2f pelog
let bSyn2 => sin freq >> pan 1 >> perc 0 1 0.25
where
freq = psin 0.075 >> prange 0 6 >> pmul (pseq [1,2,3]) >> pwarp (psquare 0.15) >> bp >> d2f pelog
-- Sooo many notes, make it stttoooooppppp
stop beatSynthesizer
{-
pbind
Above we've shown solo streams (~>) and impulse streams (+>), but there's a third kind of stream generator, pbind.
pbind is similar to the SuperCollider Pbind and Pdef
It takes:
a pattern name (used to store in a global dictionary for updates via redefinition)
a synth definition (can be a pattern of synth definitions)
a list of arguments (each argument can be a pattern)
and a duration (can be pattern)
pbind name synthDef arguments duration
-}
let t f dur => sin (d2f slendro f) >> pan -1 >> perc 0 0.5 (dur * 1.5)
let u f dur => sin (d2f slendro f) >> pan 1 >> perc 0 0.5 (dur * 1.5)
-- pbind name synthDef arguments duration
let testp = pbind "test" t [5, dur] dur
where dur = 2
-- redefine, using patterns to make the sequencing more interesting
let testp = pbind "test" (pseq [t,u,t,t,u,u,u]) [pshuf [0..5], dur] dur
where
dur = (pseq [1/2,1/2,1])
-- even more patterning
let testp = pbind "test" syn [freqs >> pstutter 3, dur] dur
where
syn = pseq [t,u] >> pwarp (psaw 0.3 >> pmul 10)
freqs = pseries 0 7 >> pmod (pseq [1..10] >> pwarp (psaw 0.03 >> pmul 20) >> pstutter 2)
dur = pseq (map (div 1) [1..6]) >> pwarp (psaw 0.7 >> pmul 10) >> pstutter 3
stop testp
{-
Chord Progressions
-}
let mc = Chord minor 4
-- chordProgression returns a pbind that just sequences through the chords. They can be queried using ::value
let doom = chordProgression "doom" [mc [0,2,4], mc [1,3,5], mc [2,4,6], mc [3,5,7], mc [4,6,8], mc [5,7,9]] [8]
let synth freq => saw freq >> lowpass (freq * 4) 1 >> perc 0 0.6 0.5
-- We can use the progression similar to a scale, with c2f instead of d2f. c2f takes a scale and a chord degree.
-- The chord degree can be any integer including negative numbers and will scale octaves appropriately
bass ~> synth (pseries 0 (pseq [-2,5]) >> pfold -2 6) | (c2f doom)
stop bass
-- We can play block chords by using the map function in conjunction with the blockChord function.
-- Make sure the doom progression is playing!
-- Note: block chords don't work in patterns yet :(
let chordSynth freqs => map sf freqs
where
sf freq = saw freq >> lowpass (freq * 16) 0 >> perc 0 0.3 0.3
map play $ chordSynth $ blockChord doom
-- In haskell we use $ similar to using (), it's just a fast way to separate function application. The next line is the same as the last.
map play (chordSynth (blockChord doom))
{-
Buffers
-}
-- We can create an audio buffer by using the newBuffer function
let myBuf = newBuffer sampleRate -- Returns a Float32Array
-- You can use buffers with the recPlayBuf ugen. This will simultaneously record into and play from a buffer, at variable rates
let noisySynth = sin 440 >> recPlayBuf (newBuffer sampleRate) (noiseX 1) (noiseX 1 >> gain 2) >> gain 0.5 >> play
stop noisySynth
-- Even more chaotic
let noisySynth = sin (noiseL 2 >> exprange 200 2000) >> rec >> gain 0.5 >> play
where
rec = recPlayBuf (newBuffer sampleRate) (noiseL 7) (sin (noiseX 1 >> exprange 1 40) >> gain (noiseL 3 >> range 0 4))
stop noisySynth
-- You can create a buffer as a variable and have multiple synths share it for interesting effects
let myBuf = newBuffer (sampleRate * 4)
let bufSynth freq => square freq >> recPlayBuf myBuf 1 -2 >> perc 0 0.5 2
recSequence ~> bufSynth (pseq [0..10]) | (d2f koto) -- Generate a sequence of synths that share the same buffer
stop recSequence
{-
Effects and Buses
We can route audio using the auxIn and auxOut ugens
By default there are 10 global aux buses, but you can also create local buses.
-}
-- evaluate the next several lines in turn. You won't hurry any sound until you get to the drumDecimFX line below.
let ff = degree2Freq coleJI
let snare => pink 1 >> delay (1/(ff -8)) 0.1 >> perc 0.0 0.9 0.3 >> auxOut 5
let ss => violet 1 >> bandpass (ff 3) 30 >> gain 30 >> perc 0.0 0.4 0.1 >> auxOut 5
let lkick => square (ff -12) >> add (tri 40) >> perc 0 0.3 0.1 >> auxOut 5
-- This won't make sound yet because the synths are writing to bus 5
lkicker2 +> [lkick lkick] [snare _ ss snare] [ss [lkick lkick]] [_ ss snare _] | (*2)
-- Execute the next line to hear crunchy drums.
let drumDecimFX = auxIn 5 >> delay 0.125 0.2 >> crush 1 >> decimate (add 8000 (simplex 0.1 >> gain 3500)) >> gain 0.5 >> play
stop drumDecimFX
stop lkicker2
-- We can use aux buses for feedback
let s3 = auxIn 1 >> exprange 40 160 >> square >> gain 0.2 >> delay 1 0.5 >> auxThrough 1 >> gain 2 >> play
stop s3
-- We can also use a localBus so as to not collide with anything else that might use a particular auxillary bus
let s4 = feed >> exprange 40 160 >> square >> gain 0.2 >> delay 1 0.5 >> auxThrough feed >> gain 2 >> play
where feed = localBus 1 -- local bus!
stop s4
-- Filters and feed back go great together. fir is a good choice because it's more stable than an iir.
-- Stop and execute this several times to hear different harmonics
let feedSynth = sq >> add (auxIn 0 >> delay (1/6) 0) >> fil >> limiter -3 >> auxThrough 0 >> gain 0.3 >> play
where
sq = square (saw 1 >> exprange 1 20000)
fil = fir [random (-1/x) (1/x) | x <- [1..20]] -- haskell-like list comprehensions
stop feedSynth
{-
Graphics
Careful, the graphics can really bog down the editor!
The graphics library is still young and needs more development.
At the moment it's very stateful, which isn't ideal, but hey, it works! ...ish
-}
initGraphics 0 -- MUST DO THIS FIRST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
setBackground (random 0 255) (random 0 255) (random 0 255)
let s = sphere [random -100 100, random -100 100, random -10 10] 50 [random 0 255, random 0 255, random 0 255]
let c = cube [random -100 100, random -100 100, random -100 100] [25, 25, 25] [random 0 (2*pi), random 0 (2*pi), random 0 (2*pi)] [0,200,150]
setShader $ choose shaders
deleteMesh s
deleteMesh c
deleteScene
shaders
let s = sphere [random -100 100, random -100 100, random -10 10] 50 [random 0 255, random 0 255, random 0 255]
let c = cube [random -100 100, random -100 100, random -100 100] [25, 25, 25] [random 0 (2*pi), random 0 (2*pi), random 0 (2*pi)] [0,200,150]
setShaders $ map (choose) $ replicate 2 shaders
clearShaders 0
wireframe true s
wireframe true c
wireframe false s
wireframe false c
wireframeAll (choose [true, false])
move [-1, -10, -2] s
move [-1, 1, -1] c
moveAll [random -1 1, random -1 1, random -1 1]
setColor [random 0 255, random 0 255, random 0 255] c
setColor [random 0 255, random 0 255, random 0 255] s
setColorAll [random 0 255, random 0 255, random 0 255]
rotate [random -1 1, random -1 1, random -1 1] c
rotateAll [random -1 1, random -1 1, random -1 1]
linear [0, 0, 1] c
linearAll [random -1 1, random -1 1, random -1 1]
angular [random -0.1 0.1, random -0.1 0.1, random -0.1 0.1] c
angularAll [random -0.1 0.1, random -0.1 0.1, random -0.1 0.1]
setPosition [random -50 50, random -50 50, random -50 50] s
setPositionAll [random -50 50, random -50 50, random -50 50]
scale [random 0.1 1, random 0.1 1, random 0.1 1] s
scaleAll [random 0.1 1, random 0.1 1, random 0.1 10]
-- supports streaming style syntax
s >> wireframe (choose [true, false]) >> setPosition [random -10 10, random -10 10, random -10 10] >> setColor [random 0 255, random 0 255, random 0 255] >> linear [random -1 1, random -1 1, random -1 1]
deleteScene 0 -- clear out the scene
cloudMesh (random 10 100) [random 0 255, random 0 255, random 0 255]
deleteScene 0 -- clear out the scene
gaussianMesh (random 10 100) [random 0 255, random 0 255, random 0 255]
deleteScene 0 -- clear out the scene
sinMesh (random 10 100) [random 0 255, random 0 255, random 0 255]
deleteScene 0 -- clear out the scene
sinMapMesh 30 30 [random 0 255, random 0 255, random 0 255]
scaleAll [4, 4, 4]
deleteScene 0 -- clear out the scene
let n = noiseMapMesh 30 30 [random 0 255, random 0 255, random 0 255]
scale [4, 4, 4] n
angular [0.01, 0.01, 0.03] n
deleteMesh n
let g = gaussianMapMesh 30 30 [random 0 255, random 0 255, random 0 255]
scale [4, 4, 4] g
angular [0.01, 0.01, 0.03] g
deleteMesh g
let s = squareMapMesh 30 30 [random 0 255, random 0 255, random 0 255]
scale [6,6,6] s
angular [0.01, 0.01, 0.03] s
deleteMesh s
let s = sawMapMesh 30 30 [random 0 255, random 0 255, random 0 255]
scale [4, 4, 4] s
angular [0.01, 0.01, 0.03] s
deleteMesh s
let t = triMapMesh 30 30 [random 0 255, random 0 255, random 0 255]
scale [10, 10, 10] t
angular [0.01, 0.01, 0.03] t
deleteMesh t
-- Won't be able to initially see it because of culling, set the angular!
let f = flatMapMesh 30 30 [random 0 255, random 0 255, random 0 255]
scale [4, 4, 4] f
angular [0.01, 0.01, 0.03] f
deleteMesh f
deleteScene
let s = sawMapMesh 20 20 [random 0 255, random 0 255, random 0 255]
scale [7, 7, 7] s
angular [0.01, 0.01, 0.03] s
-- Generate random shader. Potentially very chaotic! Get ready to execute the line below!
spliceShader $ randomString $ random 1 5
clearShaders 0
deleteScene 0
-- Patterns are supported as well!
let s = sphere [random -100 100, random -100 100, random -10 10] 50 [random 0 255, random 0 255, random 0 255]
let c = cube [random -100 100, random -100 100, random -10 10] [50,50,50] [0.1,0,0] [random 0 255, random 0 255, random 0 255]
-- execute these two "meshSynths", then the pattern below
let meshSynth => s >> wireframe (choose [true, false]) >> po >> co >> li
where
po = setPosition [random -10 10, random -10 10, random -10 10]
co = setColor [random 0 255, random 0 255, random 0 255]
li = linear [random -1 1, random -1 1, random -1 1]
let meshSynth2 => c >> wireframe (choose [true, false]) >> po >> co >> li
where
po = setPosition [random -10 10, random -10 10, random -10 10]
co = setColor [random 0 255, random 0 255, random 0 255]
li = linear [random -1 1, random -1 1, random -1 1] >> angular [random 0 0.1, random 0 0.1, random 0 0.1]
meshSeq +> meshSynth meshSynth2 meshSynth [meshSynth2 meshSynth]
-- Shader sequencing, yay!
let shaderSynth => spliceShader $ randomString $ random 1 5
shaderSeq +> shaderSynth | (*2)
stop shaderSeq
clearShaders 0
stop meshSeq
deleteScene 0
</textarea>
<script LANGUAGE="JavaScript" type="text/javascript">
fillClientTerminal(document.getElementById("demoCode").value);
connectToWebSocketServer();
</script>
</body>
</html>