xxxxxxxxxx
55
let osc, distort, playing, amount;
let amp = 0.7, freq = 230;
let oversamples = ['none', '2x', '4x']
let oversample = 0;
function setup() {
let cnv = createCanvas(400, 400);
cnv.mousePressed(playOscillator);
osc = new p5.Oscillator('sine');
osc.disconnect();
distort = new p5.Distortion(0.25, 'none')
distort.process(osc);
}
function draw() {
background(220)
// freq = constrain(map(mouseX, 0, width, 100, 500), 100, 500);
amp = constrain(map(mouseX, 0, width, 0, 1), 0, 1);
amount = constrain(map(mouseY, height, 0, 0, 1), 0, 1);
text('tap to play', 20, 20);
text('freq: ' + freq, 20, 40);
text('amp: ' + amp, 20, 60);
text('amount: ' + amount, 20, 80);
if (playing) {
// smooth the transitions by 0.1 seconds
osc.freq(freq, 0.1);
osc.amp(amp, 0.1);
distort.set(amount, oversamples[oversample]);
}
}
function playOscillator() {
// starting an oscillator on a user gesture will enable audio
// in browsers that have a strict autoplay policy.
// See also: userStartAudio();
osc.start();
playing = true;
}
function keyPressed(){
if(key==' '){
oversample = (oversample + 1)%3;
print(oversamples[oversample])
}
}
function mouseReleased() {
// ramp amplitude to 0 over 0.5 seconds
osc.amp(0, 0.5);
playing = false;
}