-
Notifications
You must be signed in to change notification settings - Fork 2
/
TTEM.html
1681 lines (1415 loc) Β· 67.1 KB
/
TTEM.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
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
<?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>
<!-- 2021-09-20 Mon 09:10 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The Theory and Technique of Electronic Music Exercises in Ad Libitum</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Ruslan Prakapchuk" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-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: auto;
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; margin-top: 14px;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.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; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/readtheorg_theme/css/htmlize.css"/>
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/readtheorg_theme/css/readtheorg.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/lib/js/jquery.stickytableheaders.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/readtheorg_theme/js/readtheorg.js"></script>
<script type="text/javascript">
// @license magnet:?xt=urn:btih:e95b018ef3580986a04669f1b5879592219e2a7a&dn=public-domain.txt Public Domain
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.classList.add("code-highlighted");
target.classList.add("code-highlighted");
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.classList.remove("code-highlighted");
target.classList.remove("code-highlighted");
}
}
/*]]>*///-->
// @license-end
</script>
</head>
<body>
<div id="content">
<h1 class="title">The Theory and Technique of Electronic Music Exercises in Ad Libitum</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org40f526f">1. Sinusoids, amplitude and frequency</a>
<ul>
<li><a href="#orgbe3d8ed">1.1. Measures of Amplitude</a></li>
<li><a href="#org440b04c">1.2. Units of Amplitude</a></li>
<li><a href="#org1795a59">1.3. Controlling Amplitude</a></li>
<li><a href="#org2bcbb48">1.4. Frequency</a></li>
<li><a href="#org8884978">1.5. Synthesizing a sinusoid</a></li>
<li><a href="#orgfe58253">1.6. Superposing Signals</a></li>
<li><a href="#org177431a">1.7. Periodic Signals</a></li>
<li><a href="#org7b3ebf7">1.8. About the Software Examples</a></li>
<li><a href="#orgf7b8c12">1.9. Examples</a>
<ul>
<li><a href="#org794f834">1.9.1. Constant amplitude scaler</a></li>
<li><a href="#orgf0f0e60">1.9.2. Amplitude control in decibels</a></li>
<li><a href="#org720e655">1.9.3. Smoothed envelope control with an envelope generator</a></li>
<li><a href="#orga082915">1.9.4. Major triad</a></li>
<li><a href="#org11e277c">1.9.5. Conversion between frequency and pitch</a></li>
<li><a href="#org8ec6d2e">1.9.6. More additive synthesis</a></li>
</ul>
</li>
<li><a href="#orge68457c">1.10. Exercises</a></li>
</ul>
</li>
<li><a href="#orgd28cb34">2. Wavetables and samplers</a>
<ul>
<li><a href="#org78bfb44">2.1. The Wavetable Oscillator</a></li>
<li><a href="#org137cc3d">2.2. Sampling</a></li>
<li><a href="#org3851d0b">2.3. Enveloping samplers</a></li>
<li><a href="#org7db807a">2.4. Timbre stretching</a></li>
<li><a href="#org8560452">2.5. Interpolation</a></li>
<li><a href="#org5c0a771">2.6. Examples</a>
<ul>
<li><a href="#org318e961">2.6.1. Wavetable oscillator</a></li>
<li><a href="#orgc589e92">2.6.2. Wavetable lookup in general</a></li>
<li><a href="#orgb12b1a7">2.6.3. Using wavetable as a sampler</a></li>
<li><a href="#org0b462c2">2.6.4. Looping sampler</a></li>
<li><a href="#org2b1602e">2.6.5. Overlapping sample looper</a></li>
</ul>
</li>
<li><a href="#orga97eaa3">2.7. Exercises</a></li>
</ul>
</li>
<li><a href="#org3c1e251">3. Audio and control computations</a>
<ul>
<li><a href="#org84a0bb0">3.1. The sampling theorem</a></li>
<li><a href="#orgd0316a1">3.2. Control</a></li>
<li><a href="#orgc389059">3.3. Control streams</a></li>
</ul>
</li>
</ul>
</div>
</div>
<p>
It's my exercise book for studying "The Theory and Technique of Electronic
Music" by Miller Puckette. Work is done in Ad Libitum instead of PureData, and
some pieces of code migrate back to Ad Libitum standard library.
</p>
<p>
You might want to read this file here <a href="http://ul.mantike.pro/ad-libitum/TTEM.html">http://ul.mantike.pro/ad-libitum/TTEM.html</a>
</p>
<div id="outline-container-org40f526f" class="outline-2">
<h2 id="org40f526f"><span class="section-number-2">1</span> Sinusoids, amplitude and frequency</h2>
<div class="outline-text-2" id="text-1">
<p>
Ad Libitum deals with continous representation of time, it's expressed as a
float number of seconds passed from the stream start. For any signal either
continous or discrete feels more natural, and there is no clear measure of
their shares. Good news that conversion is relatively easy in both directions.
</p>
<p>
<code>~<</code> is a syntax sugar which wraps code into function of two parameters:
<code>time</code> and <code>channel</code>, which are thrown in scope. Such kind of function is how
Ad Libitum represents audio signals. Think about time as a continuous
counterpart of sample number <code>n</code> in TTEM. <code>channel</code> will be discussed later.
Think about <code>~<</code> as the thing which animates your formula to produce signal.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">sinusoid</span> a ω φ) (~< (* a (cos (+ (* ω time) φ)))))
</pre>
</div>
<p>
<code>play!</code> connects your signal to audio output and you can hear it! Amplitude
range in Ad Libitum is [-1, 1], setting a=0.2 we are producing signal in 1/5
of it.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(play! (sinusoid 0.2 2000.0 0.0))
</pre>
</div>
<p>
And the most important function in Ad Libitum β <code>h!</code> hush!
</p>
<div class="org-src-container">
<pre class="src src-scheme">(h!)
</pre>
</div>
<p>
Let's make sinusoid to receive regular frequency instead of angular one.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">ƒ</span>→ω ƒ)
(* 2π ƒ))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">ω</span>→ƒ ω)
(/ ω 2π))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">sinusoid</span> a ƒ φ)
(~< (* a (cos (+ (* (ƒ→ω ƒ) time) φ)))))
(play! (sinusoid 0.2 440.0 0.0))
(h!)
</pre>
</div>
</div>
<div id="outline-container-orgbe3d8ed" class="outline-3">
<h3 id="orgbe3d8ed"><span class="section-number-3">1.1</span> Measures of Amplitude</h3>
<div class="outline-text-3" id="text-1-1">
<p>
This is not a blood pact, but many of Ad Libitum code relies on fact that
audiosignal is called sample by sample, without skips. Making that assumption
we are able to write RMS amplitude measurement even in continuous
representation of time.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">rms-amplitude</span> window-width s)
(<span style="font-weight: bold;">let</span> ([windows (make-vector *channels*)]
[N (-> window-width (* *sample-rate*) (ceiling) (exact))]
[cursor -1])
(do-ec (: i *channels*)
(vector-set! windows i (make-vector N 0.0)))
(~<
(<span style="font-weight: bold;">when</span> (zero? channel)
(set! cursor (mod (+ cursor 1) N)))
(<span style="font-weight: bold;">let</span> ([window (vector-ref windows channel)]
[x 0.0])
(vector-set! window cursor (<~ s))
(vector-for-each (<span style="font-weight: bold;">λ</span> (y) (set! x (+ x (* y y)))) window)
(sqrt (/ x N))))))
</pre>
</div>
<p>
Let's make sinusoid amplitude a signal, and set it to rms amplitude measured
from sinusoid with peak amplitude 1.0
Note <code><~</code> syntax sugar to apply audiosignal function to time and channel.
<code>(<~ a)</code> is equivalent to <code>(a time channel)</code>
Also note how we make constant signal with <code>(~< 1.0)</code>
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">sinusoid</span> a ƒ φ)
(~< (* (<~ a) (cos (+ (* (ƒ→ω ƒ) time) φ)))))
</pre>
</div>
<p>
Note that computing rms in realtime is very expensive.
Try to increase window width until you get audio buffer underflow glitches.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(play! (sinusoid (rms-amplitude 2/440 (sinusoid (~< 1.0) 440.0 0.0)) 440.0 0.0))
(h!)
</pre>
</div>
<p>
Compare with 1.0 peak itself, it's louder!
</p>
<div class="org-src-container">
<pre class="src src-scheme">(play! (sinusoid (~< 1.0) 440.0 0.0))
(h!)
</pre>
</div>
<p>
Let's throttle <code>rms-amplitude</code> to make it less hungry
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">rms-amplitude</span> window-width s)
(<span style="font-weight: bold;">let</span> ([windows (make-vector *channels*)]
[N (-> window-width (* *sample-rate*) (ceiling) (exact))]
[cursor -1]
[amplitudes (make-vector *channels* 0.0)])
(do-ec (: i *channels*)
(vector-set! windows i (make-vector N 0.0)))
(~<
(<span style="font-weight: bold;">when</span> (zero? channel)
(set! cursor (mod (+ cursor 1) N)))
(<span style="font-weight: bold;">let</span> ([window (vector-ref windows channel)])
(vector-set! window cursor (<~ s))
(<span style="font-weight: bold;">when</span> (zero? cursor)
(vector-set!
amplitudes
channel
(<span style="font-weight: bold;">let</span> ([x 0.0])
(vector-for-each (<span style="font-weight: bold;">λ</span> (y) (set! x (+ x (* y y)))) window)
(sqrt (/ x N))))))
(vector-ref amplitudes channel))))
(play! (sinusoid (rms-amplitude 1 (+~ (sinusoid (~< 1.0) 440.0 0.0))) 440.0 0.0))
(h!)
</pre>
</div>
<p>
Chez Scheme GC is so cool that though we produce a lot of garbage every
second it doesn't interrupt sound! But we still have a noticeable lag on
window initialization. Take it into account if you are going to spawn
capacitive <code>rms-amplitude</code> signals frequently.
</p>
<p>
In future for sinusoid we will use built-in Ad Libitum oscillator available
as <code>osc:sine</code> (takes phasor signal as input, we'll cover it later) and
<code>osc:sine///</code> (takes frequency and optional initial phase).
</p>
<div class="org-src-container">
<pre class="src src-scheme">(play! (osc:sine (osc:phasor 440.0 0.0)))
(play! (osc:sine/// 440.0 0.0))
(h!)
</pre>
</div>
</div>
</div>
<div id="outline-container-org440b04c" class="outline-3">
<h3 id="org440b04c"><span class="section-number-3">1.2</span> Units of Amplitude</h3>
<div class="outline-text-3" id="text-1-2">
<p>
Convert amplitude to decibels, with a0 = 1e-5 as suggested in TTEM
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">amp->dB</span> x)
(* 20.0 (log (* x 1e5) 10.0)))
(amp->dB 1.0)
(amp->dB 0.5)
</pre>
</div>
<p>
But setting a0 to 1.0 is also very convenient β maximum amplitude is then 0dB
and any one below is just negative. All relations stays the same.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">amp->dB</span> x)
(* 20.0 (log x 10.0)))
(amp->dB 0.5)
(amp->dB 1e-5)
</pre>
</div>
<p>
And convert decibels back to amplitude:
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">dB->amp</span> x)
(expt 10.0 (/ x 20.0)))
(dB->amp -100.0)
(dB->amp -50.0)
(dB->amp -10.0)
(dB->amp 0.0)
</pre>
</div>
<p>
Amplitude is related in an inexact way to the perceived loudness of a sound.
In general, two signals with the same peak or RMS amplitude wonβt necessarily
have the same loudness at all. But amplifying a signal by 3 dB, say, will
fairly reliably make it sound about one βstepβ louder.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">*volume-step-dB*</span> 3.0)
</pre>
</div>
<p>
Let's test it!
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">sinusoid</span> a ƒ φ)
(~< (* a (cos (+ (* (ƒ→ω ƒ) time) φ)))))
(play! (sinusoid (dB->amp -10.0) 440.0 0.0))
(play! (sinusoid (dB->amp (- 10.0 *volume-step-dB*)) 440.0 0.0))
(h!)
</pre>
</div>
<p>
Try to change step. For that wave personally I hear 2dB difference.
</p>
</div>
</div>
<div id="outline-container-org1795a59" class="outline-3">
<h3 id="org1795a59"><span class="section-number-3">1.3</span> Controlling Amplitude</h3>
<div class="outline-text-3" id="text-1-3">
<p>
We already controlled amplitude by multiplying every sample by <code>a</code>
Let's do it by multiplying sinusoid by constant signal.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(play! (*~ (~< 0.5) (osc:sine/// 440.0)))
(play! (*~ (~< 0.2) (osc:sine/// 440.0)))
(h!)
</pre>
</div>
</div>
</div>
<div id="outline-container-org2bcbb48" class="outline-3">
<h3 id="org2bcbb48"><span class="section-number-3">1.4</span> Frequency</h3>
<div class="outline-text-3" id="text-1-4">
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">midi-pitch->frequency</span> m)
(* 440.0 (expt 2.0 (/ (- m 69.0) 12.0))))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">frequency->midi-pitch</span> f)
(+ 69 (exact (round (* 12.0 (log (/ f 440.0) 2.0))))))
(play! (osc:sine/// (midi-pitch->frequency 69)))
(play! (osc:sine/// (midi-pitch->frequency 72)))
(h!)
</pre>
</div>
<p>
Ad Libitum allows you to use MIDI controller. Support is still incomplete and
relies on many assumptions. Your MIDI input device should be connected and
identified as the first one.
<code>now</code> is Ad Libitum clock function. It's required for MIDI module to put
proper timestamps on events.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(midi:start now)
</pre>
</div>
<p>
Let's defined so called control signal for our frequency. We'll speak about
control signals later, but putting it simply, control signal is an audio
signal which is updated in non-audio rate by calling its setter.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(ctrl:define-control frequency 440.0)
</pre>
</div>
<p>
Let's set callback which will be called for every control change MIDI event.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(midi:set-cc! (<span style="font-weight: bold;">λ</span> (t knob value channel)
(frequency-set! (midi-pitch->frequency value))))
(play! (osc:sine/// frequency~))
(h!)
</pre>
</div>
<p>
Notice that abrupt change of frequency cause "pops" discussed in TTEM 1.5
</p>
</div>
</div>
<div id="outline-container-org8884978" class="outline-3">
<h3 id="org8884978"><span class="section-number-3">1.5</span> Synthesizing a sinusoid</h3>
<div class="outline-text-3" id="text-1-5">
<p>
To make transition smooth we could use built-in <code>env:linear-transition</code>
</p>
<div class="org-src-container">
<pre class="src src-scheme">(ctrl:define-control frequency 440.0)
(play! (osc:sine/// (env:linear-transition (~< 0.05) frequency~)))
(h!)
</pre>
</div>
<p>
Besides of using MIDI input we could make frequency change programmatically.
<code>schedule</code> allows you to call any function later at given point of time.
Any function could schedule itself. It is called temporal recursion.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">swap-frequency</span> i)
(<span style="font-weight: bold;">if</span> (zero? i)
(frequency-set! 440.0)
(frequency-set! 220.0))
(schedule (+ (now) 1/4) 'swap-frequency (- 1 i)))
(swap-frequency 0)
(play! (osc:sine/// frequency~))
(play! (osc:sine/// (env:linear-transition (~< 0.05) frequency~)))
(play! (osc:sine/// (env:quadratic-transition (~< 0.05) frequency~)))
(h!)
</pre>
</div>
</div>
</div>
<div id="outline-container-orgfe58253" class="outline-3">
<h3 id="orgfe58253"><span class="section-number-3">1.6</span> Superposing Signals</h3>
<div class="outline-text-3" id="text-1-6">
<p>
To superpose signals in Ad Libitum signal sum operator <code>+~</code> is available.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(play! (+~ (*~ (~< 0.5) (osc:sine/// (midi-pitch->frequency 69)))
(*~ (~< 0.5) (osc:sine/// (midi-pitch->frequency 72)))))
(h!)
</pre>
</div>
<p>
Let's measure how peak and rms amplitude of sinusoids superposition relates
to sum of their amplitudes.
</p>
<p>
For that we need to define <code>peak-amplitude</code> signal.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">peak-amplitude</span> signal)
(<span style="font-weight: bold;">let</span> ([peaks (make-vector *channels* 0.0)])
(~<
(<span style="font-weight: bold;">let*</span> ([sample (<~ signal)]
[peak (max sample (vector-ref peaks channel))])
(vector-set! peaks channel peak)
peak))))
</pre>
</div>
<p>
Because Ad Libitum signals are kind of pull FRP, we can't just wrap our
signal with <code>rms-amplitude</code> and then play initial signal and have RMS one
updated. Let's define useful signal which keep signals given to it updated,
but plays only first one.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">solo</span> audio . muted)
(~<
(<span style="font-weight: bold;">for-each</span> (cut <> time channel) muted)
(<~ audio)))
</pre>
</div>
<p>
Uncorrelated signals.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">signal-1</span> (*~ (~< 0.5) (osc:sine/// (midi-pitch->frequency 69))))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">signal-2</span> (*~ (~< 0.5) (osc:sine/// (midi-pitch->frequency 72))))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">superposed-signal</span> (+~ signal-1 signal-2))
<span style="font-weight: bold; font-style: italic;">;; </span><span style="font-weight: bold; font-style: italic;">signal-1 and signal-2 peaks are obviously 0.5</span>
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">measure-peak</span> (peak-amplitude superposed-signal))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">measure-rms-signal-1-2</span> (rms-amplitude 0.1 signal-1))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">measure-rms</span> (rms-amplitude 0.1 superposed-signal))
(play! (solo superposed-signal measure-peak measure-rms measure-rms-signal-1-2))
(h!)
(measure-peak 0.0 0) <span style="font-weight: bold; font-style: italic;">;; </span><span style="font-weight: bold; font-style: italic;">=> 0.9999931184993082</span>
(measure-rms 0.0 0) <span style="font-weight: bold; font-style: italic;">;; </span><span style="font-weight: bold; font-style: italic;">=> 0.5038381755150125</span>
(measure-rms-signal-1-2 0.0 0) <span style="font-weight: bold; font-style: italic;">;; </span><span style="font-weight: bold; font-style: italic;">=> 0.3535533905956031</span>
</pre>
</div>
<p>
Correlated signals.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">signal-1</span> (*~ (~< 0.5) (osc:sine/// 440.0)))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">signal-2</span> (*~ (~< 0.5) (osc:sine/// 440.0)))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">superposed-signal</span> (+~ signal-1 signal-2))
<span style="font-weight: bold; font-style: italic;">;; </span><span style="font-weight: bold; font-style: italic;">signal-1 and signal-2 peaks are obviously 0.5</span>
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">measure-peak</span> (peak-amplitude superposed-signal))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">measure-rms-signal-1-2</span> (rms-amplitude 0.1 signal-1))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">measure-rms</span> (rms-amplitude 0.1 superposed-signal))
(play! (solo superposed-signal measure-peak measure-rms measure-rms-signal-1-2))
(h!)
(measure-peak 0.0 0) <span style="font-weight: bold; font-style: italic;">;; </span><span style="font-weight: bold; font-style: italic;">=> 1.0</span>
(measure-rms 0.0 0) <span style="font-weight: bold; font-style: italic;">;; </span><span style="font-weight: bold; font-style: italic;">=> 0.7071067811829478</span>
(measure-rms-signal-1-2 0.0 0) <span style="font-weight: bold; font-style: italic;">;; </span><span style="font-weight: bold; font-style: italic;">=> 0.3535533905914739</span>
</pre>
</div>
<p>
To be honest, trick with <code>solo</code> points to drawbacks in <code>rms-amplitude</code> and
<code>peak-amplitude</code> design. It would be better for them to just proxy input
signal and provide some accessor to measurement result.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">peak-amplitude</span> signal)
(<span style="font-weight: bold;">let</span> ([peaks (make-vector *channels* 0.0)])
(values
(~<
(<span style="font-weight: bold;">let*</span> ([sample (<~ signal)]
[peak (max sample (vector-ref peaks channel))])
(vector-set! peaks channel peak)
sample))
(<span style="font-weight: bold;">λ</span> () peaks))))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">window</span> width signal)
(<span style="font-weight: bold;">let</span> ([windows (make-vector *channels*)]
[N (-> width (* *sample-rate*) (ceiling) (exact))]
[cursor -1])
(do-ec (: i *channels*)
(vector-set! windows i (make-vector N 0.0)))
(values
(~<
(<span style="font-weight: bold;">when</span> (zero? channel)
(set! cursor (mod (+ cursor 1) N)))
(<span style="font-weight: bold;">let</span> ([sample (<~ signal)]
[window (vector-ref windows channel)])
(vector-set! window cursor sample)
sample))
(<span style="font-weight: bold;">λ</span> () windows))))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">rms-amplitude</span> window-width signal)
(<span style="font-weight: bold;">let-values</span> ([(signal windows) (window window-width signal)])
(values
signal
(<span style="font-weight: bold;">λ</span> ()
(vector-map
(<span style="font-weight: bold;">λ</span> (window)
(<span style="font-weight: bold;">let</span> ([x 0.0])
(vector-for-each (<span style="font-weight: bold;">λ</span> (y) (set! x (+ x (* y y)))) window)
(sqrt (/ x (vector-length window)))) )
(windows))))))
</pre>
</div>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">signal-1</span> (*~ (~< 0.5) (osc:sine/// (midi-pitch->frequency 69))))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">signal-2</span> (*~ (~< 0.5) (osc:sine/// (midi-pitch->frequency 72))))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">superposed-signal</span> (+~ signal-1 signal-2))
(<span style="font-weight: bold;">define-values</span> (superposed-signal measure-peak) (peak-amplitude superposed-signal))
(<span style="font-weight: bold;">define-values</span> (superposed-signal measure-rms) (rms-amplitude 0.1 superposed-signal))
(play! superposed-signal)
(h!)
(measure-peak)
(measure-rms)
</pre>
</div>
</div>
</div>
<div id="outline-container-org177431a" class="outline-3">
<h3 id="org177431a"><span class="section-number-3">1.7</span> Periodic Signals</h3>
<div class="outline-text-3" id="text-1-7">
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">s1</span> (*~ (~< 0.5) (osc:sine/// 220.0 0.0)))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">s2</span> (*~ (~< 0.3) (osc:sine/// 440.0 0.1)))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">s3</span> (*~ (~< 0.2) (osc:sine/// 660.0 0.2)))
(play! (+~ s1 s2 s3))
(h!)
</pre>
</div>
</div>
</div>
<div id="outline-container-org7b3ebf7" class="outline-3">
<h3 id="org7b3ebf7"><span class="section-number-3">1.8</span> About the Software Examples</h3>
<div class="outline-text-3" id="text-1-8">
<p>
Oh, well… You are already running Ad Libitum at this point.
</p>
</div>
</div>
<div id="outline-container-orgf7b8c12" class="outline-3">
<h3 id="orgf7b8c12"><span class="section-number-3">1.9</span> Examples</h3>
<div class="outline-text-3" id="text-1-9">
</div>
<div id="outline-container-org794f834" class="outline-4">
<h4 id="org794f834"><span class="section-number-4">1.9.1</span> Constant amplitude scaler</h4>
<div class="outline-text-4" id="text-1-9-1">
<p>
Note how Pd boxes corresponds to Ad Libitum expressions in parenthesis, and
instead of graph-like connections tree structure is used. Don't judge fast
it as limiting, you always can reuse expression by naming it with help of
<code>define</code> or <code>let</code>, and we will show later how powerful is textual
representation.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(play!
(*~
(~< 0.05)
(osc:sine/// 440.0)))
(h!)
<span style="font-weight: bold; font-style: italic;">;;; </span><span style="font-weight: bold; font-style: italic;">or</span>
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">sinusoid</span> (osc:sine/// 440.0))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">amplitude</span> (~< 0.05))
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">scaled-sinusoid</span> (*~ amplitude sinusoid))
(play! scaled-sinusoid)
(h!)
</pre>
</div>
</div>
</div>
<div id="outline-container-orgf0f0e60" class="outline-4">
<h4 id="orgf0f0e60"><span class="section-number-4">1.9.2</span> Amplitude control in decibels</h4>
<div class="outline-text-4" id="text-1-9-2">
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">amplitude</span> 0.0)
(<span style="font-weight: bold;">define</span> <span style="font-weight: bold;">amplitude~</span> (live-value 'amplitude))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">set-amplitude!</span> dB)
(set! amplitude (dB->amp dB)))
(play! (*~ amplitude~ (osc:sine/// 440.0)))
(set-amplitude! 0) <span style="font-weight: bold; font-style: italic;">;; </span><span style="font-weight: bold; font-style: italic;">beware of loud sound</span>
(set-amplitude! -10)
(set-amplitude! -20)
(set-amplitude! -50)
(h!)
</pre>
</div>
</div>
</div>
<div id="outline-container-org720e655" class="outline-4">
<h4 id="org720e655"><span class="section-number-4">1.9.3</span> Smoothed envelope control with an envelope generator</h4>
<div class="outline-text-4" id="text-1-9-3">
<p>
Note, that in <b>ππ</b> instead of messages we just set values or invoke function
which do that.
</p>
<div class="org-src-container">
<pre class="src src-scheme">(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">make-line</span>)
(ctrl:define-control value 0.0)
(ctrl:define-control Δt 0.0)
(values
(env:linear-transition Δt~ value~)
value-set!
Δt-set!))
(<span style="font-weight: bold;">define-values</span> (line~ set-amplitude! set-Δt!) (make-line))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">slow-on</span>)
(set-Δt! 2.0)
(set-amplitude! 0.1))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">fast-on</span>)
(set-Δt! 0.05)
(set-amplitude! 0.1))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">instant-on</span>)
(set-Δt! 0.0)
(set-amplitude! 0.1))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">slow-off</span>)
(set-Δt! 2.0)
(set-amplitude! 0.0))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">fast-off</span>)
(set-Δt! 0.05)
(set-amplitude! 0.0))
(<span style="font-weight: bold;">define</span> (<span style="font-weight: bold;">instant-off</span>)
(set-Δt! 0.0)
(set-amplitude! 0.0))
(play! (*~ (osc:sine/// 440.0) line~))
(slow-on)
(slow-off)
(fast-on)
(fast-off)
(instant-on)
(instant-off)
(h!)
</pre>
</div>
</div>
</div>
<div id="outline-container-orga082915" class="outline-4">
<h4 id="orga082915"><span class="section-number-4">1.9.4</span> Major triad</h4>
<div class="outline-text-4" id="text-1-9-4">
<p>
<code>mix</code> is normalizing <code>+~</code>
</p>
<div class="org-src-container">
<pre class="src src-scheme">(play!
(mix (osc:sine/// 440.0)
(osc:sine/// 550.0)
(osc:sine/// 660.0)))
(h!)
</pre>
</div>
</div>
</div>
<div id="outline-container-org11e277c" class="outline-4">
<h4 id="org11e277c"><span class="section-number-4">1.9.5</span> Conversion between frequency and pitch</h4>
<div class="outline-text-4" id="text-1-9-5">
<p>
This is covered well before. Only worth to note once more that in <b>ππ</b>
instead of message passing we just give names, set values and call functions.
</p>
</div>