-
Notifications
You must be signed in to change notification settings - Fork 2
/
meetup-wk3.html
882 lines (753 loc) · 36.7 KB
/
meetup-wk3.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
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Processing, SuperCollider and Pure Data OSC communication, and supercolliderjs & processingjs</title>
<!-- 2016-10-30 Κυρ 18:02 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="generator" content="Org-mode" />
<meta name="author" content="Georgios Diapoulis" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center; }
.todo { font-family: monospace; color: red; }
.done { color: green; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right { margin-left: auto; margin-right: 0px; text-align: right; }
.left { margin-left: 0px; margin-right: auto; text-align: left; }
.center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.right { text-align: center; }
th.left { text-align: center; }
th.center { text-align: center; }
td.right { text-align: right; }
td.left { text-align: left; }
td.center { text-align: center; }
dt { font-weight: bold; }
.footpara:nth-child(2) { display: inline; }
.footpara { display: block; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<link rel="stylesheet" type="text/css"
href="http://sachachua.com/blog/wp-content/themes/sacha-v3/foundation/css/foundation.min.css"></link>
<link rel="stylesheet" type="text/css" href="http://sachachua.com/org-export.css"></link>
<link rel="stylesheet" type="text/css" href="http://sachachua.com/blog/wp-content/themes/sacha-v3/style.css"></link>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="preamble" class="status">
<a name="top" id="top"></a>
</div>
<div id="content">
<h1 class="title">Processing, SuperCollider and Pure Data OSC communication, and supercolliderjs & processingjs</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1. Processing basics</a>
<ul>
<li><a href="#sec-1-1">1.1. Simple scripts in Processing</a>
<ul>
<li><a href="#sec-1-1-1">1.1.1. Print fonts</a></li>
<li><a href="#sec-1-1-2">1.1.2. Push style</a></li>
</ul>
</li>
<li><a href="#sec-1-2">1.2. Audio library for Processing</a></li>
</ul>
</li>
<li><a href="#sec-2">2. SC3 and Processing via OSC</a>
<ul>
<li><a href="#sec-2-1">2.1. Send OSC messages from Processing to SC3</a></li>
<li><a href="#sec-2-2">2.2. Send OSC message from SC3 to Processing</a></li>
<li><a href="#sec-2-3">2.3. SuperCollider client for Processing</a></li>
</ul>
</li>
<li><a href="#sec-3">3. SC3 and Pure Data OSC communication</a></li>
<li><a href="#sec-4">4. supercolliderjs and processingjs</a>
<ul>
<li><a href="#sec-4-1">4.1. processingjs</a>
<ul>
<li><a href="#sec-4-1-1">4.1.1. Call in html</a></li>
<li><a href="#sec-4-1-2">4.1.2. Get processingjs online</a></li>
<li><a href="#sec-4-1-3">4.1.3. Processing script</a></li>
</ul>
</li>
<li><a href="#sec-4-2">4.2. supercollidejs</a>
<ul>
<li><a href="#sec-4-2-1">4.2.1. Print sclang output on your browser</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> Processing basics</h2>
<div class="outline-text-2" id="text-1">
<p>
<a href="http://processing.org/">Processing</a> is a Java-based programming environment for making visualizations. A basic script has two main structures, a <code>void setup()</code> and a <code>void draw()</code> code blocks. In the former you define initialization settings, and in the latter you run time-dependent processes. The <code>void draw()</code> loop frequency is define using the <code>frameRate()</code>. The default rate is 60fps.
</p>
<dl class="org-dl">
<dt> Processing Reference Online </dt><dd><a href="https://processing.org/reference/">https://processing.org/reference/</a>
</dd>
<dt> HINT </dt><dd>Install <a href="https://atom.io/">Atom editor</a> and install the packages <a href="https://atom.io/packages/processing">processing</a> (run sketches) and <a href="https://atom.io/packages/processing-language">processing-language</a> (syntax highlighting)
</dd>
</dl>
</div>
<div id="outline-container-sec-1-1" class="outline-3">
<h3 id="sec-1-1"><span class="section-number-3">1.1</span> Simple scripts in Processing</h3>
<div class="outline-text-3" id="text-1-1">
</div><div id="outline-container-sec-1-1-1" class="outline-4">
<h4 id="sec-1-1-1"><span class="section-number-4">1.1.1</span> Print fonts</h4>
<div class="outline-text-4" id="text-1-1-1">
<dl class="org-dl">
<dt> Link to processing script below </dt><dd><a href="./src/helloText.pde">./src/helloText.pde</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-java"><span class="org-type">void</span> <span class="org-function-name">setup</span>(){
size(400, 400); <span class="org-comment-delimiter">// </span><span class="org-comment">size of the window</span>
background(255); <span class="org-comment-delimiter">// </span><span class="org-comment">color of backroung (monochrome)</span>
<span class="org-comment-delimiter">// </span><span class="org-comment">3 arguments for RGB colors</span>
frameRate(30);
smooth(); <span class="org-comment-delimiter">// </span><span class="org-comment">apply smooth filter in graphics</span>
}
<span class="org-type">void</span> <span class="org-function-name">draw</span>(){
<span class="org-type">int</span> <span class="org-variable-name">i</span>; <span class="org-comment-delimiter">// </span><span class="org-comment">a local variable</span>
<span class="org-comment-delimiter">// </span><span class="org-comment">modulo will have effect only if u comment if condition</span>
i = frameCount%256; <span class="org-comment-delimiter">// </span><span class="org-comment">frameCount: starts from value 1 in void draw</span>
<span class="org-comment-delimiter">// </span><span class="org-comment">: has the value 0 in void setup</span>
textAlign(CENTER);
fill(i);
textSize(45);
text(<span class="org-string">"Hello World"</span>, width/2, height/2);
println(i); <span class="org-comment-delimiter">// </span><span class="org-comment">print and change line frameCount</span>
<span class="org-keyword">if</span>(i == 255){
noLoop(); <span class="org-comment-delimiter">// </span><span class="org-comment">stop when fades out</span>
}
}
</pre>
</div>
<div class="figure">
<p><img src="./img/animation.gif" alt="animation.gif" />
</p>
<p><span class="figure-number">Figure 1:</span> Output of the script above (1.1)</p>
</div>
</div>
</div>
<div id="outline-container-sec-1-1-2" class="outline-4">
<h4 id="sec-1-1-2"><span class="section-number-4">1.1.2</span> Push style</h4>
<div class="outline-text-4" id="text-1-1-2">
<p>
It is possible to load different <code>fill()</code>, <code>stroke()</code>, <code>strokeWeight()</code> and similar functions based on <a href="https://processing.org/reference/pushStyle_.html">pushStyle()</a> function.
</p>
<dl class="org-dl">
<dt> Link to processing script below </dt><dd><a href="./src/pushStyle.pde">./src/pushStyle.pde</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-java"><span class="org-type">void</span> <span class="org-function-name">setup</span>(){
size(300, 300);
background(255);
smooth();
<span class="org-comment-delimiter">// </span><span class="org-comment">frameRate(2);</span>
}
<span class="org-type">void</span> <span class="org-function-name">draw</span>(){
<span class="org-type">float</span> <span class="org-variable-name">x</span> = width/3;
<span class="org-type">float</span> <span class="org-variable-name">y</span> = height/2;
<span class="org-comment-delimiter">// </span><span class="org-comment">println(frameCount);</span>
smooth();
pushStyle();
stroke(255, 180, 23, 23); <span class="org-comment-delimiter">// </span><span class="org-comment">yellow circle style</span>
strokeWeight(30);
ellipse(x, y, y, y);
pushStyle(); <span class="org-comment-delimiter">// </span><span class="org-comment">it builds on the current style information</span>
stroke(255, 23, 32, 80); <span class="org-comment-delimiter">// </span><span class="org-comment">red circle style</span>
strokeWeight(20);
ellipse(2*x, y, y, y);
pushStyle();
stroke(0, 12, 241, 120);
strokeWeight(30);
point(x, y);
popStyle();
popStyle();
popStyle();
<span class="org-comment-delimiter">//</span><span class="org-comment">saveFrame("circle.png");</span>
}
</pre>
</div>
<div class="figure">
<p><img src="./img/pushStyle.png" alt="pushStyle.png" />
</p>
<p><span class="figure-number">Figure 2:</span> The output of the script above (1.2 Push style)</p>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-1-2" class="outline-3">
<h3 id="sec-1-2"><span class="section-number-3">1.2</span> Audio library for Processing</h3>
<div class="outline-text-3" id="text-1-2">
<p>
<a href="http://code.compartmental.net/tools/minim/">Minim</a> is a library which enables Processing to process audio information.
</p>
<dl class="org-dl">
<dt> Link to processing script below </dt><dd><a href="./src/minimInput.pde">./src/minimInput.pde</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-java"><span class="org-keyword">import</span> <span class="org-constant">ddf</span>.<span class="org-constant">minim</span>.*;
<span class="org-type">Minim</span> <span class="org-variable-name">minim</span>;
<span class="org-type">AudioInput</span> <span class="org-variable-name">in</span>;
<span class="org-type">void</span> <span class="org-function-name">setup</span>(){
size(400, 400, P3D);
frameRate(24);
minim = <span class="org-keyword">new</span> <span class="org-type">Minim</span>(<span class="org-keyword">this</span>);
minim.debugOn();
<span class="org-comment-delimiter">// </span><span class="org-comment">get a line in from Minim, default bit depth is 16</span>
in = minim.getLineIn(<span class="org-constant">Minim</span>.STEREO, 512);
}
<span class="org-type">void</span> <span class="org-function-name">draw</span>(){
background(0);
stroke(255);
smooth();
<span class="org-type">float</span> <span class="org-variable-name">x</span> = width;
<span class="org-type">float</span> <span class="org-variable-name">y</span> = height;
<span class="org-type">float</span> <span class="org-variable-name">angle</span> = 1.019;
<span class="org-type">float</span> <span class="org-variable-name">r</span> = 0.0;
translate(width/2, height/2);
r = 10.0;
<span class="org-keyword">for</span>(<span class="org-type">int</span> <span class="org-variable-name">i</span> = 0; i < in.<span class="org-type">bufferSize</span>()-1; i+=8){
<span class="org-keyword">for</span>(<span class="org-type">int</span> <span class="org-variable-name">j</span> = 1; j <= 17; j++){
<span class="org-keyword">for</span>(<span class="org-type">int</span> <span class="org-variable-name">k</span> = i; k <= j; k++){
rotate(2*PI/19.0*in.right.get(i));
stroke(255, (j-k)*angle);
strokeWeight(1.91 + abs(in.left.get(i)*402));
beginShape(LINES);
<span class="org-comment-delimiter">// </span><span class="org-comment">beginShape( TRIANGLE_FAN );</span>
vertex(in.left.get(i)*r, j*r, k*r); <span class="org-comment-delimiter">// </span><span class="org-comment">left channel</span>
vertex(in.right.get(i)*r*x/5, i*r, -j*r); <span class="org-comment-delimiter">// </span><span class="org-comment">right channel</span>
endShape();
}
}
}
}
<span class="org-type">void</span> <span class="org-function-name">stop</span>(){
<span class="org-comment-delimiter">// </span><span class="org-comment">always close Minim audio classes when you are done with them</span>
in.close();
minim.stop();
<span class="org-keyword">super</span>.stop();
}
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2"><span class="section-number-2">2</span> SC3 and Processing via OSC</h2>
<div class="outline-text-2" id="text-2">
<p>
Processing OSC communication is based on the <a href="http://www.sojamo.de/libraries/oscP5/">oscP5</a> library.
</p>
</div>
<div id="outline-container-sec-2-1" class="outline-3">
<h3 id="sec-2-1"><span class="section-number-3">2.1</span> Send OSC messages from Processing to SC3</h3>
<div class="outline-text-3" id="text-2-1">
<dl class="org-dl">
<dt> Run this script in Processing </dt><dd>see <a href="http://www.sojamo.de/libraries/oscP5/examples/oscP5sendReceive/oscP5sendReceive.pde">oscP5sendReceive</a>
</dd>
<dt> Link to processing script below </dt><dd><a href="./src/p5toSC.pde">./src/p5toSC.pde</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-java"><span class="org-keyword">import</span> <span class="org-constant">oscP5</span>.*;
<span class="org-keyword">import</span> <span class="org-constant">netP5</span>.*;
<span class="org-type">OscP5</span> <span class="org-variable-name">oscP5</span>;
<span class="org-type">NetAddress</span> <span class="org-variable-name">myRemoteLocation</span>;
<span class="org-type">void</span> <span class="org-function-name">setup</span>() {
size(400, 400);
frameRate(25);
<span class="org-comment-delimiter">/* </span><span class="org-comment">start oscP5, listening for incoming messages at port 12000 */</span>
oscP5 = <span class="org-keyword">new</span> <span class="org-type">OscP5</span>(<span class="org-keyword">this</span>, 12000);
<span class="org-comment-delimiter">/* </span><span class="org-comment">myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,</span>
<span class="org-comment"> * an ip address and a port number. myRemoteLocation is used as parameter in</span>
<span class="org-comment"> * oscP5.send() when sending osc packets to another computer, device,</span>
<span class="org-comment"> * application.</span>
<span class="org-comment"> */</span>
myRemoteLocation = <span class="org-keyword">new</span> <span class="org-type">NetAddress</span>(<span class="org-string">"127.0.0.1"</span>, 12000);
}
<span class="org-type">void</span> <span class="org-function-name">draw</span>() {
background(0);
}
<span class="org-type">void</span> <span class="org-function-name">mousePressed</span>() {
<span class="org-type">OscMessage</span> <span class="org-variable-name">myMessage</span> = <span class="org-keyword">new</span> <span class="org-type">OscMessage</span>(<span class="org-string">"/test"</span>);
myMessage.add(<span class="org-type">int</span>(random(123))); <span class="org-comment-delimiter">/* </span><span class="org-comment">add an int to the osc message */</span>
<span class="org-comment-delimiter">/* </span><span class="org-comment">send the message */</span>
oscP5.send(myMessage, myRemoteLocation);
}
</pre>
</div>
<dl class="org-dl">
<dt> Link to SC3 script below </dt><dd><a href="./src/p5toSC.scd">./src/p5toSC.scd</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-sclang"><span class="org-keyword">thisProcess</span>.openUDPPort(12000); <span class="org-comment-delimiter">// </span><span class="org-comment">open port 12000 to rcv from p5</span>
<span class="org-keyword">thisProcess</span>.openPorts; <span class="org-comment-delimiter">// </span><span class="org-comment">list all open ports</span>
o = <span class="org-type">OSCFunc</span>({|msg, time, addr, recvPort| <span class="org-constant">\matching</span>.postln; msg.postln; }, <span class="org-constant">'/test'</span>);
o.free;
</pre>
</div>
</div>
</div>
<div id="outline-container-sec-2-2" class="outline-3">
<h3 id="sec-2-2"><span class="section-number-3">2.2</span> Send OSC message from SC3 to Processing</h3>
<div class="outline-text-3" id="text-2-2">
<dl class="org-dl">
<dt> Link to processing script below </dt><dd><a href="./src/scP5.pde">./src/scP5.pde</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-java"><span class="org-keyword">import</span> <span class="org-constant">oscP5</span>.*;
<span class="org-keyword">import</span> <span class="org-constant">netP5</span>.*;
<span class="org-type">OscP5</span> <span class="org-variable-name">oscP5</span>;
<span class="org-type">float</span> <span class="org-variable-name">x</span>; <span class="org-comment-delimiter">// </span><span class="org-comment">global variable</span>
<span class="org-type">void</span> <span class="org-function-name">setup</span>() {
size(400, 300);
frameRate(24);
background(0);
smooth();
<span class="org-type">OscProperties</span> <span class="org-variable-name">properties</span> = <span class="org-keyword">new</span> <span class="org-type">OscProperties</span>();
properties.setListeningPort(47120); <span class="org-comment-delimiter">// </span><span class="org-comment">osc receive port (from sc)</span>
oscP5 = <span class="org-keyword">new</span> <span class="org-type">OscP5</span>(<span class="org-keyword">this</span>, properties);
}
<span class="org-type">void</span> <span class="org-function-name">oscEvent</span>(<span class="org-type">OscMessage</span> <span class="org-variable-name">msg</span>) {
<span class="org-keyword">if</span> (msg.checkAddrPattern(<span class="org-string">"/sc3p5"</span>)) {
x = msg.get(0).floatValue(); <span class="org-comment-delimiter">// </span><span class="org-comment">receive floats from sc</span>
}
}
<span class="org-type">void</span> <span class="org-function-name">draw</span>() {
background(x, x, x);
println(<span class="org-string">"POST: "</span>, x);
<span class="org-comment-delimiter">// </span><span class="org-comment">draw rect</span>
stroke(256-x/2, 256-x*abs(sin(x)), 256-x/4);
strokeWeight(4);
fill(256-x/2, 256-x, 256-x*abs(sin(x)));
translate(width/2, height/2);
rotate(x%64);
rect(x%64, x%64, x*abs(sin(x))%128, x*abs(sin(x))%128, 6);
<span class="org-comment-delimiter">// </span><span class="org-comment">saveFrame("./img/rect###.png");</span>
}
</pre>
</div>
<dl class="org-dl">
<dt> Link to SC3 script below </dt><dd><a href="./src/scP5.scd">./src/scP5.scd</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-sclang">s.boot;
n = <span class="org-type">NetAddr</span>(<span class="org-string">"127.0.0.1"</span>, 47120); <span class="org-comment-delimiter">// </span><span class="org-comment">open 47120 on localhost server</span>
(
<span class="org-type">SynthDef</span>(<span class="org-constant">\blip</span>, { | freq = 440, amp = 0.85, att = 0.01, rel = 0.06, ffreq = 1000 |
<span class="org-keyword">var</span> sig, env, lfo;
sig = <span class="org-type">SinOsc</span>.<span class="org-function-name">ar</span>(freq, 0, amp);
env = <span class="org-type">EnvGen</span>.<span class="org-function-name">ar</span>(<span class="org-type">Env</span>.perc(att, rel), <span class="org-variable-name">doneAction:</span>2);
lfo = <span class="org-type">SinOsc</span>.<span class="org-function-name">kr</span>(rel * ffreq);
<span class="org-type">Out</span>.<span class="org-function-name">ar</span>(0, <span class="org-type">Pan2</span>.<span class="org-function-name">ar</span>(<span class="org-type">RHPF</span>.<span class="org-function-name">ar</span>(sig*env, ffreq), <span class="org-type">SinOsc</span>.<span class="org-function-name">kr</span>(211*lfo)))
}).add;
)
<span class="org-type">Synth</span>(<span class="org-constant">\blip</span>);
(
f = fork {
<span class="org-function-name">loop</span> {
256 <span class="org-variable-name">do:</span> { |i|
n.sendMsg(<span class="org-string">"/sc3p5"</span>, i.asFloat); <span class="org-comment-delimiter">// </span><span class="org-comment">send OSC message to P5</span>
<span class="org-type">Synth</span>(<span class="org-constant">\blip</span>, [<span class="org-constant">\freq</span>, 440+i, <span class="org-constant">\ffreq</span>, 1000+i*2]);
((i+1).reciprocal*2).wait;
}
}
};
)
f.stop;
</pre>
</div>
<p>
The output of these two programs looks like the video below.
</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/xJ8jNIcciSI" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<div id="outline-container-sec-2-3" class="outline-3">
<h3 id="sec-2-3"><span class="section-number-3">2.3</span> SuperCollider client for Processing</h3>
<div class="outline-text-3" id="text-2-3">
<p>
There is a SuperCollider library (<a href="http://www.erase.net/projects/processing-sc/">processing-sc</a>) which makes possible to send message to sclang.
</p>
<dl class="org-dl">
<dt> Link to processing script below </dt><dd><a href="./src/processingSClib.pde">./src/processingSClib.pde</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-java"><span class="org-keyword">import</span> <span class="org-constant">oscP5</span>.*;
<span class="org-keyword">import</span> <span class="org-constant">netP5</span>.*;
<span class="org-keyword">import</span> <span class="org-constant">supercollider</span>.*;
<span class="org-type">Synth</span> <span class="org-variable-name">synthTri</span>;
<span class="org-type">Synth</span> <span class="org-variable-name">synthSaw</span>;
<span class="org-type">void</span> <span class="org-function-name">setup</span>(){
size(400, 400);
background(0);
synthTri = <span class="org-keyword">new</span> <span class="org-type">Synth</span>(<span class="org-string">"lftri"</span>);
synthSaw = <span class="org-keyword">new</span> <span class="org-type">Synth</span>(<span class="org-string">"lfsaw"</span>);
synthTri.set(<span class="org-string">"amp"</span>, 0.0);
synthSaw.set(<span class="org-string">"amp"</span>, 0.0);
synthTri.create();
synthSaw.create();
}
<span class="org-type">void</span> <span class="org-function-name">draw</span>(){
synthTri.set(<span class="org-string">"amp"</span>, 0.1);
synthTri.set(<span class="org-string">"freq"</span>, 200+mouseX);
synthSaw.set(<span class="org-string">"amp"</span>, 0.1);
synthSaw.set(<span class="org-string">"freq"</span>, 200+mouseY);
}
</pre>
</div>
<dl class="org-dl">
<dt> Link to SC3 script below </dt><dd><a href="./src/processingSClib.scd">./src/processingSClib.scd</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-sclang">s.waitForBoot{
<span class="org-type">SynthDef</span>(<span class="org-string">"lftri"</span>, { | freq = 400, amp = 0.1 |
<span class="org-type">Out</span>.<span class="org-function-name">ar</span>(0, <span class="org-type">FreeVerb</span>.<span class="org-function-name">ar</span>(<span class="org-type">LFTri</span>.<span class="org-function-name">ar</span>(freq, 0, amp))!2)
}).add;
<span class="org-type">SynthDef</span>(<span class="org-string">"lfsaw"</span>, { | freq = 400, amp = 0.1 |
<span class="org-type">Out</span>.<span class="org-function-name">ar</span>(0, <span class="org-type">FreeVerb</span>.<span class="org-function-name">ar</span>(<span class="org-type">LFSaw</span>.<span class="org-function-name">ar</span>(freq, 0, amp))!2)
}).add;
}
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3"><span class="section-number-2">3</span> SC3 and Pure Data OSC communication</h2>
<div class="outline-text-2" id="text-3">
<p>
In Pure Data you have to install <i>mrpeach</i> for OSC communication. In SuperCollider check the help file <a href="http://doc.sccode.org/Guides/OSC_communication.html">OSC communication</a>.
</p>
<dl class="org-dl">
<dt> Link to SC3 script below </dt><dd><a href="./src/sc3pd-osc.scd">./src/sc3pd-osc.scd</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-sclang"><span class="org-comment-delimiter">// </span><span class="org-comment">SENDING TO PD</span>
b = <span class="org-type">NetAddr</span>.new(<span class="org-string">"127.0.0.1"</span>, 6000); <span class="org-comment-delimiter">// </span><span class="org-comment">address and port to send to pd</span>
b.sendMsg(<span class="org-string">"/hello"</span>, <span class="org-constant">\asymbol</span>); <span class="org-comment-delimiter">// </span><span class="org-comment">send to path \hello</span>
b.sendMsg(<span class="org-string">"/there"</span>, *[<span class="org-string">"pack it"</span>, 10.rand]); <span class="org-comment-delimiter">// </span><span class="org-comment">use asterisk (*) to send an array</span>
<span class="org-comment-delimiter">// </span><span class="org-comment">RECEIVING FROM PD</span>
<span class="org-keyword">thisProcess</span>.openUDPPort(9000);
<span class="org-keyword">thisProcess</span>.openPorts;
<span class="org-comment-delimiter">// </span><span class="org-comment">receiving messages from pd in the path '/test' -- no port is needed</span>
o = <span class="org-type">OSCFunc</span>({ <span class="org-keyword">arg</span> msg, time, addr, recvPort; [msg, time, addr, recvPort].postln; }, <span class="org-constant">'/test'</span>); <span class="org-comment-delimiter">// </span><span class="org-comment">create OSC responder</span>
o.free;
</pre>
</div>
<dl class="org-dl">
<dt> Link to pd patch </dt><dd><a href="./src/sc3pd-osc.pd">./src/sc3pd-osc.pd</a>
</dd>
</dl>
<div class="figure">
<p><img src="./img/pd-osc.png" alt="pd-osc.png" />
</p>
<p><span class="figure-number">Figure 3:</span> Pure Data patch</p>
</div>
</div>
</div>
<div id="outline-container-sec-4" class="outline-2">
<h2 id="sec-4"><span class="section-number-2">4</span> supercolliderjs and processingjs</h2>
<div class="outline-text-2" id="text-4">
</div><div id="outline-container-sec-4-1" class="outline-3">
<h3 id="sec-4-1"><span class="section-number-3">4.1</span> processingjs</h3>
<div class="outline-text-3" id="text-4-1">
<p>
You can take the advantage of javascript to implement Processing sketches on the web.
</p>
<dl class="org-dl">
<dt> Quickstart </dt><dd><a href="http://processingjs.org/articles/jsQuickStart.html">http://processingjs.org/articles/jsQuickStart.html</a>
</dd>
<dt> Download </dt><dd><a href="http://processingjs.org/download/">http://processingjs.org/download/</a>
</dd>
</dl>
<p>
You need 3 parts to run a processing script on the web:
</p>
<ul class="org-ul">
<li>An html file, for convenience assume an <code>index.html</code>
</li>
<li>A processing script
</li>
<li>The <a href="https://raw.githubusercontent.com/processing-js/processing-js/v1.4.8/processing.min.js">processing.min.js</a>
</li>
</ul>
<p>
In the directory that we have the <code>index.js</code> we create a <code>src/</code> folder which contains our <code>*.pde</code> sketch. The processingjs code is retrieved from the web.
</p>
</div>
<div id="outline-container-sec-4-1-1" class="outline-4">
<h4 id="sec-4-1-1"><span class="section-number-4">4.1.1</span> Call in html</h4>
<div class="outline-text-4" id="text-4-1-1">
<pre class="example">
<!DOCTYPE html>
<html>
<head>
<title>Hello Web - Processing.js Test</title>
<script src="https://raw.githubusercontent.com/processing-js/processing-js/v1.4.8/processing.min.js"></script>
</head>
<body>
<h1>Processing.js Test</h1>
<p>This is my first Processing.js web-based sketch:</p>
<canvas data-processing-sources="./src/processingjs_mouseOver.pde"></canvas>
</body>
</html>
</pre>
</div>
</div>
<div id="outline-container-sec-4-1-2" class="outline-4">
<h4 id="sec-4-1-2"><span class="section-number-4">4.1.2</span> Get processingjs online</h4>
<div class="outline-text-4" id="text-4-1-2">
<ul class="org-ul">
<li><a href="http://processingjs.org/download/">http://processingjs.org/download/</a>
</li>
</ul>
<dl class="org-dl">
<dt> Link to processing script below </dt><dd><a href="./src/processingjs_mouseOver.pde">./src/processingjs_mouseOver.pde</a>
</dd>
</dl>
</div>
</div>
<div id="outline-container-sec-4-1-3" class="outline-4">
<h4 id="sec-4-1-3"><span class="section-number-4">4.1.3</span> Processing script</h4>
<div class="outline-text-4" id="text-4-1-3">
<div class="org-src-container">
<pre class="src src-java"><span class="org-type">void</span> <span class="org-function-name">setup</span>(){
size(400, 400, P3D);
frameRate(8);
}
<span class="org-type">void</span> <span class="org-function-name">draw</span>(){
background(0);
stroke(255);
smooth();
<span class="org-type">float</span> <span class="org-variable-name">x</span> = width;
<span class="org-type">float</span> <span class="org-variable-name">y</span> = height;
<span class="org-type">float</span> <span class="org-variable-name">angle</span> = 1.019 * frameCount%24;
<span class="org-type">float</span> <span class="org-variable-name">r</span> = 0.0;
translate(width/2, height/2);
r = 10.0;
<span class="org-keyword">for</span>(<span class="org-type">int</span> <span class="org-variable-name">i</span> = 0; i < <span class="org-type">mouseX</span>; i+=3){
<span class="org-keyword">for</span>(<span class="org-type">int</span> <span class="org-variable-name">j</span> = 1; j <= (mouseY/3); j++){
<span class="org-keyword">for</span>(<span class="org-type">int</span> <span class="org-variable-name">k</span> = i; k <= j; k++){
rotate(2*PI/19.0*i);
stroke(255, 23, 55, (j-k)*angle);
strokeWeight(1.91+i*402);
beginShape(LINES);
vertex(r, j*r, k*r);
vertex(r*x/5,i*r, -j*r);
endShape();
}
}
}
}
</pre>
</div>
<dl class="org-dl">
<dt> Preview on html file </dt><dd>Mouse over the image to interact with it. Start from top left corner.
</dd>
</dl>
<script src="https://raw.githubusercontent.com/processing-js/processing-js/v1.4.8/processing.min.js"></script>
<canvas data-processing-sources="./src/processingjs_mouseOver.pde"></canvas>
</div>
</div>
</div>
<div id="outline-container-sec-4-2" class="outline-3">
<h3 id="sec-4-2"><span class="section-number-3">4.2</span> supercollidejs</h3>
<div class="outline-text-3" id="text-4-2">
<p>
You can install SuperCollider on Atom editor, which is shipped with <a href="https://github.com/crucialfelix/supercolliderjs">supercollidejs</a>.
</p>
<dl class="org-dl">
<dt> See online docs </dt><dd><a href="http://supercolliderjs.readthedocs.io/en/latest/configuration.html">http://supercolliderjs.readthedocs.io/en/latest/configuration.html</a>
</dd>
<dt> SuperCollider package for Atom </dt><dd><a href="https://atom.io/packages/supercollider">https://atom.io/packages/supercollider</a>
</dd>
</dl>
</div>
<div id="outline-container-sec-4-2-1" class="outline-4">
<h4 id="sec-4-2-1"><span class="section-number-4">4.2.1</span> Print sclang output on your browser</h4>
<div class="outline-text-4" id="text-4-2-1">
<p>
The script below prints the output of sclang (<code>(1..10).asString</code>) in your browser. You have to make a folder in your supercollidejs root directory and put <code>sc3-http.js</code> script, in order the to find the <code>index.js</code> (see 1st line on script below). You run on your terminal <code>node sc3-http.js</code>. Then you open your browser and go to <code>127.0.0.1:8000</code> to see the output, or you can inspect from terminal using <code>curl http://localhost:8000/</code>.
</p>
<dl class="org-dl">
<dt> Link to js script below </dt><dd><a href="./src/sc3-http.js">./src/sc3-http.js</a>
</dd>
</dl>
<div class="org-src-container">
<pre class="src src-js"><span class="org-keyword">var</span> <span class="org-variable-name">sc</span> = require(<span class="org-string">'../index.js'</span>);
<span class="org-keyword">var</span> <span class="org-variable-name">http</span> = require(<span class="org-string">'http'</span>);
<span class="org-keyword">var</span> <span class="org-variable-name">s</span> = http.createServer(<span class="org-keyword">function</span>(<span class="org-variable-name">req</span>, <span class="org-variable-name">res</span>){
sc.lang.boot({debug: <span class="org-constant">false</span>}).then(<span class="org-keyword">function</span>(<span class="org-variable-name">sclang</span>) {
sclang.interpret(<span class="org-string">'(1..10).asString'</span>).then(<span class="org-keyword">function</span>(<span class="org-variable-name">answer</span>){
console.log(<span class="org-string">'array = '</span> + answer);
<span class="org-comment-delimiter">//</span><span class="org-comment">return answer;</span>
res.writeHead(200, { <span class="org-string">'content-type'</span>: <span class="org-string">'text/plain'</span> });
res.end(<span class="org-string">'ANSWER: '</span> + answer);
console.log(answer);
}, console.error);
});
});
s.listen(8000);
</pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<style type="text/css">
.back-to-top {
position: fixed;
bottom: 2em;
right: 0px;
text-decoration: none;
color: #000000;
background-color: rgba(235, 235, 235, 0.80);
font-size: 12px;
padding: 1em;
display: none;
}
.back-to-top:hover {
background-color: rgba(135, 135, 135, 0.50);
}
</style>
<div class="back-to-top">
<a href="#top">Back to top</a> | <a href="mailto:[email protected]">E-mail me</a>
<p></p>
<center>
<a href="http://users.jyu.fi/~gediapou">Georgios Diapoulis</a>
</center>
</div>
<script type="text/javascript">
var offset = 220;
var duration = 500;
jQuery(window).scroll(function() {
if (jQuery(this).scrollTop() > offset) {
jQuery('.back-to-top').fadeIn(duration);
} else {
jQuery('.back-to-top').fadeOut(duration);
}
});
</script>
</div>
</body>
</html>