dickin about with some audio
This commit is contained in:
parent
d37e81ae11
commit
909ba64e1a
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 8.7 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
Before Width: | Height: | Size: 457 B After Width: | Height: | Size: 457 B |
|
Before Width: | Height: | Size: 506 B After Width: | Height: | Size: 506 B |
|
|
@ -4,6 +4,7 @@
|
|||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Mishka Henner - Conductor</title>
|
||||
<link rel="icon" href="/Logo.svg" type="image/svg+xml">
|
||||
<link rel="stylesheet" href="styles.css" />
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
|||
158
src/script.js
158
src/script.js
|
|
@ -1,89 +1,143 @@
|
|||
import { Client, BrowserSocketFactory } from './blitz.js';
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
import { Client, BrowserSocketFactory } from "./blitz.js";
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||
const socketFactory = new BrowserSocketFactory();
|
||||
const client = new Client(socketFactory);
|
||||
client.connect();
|
||||
|
||||
const svgFiles = [
|
||||
{ fileName: 'Gong.svg', yPos: '20%' },
|
||||
{ fileName: 'Orchestral Bass.svg', yPos: '10%' },
|
||||
{ fileName: 'Snare.svg', yPos: '20%' },
|
||||
{ fileName: 'Surdo Napa.svg', yPos: '30%' },
|
||||
{ fileName: 'Surdo.svg', yPos: '25%' },
|
||||
{ fileName: 'Timpani Large.svg', yPos: '40%' },
|
||||
{ fileName: 'Timpani Small.svg', yPos: '30%' },
|
||||
{ fileName: 'Toms.svg', yPos: '25%' },
|
||||
{ fileName: 'Tubular Bells.svg', yPos: '0%' }
|
||||
const instruments = [
|
||||
{ image: "Gong.svg", yPos: "20%", sample: "Gong.mp3" },
|
||||
{
|
||||
image: "Bass.svg",
|
||||
yPos: "10%",
|
||||
sample: "Bass.mp3",
|
||||
},
|
||||
{
|
||||
image: "Snare.svg",
|
||||
yPos: "20%",
|
||||
sample: "Snare.mp3",
|
||||
},
|
||||
{ image: "Surdo Napa.svg", yPos: "30%", sample: "Surdo.mp3" },
|
||||
{
|
||||
image: "Surdo.svg",
|
||||
yPos: "25%",
|
||||
sample: "Surdo.mp3",
|
||||
},
|
||||
{
|
||||
image: "Timpani Large.svg",
|
||||
yPos: "40%",
|
||||
sample: "Timpani.wav",
|
||||
},
|
||||
{
|
||||
image: "Timpani Small.svg",
|
||||
yPos: "30%",
|
||||
sample: "Timpani.wav",
|
||||
},
|
||||
{
|
||||
image: "Toms.svg",
|
||||
yPos: "25%",
|
||||
sample: "Toms.mp3",
|
||||
},
|
||||
{
|
||||
image: "Tubular-Bells.svg",
|
||||
yPos: "0%",
|
||||
sample: "Tubular-Bells.wav",
|
||||
},
|
||||
];
|
||||
|
||||
(async () => {
|
||||
for (let i = 0; i < instruments.length; i++) {
|
||||
try {
|
||||
const response = await fetch(`/assets/sounds/${instruments[i].sample}`);
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
||||
instruments[i][`soundBuffer`] = audioBuffer;
|
||||
console.log(`Loaded ${i}`);
|
||||
console.log(instruments[i][`soundBuffer`]);
|
||||
} catch (error) {
|
||||
console.error(`Failed to load ${i}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
const numStaves = 6;
|
||||
const numFiles = svgFiles.length;
|
||||
const numFiles = instruments.length;
|
||||
const sheetWindow = document.getElementById("music-window");
|
||||
|
||||
for (let i = 1; i <= numStaves; i++) {
|
||||
const staveWrapper = document.createElement('div');
|
||||
const staveWrapper = document.createElement("div");
|
||||
staveWrapper.classList.add(`stave-wrapper`);
|
||||
staveWrapper.setAttribute("id", `stave-wrapper-${i}`);
|
||||
staveWrapper.style.position = 'relative';
|
||||
const staveObject = document.createElement('object');
|
||||
staveObject.type = 'image/svg+xml';
|
||||
staveObject.data = 'assets/svg/Stave.svg';
|
||||
staveObject.classList.add('stave-svg');
|
||||
staveWrapper.style.position = "relative";
|
||||
const staveObject = document.createElement("object");
|
||||
staveObject.type = "image/svg+xml";
|
||||
staveObject.data = "assets/svg/Stave.svg";
|
||||
staveObject.classList.add("stave-svg");
|
||||
|
||||
staveWrapper.appendChild(staveObject);
|
||||
sheetWindow.appendChild(staveWrapper);
|
||||
}
|
||||
|
||||
const timeIndicator = document.querySelector('#time-indicator');
|
||||
|
||||
// Debugging log to ensure the element is found
|
||||
if (timeIndicator) {
|
||||
console.log('timeIndicator element found');
|
||||
} else {
|
||||
console.log('timeIndicator element not found');
|
||||
}
|
||||
const timeIndicator = document.querySelector("#time-indicator");
|
||||
|
||||
timeIndicator.addEventListener("animationiteration", () => {
|
||||
const icons = document.querySelectorAll('.event-icon');
|
||||
const icons = document.querySelectorAll(".event-icon");
|
||||
|
||||
icons.forEach((icon) => {
|
||||
icon.classList.add('fade-out');
|
||||
icon.addEventListener('transitionend', () => {
|
||||
icon.classList.add("fade-out");
|
||||
icon.addEventListener("transitionend", () => {
|
||||
icon.remove();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
client.on('data', (strike) => {
|
||||
const playSound = (num) => {
|
||||
const source = audioContext.createBufferSource();
|
||||
const rando = Math.floor(Math.random() * instruments.length);
|
||||
source.buffer = instruments[num].soundBuffer; // Example: Play the 'Gong' sound
|
||||
source.connect(audioContext.destination);
|
||||
source.start();
|
||||
};
|
||||
|
||||
if (strike){
|
||||
const placeIcon = (region) => {
|
||||
// create random numbers
|
||||
const randomStaveNumber = Math.floor((region % 6) + 1);
|
||||
const randomFileNumber = Math.floor(region);
|
||||
|
||||
const randomStaveNumber = Math.floor(Math.random() * numStaves + 1);
|
||||
const randomFileNumber = Math.floor(Math.random() * numFiles + 1);
|
||||
// Determine the position of the time indicator
|
||||
const rect = timeIndicator.getBoundingClientRect();
|
||||
const sheetLeft = sheetWindow.getBoundingClientRect().left;
|
||||
const xPosition = rect.left + window.scrollX - sheetLeft;
|
||||
|
||||
|
||||
const newObject = document.createElement('div');
|
||||
newObject.classList.add('event-icon');
|
||||
newObject.style.position = 'absolute';
|
||||
// Create the icon div and give it its location data
|
||||
const newObject = document.createElement("div");
|
||||
newObject.classList.add("event-icon");
|
||||
newObject.style.position = "absolute";
|
||||
newObject.style.left = `${xPosition}px`;
|
||||
newObject.style.top = `${svgFiles[randomFileNumber].yPos}`;
|
||||
const eventIcon = document.createElement('object');
|
||||
eventIcon.type = 'image/svg+xml';
|
||||
eventIcon.data = `assets/svg/${svgFiles[randomFileNumber].fileName}`;
|
||||
eventIcon.style.width = '35px';
|
||||
eventIcon.style.height = '35px';
|
||||
newObject.style.top = `${instruments[randomFileNumber].yPos}`;
|
||||
|
||||
// create the icon
|
||||
const eventIcon = document.createElement("object");
|
||||
eventIcon.type = "image/svg+xml";
|
||||
eventIcon.data = `assets/svg/${instruments[randomFileNumber].image}`;
|
||||
eventIcon.style.width = "35px";
|
||||
eventIcon.style.height = "35px";
|
||||
|
||||
// append icon to div
|
||||
newObject.appendChild(eventIcon);
|
||||
|
||||
const staveWrapper = document.getElementById(`stave-wrapper-${strike.region + 1 % 6}`);
|
||||
console.log(strike.region);
|
||||
// select the staveWrapper in which to place the div
|
||||
const staveWrapper = document.getElementById(
|
||||
`stave-wrapper-${randomStaveNumber}`
|
||||
);
|
||||
|
||||
// append the div to the staveWrapper
|
||||
staveWrapper.appendChild(newObject);
|
||||
}
|
||||
};
|
||||
|
||||
client.on("data", (strike) => {
|
||||
placeIcon(strike.region % 9);
|
||||
playSound(strike.region % 9);
|
||||
});
|
||||
|
||||
client.connect();
|
||||
|
||||
})
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue