Debian Codegraph @

examples/deep/ks-chord.ck

raw
1 //-----------------------------------------------------------------------------
2 // name: ks-chord.ck
3 // desc: karplus strong comb filter bank
4 //
5 // authors: Madeline Huberth ([email protected])
6 // Ge Wang ([email protected])
7 // date: summer 2014
8 // Stanford Center @ Peking University
9 //-----------------------------------------------------------------------------
10
11 // single voice Karplus Strong chubgraph
12 class KS extends Chugraph
13 {
14 // sample rate
15 second / samp => float SRATE;
16
17 // ugens!
18 DelayA delay;
19 OneZero lowpass;
20
21 // noise, only for internal use
22 Noise n => delay;
23 // silence so it doesn't play
24 0 => n.gain;
25
26 // the feedback
27 inlet => delay => lowpass => delay => outlet;
28 // max delay
29 1::second => delay.max;
30 // set lowpass
31 -1 => lowpass.zero;
32 // set feedback attenuation
33 .9 => lowpass.gain;
34
35 // mostly for testing
36 fun void play( float pitch, dur T )
37 {
38 tune( pitch ) => float length;
39 // turn on noise
40 1 => n.gain;
41 // fill delay with length samples
42 length::samp => now;
43 // silence
44 0 => n.gain;
45 // let it play
46 T-length::samp => now;
47 }
48
49 // tune the fundamental resonance
50 fun float tune( float pitch )
51 {
52 // computes further pitch tuning for higher pitches
53 pitch - 43 => float diff;
54 0 => float adjust;
55 if( diff > 0 ) diff * .0125 => adjust;
56 // compute length
57 computeDelay( Std.mtof(pitch+adjust) ) => float length;
58 // set the delay
59 length::samp => delay.delay;
60 //return
61 return length;
62 }
63
64 // set feedback attenuation
65 fun float feedback( float att )
66 {
67 // sanity check
68 if( att >= 1 || att < 0 )
69 {
70 <<< "set feedback value between 0 and 1 (non-inclusive)" >>>;
71 return lowpass.gain();
72 }
73
74 // set it
75 att => lowpass.gain;
76 // return
77 return att;
78 }
79
80 // compute delay from frequency
81 fun float computeDelay( float freq )
82 {
83 // compute delay length from srate and desired freq
84 return SRATE / freq;
85 }
86 }
87
88 // chord class for KS
89 public class KSChord extends Chugraph
90 {
91 // array of KS objects
92 KS chordArray[4];
93
94 // connect to inlet and outlet of chubgraph
95 for( int i; i < chordArray.size(); i++ ) {
96 inlet => chordArray[i] => outlet;
97 }
98
99 // set feedback
100 fun float feedback( float att )
101 {
102 // sanith check
103 if( att >= 1 || att < 0 )
104 {
105 <<< "set feedback value between 0 and 1 (non-inclusive)" >>>;
106 return att;
107 }
108
109 // set feedback on each element
110 for( int i; i < chordArray.size(); i++ )
111 {
112 att => chordArray[i].feedback;
113 }
114
115 return att;
116 }
117
118 // tune 4 objects
119 fun void tune( float pitch1, float pitch2, float pitch3, float pitch4 )
120 {
121 pitch1 => chordArray[0].tune;
122 pitch2 => chordArray[1].tune;
123 pitch3 => chordArray[2].tune;
124 pitch4 => chordArray[3].tune;
125 }
126 }
127
128
129 // sound to chord to dac
130 SndBuf buffy => KSChord object => dac;
131 // load a sound
132 "special:dope" => buffy.read;
133 // set feedback
134 object.feedback( .96 );
135
136 // offset
137 -12 => int x;
138 // tune
139 object.tune( 60+x, 64+x, 72+x, 79+x );
140 // loop
141 while( true )
142 {
143 // set playhead to beginning
144 0 => buffy.pos;
145 // set rate
146 1 => buffy.rate;
147 // advance time
148 550::ms / buffy.rate() => now;
149 }