Omkara frequency sphere code

Source codeOmkara in Base64 codeOmkara in binary codeSri Yantra as postscript
JavaScript for Omkara frequency visualisation

<canvas id="spectrogram"></canvas>
<script src="https://threejs.org/build/three.js"></script>

<script>
document.addEventListener('DOMContentLoaded', () => {
    const audioContext = new (window.AudioContext || window.webkitAudioContext)();
    const analyser = audioContext.createAnalyser();
    const audioElement = document.querySelector('audio');
    const source = audioContext.createMediaElementSource(audioElement);

    source.connect(analyser);
    analyser.connect(audioContext.destination);

    analyser.fftSize = 128;
    const bufferLength = analyser.frequencyBinCount;
    const dataArray = new Uint8Array(bufferLength);

    // Setup three.js scene
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('spectrogram') });
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setClearColor(0x000000, 0); // Set background color to transparent

    // Create a sphere
    const geometry = new THREE.BufferGeometry();
    const positions = new Float32Array(bufferLength * bufferLength * 3);
    const indices = [];
    const material = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true, transparent: true, opacity: 0.5 });
    const mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);

    camera.position.z = 200;

    const mouse = { x: 0, y: 0 };

    document.addEventListener('mousemove', (event) => {
        // Normalize mouse coordinates to range [-1, 1]
        mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
        mouse.y = (event.clientY / window.innerHeight) * 2 - 1;

        // Rotate the sphere based on mouse movement
        mesh.rotation.x = mouse.y * Math.PI;
        mesh.rotation.y = mouse.x * Math.PI;
    });

    function initGeometry() {
        let vertexIndex = 0;

        for (let i = 0; i < bufferLength; i++) {
            for (let j = 0; j < bufferLength; j++) {
                const u = i / (bufferLength - 1);
                const v = j / (bufferLength - 1);

                const theta = u * Math.PI * 2;
                const phi = v * Math.PI;

                const x = Math.cos(theta) * Math.sin(phi) * 100;
                const y = Math.cos(phi) * 100;
                const z = Math.sin(theta) * Math.sin(phi) * 100;

                positions[vertexIndex++] = x;
                positions[vertexIndex++] = y;
                positions[vertexIndex++] = z;

                if (i < bufferLength - 1 && j < bufferLength - 1) {
                    const currentIndex = i * bufferLength + j;
                    const nextIndex = currentIndex + bufferLength;

                    indices.push(currentIndex, nextIndex, currentIndex + 1);
                    indices.push(nextIndex, nextIndex + 1, currentIndex + 1);
                }
            }
        }

        geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
        geometry.setIndex(indices);
    }

    initGeometry();

    function animate() {
        requestAnimationFrame(animate);

        analyser.getByteFrequencyData(dataArray);

        // Update all vertices based on audio data
        const positions = geometry.attributes.position.array;

        for (let i = 0; i < bufferLength * bufferLength; i++) {
            const value = dataArray[i % bufferLength] / 255;
            positions[i * 3 + 1] = value * 50; // Update y-coordinate
        }

        geometry.attributes.position.needsUpdate = true;

        renderer.render(scene, camera);
    }

    animate();

    // Connect the audio element to the audio context
    source.connect(audioContext.destination);
});
</script>


Based on: Huet, G.. (2002). Śrı̄ Yantra Geometry. Theoretical Computer Science, 281(1–2), 609–628.

