xxxxxxxxxx
338
i = 0;
let arr = []
let foo;
let artwork = false;
let listen;
let synth, soundLoop, filter;
let song;
let rates = 1;
let type;
let pain;
let shape;
let beatsYas = false;
let MIDIYas = false;
let soundYas = false;
let finaleYas = false;
let beatsIndex
let beatsNote
let MIDINote
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];
let soundIndex;
let soundNote;
let finaleIndex;
let finaleNote;
function preload() {
psych = loadJSON("Psych.json")
afinn = loadJSON("afinn111.json")
song = loadSound('Pad-Solbath.wav');
type = loadFont("Kenia-Regular.ttf")
pain = loadImage("Rembrandt.jpg")
}
function setup() {
createCanvas(windowWidth, windowHeight);
textFont(type)
rectMode(CENTER);
foo = new p5.Speech(); // speech synthesis object
let lang = navigator.language || 'en-US'
let listen = new p5.SpeechRec(lang, gotSpeech);
song.amp(0.03)
song.loop();
// console.log("winndow", width)
// console.log("OG:",pain.width, pain.height )
ratio = pain.height / pain.width
// console.log("ratio:",ratio )
pain.width = windowWidth
pain.height = windowWidth * ratio
// console.log("NEW:",painting.width,painting.height )
// shape = rect(0, 0, width, height, height/10)
// pain.mask(shape)
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.03
typing()
next()
}
if (listen.resultString == 'no' && !artwork) {
rates -= 0.3
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/);
image(pain, 0, 0)
noStroke()
fill(255)
textAlign(CENTER, CENTER);
textSize(55)
text(psych.personality_test[i], windowWidth / 2, windowHeight / 2, windowWidth - 30, 500);
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)
text(arr.length, windowWidth / 2 - 70, 32.5)
textSize(5)
text((psych.personality_test[i - 1]), windowWidth / 2 + 70, 32, 20, 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)
loadPixels()
let d = pixelDensity();
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
for (let i = 0; i < d; i++) {
for (let j = 0; j < d; j++) {
index = 4 * ((y * d + j) * width * d + (x * d + i));
let r = pixels[index];
let g = pixels[index + 1] ;
let b = pixels[index + 2] ;
let bright = (r+g+b)/3
if (beatsYas) {
strokeWeight(2)
console.log("r", r)
r = r+arr[beatsIndex]*50
console.log("r", r)
// line(arr[beatsIndex], beatsNote, arr[beatsIndex] + 100, beatsNote)
}
if (MIDIYas) {
}
if (soundYas) {
}
if (finaleYas) {
}
}
}
}
}
updatePixels()
}
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)
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")
if (!MIDILoop.isPlaying) {
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) {
beatsIndex = (beatsLoop.iterations - 1) % arr.length;
synth.amp(0.8)
beatsNote = (arr[beatsIndex] * 60);
synth.play(beatsNote, 0.6, timeFromNow, 1 / 2);
beatsYas = true;
}
function onMIDILoop(timeFromNow) {
for (let index = 0; index <= arr.length; index++) {
MIDIIndex = map(arr[index], -5, 5, 0, 10)
}
MIDINote = midiToFreq(notePattern[MIDIIndex]);
MIDIsynth.play(MIDINote, 0.8, timeFromNow);
MIDIYas = true;
}
function onSoundLoop(timeFromNow) {
soundIndex = (soundLoop.iterations - 1) % arr.length;
soundNote = (arr[soundIndex] * 100);
synth.play(soundNote, 0.8, timeFromNow, 1 / 4);
soundYas = true;
}
function onFinaleLoop(timeFromNow) {
finaleIndex = (finaleLoop.iterations - 1) % arr.length;
finaleNote = (arr[finaleIndex] * 200);
synth.play(finaleNote, 0.6, timeFromNow, 1 / 16);
finaleYas = true;
}