xxxxxxxxxx
249
i = 0;
let arr = []
let foo;
let artwork = false;
let listen;
let synth, soundLoop, filter;
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")
}
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);
let continuous = true;
let interim = false;
listen.start(continuous, interim);
let intervalInBeats = 0.4;
beatsLoop = new p5.SoundLoop(onBeatsLoop, intervalInBeats);
// MIDIosc = new p5.TriOsc();
MIDIenv = new p5.Envelope();
let intervalInMIDI = 0.2;
MIDILoop = new p5.SoundLoop(onMIDILoop, intervalInMIDI);
MIDIsynth = new p5.MonoSynth();
MIDIsynth.amp(1)
let intervalInSound = 0.3;
soundLoop = new p5.SoundLoop(onSoundLoop, intervalInSound);
let intervalInFinale = 0.4;
finaleLoop = new p5.SoundLoop(onFinaleLoop, intervalInFinale);
filter = new p5.BandPass();
synth = new p5.MonoSynth();
synth.amp(0.2);
synth.connect(filter);
function gotSpeech(){
console.log(listen.resultString)
if (listen.resultString == 'yes' && !artwork){
typing()
next()
}
if(listen.resultString == 'no' && !artwork ){
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];
}
function draw() {
background(255);
textAlign(CENTER)
textStyle(ITALIC)
textAlign(CENTER);
textSize(50)
text(psych.personality_test[i], windowWidth /2, windowHeight /2);
textSize(5);
}
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;
let note = (arr[beatsIndex] * 200);
synth.play(note, 0.6, timeFromNow, 1/8);
// background(noteIndex * 360 / notePattern.length, 50, 100);
// synth.Vol(0.2)
}
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.5, timeFromNow);
// MIDIenv.ramp(MIDIsynth, 0, 1.0, 0);
// background(noteIndex * 360 / notePattern.length, 50, 100);
// for (let index = 0; index <= arr.length; index ++){
// // MIDIIndex = arr[index]
// MIDIIndex = map(arr[index], -5, 5, 0, 10)
// if (index > arr.length){
// index = 0
// }
// }
// MIDIosc.start();
// MIDIVal = MIDINotes[MIDIIndex];
// MIDIfreq = midiToFreq(MIDIVal);
// MIDIosc.freq(MIDIfreq);
// MIDIenv.ramp(MIDIosc, 0, 1.0, 0);
// //MIDIIndex++;
}
function onSoundLoop(timeFromNow) {
let soundIndex = (soundLoop.iterations - 1) % arr.length;
let note = (arr[soundIndex] * 400);
synth.play(note, 0.6, timeFromNow, 1/10);
// background(noteIndex * 360 / notePattern.length, 50, 100);
}
function onFinaleLoop(timeFromNow) {
let finaleIndex = (finaleLoop.iterations - 1) % arr.length;
let note = (arr[finaleIndex] * 600);
synth.play(note, 0.6, timeFromNow, 1/5);
// background(noteIndex * 360 / notePattern.length, 50, 100);
}