Debian Codegraph @

examples/spatial/ambisonic-encoding.ck

raw
1 // name: ambisonic-encoding.ck
2 // desc: utilizes an array of Gain to encode a virtual source in nth order ambisonics
3 // author: everett m. carpenter
4 // date: spring 2026
5
6 // NOTE: various orientations exist across ambisonic literature, Math.sh is implemented
7 // so that an azimuth of 0 is directly infront of the listener, and increasing azimuth
8 // will move the projection counterclockwise. additionally, an elevation of 0 is located
9 // on the horizontal plane, -90 degrees equates to the south pole, and 90 degrees the north.
10
11 // let's do second order (Math.sh() supports up to 5th)
12 2 => int order;
13 0.0 => float azimuth;
14 0.0 => float elevation;
15
16 // nth order ambisonics requires (n+1)^2 channels for three dimensional encoding
17 SinOsc sing(352.0) => Gain encoder[(order+1)*(order+1)] => blackhole;
18
19 while(true)
20 {
21 Math.sh(order, azimuth, elevation) @=> float sphericalHarmonics[];
22
23 // Math.sh() will return a (n+1)^2 sized array, one value for the projection upon each harmonic
24 for(int i; i < sphericalHarmonics.size(); i++)
25 {
26 sphericalHarmonics[i] => encoder[i].gain;
27 }
28
29 // rotate horizontally
30 15.0 +=> azimuth;
31
32 // wait some time
33 500::ms => now;
34 }