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

#include <klang.h> using namespace klang::optimised; // Physical model of a guitar // (based on Karplus-Strong) struct Guitar : Synth { struct String : Note { struct Excitation : Generator { Envelope env; LPF filter; Noise noise; void process() { (env * noise) >> filter >> out; } } pluck; struct Resonator : Modifier { Delay<44100> delay; LPF filter; param gain; void process() { signal feedback = (delay * gain) >> filter; (in + feedback) >> out; delay << out; // feed back to delay } } string; void process() { pluck >> string >> out; } event on(Pitch pitch, Amplitude velocity) { const param resonance = controls[0],       brightness = controls[1],       finger = controls[2]; const param f = (pitch -> Frequency); const param delay = 1 / f * klang::fs; pluck.filter.set(controls[2]); pluck.env = { { 0.000, 0         },       { 0.001, velocity  },       { 0.003, -velocity },       { 0.004, 0         } }; string.delay.clear(); string.delay.set(delay); string.filter.set(brightness); string.gain = resonance; } };   Guitar() { controls = { Dial("Resonance", 0.97, 0.999, 0.994), Dial("Brightness", -0.1, 0.1, 0.025), Dial("Pluck", 0.5, 1.0, 0.6), }; presets = { { "Classical", { 0.994, 0.025, 0.6 } }, { "Banjo", { 0.990, 0.144, 0.6 } }, }; notes.add<String>(6); } };