xxxxxxxxxx
76
let carrier; // this is the oscillator we will hear
let modulator; // this oscillator will modulate the amplitude of the carrier
let fft; // we'll visualize the waveform
function setup() {
createCanvas(windowWidth, windowHeight);
noFill();
background(accelerationX*10,accelerationY*10, accelerationZ*10); // alpha
carrier = new p5.Oscillator(); // connects to master output by default
carrier.freq(340);
carrier.amp(0);
// carrier's amp is 0 by default, giving our modulator total control
carrier.start();
modulator = new p5.Oscillator('sine');
modulator.disconnect(); // disconnect the modulator from master output
modulator.freq(5);
modulator.amp(1);
modulator.start();
// Modulate the carrier's amplitude with the modulator
// Optionally, we can scale the signal.
carrier.amp(modulator.scale(-1, 1, 1, -1));
// create an fft to analyze the audio
fft = new p5.FFT();
}
function draw() {
// background(30, 30, 30, 100); // alpha
background(accelerationX*100,accelerationY*100, accelerationZ*100);
// map mouseY to moodulator freq between 0 and 20hz
let modFreq = map(accelerationY, -0.8, 0.8, 10, 0);
// let modFreq=accelerationY;
modulator.freq(modFreq);
let modAmp = map(accelerationX, -0.8,0.8, 0, 1);
// let modAmp = accelerationX;
modulator.amp(modAmp, 0.01); // fade time of 0.1 for smooth fading
// analyze the waveform
waveform = fft.waveform();
// draw the shape of the waveform
drawWaveform();
drawText(modFreq, modAmp);
}
function drawWaveform() {
stroke(240);
strokeWeight(4);
beginShape();
for (let i = 0; i < waveform.length; i++) {
let x = map(i, 0, waveform.length, 0, width);
let y = map(waveform[i], -1, 1, -height / 2, height / 2);
vertex(x, y + height / 2);
}
endShape();
}
function drawText(modFreq, modAmp) {
strokeWeight(1);
text('Modulator Frequency: ' + modFreq.toFixed(3) + ' Hz', 20, 20);
text('Modulator Amplitude: ' + modAmp.toFixed(3), 20, 40);
text('x '+accelerationX, windowWidth-40,20);
text('y '+accelerationY, windowWidth-40,40);
text('z '+accelerationZ, windowWidth-40,60);
}