xxxxxxxxxx
257
//all code highly based off of Dan Shiffman's processing tutorials https://www.youtube.com/watch?v=IKB1hWWedMk
//SoundFile file;
var fft;
let angle = 0;
let fft2;
var mic;
var bands = 1024; // resolution of the FFT
var spectrum, spectrum2;
var terrain = [];
var terrain2 = [];
var terrain3 = [];
let zoom = 2;
let peak = 50;
//let notes = ["A7","F6","G6","E6","D6"," "," "," "," "," "," "];
let notes = [
["A5", "C#5", "E5"],
["A5", "C5", "E5"],
["D5", "F#5", "A5"],
["G4", "B5", "D5"],
["E4", "G4", "B4"],
["C3", "E3", "G3"],
["F3", "A3", "C4"]
];
let keynotes = [
["C2", "E2", "G2"],
["F2", "A2", "C2"],
["A2", "C2", "E2"],
["B2", "D2", "F#2"],
["C3", "E3", "G3"],
["F3", "A3", "C3"],
["A3", "C3", "E3"],
["B3", "D3", "F#3"]
];
var lines = 60;
let duration;
let durationk;
let trigger;
var synth;
let synthkey;
let px = 0;
let notecounter = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
//frameRate(15);
//file = new SoundFile(this, "03badnews.mp3");
// file = new SoundFile(this, "popcorn.mp3");
//terrain = new float [lines][bands];
// create fft object
fft = new p5.FFT(0.85, bands);
fft2 = new p5.FFT(0.85, bands);
fft3 = new p5.FFT(0.85, bands);
// create audion in
mic = new p5.AudioIn();
//// start the Audio Input
mic.start();
synth = new p5.PolySynth();
synthkey = new p5.PolySynth();
// synth.AudioVoice = 16;
fft.setInput(mic);
// fft.setInput(synth);
//fft.setInput(synthkey);
fft3.setInput(mic);
for (let y = 0; y < lines; y++) {
terrain[y] = [];
for (let x = 0; x < bands; x++) {
terrain[y][x] = 0;
}
}
for (let y = 0; y < lines; y++) {
terrain2[y] = [];
for (let x = 0; x < bands; x++) {
terrain2[y][x] = 0;
}
}
for (let y = 0; y < lines; y++) {
terrain3[y] = [];
for (let x = 0; x < bands; x++) {
terrain3[y][x] = 0;
}
}
//strokeWeight(2);
// setInterval(chooseNote, 120);
}
function draw() {
background(0);
// fill(255, 255, 0);
noFill();
// noFill();
// translate(-width / 2, 30,0);
// rotateX(radians(-60));
zoom = map(mouseX, 0, width, 2, 100);
// translate(0, 0);
//rotateZ(radians(rotZ));
//-translate(0, -600);
spectrum = fft.analyze();
spectrum2 = fft2.analyze();
spectrum3 = fft3.analyze();
// beginShape();
push();
translate(width / 2, height / 2);
fill(255);
for (let x = 0; x < spectrum.length; x++) {
let amp = map(spectrum[x], 0, 255, 0, 175);
stroke(255,spectrum[x], spectrum[x]);
angle = map(x, 0, spectrum.length, 0, TWO_PI);
rotate(angle);
// console.log(angle);
// vertex(0,100);
// ellipse(0, 0, 5, 5);
if (x == 0) {
// line(0,0,0,amp);
}
if (x >= 1) {
// line(0,amp,0, map(spectrum[x-1],0,255,0,175));
}
point(0, amp+50);
// vertex(0, amp));
}
// endShape();
// vertex(width / 2, ((height / lines) * y + 70))
pop();
// endShape();
// fill(0, 255, 255, 100);
// stroke(255, 255, 0, 127);
// for (let y = 0; y < terrain2.length; y++) {
// beginShape();
// vertex(0, ((height / lines) * y + 70));
// vertex(20, ((height / lines) * y + 70));
// for (let x = 0; x < spectrum2.length; x++) {
// // stroke(255,255,map(x,0,bands/zoom,0,255))
// vertex(map(x, 0, bands / zoom, 20, width), (map(terrain2[y][x], 0, 255, 0, -peak)) + ((height / lines) * y + 70));
// }
// vertex(0, ((height / lines) * y + 70));
// endShape();
// }
// fill(255, 0, 255, 77);
// stroke(0);
// for (let y = 0; y < terrain3.length; y++) {
// beginShape();
// vertex(0, ((height / lines) * y + 70));
// vertex(20, ((height / lines) * y + 70));
// for (let x = 0; x < spectrum3.length; x++) {
// // stroke(255,255,map(x,0,bands/zoom,0,255))
// vertex(map(x, 0, bands / zoom, 20, width), (map(terrain3[y][x], 0, 255, 0, -peak)) + ((height / lines) * y + 60));
// }
// vertex(0, ((height / lines) * y + 70));
// endShape();
// }
// terrain[0] = spectrum;
// for (let j = terrain.length - 1; j > 0; j--) {
// for (let y = 1; y < terrain.length; y++) {
// if (y > 0) {
// terrain[y] = terrain[(y - 1)];
// }
// }
// for (let y = 0; y < terrain.length; y++) {
for (var y = lines - 1; y >= 0; y--) {
//for (var i = 0; i < bands; i++) {
terrain2[0] = spectrum2;
if (y > 0) {
if (y < lines) {
terrain2[y] = terrain2[y - 1];
}
}
}
for (let y = lines - 1; y >= 0; y--) {
//for (var i = 0; i < bands; i++) {
terrain[0] = spectrum;
if (y > 0) {
if (y < lines) {
terrain[y] = terrain[y - 1];
}
}
}
for (let y = lines - 1; y >= 0; y--) {
//for (var i = 0; i < bands; i++) {
terrain3[0] = spectrum3;
if (y > 0) {
if (y < lines) {
terrain3[y] = terrain3[y - 1];
}
}
}
}
function keyPressed() {
chooseKeyNote();
}
function keyReleased() {
synthkey.noteRelease();
}
function mousePressed() {
chooseNote();
}
function mouseReleased() {
synth.noteRelease();
}
function chooseNote() {
// synth.noteRelease();
let chordPicker = floor(random(notes.length));
// console.log(chordPicker);
duration = random(0.2, 1.0);
// play(note, [velocity], [secondsFromNow], [sustainTime])
//synth.play(notes[floor(random(notes.length))], 0.5, .15, .1);
//synth.triggerAttack(notes[floor(random(notes.length))], 0.3, 0, duration);
for (let i = 0; i < 3; i++) {
synth.noteAttack(notes[chordPicker][i], i * 0.2, 0, duration);
}
}
function chooseKeyNote() {
// synth.noteRelease();
let chordPicker2 = floor(random(keynotes.length));
// console.log(chordPicker2);
durationk = random(0.2, 1.0);
// play(note, [velocity], [secondsFromNow], [sustainTime])
//synth.play(notes[floor(random(notes.length))], 0.5, .15, .1);
//synth.triggerAttack(notes[floor(random(notes.length))], 0.3, 0, duration);
for (let i = 0; i < 3; i++) {
synthkey.noteAttack(keynotes[chordPicker2][i], i * 0.2, 0, durationk);
}
}