Debian Codegraph @

examples/deep/chant.ck

raw
1 //--------------------------------------------------------------------
2 // name: chant.ck
3 // desc: chant synthesizer; demonstrates multi-shredded concurrency,
4 // variable rates, source-filter model, and interpolation.
5 //
6 // This is a classic source-filter model for rudimentary singing
7 // synthesis: an impulse train (the "source", crudely modeling
8 // opening/closing of the glottis in the vocal tract) going through
9 // a bank of three formant filters (roughly modeling the filtering
10 // by the vocal cavity to induce the perception of different vowels).
11 //
12 // This example demonstrates an elegant way to implement the above
13 // in ChucK, by breaking up the tasks into three concurrent shreds:
14 // 1. a main shred selects the next target pitch and formants
15 // 2. doImpulse() generates the impulse train; using ChucK's
16 // strongly-timed mechanisms to modulate the impulse train
17 // period to create vibrato
18 // 3. doInterpolation() interpolates the period and formants,
19 // to smoothly glide from note to note, vowel to vowel
20 //
21 // author: Perry R. Cook (2006)
22 // modified by Rebecca Fiebrink and Ge Wang (2007, 2021)
23 // published in ChucK examples 2021
24 //--------------------------------------------------------------------
25
26 // synthesis patch
27 Impulse i => TwoZero t => TwoZero t2 => OnePole p;
28 // formant filters
29 p => TwoPole f1 => Gain g;
30 p => TwoPole f2 => g;
31 p => TwoPole f3 => g;
32 // reverbs
33 g => JCRev r => dac;
34 g => JCRev rL => dac.left;
35 g => JCRev rR => dac.right;
36 // delays
37 g => Delay d1 => Gain g1 => r;
38 g => Delay d2 => Gain g2 => rL;
39 g => Delay d3 => Gain g3 => rR;
40 // connect gains to delays
41 g1 => d1; g2 => d2; g3 => d3;
42
43 // source gain (amplitude of the impulse train)
44 0.25 => float sourceGain;
45
46 // set filter coefficients
47 1.0 => t.b0; 0.0 => t.b1; -1.0 => t.b2;
48 1.0 => t2.b0; 0.0 => t2.b1; 1.0 => t2.b2;
49 // set gains
50 0.1 => g1.gain; 0.1 => g2.gain; 0.1 => g3.gain;
51 // set reverb mix
52 0.025 => r.mix;
53 // set delay max and length
54 1.5 :: second => d1.max;
55 2.0 :: second => d2.max;
56 2.8 :: second => d3.max;
57 1.41459 :: second => d1.delay;
58 1.97511 :: second => d2.delay;
59 2.71793 :: second => d3.delay;
60
61 // set two pole filter radii and gain
62 0.997 => f1.radius; 0.997 => f2.radius; 0.997 => f3.radius;
63 1.0 => f1.gain; 0.8 => f2.gain; 0.6 => f3.gain;
64 // randomize initial formant frequencies
65 Math.random2f( 230.0, 660.0 ) => f1.freq;
66 Math.random2f( 800.0, 2300.0 ) => f2.freq;
67 Math.random2f( 1700.0, 3000.0 ) => f3.freq;
68
69 // variables for interpolating current and target formant frequencies
70 400.0 => float f1freq;
71 1000.0 => float f2freq;
72 2800.0 => float f3freq;
73 400.0 => float target_f1freq;
74 1000.0 => float target_f2freq;
75 2800.0 => float target_f3freq;
76
77 // leaky integrator
78 0.99 => p.pole;
79 1.0 => p.gain;
80
81 // variables that control impulse train source
82 0.013 => float period;
83 0.013 => float targetPeriod;
84 0.0 => float modphase;
85 0.0001 => float vibratoDepth;
86
87 // scale
88 [ 0, 1, 5, 7,
89 8, 11, 8, 7,
90 11, 12, 14, 15,
91 19, 17, 20, 24 ] @=> int scale[];
92 // names (for printing)
93 [ "ut0", "ra0", "fa0", "ut0",
94 "ra0", "mi0", "ra1", "ut1",
95 "mi0", "ut1", "re1", "mi1",
96 "ut1", "fa1", "re1", "ut2" ] @=> string names[];
97 // current location in scale
98 9 => int scalepoint;
99 // frequency
100 float theFreq;
101
102 // spork two concurrent child shreds...
103 spork ~ doImpulse(); // generate voice source
104 spork ~ doInterpolation( 10::ms ); // interpolate pitch and formants
105
106 // main shred loop
107 while( true )
108 {
109 // determine new formant targets
110 Math.random2f( 230.0, 660.0 ) => target_f1freq;
111 Math.random2f( 800.0, 2300.0 ) => target_f2freq;
112 Math.random2f( 1700.0, 3000.0 ) => target_f3freq;
113
114 // next pitch (random walk the scale)
115 Math.random2(-1,1) + scalepoint => scalepoint;
116 if( scalepoint < 0 ) 0 => scalepoint;
117 if( scalepoint > 15 ) 15 => scalepoint;
118 // compute the frequency
119 32 + scale[scalepoint] => Std.mtof => theFreq;
120 // print things for fun
121 <<< names[scalepoint], theFreq >>>;
122 // calculate corresponding target period
123 1.0 / theFreq => targetPeriod;
124
125 // wait until next note
126 Math.random2f( 0.2, 0.9 )::second => now;
127 }
128
129 // entry point for shred: generate source impulse train
130 fun void doImpulse()
131 {
132 // infinite time-loop
133 while( true )
134 {
135 // fire impulse
136 sourceGain => i.next;
137 // phase variable
138 modphase + period => modphase;
139 // vibrato depth
140 .0001 => vibratoDepth;
141 // modulate wait time until next impulse: vibrato
142 (period + vibratoDepth*Math.sin(2*pi*modphase*6.0))::second => now;
143 }
144 }
145
146 // entry point for shred: interpolate period and formant frequencies
147 fun void doInterpolation( dur T )
148 {
149 // percentage progress per time slice
150 0.10 => float slew;
151 // infinite time-loop
152 while( true )
153 {
154 // go towards target period (pitch)
155 (targetPeriod - period) * slew + period => period;
156 // go towards targat formant frequencies
157 (target_f1freq - f1freq) * slew + f1freq => f1freq => f1.freq;
158 (target_f2freq - f2freq) * slew + f2freq => f2freq => f2.freq;
159 (target_f3freq - f3freq) * slew + f3freq => f3freq => f3.freq;
160
161 // interpolation rate
162 T => now;
163 }
164 }