Sound

This was the sketch Julia Qi and I made. We started together exploring what we learned in class implamenting the drums. I wanted something which pitched up over time and kind of ascended up into space. Trying to figure this out, I wrote the concatinated drum line. I planned to revsit it, but was so busy in this part of the semester that I let it be. Julia wrote the melody.

I used claude to generate the start and stop button. The code before I did so is written below:


let drum
let factor = 1
let minInterval = 10
let maxInterval = 60
let interval = maxInterval
let beat = 60
let melodyNotes = [
  300,200,800,700,500,400,200,100, 
  500,400,800,700,500,400,100,200,
  300,200,800,700,500,400,200,100, 
  500,400,800,700,500,400,100,200,
  300,200,800,700,500,400,200,100, 
  500,400,800,700,500,400,100,200,
  300,200,800,700,500,400,200,100, 
  500,400,800,700,500,400,100,200,
]
let melodyRhythm = [1, 1, 1, 1, 1, 1, 1,1,
                  1, 1, 1, 1, 1, 1, 1, 1,
                   0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
                    0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
                   0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,
                   0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,
                   1,0.25,1,0.25,1,0.25,1,0.25,
                   1,0.25,1,0.25,1,0.25,1,0.25];
let currentNote = 0;
let melodyOsc;
let isTransitioning = false; 
function preload() {
  drum = loadSound('crisp.mp3');
}
function setup() {
  createCanvas(windowWidth, windowHeight);
  //h = height/sounds.length;
  noStroke();
   melodyOsc = new p5.Oscillator('triangle');
  melodyOsc.amp(0.12);
  melodyOsc.start(); 
  melodyOsc.freq(melodyNotes[0]); 
  noiseSeed(1);
}
function draw() {

  if (frameCount%beat == 1) {
      playSound(.1);
      console.log(1);
    }
  if (frameCount % floor(beat / 3) == 1) {
    playSound(2)
    console.log(2);
  }
  if (frameCount % floor(beat / 6) == 1) {
    playSound(3)
    console.log(3);
  }

   if (frameCount % (beat * melodyRhythm[currentNote]) === 1 && !isTransitioning) {
    let nextNote = (currentNote + 1) % melodyNotes.length;
    smoothTransition(melodyNotes[currentNote], melodyNotes[nextNote], 0.3); 
    currentNote = nextNote;
  }
}
function playSound(interval){
  drum.rate(interval);
  let volNoise = 0.8 + noise(frameCount * 0.03) * 0.4;
  drum.setVolume((interval/4) * volNoise);
  drum.play();
}
function smoothTransition(fromFreq, toFreq, duration) {
  isTransitioning = true;
  let startTime = millis();
  let transitionInterval = setInterval(() => {
    let elapsed = millis() - startTime;
    let progress = constrain(elapsed / (duration * 1000), 0, 1);
    let baseFreq = fromFreq + (toFreq - fromFreq) * (1 - cos(progress * PI)) / 2;
    let noiseVal = noise(frameCount * 0.01) * 40 - 20; 
    let currentFreq = baseFreq + noiseVal;
    melodyOsc.freq(currentFreq);

    if (progress === 1) {
      clearInterval(transitionInterval);
      isTransitioning = false;
    }
  }); 
}