help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
star-pattern-maker
/
js
RSK World
star-pattern-maker
Star Pattern Maker - HTML5 Canvas + 3D Rendering + Physics Simulation + AI Patterns + Generative Audio + Modern UI + Glassmorphism Design
js
  • 3d-engine.js3.8 KB
  • ai-patterns.js5.6 KB
  • animations.js1.4 KB
  • audio.js1.3 KB
  • controls.js12.2 KB
  • export.js3.9 KB
  • filters.js3.5 KB
  • fractals.js3.5 KB
  • gallery.js2.5 KB
  • main.js1.9 KB
  • particles.js1.4 KB
  • physics.js1.7 KB
  • presets.js2.8 KB
  • randomizer.js1.4 KB
  • renderer.js7 KB
  • shaders.js6.5 KB
  • shapes.js1.7 KB
  • sound-gen.js4.4 KB
  • state.js1.9 KB
  • symmetry.js3.9 KB
  • themes.js1.4 KB
  • timeline.js4.2 KB
  • utils.js546 B
sound-gen.js
js/sound-gen.js
Raw Download
Find: Go to:
/* V15 Sound Generation Module - Pattern Sonification */
import { state } from './state.js';

let audioContext = null;
let currentOscillators = [];

/**
 * Initialize audio context
 */
function initAudioContext() {
    if (!audioContext) {
        audioContext = new (window.AudioContext || window.webkitAudioContext)();
    }
    return audioContext;
}

/**
 * Generate tone from frequency
 */
export function generateTone(frequency, duration = 0.5, type = 'sine') {
    const ctx = initAudioContext();
    const oscillator = ctx.createOscillator();
    const gainNode = ctx.createGain();

    oscillator.type = type;
    oscillator.frequency.value = frequency;

    gainNode.gain.setValueAtTime(0.3, ctx.currentTime);
    gainNode.gain.exponentialRampToValueAtTime(0.01, ctx.currentTime + duration);

    oscillator.connect(gainNode);
    gainNode.connect(ctx.destination);

    oscillator.start(ctx.currentTime);
    oscillator.stop(ctx.currentTime + duration);

    currentOscillators.push(oscillator);

    // Clean up
    setTimeout(() => {
        const index = currentOscillators.indexOf(oscillator);
        if (index > -1) currentOscillators.splice(index, 1);
    }, duration * 1000);
}

/**
 * Map color to frequency
 */
function colorToFrequency(r, g, b) {
    // Map RGB to musical scale
    const hue = rgbToHue(r, g, b);
    const baseFreq = 220; // A3
    const octaves = 3;

    // Map hue (0-360) to frequency
    const noteIndex = Math.floor((hue / 360) * 12); // 12 notes in octave
    const semitone = Math.pow(2, noteIndex / 12);

    return baseFreq * semitone;
}

/**
 * Convert RGB to Hue
 */
function rgbToHue(r, g, b) {
    r /= 255;
    g /= 255;
    b /= 255;

    const max = Math.max(r, g, b);
    const min = Math.min(r, g, b);
    const delta = max - min;

    let hue = 0;

    if (delta !== 0) {
        if (max === r) {
            hue = ((g - b) / delta) % 6;
        } else if (max === g) {
            hue = (b - r) / delta + 2;
        } else {
            hue = (r - g) / delta + 4;
        }
        hue *= 60;
        if (hue < 0) hue += 360;
    }

    return hue;
}

/**
 * Generate sound from pattern
 */
export function patternToSound(canvas) {
    if (!state.soundEnabled) return;

    const ctx = canvas.getContext('2d');
    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    const data = imageData.data;

    // Sample colors from pattern
    const samples = 8;
    const step = Math.floor(data.length / (samples * 4));

    for (let i = 0; i < samples; i++) {
        const idx = i * step * 4;
        const r = data[idx];
        const g = data[idx + 1];
        const b = data[idx + 2];

        const freq = colorToFrequency(r, g, b);
        const delay = i * 0.1;

        setTimeout(() => {
            generateTone(freq, 0.3, 'sine');
        }, delay * 1000);
    }
}

/**
 * Generate melody from geometry
 */
export function geometryToMelody() {
    if (!state.soundEnabled) return;

    const notes = [];
    const baseFreq = 220; // A3

    // Map spikes to notes
    for (let i = 0; i < state.spikes; i++) {
        const semitone = (i % 12);
        const freq = baseFreq * Math.pow(2, semitone / 12);
        notes.push(freq);
    }

    // Play melody
    notes.forEach((freq, i) => {
        setTimeout(() => {
            generateTone(freq, 0.2, 'triangle');
        }, i * 150);
    });
}

/**
 * Stop all sounds
 */
export function stopAllSounds() {
    currentOscillators.forEach(osc => {
        try {
            osc.stop();
        } catch (e) {
            // Already stopped
        }
    });
    currentOscillators = [];
}

/**
 * Create ambient drone from pattern
 */
export function createAmbientDrone() {
    if (!state.soundEnabled) return;

    const ctx = initAudioContext();

    // Create multiple oscillators for richness
    const frequencies = [110, 165, 220]; // A2, E3, A3

    frequencies.forEach((freq, i) => {
        const osc = ctx.createOscillator();
        const gain = ctx.createGain();

        osc.type = 'sine';
        osc.frequency.value = freq;

        gain.gain.setValueAtTime(0, ctx.currentTime);
        gain.gain.linearRampToValueAtTime(0.1, ctx.currentTime + 2);

        osc.connect(gain);
        gain.connect(ctx.destination);

        osc.start(ctx.currentTime);

        currentOscillators.push(osc);
    });
}
187 lines•4.4 KB
javascript

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer