Debian Codegraph @

examples/effects/autotune.ck

raw
1 //---------------------------------------------------------------
2 // name: autotune.ck
3 // desc: because no one asked for it
4 // (and it shows how PitchTrack works)
5 //
6 // API reference for PitchTrack
7 // https://chuck.stanford.edu/doc/reference/chugins.html#PitchTrack
8 //
9 // required data file:
10 // https://chuck.stanford.edu/doc/examples/effects/data/obama.wav
11 //
12 // note: PitchTrack is a standard distribution chugin (ChucK plugin)
13 // that is included with the macOS and Windows installers;
14 // on Linux or if you are building chuck from source, you will
15 // need to compile PitchTrack.chug for your system
16 // https://github.com/ccrma/chugins/
17 //---------------------------------------------------------------
18
19 // ananlysis
20 SndBuf obama => PitchTrack tracker => blackhole;
21 // frame size
22 512 => tracker.frame;
23 // frame overlap
24 4 => tracker.overlap;
25
26 // synthesis
27 obama => HPF highpass => Delay del => PitShift autotune => Envelope fade => dac;
28 // high pass to get rid of the low din
29 100 => highpass.freq;
30
31 // ramp duration
32 1::second => fade.duration;
33 // set up envelope value and target
34 0 => fade.value;
35 1 => fade.target;
36
37 // input file name
38 "data/obama.wav" => string filename;
39 // print
40 cherr <= "reading input file: '" <= filename <= "'..." <= IO.nl();
41 // convert to full path relative to this file; read into SndBuf
42 me.dir() + filename => obama.read;
43
44 // modify the '0' to '1' to record a wav file of the output in real-time
45 // FYI to render faster-than-realtime, run this example with `--silent`
46 // > chuck autotune.ck --silent
47 // FYI this code always records if --silent is enabled
48 if( 0 || !Machine.realtime() )
49 {
50 dac => WvOut2 record => blackhole;
51 // output file name
52 "./autotuned-obama.wav" => string outfile;
53 // print
54 cherr <= "recording to file: '" <= outfile <= "'..." <= IO.nl();
55 // set output file name
56 me.dir() + outfile => record.wavFilename;
57 }
58 else
59 {
60 // print
61 cherr <= "recording to file: (disabled)" <= IO.nl();
62 }
63
64 // set delay using pitch tracker frame size
65 tracker.frame()::samp => del.delay;
66 // set input file to start
67 0 => obama.pos;
68 // length of input file
69 obama.length() => dur length;
70
71 // effect mix
72 1 => autotune.mix;
73 // start at 100% (no pitch shift yet)
74 1 => autotune.shift;
75
76 // echoing bells matching vocal pitch
77 TriOsc tri => ADSR env => Echo e => dac;
78 // some feedback for recirculating echoes
79 e => e;
80 // set oscilator gain
81 0.15 => tri.gain;
82 // set ADSR envelope
83 env.set(5::ms, 250::ms, 0, 0::ms);
84 // set delay
85 250::ms => e.max => e.delay;
86 // set echo
87 0.5 => e.mix;
88 // volume
89 0.8 => e.gain;
90
91 // m7 chord
92 [0, 3, 7, 10] @=> int pentatonic[];
93
94 // some variables we'll need later
95 float last_target;
96 // compute end time (not to be confused with end times, which is what we are living in)
97 now + length => time endtime;
98 // compute when to start fade out
99 now + (length - fade.duration()) => time fadetime;
100
101 // spork parallel processes (can comment out to remove their effect)
102
103 // scale switcher (chord progression generator)
104 spork ~ switchScale();
105 // play the ostinato pattern
106 spork ~ ostinato();
107
108 // for the duration of the file
109 while( now < endtime )
110 {
111 // one sample at a time
112 samp => now;
113 // get pitch estimate
114 tracker.get() => float track;
115
116 // find closest pitch
117 closest( track, pentatonic ) => float target;
118
119 // only trigger new note if different from previous
120 if( target > 60 && Std.fabs(target - last_target) > 0.1 )
121 {
122 1 => env.keyOn;
123 target => tri.freq;
124 target => last_target;
125 }
126
127 // perform autotune
128 if( track > 0 ) target / track => autotune.shift;
129 // begin fade out if the time is right
130 if( now >= fadetime ) 0 => fade.target;
131 }
132
133 // wait a little extra time
134 5::second => now;
135
136 // hold pitch
137 fun void hold_pitch( float t )
138 {
139 t => last_target;
140 250::ms => now;
141 -100 => last_target;
142 }
143
144 // scale switcher (chord progression generator)
145 fun void switchScale()
146 {
147 while( true )
148 {
149 12::second => now;
150 pentatonic[0]--;
151 pentatonic[2]--;
152 pentatonic[3]++;
153 11.5::second => now;
154 pentatonic[0]--;
155 2 -=> pentatonic[1];
156 pentatonic[2]--;
157 3 -=> pentatonic[3];
158 for (int i; i<pentatonic.size(); i++)
159 {
160 if (pentatonic[i] < -5) 12 +=> pentatonic[i];
161 }
162 }
163 }
164
165 // our repeated pattern
166 fun void ostinato()
167 {
168 // modal bar into revert
169 ModalBar mb => GVerb rev => fade;
170 // set gain
171 0.5 => mb.gain;
172 // which Modal Bar preset
173 1 => mb.preset;
174 // 0.01 => rev.mix;
175
176 // time loop
177 while( true )
178 {
179 // go through the scale
180 for( int degree : pentatonic )
181 {
182 // pitch to frequency and set in modal bar
183 (degree + 48) => Std.mtof => mb.freq;
184 // note one!
185 1 => mb.noteOn;
186 // for an 1/8 of a second
187 125::ms => now;
188 }
189 }
190 }
191
192
193 // helper function to find equal tempered pitch in list
194 // which is closest to freq testval
195 fun float closest( float testval, int list[] )
196 {
197 // list length
198 list.size() => int len;
199
200 int octave;
201 // freq to midi note number
202 Std.ftom(testval) => float testmidi;
203 // calculate octave
204 while( testmidi - (list[len-1] + octave) > 12 )
205 {
206 12 +=> octave;
207 }
208
209 48000.0 => float lowdiff;
210 int closest_index;
211 int closest_octave;
212
213 // loop over list
214 for( int i; i<len; i++ )
215 {
216 Std.mtof(octave + list[i]) => float listnote;
217 Math.fabs(listnote - testval) => float diff;
218 if (diff < lowdiff)
219 {
220 i => closest_index;
221 diff => lowdiff;
222 octave => closest_octave;
223 }
224 }
225
226 // loop over list 8va
227 for( int i; i<len; i++ )
228 {
229 Std.mtof(octave + 12 + list[i]) => float listnote;
230 Math.fabs(listnote - testval) => float diff;
231 if (diff < lowdiff)
232 {
233 i => closest_index;
234 diff => lowdiff;
235 octave + 12 => closest_octave;
236 }
237 }
238
239 // convert back to freq
240 return Std.mtof( closest_octave + list[closest_index] );
241 }