Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido van Helvoort committed Nov 27, 2022
1 parent a5fa24b commit 8e9c647
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
57 changes: 28 additions & 29 deletions src/CDGKaraokePlayer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {
WIDTH,
HEIGHT,
} from './constants';
import { WIDTH, HEIGHT } from "./constants";

import CDGPlayer from './CDGPlayer';
import CDGPlayer from "./CDGPlayer";

/**
* CDG Karoake Player
Expand Down Expand Up @@ -73,7 +70,7 @@ export default class CDGKaraokePlayer {
* @return {HTMLCanvasElement} display canvas
*/
createDisplayCanvas(width, height) {
const canvas = document.createElement('canvas');
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
return canvas;
Expand All @@ -86,7 +83,7 @@ export default class CDGKaraokePlayer {
* @return {CanvasRenderingContext2D} created context
*/
createCanvasContext(canvas) {
const ctx = canvas.getContext('2d');
const ctx = canvas.getContext("2d");
ctx.mozImageSmoothingEnabled = false;
ctx.webkitImageSmoothingEnabled = false;
ctx.msImageSmoothingEnabled = false;
Expand All @@ -100,31 +97,31 @@ export default class CDGKaraokePlayer {
* @return {HTMLAudioElement} audio player element
*/
createAudio() {
return document.createElement('audio');
return document.createElement("audio");
}

/**
* Adds event listeners to the audio element, which in turn controls the CDG playback
*/
addAudioListeners() {
this.audio.addEventListener('playing', () => {
this.audio.addEventListener("playing", () => {
this.player.play();
});

this.audio.addEventListener('pause', () => {
this.audio.addEventListener("pause", () => {
this.player.stop();
});

this.audio.addEventListener('seeking', () => {
this.audio.addEventListener("seeking", () => {
this.player.stop();
});

this.audio.addEventListener('seeked', () => {
this.audio.addEventListener("seeked", () => {
this.player.reset();
});

// sync to audio element's currentTime property
this.audio.addEventListener('timeupdate', () => {
this.audio.addEventListener("timeupdate", () => {
this.player.sync(this.audio.currentTime * 1000); // convert to ms
});
}
Expand Down Expand Up @@ -155,7 +152,7 @@ export default class CDGKaraokePlayer {
*/
setContainerBackgroundColor(context) {
if (this.backgroundContainer) {
const rgb = context.clut[context.getBackground()].join(',');
const rgb = context.clut[context.getBackground()].join(",");
this.backgroundContainer.style.backgroundColor = `rgb(${rgb})`;
}
}
Expand All @@ -173,8 +170,14 @@ export default class CDGKaraokePlayer {
// Copy from source canvas to the target canvas
this.ctx.drawImage(
context.canvas,
0, 0, context.canvas.width, context.canvas.height,
0, 0, this.canvas.width, this.canvas.height,
0,
0,
context.canvas.width,
context.canvas.height,
0,
0,
this.canvas.width,
this.canvas.height
);
}

Expand All @@ -200,10 +203,7 @@ export default class CDGKaraokePlayer {
* @return {Promise} that resolves when everything is loaded and ready to play
*/
load(audioUrl, cdgUrl) {
return Promise.all([
this.loadAudioSrc(audioUrl),
this.loadCdgData(cdgUrl),
]);
return Promise.all([this.loadAudioSrc(audioUrl), this.loadCdgData(cdgUrl)]);
}

/**
Expand All @@ -214,10 +214,9 @@ export default class CDGKaraokePlayer {
* @return {Promise} that resolves when everything is loaded and ready to play
*/
loadAndPlay(audioUrl, cdgUrl) {
return this.load(audioUrl, cdgUrl)
.then(() => {
this.play();
});
return this.load(audioUrl, cdgUrl).then(() => {
this.play();
});
}

/**
Expand All @@ -238,12 +237,12 @@ export default class CDGKaraokePlayer {
reject();
};
removeListeners = () => {
this.audio.removeEventListener('canplaythrough', onCanPlay);
this.audio.removeEventListener('error', onError);
this.audio.removeEventListener("canplaythrough", onCanPlay);
this.audio.removeEventListener("error", onError);
};

this.audio.addEventListener('canplaythrough', onCanPlay);
this.audio.addEventListener('error', onError);
this.audio.addEventListener("canplaythrough", onCanPlay);
this.audio.addEventListener("error", onError);

this.audio.src = audioUrl;
});
Expand All @@ -266,7 +265,7 @@ export default class CDGKaraokePlayer {
error.response = response;
throw error;
})
.then(response => response.arrayBuffer())
.then((response) => response.arrayBuffer())
.then((buffer) => {
// convert arrayBuffer to Uint8Array to normal Array
this.player.load(Array.from(new Uint8Array(buffer)));
Expand Down
31 changes: 18 additions & 13 deletions src/CDGPlayer.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
import {
PACKETS_PER_SECTOR,
SECTORS_PER_SECOND,
} from './constants';
import { PACKETS_PER_SECTOR, SECTORS_PER_SECOND } from "./constants";

import CDGContext from './CDGContext';
import CDGParser from './CDGParser';
import CDGContext from "./CDGContext";
import CDGParser from "./CDGParser";

/**
* Calculates current time for the sake of determining playback intervals
*
* @return {number} milliseconds
*/
function now() {
if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
if (
typeof performance !== "undefined" &&
typeof performance.now === "function"
) {
return performance.now();
} else if (typeof process !== 'undefined' && typeof process.hrtime === 'function') {
} else if (
typeof process !== "undefined" &&
typeof process.hrtime === "function"
) {
const [seconds, microseconds] = process.hrtime();
return (seconds * 1000) + (microseconds / 1000000);
return seconds * 1000 + microseconds / 1000000;
}
return Date.now();
}

function requestFrame(callback) {
if (typeof requestAnimationFrame === 'function') {
if (typeof requestAnimationFrame === "function") {
return window.requestAnimationFrame(callback);
}
return setTimeout(callback, 25);
}

function cancelFrame(id) {
if (typeof cancelAnimationFrame === 'function') {
if (typeof cancelAnimationFrame === "function") {
return cancelAnimationFrame(id);
}
return clearTimeout(id);
Expand Down Expand Up @@ -104,7 +107,9 @@ export default class CDGPlayer {

// determine packet we should be at, based on spec
// of 4 packets per sector @ 75 sectors per second
const newPc = Math.floor(SECTORS_PER_SECOND * PACKETS_PER_SECTOR * (this.pos / 1000));
const newPc = Math.floor(
SECTORS_PER_SECOND * PACKETS_PER_SECTOR * (this.pos / 1000)
);

const ffAmt = newPc - this.pc;
if (ffAmt > 0) {
Expand Down Expand Up @@ -188,7 +193,7 @@ export default class CDGPlayer {
* @return {self}
*/
executeInstruction(instruction) {
if (instruction && typeof instruction.execute === 'function') {
if (instruction && typeof instruction.execute === "function") {
instruction.execute(this.context);
}
return this;
Expand Down

0 comments on commit 8e9c647

Please sign in to comment.