Skip to content

Commit

Permalink
Oscillator fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tylermaginnis authored Sep 15, 2024
1 parent 5afcc06 commit e1099e1
Showing 1 changed file with 42 additions and 36 deletions.
78 changes: 42 additions & 36 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -307,41 +307,46 @@ <h1>Advanced Organ Composition with Markov Chains</h1>

// Function to create a rich organ sound with careful layering
function createOrganOscillator(frequency, startTime, duration, isChord = false) {
const osc1 = audioContext.createOscillator();
const gainNode = audioContext.createGain();

// Create filters
const lowPassFilter = audioContext.createBiquadFilter();
lowPassFilter.type = 'lowpass';
lowPassFilter.frequency.setValueAtTime(2000, startTime); // Set cutoff frequency for low pass

const highPassFilter = audioContext.createBiquadFilter();
highPassFilter.type = 'highpass';
highPassFilter.frequency.setValueAtTime(200, startTime); // Set cutoff frequency for high pass

// Use a sine wave (for the smoothest organ-like sound)
osc1.type = 'sine';
osc1.frequency.setValueAtTime(frequency, startTime);

const targetGain = isChord ? 0.25 : 0.7; // Lowered gain for chords
gainNode.gain.setValueAtTime(0, startTime);
gainNode.gain.linearRampToValueAtTime(targetGain, startTime + 0.05); // Smooth attack
gainNode.gain.setValueAtTime(targetGain, startTime + duration - 0.05); // Sustain
gainNode.gain.linearRampToValueAtTime(0, startTime + duration); // Release

// Connect nodes: Oscillator -> High-Pass -> Low-Pass -> Gain -> Destination
osc1.connect(highPassFilter);
highPassFilter.connect(lowPassFilter);
lowPassFilter.connect(gainNode);
gainNode.connect(audioContext.destination);

osc1.start(startTime);
osc1.stop(startTime + duration);

oscillators.push({ osc1, gainNode });
logToConsole(`Organ ${isChord ? "Chord" : "Melody"} played: <span class="frequency">${frequency.toFixed(2)}Hz</span>`, 'info');
}

const osc1 = audioContext.createOscillator();
const osc2 = audioContext.createOscillator();
const gainNode = audioContext.createGain();

// Create filters
const lowPassFilter = audioContext.createBiquadFilter();
lowPassFilter.type = 'lowpass';
lowPassFilter.frequency.setValueAtTime(4000, startTime); // Increased cutoff frequency for a brighter sound

const highPassFilter = audioContext.createBiquadFilter();
highPassFilter.type = 'highpass';
highPassFilter.frequency.setValueAtTime(200, startTime); // Keep high pass frequency

// Use a sine wave and triangle wave for richness
osc1.type = 'sine';
osc2.type = 'triangle'; // Slightly detuned for a richer sound
osc1.frequency.setValueAtTime(frequency, startTime);
osc2.frequency.setValueAtTime(frequency * 1.005, startTime); // Small detuning

const targetGain = isChord ? 0.2 : 0.5; // Softer for chords
gainNode.gain.setValueAtTime(0, startTime);
gainNode.gain.linearRampToValueAtTime(targetGain, startTime + 0.1); // Smoother attack
gainNode.gain.setValueAtTime(targetGain, startTime + duration - 0.1); // Sustain
gainNode.gain.linearRampToValueAtTime(0, startTime + duration); // Smoother release

// Connect nodes: Oscillators -> High-Pass -> Low-Pass -> Gain -> Destination
osc1.connect(highPassFilter);
osc2.connect(highPassFilter);
highPassFilter.connect(lowPassFilter);
lowPassFilter.connect(gainNode);
gainNode.connect(audioContext.destination);

osc1.start(startTime);
osc2.start(startTime);
osc1.stop(startTime + duration);
osc2.stop(startTime + duration);

oscillators.push({ osc1, osc2, gainNode });
logToConsole(`Organ ${isChord ? "Chord" : "Melody"} played: <span class="frequency">${frequency.toFixed(2)}Hz</span>`, 'info');
}

function playChord(chord, currentTime) {
logBox(`=== Chord Strategy: <span class="chord">${currentChordStrategy.toUpperCase()}</span> ===`);
Expand Down Expand Up @@ -401,8 +406,9 @@ <h1>Advanced Organ Composition with Markov Chains</h1>
clearTimeout(schedulerId);
logToConsole('Music playback stopped.', 'warn');

oscillators.forEach(({ osc1, gainNode }) => {
oscillators.forEach(({ osc1, osc2, gainNode }) => {
osc1.stop();
osc2.stop();
gainNode.disconnect();
});
oscillators = [];
Expand Down

0 comments on commit e1099e1

Please sign in to comment.