xxxxxxxxxx
161
let osc; // single oscillator
let env; // single envelope
let pluckOsc; // additional oscillator for plucking sounds
let pluckEnv; // envelope for plucking
let melodyOsc; // additional oscillator for soft melodic tones
let melodyEnv; // envelope for melody
let rhythmPattern = [1, 0, 1, 0]; // simplified rhythm pattern (simplified 4/4)
let rhythmIndex = 0;
let beatDuration = 0.5; // tempo (500ms per beat)
let duration = 60 * 1000; // 60 seconds in milliseconds
let startTime;
let oceanWave; // ocean wave sound
let reverb; // reverb effect
function preload() {
// Load the ocean wave sound
oceanWave = loadSound('1.mp3');
}
function setup() {
createCanvas(400, 200);
frameRate(30);
noLoop(); // no visuals
// seed random generator for reproducibility
randomSeed(0);
// create a single oscillator
osc = new p5.Oscillator('sine');
// envelope for shaping sound
env = new p5.Env();
env.setADSR(0.01, 0.05, 0.15, 0.25); // Softer attack and quick decay for rhythm
// initialize the oscillator with no immediate sound
osc.start();
// create another oscillator for plucking sounds
pluckOsc = new p5.Oscillator('triangle');
pluckEnv = new p5.Env();
pluckEnv.setADSR(0.01, 0.05, 0.2, 0.4); // short and snappy pluck-like envelope
pluckOsc.start();
pluckOsc.amp(0); // initially silent
// create an additional oscillator for melodic tones
melodyOsc = new p5.Oscillator('sine');
melodyEnv = new p5.Env();
melodyEnv.setADSR(0.05, 0.2, 0.4, 0.6); // Softer, longer tones for melody
melodyOsc.start();
melodyOsc.amp(0); // initially silent
// Add reverb for natural acoustic feel
reverb = new p5.Reverb();
reverb.process(pluckOsc, 3, 2); // 3-second reverb time, 2-second decay
reverb.process(melodyOsc, 3, 2); // Add reverb to melody
// Play the ocean wave sound as a loop
oceanWave.loop();
oceanWave.setVolume(1.0); // Maximize wave sound volume
startTime = millis();
playComposition();
}
function playComposition() {
// schedule sound updates over time
let interval = setInterval(() => {
if (millis() - startTime >= duration) {
endComposition();
clearInterval(interval); // stop the interval
} else {
updateSounds();
}
}, beatDuration * 1000); // update every 500ms (slower tempo)
// rhythm pattern with gaps
let rhythmInterval = setInterval(() => {
playRhythms();
if (millis() - startTime >= duration) {
clearInterval(rhythmInterval); // stop rhythm pattern at the end
}
}, beatDuration * 1000); // update rhythm every 500ms
// Add a random plucking sound periodically
let pluckInterval = setInterval(() => {
if (millis() - startTime >= duration) {
clearInterval(pluckInterval); // stop plucking sounds at the end
} else {
playPluck();
}
}, random(2000, 4000)); // random interval between 2-4 seconds
// Add soft melodic tones periodically
let melodyInterval = setInterval(() => {
if (millis() - startTime >= duration) {
clearInterval(melodyInterval); // stop melody at the end
} else {
playMelody();
}
}, random(3000, 5000)); // random interval between 3-5 seconds
}
function updateSounds() {
// randomly change frequency for evolving sound
osc.freq(random(100, 200)); // simple melodic line
// introduce amplitude variation for glitchy feel
osc.amp(random(0.01, 0.03), 0.1); // Greatly reduced amplitude for subtle tones
}
function playRhythms() {
// Skip the first beat by starting only after the first rhythmIndex cycle
if (rhythmIndex !== 0) {
let note = rhythmPattern[rhythmIndex];
if (note === 1) {
playSound();
}
}
rhythmIndex = (rhythmIndex + 1) % rhythmPattern.length;
}
function playSound() {
// Randomize frequency for tonal shifts
let freq = random(150, 400); // Softer melodic tones
// set oscillator frequency
osc.freq(freq);
// trigger envelope with short bursts
env.play(osc, 0, random(0.01, 0.03)); // Softer attack and lower amplitude
}
function playPluck() {
// Randomize pluck frequency to mimic string sounds
let pluckFreq = random(200, 600);
pluckOsc.freq(pluckFreq);
pluckEnv.play(pluckOsc, 0, 0.1); // Short and snappy pluck
}
function playMelody() {
// Randomize melody frequency to create soft, soothing tones
let melodyFreq = random(300, 500);
melodyOsc.freq(melodyFreq);
melodyEnv.play(melodyOsc, 0, 0.5); // Smooth and gentle melody
}
function endComposition() {
osc.amp(0, 4); // fade out oscillator
noLoop();
setTimeout(() => {
osc.stop();
pluckOsc.stop(); // stop plucking oscillator
melodyOsc.stop(); // stop melody oscillator
oceanWave.stop(); // stop the ocean wave sound
}, 4000); // stop oscillators and wave after fade-out
}