% Shri Yantra in Color Postscript
% Lines settings
0.52 setlinewidth % 0.35 for printer
0 setlinecap
0 setlinejoin
1.5 setmiterlimit
0 0 0 setrgbcolor
% Scaling
3.937 % 10 cm in inches
0.72 % 72 units in 1 inch
mul % 1 mm in units
dup scale % new unit=1mm
/UN 100 def % unit=10cm
% Triangles
105 90 translate % Origin at O
/YO 0 def /XO 0 def % O=(0,0)
/YT UN def /XT 0 def % T=(0,1)
% Inputs - Obtained by Newton approximation computed by Caml program
/YQ 66.77 def /XF 12.6 def /YP 46.27 def /XA 18.7 def /YJ 39.8 def
% Coordinates computations
% circle alpha 4*X^2+(2*Y-1)^2=1
/sq {dup mul} def % squaring
/alpha {UN sq Y 2 mul UN sub sq sub sqrt 2 div} def % X=alpha(Y)>0
/XQ /Y YQ def alpha def % Q on alpha; Q determined
% Q & O determine e: Y*XQ=X*YQ
/YF YQ def % F determined
/XJ /Y YJ def alpha def % J on alpha; J determined
% J & T determine h: (1-Y)*XJ=X*(1-YJ)
/YA YQ XA mul XQ div def % A on e determined
/XH YJ XQ mul YQ div def % YH=YJ, H on e determined
/XI XJ UN YQ sub UN YJ sub div mul def % YI=YQ, I on h determined
% XU=0, YU=YQ, U & A determine f: XA*(Y-YQ)=X*(YA-YQ)
/XC XA YQ YP sub YQ YA sub div mul def % YC=YP, C on f determined
% F & H determine g: (Y-YF)*(XH-XF)=(YJ-YF)*(X-XF)
/YV YF YF YJ sub XF mul XH XF sub div add def % XV=0, V on g determined
% XW=0, YW=YP determine W
% F & W determine a: (Y-YP)*XF=(YF-YP)*X
/T1 XA YF YP sub mul def
/T2 XF YQ YA sub mul def
/YD T1 YQ mul T2 YP mul add T1 T2 add div def % D determined as a*f
/T3 XJ YQ YP sub mul def
/T4 XF UN YJ sub mul def
/YG UN T3 mul T4 YP mul add T3 T4 add div def % G determined as a*h
/T5 YQ YJ sub def
/XB XA T5 mul YQ YA sub div def % YB=YJ, B on f determined
% XZ=0, YZ=YA determine Z
% C & Z determine c: (Y-YA)*XC=X*(YP-YA)
/YM YP YA sub XF T5 mul YQ XH XF sub mul add mul T5 YA mul XC mul add
XC T5 mul YP YA sub XH XF sub mul add div def % M determined as c*g
/XM YM YA sub XC mul YP YA sub div def
% I & B determine d: (Y-YJ)*(XI-XB)=(YQ-YJ)*(X-XB)
/YL YJ T5 XB mul XI XB sub div sub def % (0,YL) sur d
/XL XA YL YQ sub mul YA YQ sub div def % L on f determined
/XP XB YP YJ sub XI XB sub mul T5 div add def % P on d determined
/XE XF YM YP sub mul YF YP sub div def % YE=YM, E on a determined
% P & E determine i: (Y-YP)*(XE-XP)=(YM-YP)*(X-XP)
/XS XF YV YP sub mul YF YP sub div def % YS=YV, S on a determined
/XK XF YA YF sub XH XF sub mul YJ YF sub div add def % YK=YA, K on g determined
/XN XP YD YP sub XE XP sub mul YM YP sub div add def % YN=YD, N on i determined
/XR XB YG YJ sub XI XB sub mul T5 div add def % YR=YG, R on d determined
/XD YD YP sub XF mul YF YP sub div def % D on a determined
/XG YG YP sub XF mul YF YP sub div def % G on a determined
% Input is correct if (0,YG) on i: YG=YP-XP*(YM-YP)/(XE-XP)
/red {gsave 1 0 0.1 setrgbcolor fill grestore} def % coloring in red
/blue {gsave 0 0.8 1 setrgbcolor fill grestore} def % coloring in blue
/yellow {gsave 1 1 0.1 setrgbcolor fill grestore} def % coloring in yellow
/eored {gsave 1 0 0.1 setrgbcolor eofill grestore} def % eo-coloring red
/eoblue {gsave 0 0.8 1 setrgbcolor eofill grestore} def % eo-coloring blue
/eoyellow {gsave 1 1 0.1 setrgbcolor eofill grestore} def % eo-coloring yellow
% Bindu - The bindu is not at the center of alpha, but at the center of the
% innermost triangle, assumed to be equilateral
/BIN YP YD 2 mul add 3 div def
newpath 0.6 BIN moveto 0 BIN 0.6 0 360 arc closepath red
% Drawing triangle (trikona)
newpath
0 YP moveto XD YD lineto XD neg YD lineto closepath stroke
% Drawing 8 star (vasukona)
newpath
XN YD moveto XD YD lineto XE YM lineto XE neg YM lineto XD neg YD
lineto XN neg YD lineto 0 YJ lineto closepath
0 YQ moveto XC YP lineto XC neg YP lineto closepath
red stroke
% Drawing first 10 star (dasharayugma)
newpath
0 YG moveto XP YP lineto XC YP lineto XB YJ lineto XB neg YJ lineto
XC neg YP lineto XP neg YP lineto closepath
0 YA moveto XM YM lineto XE YM lineto XF YQ lineto XF neg YQ lineto
XE neg YM lineto XM neg YM lineto closepath
blue stroke
% Drawing second 10 star
newpath
0 YV moveto XH YJ lineto XB YJ lineto XA YA lineto XA neg YA lineto
XB neg YJ lineto XH neg YJ lineto closepath
0 YL moveto XI YQ lineto XF YQ lineto XG YG lineto XG neg YG lineto
XF neg YQ lineto XI neg YQ lineto closepath
red stroke
% Drawing 14 star (manvashra)
newpath
0 0 moveto XQ YQ lineto XI YQ lineto XR YG lineto XG YG lineto
XS YV lineto XS neg YV lineto XG neg YG lineto XR neg YG lineto
XI neg YQ lineto XQ neg YQ lineto closepath
0 YT moveto XJ YJ lineto XH YJ lineto XK YA lineto XA YA lineto
XL YL lineto XL neg YL lineto XA neg YA lineto XK neg YA lineto
XH neg YJ lineto XJ neg YJ lineto closepath
blue stroke
% Padmas
/PI 180 def /D1 7 def /D2 12 def /D3 2 def /D4 2 def /R1 50 def
/R2 R1 D1 add def /R3 R2 D2 add def
/R4 R2 D3 add def /R5 R3 D4 add def
/R6 R5 D4 add def /R7 R6 D4 add def
0 R1 translate % Origin at center of circles
/petal { % We assume we start at X3 Y3 neg
X2 Y2 neg X1 Y1 neg R 0 curveto
X1 Y1 X2 Y2 X3 Y3 curveto
} def % One padma petal
/THETA0 PI 4 div def
% Inner 8-petal padma
/R R2 def
/THETA1 PI 8 div def
/U1 5 def /V1 8 def
/DELTA1 20 def /GAMMA1 5 def
/X1 R2 U1 DELTA1 cos mul sub def
/Y1 U1 DELTA1 sin mul def
/X3 R1 THETA1 cos mul def
/Y3 R1 THETA1 sin mul def
/X2 X3 V1 THETA1 DELTA1 sub cos mul add def
/Y2 Y3 V1 THETA1 DELTA1 sub sin mul add def
gsave
newpath X3 Y3 neg moveto
8 {petal THETA0 rotate} repeat
closepath
% Drawing circle alpha
R1 0 moveto 0 0 R1 0 360 arc closepath
eored stroke
grestore
% Outer 16-petal padma
/R R3 def
/THETA2 PI 16 div def
/U2 4 def /V2 6 def
/DELTA2 15 def /GAMMA2 5 def
/X1 R3 U2 DELTA2 cos mul sub def
/Y1 U2 DELTA2 sin mul def
/X3 R4 THETA2 cos mul def
/Y3 R4 THETA2 sin mul def
/X2 X3 V2 THETA2 DELTA2 sub cos mul add def
/Y2 Y3 V2 THETA2 DELTA2 sub sin mul add def
gsave
newpath X3 Y3 neg moveto
16 {petal THETA1 rotate} repeat
closepath
% Drawing circle beta
R2 0 moveto 0 0 R2 0 360 arc closepath
eoblue stroke
grestore
% Drawing circle gamma
newpath
R3 0 moveto 0 0 R3 0 360 arc closepath
% Drawing 3 outer circles
R5 0 moveto 0 0 R5 0 360 arc closepath
R6 0 moveto 0 0 R6 0 360 arc closepath
stroke
newpath
R7 0 moveto 0 0 R7 0 360 arc closepath
% Squares
/greek {L1 L1 neg moveto
4 {L1 L3 neg lineto
L1 L2 add L3 neg lineto
L1 L2 add L3 L4 add neg lineto
L1 L2 add L5 add L3 L4 add neg lineto
L1 L2 add L5 add L3 L4 add lineto
L1 L2 add L3 L4 add lineto
L1 L2 add L3 lineto
L1 L3 lineto
L1 L1 lineto
90 rotate} repeat
closepath} def % Drawing a greek frieze
/W1 2 def % Spacing between greeks
/W2 3 def
% Inner greek
/L1 R7 def
/L2 W1 4 mul W2 add def
/L3 L1 3 div def
/L4 L1 L3 sub 2 div def
/L5 W2 def
greek eoyellow stroke
/transform {/L1 L1 W1 add def
/L2 L2 W1 2 mul sub def
/L3 L3 W1 add def
/L5 L5 W1 2 mul add def} def
% Middle greek
transform greek stroke
% Outer greek
transform greek stroke
showpage