Debian Codegraph @

examples/analysis/paulstretch.ck

raw
1 //--------------------------------------------------------
2 // name: paulstretch.ck
3 // desc: Paul's Extreme Sound Stretch (Paulstretch)
4 // (requires: chuck-1.5.5.6 or higher)
5 //
6 // author: Celeste Betancur Gutierrez
7 // adapted from Paul Nasca's Python implementation
8 // date: Fall 2025
9 //--------------------------------------------------------
10
11 //--------------------------------------------------------
12 // this is the stretch factor
13 // e.g., .05 is 20x time-stretch
14 // 1 is normal/no stretch
15 // 0 is freeze
16 .05 => float resolution;
17 //--------------------------------------------------------
18 // FFT window size
19 // larger == better quality but more bursty CPU load
20 4096*4 => int windowSize;
21
22 // ensure even window size
23 (windowSize / 2) * 2 => windowSize;
24 windowSize / 2 => int halfWindowSize;
25
26 // preview
27 SndBuf input => dac;
28 // feel free to use your own sample here
29 "special:dope" => input.read;
30 // print
31 cherr <= "DOH" <= IO.nl();
32 // play it without stretching
33 input.length() => now;
34 // disconnect preview patch
35 input =< dac;
36 // wait a bit
37 .5::second => now;
38
39 // audio analysis
40 input => FFT fft => blackhole;
41 // playhead
42 0 => input.pos;
43 // loop it
44 1 => input.loop;
45
46
47 // audio synthesis
48 IFFT ifft1 => Gain output => dac;
49 IFFT ifft2 => output => dac;
50
51 // set window size
52 windowSize => fft.size => ifft1.size => ifft2.size;
53
54 // Hann window to have a nice Overlap-add :)
55 (halfWindowSize/2) => int overlapAdd;
56 Windowing.hann(windowSize) => fft.window => ifft1.window => ifft2.window;
57 // complex frequency bins
58 complex freqBuffer[halfWindowSize];
59 // compute playback hop size
60 (overlapAdd * (input.sampleRate()) / (second/samp)$float)$int => int playbackHop;
61
62 // shred for randomize phase
63 fun void randomizePhase()
64 {
65 while( true )
66 {
67 // trigger ananlysis up the UAna chain from fft
68 fft.upchuck();
69
70 // iterate over frequency bins
71 for (int i; i < halfWindowSize; i++)
72 {
73 // compute magnitude
74 (fft.cval(i)$polar).mag => float mag;
75 // generate random phase
76 Math.random2f(-Math.PI, Math.PI) => float randomPhase;
77 // reconstruct with random phase
78 #(mag * Math.cos(randomPhase), mag * Math.sin(randomPhase)) => freqBuffer[i];
79 }
80 // advance time by overlap-add duration
81 overlapAdd::samp => now;
82
83 // return the playhead by resolution factor
84 (input.pos()) - ((playbackHop - playbackHop*(resolution))$int) => input.pos;
85 }
86 }
87
88 // shred for reconstructing the audio
89 fun void reconstruct()
90 {
91 while( true )
92 {
93 // overlap-add
94 ifft1.transform( freqBuffer );
95 overlapAdd::samp => now;
96 ifft2.transform( freqBuffer );
97 overlapAdd::samp => now;
98 }
99 }
100
101 fun void transcribe()
102 {
103 100::ms => dur T;
104 while( true )
105 {
106 // the beginning
107 if( input.pos() < 5 ) cherr <= "D";
108 // this does not work as intended but works in effect
109 else if( input.samples() - input.pos() < T/samp ) cherr <= "H";
110 else cherr <= "O";
111 T => now;
112 }
113 }
114
115 // go
116 spork~randomizePhase();
117 spork~reconstruct();
118 spork~transcribe();
119
120 // advance time
121 while( true ) 1::second => now;