In 1st code, please add a 2 second delay between clicking the button and the selected cell being displayed, and within that 2 second delay, play the sound from the synth in the 2nd code:
First program:
const sentenceModels = [
"The [building/place] is located on [Street Name].",
"Turn [left/right] at the [building/place].",
"Go straight for [number] blocks.",
"The [building/place] is next to the [another building/place].",
"The [building/place] is across the street from the [another building/place].",
"The [building/place] is at the corner of [Street 1] and [Street 2].",
"The [building/place] is behind the [another building/place].",
"The [building/place] is close to the [another building/place].",
"Take the [bus/train] to [Street Name] and then walk [direction].",
"Can you tell me how to get to the [building/place]?"
];
const exampleData = {
"building/place": ["library", "museum", "restaurant", "bank"],
"Street Name": ["Main St", "First Ave", "Elm St", "Broadway"],
"left/right": ["left", "right"],
"number": [1, 2, 3, 4, 5],
"another building/place": ["post office", "park", "movie theater", "school"],
"Street 1": ["Maple St", "Oak St", "Pine St", "Sunset Blvd"],
"Street 2": ["Second Ave", "Third Ave", "River Rd", "Fifth St"],
"bus/train": ["bus", "train"],
"direction": ["north", "south", "east", "west"]
};
const flatExampleData = [].concat.apply([], Object.values(exampleData));
const numBoxes = flatExampleData.length;
const colors = ["red", "green", "blue", "yellow"];
let buildings = [];
let sentenceWords = [];
let posY;
// function setup() {
// createCanvas(800, 1000);
// textSize(26);
// sentenceWords = buildRandomSentence().split(" ");
// const filteredWords = sentenceWords.filter((word) =>
// word.includes("[") && word.includes("]")
// );
// for (const [i, building] of filteredWords.entries()) {
// buildings.push(
// new Building(
// 10 + i * 120,
// 500,
// building.substring(1, building.length - 1),
// colors[i % colors.length]
// )
// );
// }
// background(220);
// displayText(sentenceWords.join(" "), 10, 400, 700);
// }
function setup() {
createCanvas(800, 1000);
textSize(26);
generateNewSentence();
background(220);
displayText(sentenceWords.join(""), 10, posY, 700);
}
class Building {
constructor(x, y, name, color) {
this.x = x;
this.y = y;
this.width = 100;
this.height = 100;
this.color = color;
this.name = name;
this.dragging = false;
this.offsetX = 0;
this.offsetY = 0;
}
display() {
fill(this.color);
if (this.dragging) {
this.x = mouseX + this.offsetX;
this.y = mouseY + this.offsetY;
}
rect(this.x, this.y, this.width, this.height);
fill(0);
text(this.name, this.x + 5, this.y + 50);
}
contains(x, y) {
return (
x > this.x &&
x < this.x + this.width &&
y > this.y &&
y < this.y + this.height
);
}
}
function draw() {
for (const building of buildings) {
building.display();
}
}
function mouseClicked() {
// If the mouse is clicked within the posY range of the sentence text
if (mouseY >= posY - 26 && mouseY <= posY) {
generateNewSentence();
}
background(220);
displayText(sentenceWords.join(" "), 10, posY, 700);
}
function mousePressed() {
for (const building of buildings) {
if (building.contains(mouseX, mouseY)) {
building.dragging = true;
building.offsetX = building.x - mouseX;
building.offsetY = building.y - mouseY;
}
}
}
function mouseReleased() {
for (const building of buildings) {
building.dragging = false;
}
}
function buildRandomSentence() {
let model = random(sentenceModels);
const regex = new RegExp(Object.keys(exampleData).join("|"), "g");
return model.replace(regex, (match) => random(exampleData[match]));
}
function displayText(str, x, y, maxWidth) {
const words = str.split(" ");
let line = "";
posY = y;
for (let n = 0; n < words.length; n++) {
const testLine = line + words[n] + " ";
const testWidth = textWidth(testLine);
if (testWidth > maxWidth && n > 0) {
text(line, x, posY);
line = words[n] + " ";
posY += textLeading();
} else {
line = testLine;
}
}
text(line, x, posY);
}
function generateNewSentence() {
sentenceWords = buildRandomSentence().split(" ");
const filteredWords = sentenceWords.filter((word) =>
word.includes("[") && word.includes("]")
);
buildings = [];
for (const [i, building] of filteredWords.entries()) {
buildings.push(
new Building(
10 + i * 120,
500,
building.substring(1, building.length - 1),
colors[i % colors.length]
)
);
}
}
This is 2nd program:
const playButton = document.getElementById("playButton");
playButton.addEventListener('click', () => {
playRandomRobotSound();
});
function playRandomRobotSound() {
const context = new AudioContext();
const numNotes = 12;
const duration = 1.5;
const interval = duration / numNotes;
for (let i = 0; i < numNotes; i++) {
const oscillator = context.createOscillator();
const gainNode = context.createGain();
oscillator.connect(gainNode);
gainNode.connect(context.destination);
const frequency = Math.random() * 500 + 200;
oscillator.frequency.setValueAtTime(frequency, context.currentTime);
const startTime = context.currentTime + (interval * i);
const endTime = startTime + interval;
gainNode.gain.setValueAtTime(1, startTime);
gainNode.gain.exponentialRampToValueAtTime(0.001, endTime);
oscillator.start(startTime);
oscillator.stop(endTime);
}
}