xxxxxxxxxx
322
i = 0;
let arr = []
let foo;
let artwork = false;
let listen;
let synth, soundLoop, filter;
let song;
let rates = 1;
let painting;
let shape;
let type;
let notePattern = [52, 53, 55, 57, 59, 60, 62, 64, 65, 67, 69];
//let MIDINotes = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];
function preload() {
psych = loadJSON("Psych.json")
afinn = loadJSON("afinn111.json")
song = loadSound('LPad-Solbath.wav');
type = loadFont("Kenia-Regular.ttf")
painting = loadImage("Rembrandt.jpg")
}
function setup() {
createCanvas(windowWidth, windowHeight);
foo = new p5.Speech(); // speech synthesis object
let lang = navigator.language || 'en-US'
let listen = new p5.SpeechRec(lang, gotSpeech);
song.amp(0.01)
song.play();
//pixelDensity(1)
// console.log("winndow", width)
// console.log("OG:",painting.width,painting.height )
// ratio = painting.height/painting.width
// console.log("ratio:",ratio )
// painting.width = windowWidth
// painting.height = windowWidth * ratio
// console.log("NEW:",painting.width,painting.height )
// console.log("width", width)
// shape = rect(0, 0, width, height, height/10)
// painting.mask(shape)
// console.log("Shapewidth", shape.width)
textFont(type)
let continuous = true;
let interim = false;
listen.start(continuous, interim);
let intervalInBeats = 0.5;
beatsLoop = new p5.SoundLoop(onBeatsLoop, intervalInBeats);
// MIDIosc = new p5.TriOsc();
MIDIenv = new p5.Envelope(0.5, 0.9, 1, 2 );
let intervalInMIDI = 2;
MIDILoop = new p5.SoundLoop(onMIDILoop, intervalInMIDI);
MIDIsynth = new p5.MonoSynth('triangle');
MIDIsynth.amp(1)
let intervalInSound = 0.7;
soundLoop = new p5.SoundLoop(onSoundLoop, intervalInSound);
let intervalInFinale = 1.2;
finaleLoop = new p5.SoundLoop(onFinaleLoop, intervalInFinale);
synth = new p5.MonoSynth();
synth.amp(0.2);
function gotSpeech(){
console.log(listen.resultString)
if (listen.resultString == 'yes' && !artwork){
rates += 0.05
typing()
next()
}
if(listen.resultString == 'no' && !artwork ){
rates -= 0.05
next()
}
}
}
function next(){
let val = round(random(psych.personality_test.length))
foo.speak(psych.personality_test[val]);
i = val
txt.value = psych.personality_test[i];
song.rate(rates)
}
function draw() {
background(198,70,26);
let textinput = txt.value;
let words = textinput.split(/\W/);
// painting.loadPixels();
// loadPixels();
// for (let y = 0; y < height; y++) {
// for (let x = 0; x < width; x++) {
// let i = (x + y * width) * 4;{
// if(beatsCall()){
// console.log("red", pain.pixels[3 + 0])
// }
// if(MIDICall()){
// console.log("green", pain.pixels[3 + 1])
// }
// if(soundCall()){
// console.log("blue", pain.pixels[3 + 2])
// }
// if (finaleCall()){
// console.log("alpha", pain.pixels[3 + 3])
// }
// }
// }
// }
//image(painting, 0, 0)
textAlign(CENTER)
textAlign(CENTER);
textSize(50)
fill(255)
text(psych.personality_test[i], windowWidth /2, windowHeight /2);
textAlign(LEFT);
textSize(15);
text((random(words)), windowWidth/2 - 25, 20)
text((random(words)), windowWidth/2 - 25, 30)
text((random(words)), windowWidth/2 - 25, 40)
text((random(words)), windowWidth/2 - 25, 50)
textSize(20)
stroke(255)
strokeWeight(3)
line(windowWidth /2 - 30, 10, windowWidth /2 - 30, 55)
line(windowWidth /2 + 30, 32.5, windowWidth/2 + 50, 32.5)
line(windowWidth /2 + 30, 10, windowWidth /2 +30, 55)
line(windowWidth /2 - 30, 32.5, windowWidth /2 - 50, 32.5)
noStroke()
fill(255)
text(arr.length, windowWidth/2 - 70, 40)
textSize(5)
text((psych.personality_test[i - 1]), windowWidth/2 + 60, 23, 20, 20);
}
function typing() {
let textinput = txt.value;
let words = textinput.split(/\W/);
console.log("JUST WORDS:", words);
let totalScore = 0;
for (let i = 0; i < words.length; i++) {
var word = words[i].toLowerCase();
if (afinn.hasOwnProperty(word)) {
let score = afinn[word];
console.log("SCORED WORD:", word, score);
totalScore += Number(score);
}
}
console.log("TOTAL SCORE:", totalScore)
// push total score to global Array 'arr'
if (totalScore != 0) {
arr.push(totalScore)
console.log("final array:", arr)
}
if (arr.length > 2) {
beatsCall()
}
if (arr.length > 5) {
MIDICall()
}
if (arr.length > 12) {
soundCall()
}
if (arr.length > 16) {
finaleCall()
}
}
// this is called when arr
function beatsCall() {
console.log("beats called")
if (!beatsLoop.isPlaying) {
beatsLoop.start();
}
}
function MIDICall() {
console.log("Tone called")
// userStartAudio();
//onMIDILoop()
if (!MIDILoop.isPlaying) {
// MIDILoop.stop();
// } else {
// // start the loop
MIDILoop.start();
}
}
function soundCall() {
console.log("sound called")
if (!soundLoop.isPlaying) {
soundLoop.start();
}
}
function finaleCall() {
console.log("finale called")
artwork = true;
if (!finaleLoop.isPlaying) {
finaleLoop.start();
}
}
function onBeatsLoop(timeFromNow) {
let beatsIndex = (beatsLoop.iterations - 1) % arr.length;
synth.amp(0.8)
let note = (arr[beatsIndex] * 60);
synth.play(note, 0.6, timeFromNow, 1/2);
// background(noteIndex * 360 / notePattern.length, 50, 100);
}
function onMIDILoop(timeFromNow) {
// let MIDIIndex = (MIDILoop.iterations - 1) % notePattern.length;
for (let index = 0; index <= arr.length; index ++){
// MIDIIndex = arr[index]
MIDIIndex = map(arr[index], -5, 5, 0, 10)
}
let note = midiToFreq(notePattern[MIDIIndex]);
MIDIsynth.play(note, 0.8, timeFromNow);
}
function onSoundLoop(timeFromNow) {
let soundIndex = (soundLoop.iterations - 1) % arr.length;
let note = (arr[soundIndex] * 100);
synth.play(note, 0.8, timeFromNow, 1/4);
// background(noteIndex * 360 / notePattern.length, 50, 100);
}
function onFinaleLoop(timeFromNow) {
let finaleIndex = (finaleLoop.iterations - 1) % arr.length;
let note = (arr[finaleIndex] * 200);
synth.play(note, 0.6, timeFromNow, 1/16);
// background(noteIndex * 360 / notePattern.length, 50, 100);
}