Skip to content

Commit

Permalink
Optimize audio processing code and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmiau committed Feb 22, 2024
1 parent 05912c1 commit 44ed181
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ const audioModule = (() => {
let animationFrameId = null;

const createAudioContext = () => {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
analyser = audioContext.createAnalyser();
analyser.fftSize = 256;
const bufferLength = analyser.frequencyBinCount;
dataArray = new Uint8Array(bufferLength);
analyser.connect(audioContext.destination);
try {
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
analyser = audioContext.createAnalyser();
analyser.fftSize = 256;
dataArray = new Uint8Array(analyser.frequencyBinCount);
analyser.connect(audioContext.destination);
}
} catch (error) {
console.error('Failed to create AudioContext:', error);
}
};

const isAudioPlaying = () => {
return currentAudio && !currentAudio.paused;
return currentAudio && !currentAudio.paused && !currentAudio.ended;
};

const initializeAudio = () => {
if (!audioContext) {
createAudioContext();
}
createAudioContext();

const sounds = ["senpai.mp3", "senpai2.mp3", "laugh.mp3"];
const randomSound = sounds[Math.floor(Math.random() * sounds.length)];
Expand Down Expand Up @@ -57,14 +60,12 @@ const audioModule = (() => {
};

const resetAvatar = () => {
if (isAudioPlaying()) {
stopAudio();
}
stopAudio();
avatar.style.transform = "scale(1)";
};

const stopAudio = () => {
if (currentAudio) {
if (currentAudio && currentAudio.currentTime > 0) {
currentAudio.pause();
currentAudio.currentTime = 0;
currentAudio = null;
Expand All @@ -84,7 +85,7 @@ const audioModule = (() => {
avatar.addEventListener("click", () => {
if (audioModule.isAudioPlaying()) {
audioModule.resetAvatar();
} else {
} else if (!audioModule.currentAudio) {
audioModule.initializeAudio();
}
});

0 comments on commit 44ed181

Please sign in to comment.