xxxxxxxxxx
438
//generative fiction key:
// e9638b34224e18383feedd02d101c6dc
//wordnik apiKey:
//vw1nl7ehkszm2x0xkmz9axtmczoqoa86mzsdo1zw8hw3jgvz4
//https://idmnyu.github.io/p5.js-speech/
//https://www.youtube.com/watch?v=v0CHV33wDsI&list=PLRqwX-V7Uu6aDUo_ia-Vq2UZZGaxJ9nRo&index=4
let plot;
let num = 0;
let story, story1, story2, story3, story4, story5;
let beginning;
let start;
let zheng, cha, dao;
let rows;
let columns;
let C;
let climax;
let api = "https://api.openweathermap.org/data/2.5/weather?q=";
let usCity;
let apiKey = "&appid=e9638b34224e18383feedd02d101c6dc";
let unit = "&units=metric";
let cityweather;
let thecity;
let cityname, citystate, population;
let weatherdescription, coordinate, temperature, humidity, visibility;
let m2, m3, m4, m5, m6, m7;
let main2 = [];
let main3 = [];
let main4 = [];
let main5 = [];
let main6 = [];
let main7 = [];
let timer;
let totaltime = 50;
let insert = 0;
let passedtime = 0;
let font1, font2;
let title = "WHO KILLS WHO?";
let titleArray = [];
let r = 350;
let theta = 0;
let reader = new p5.Speech();
// let reader = new p5.Speech(ready);
let voicename;
let voicenames = ["Alex", "Karen", "Daniel", "Fred", "Fiona", "Rishi", "Samantha", "Tessa", "Google UK English Female", "Google US English", "Google UK English Male", "Victoria"];
// function ready() {
// console.log(reader.voices);
// }
let train;
let num1 = 0;
function preload() {
plot = loadTable("Who Kills Who - Sheet1.csv");
usCity = loadJSON("https://raw.githubusercontent.com/dariusk/corpora/9bb62927951f79bec2454f29d71b6e9b28d874b1/data/geography/us_cities.json");
font1 = loadFont("Capture it.ttf");
font2 = loadFont("cour.ttf");
}
function setup() {
createCanvas(600, 600);
fill(255);
rows = plot.getRowCount();
columns = plot.getColumnCount();
voicename = voicenames[int(random(voicenames.length))];
reader.setVoice(voicename);
// print(voicename);
reader.speak('Who kills who.');
titleArray = title.split("");
// print(titleArray.length);
train = new Train();
}
function draw() {
background(0);
switch (num) {
case 1:
// textSize(26);
textAlign(CENTER);
// textFont(font1);
// text(title, width / 2, height / 2);
translate(width / 2, height / 2);
rotate(-QUARTER_PI * 3);
for (let i = 0; i < titleArray.length; i++) {
rotate(TWO_PI / 15);
push();
translate(50, r * cos(theta));
for (let j = -25; j < 100; j += 25) {
// text(titleArray[i], 0, 100);
text(titleArray[i], 0, j);
}
pop();
}
if (theta >= -3) {
theta -= 0.01;
} else {
rotate(QUARTER_PI * 3 + TWO_PI / 15);
text(title, 0, 0);
}
break;
case 2:
textFont(font2);
textSize(14);
textAlign(LEFT);
textLeading(20);
if (askWeather) {
text(`${cityname.toUpperCase()}, ${citystate.toUpperCase()}\n${coordinate}\npopulation, ${population}\n\n${weatherdescription}\n${temperature}\nhumidity, ${humidity}\nvisibility, ${visibility}`, 50, 50);
}
break;
case 3:
textSize(16);
textLeading(18);
// text(beginning, 50, 50, 500, 500);
text(beginning.substring(0, num1), 50, 50, 500, 500);
if (num1 < beginning.length) {
num1++;
}
// passedtime = millis() - timer;
// if (passedtime > totaltime && insert <= main2.length) {
// main2.splice(main2.length, 0, m2[insert]);
// insert++;
// }
// text(main2.join(' '), 50, 50, 500, 500);
break;
case 4:
// text(story1, 50, 50, 500, 500);
passedtime = millis() - timer;
if (passedtime > totaltime && insert <= main3.length) {
main3.splice(main3.length, 0, m3[insert]);
insert++;
timer = millis();
}
text(main3.join(' '), 50, 50, 500, 500);
break;
case 5:
// text(story2, 50, 50, 500, 500);
passedtime = millis() - timer;
if (passedtime > totaltime && insert <= main4.length) {
main4.splice(main4.length, 0, m4[insert]);
insert++;
timer = millis();
}
text(main4.join(' '), 50, 50, 500, 500);
break;
case 6:
// text(story3, 50, 50, 500, 500);
passedtime = millis() - timer;
if (passedtime > totaltime && insert <= main5.length) {
main5.splice(main5.length, 0, m5[insert]);
insert++;
timer = millis();
}
text(main5.join(' '), 50, 50, 500, 500);
break;
case 7:
// text(story4, 50, 50, 500, 500);
passedtime = millis() - timer;
if (passedtime > totaltime && insert <= main6.length) {
main6.splice(main6.length, 0, m6[insert]);
insert++;
timer = millis();
}
text(main6.join(' '), 50, 50, 500, 500);
break;
case 8:
// text(story5, 50, 50, 500, 500);
text(story5.substring(0, num1), 50, 50, 500, 500);
if (num1 < story5.length) {
num1++;
}
// passedtime = millis() - timer;
// if (passedtime > totaltime && insert <= main7.length) {
// main7.splice(main7.length, 0, m7[insert]);
// insert++;
// timer = millis();
// }
// text(main7.join(' '), 50, 50, 500, 500);
break;
default:
textSize(26);
textFont(font1);
text("on the train...", width / 5, height / 4);
beginShape();
vertex(0, height);
vertex(width+3, height - sqrt(3) / 3 * height);
vertex(width+3, height);
endShape();
train.show();
train.move();
train.check();
}
}
class Train {
constructor() {
this.x = 0;
this.y = height;
}
show() {
push();
rotate(-PI / 6);
for (let i = 0; i < 515; i += 130) {
rect(this.x + i - width / 2, this.y - 135, 125, 50);
}
pop();
}
move() {
this.x += 5;
}
check() {
if (this.x >= width + 150) {
this.x = -150 * 4;
}
}
}
function askWeather() {
if (cityname) {
let url = api + cityname + apiKey + unit;
loadJSON(url, getData);
}
}
function getData(d) {
cityweather = d;
// print(cityweather);
weatherdescription = cityweather.weather[0].description;
coordinate = `coordinate, ${cityweather.coord.lon}, ${cityweather.coord.lat}`;
temperature = `${cityweather.main.temp}°C`;
humidity = `${cityweather.main.humidity}%`;
visibility = `${cityweather.visibility} `;
// print("weather description: " + weatherdescription);
}
function mousePressed() {
num++;
num = num % 9;
// print("case: " + num);
insert = 0;
passedtime = 0;
timer = millis();
num1 = 0;
if (num == 1) {
startpoint();
thecity = usCity.cities[int(random(usCity.cities.length))];
cityname = thecity.city;
citystate = thecity.state;
population = thecity.population;
// print(thecity);
askWeather();
main2 = [];
main3 = [];
main4 = [];
main5 = [];
main6 = [];
main7 = [];
}
if (num == 0) {
voicename = voicenames[int(random(voicenames.length))];
reader.setVoice(voicename);
// print(voicename);
reader.speak('Who kills who.');
}
if (num == 1) {
theta = 0;
// let voice = random(reader.voices);
// reader.setVoice(voice.name);
// print(voice.name + voice.lang);
voicename = voicenames[int(random(voicenames.length))];
reader.setVoice(voicename);
reader.speak('Who is the killer...');
}
if (num == 2) {
reader.speak(cityname + ", " + citystate + ".. Welcome aboard.");
}
if (num == 8) {
reader.speak('Screaming......' + '. ' + story5);
}
}
function keyPressed() {
if (num == 2) {
// reader.setVoice(voicename);
reader.speak("Population," + population + ". " + " Weather, " + weatherdescription + ".. " + temperature);
}
if (num == 3) {
voicename = voicenames[int(random(voicenames.length))];
reader.setVoice(voicename);
// print(voicename);
reader.speak(beginning);
}
if (num == 4) {
voicename = voicenames[int(random(voicenames.length))];
reader.setVoice(voicename);
// print(voicename);
reader.speak(story1);
}
if (num == 5) {
voicename = voicenames[int(random(voicenames.length))];
reader.setVoice(voicename);
// print(voicename);
reader.speak(story2);
}
if (num == 6) {
voicename = voicenames[int(random(voicenames.length))];
reader.setVoice(voicename);
// print(voicename);
reader.speak(story3);
}
if (num == 7) {
voicename = voicenames[int(random(voicenames.length))];
reader.setVoice(voicename);
// print(voicename);
reader.speak(story4);
}
if (num == 8) {
voicename = voicenames[int(random(voicenames.length))];
reader.setVoice(voicename);
// print(voicename);
reader.speak(story5);
}
}
function startpoint() {
start = int(random(rows));
beginning = plot.getString(start, 0);
// print(beginning);
m2 = split(beginning, " ");
if (start == 0) {
zhengxu();
} else if (start == 1) {
chaxu();
} else {
daoxu();
}
// print("start: " + start);
// print(beginning + story);
}
function character() {
C = int(random(rows));
// print("C: " + C);
if (C == 0) {
climax = plot.getString(int(random(rows - 1)), 3);
} else if (C == 1) {
climax = plot.getString(int(random(rows - 1)), 4);
} else if (C == 2) {
climax = plot.getString(int(random(rows)), 5);
} else if (C == 3) {
climax = plot.getString(int(random(rows - 1)), 6);
} else {
climax = plot.getString(int(random(rows)), 7);
}
}
function zhengxu() {
character();
story1 = plot.getString(int(random(rows)), 1);
story2 = plot.getString(C, 2);
story3 = climax;
story4 = plot.getString(0, 9);
story5 = plot.getString(C, int(random(10, 11.9999)));
m3 = split(story1, " ");
// print(m3);
m4 = split(story2, " ");
m5 = split(story3, " ");
m6 = split(story4, " ");
m7 = split(story5, " ");
// zheng = story1 + story2 + story3 + story4 + story5;
// story = zheng;
// print(zheng);
// print(story);
}
function chaxu() {
character();
story1 = plot.getString(int(random(rows - 2)), 8);
story2 = "It's a train on Staurday morning as usual. Not much passengers aboard. " + plot.getString(0, 0);
story3 = plot.getString(int(random(rows)), 1) + plot.getString(C, 2);
story4 = climax;
story5 = plot.getString(C, int(random(10, 11.9999)));
m3 = split(story1, " ");
// print(m3);
m4 = split(story2, " ");
m5 = split(story3, " ");
m6 = split(story4, " ");
m7 = split(story5, " ");
// cha = story1 + story2 + story3 + story4 + story5;
// story = cha;
// print(story);
// print(cha);
}
function daoxu() {
character();
story1 = plot.getString(0, 9);
story2 = "The police ask each suspect about the things happen on the train that day. " + plot.getString(0, 0);
story3 = plot.getString(int(random(rows)), 1) + plot.getString(C, 2);
story4 = climax;
story5 = plot.getString(C, int(random(10, 11.9999)));
m3 = split(story1, " ");
// print(m3);
m4 = split(story2, " ");
m5 = split(story3, " ");
m6 = split(story4, " ");
m7 = split(story5, " ");
// dao = story1 + story2 + story3 + story4 + story5;
// story = dao;
// print(story);
// print(dao);
}