Skip to content

Commit 4b01aa8

Browse files
committed
fix(game): Fix first start latency
1 parent 946bb26 commit 4b01aa8

4 files changed

Lines changed: 8 additions & 10 deletions

File tree

src/audio/clip.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class GameAudioClip {
3838
this.channel = null;
3939
}
4040

41-
play() {
41+
play(playOffsetFix: number = 0) {
4242
if (!this.channel) throw new Error('Cannot play a clip directly without any channel');
4343
if (this.status === EGameAudioClipStatus.PLAY) return;
4444

@@ -47,12 +47,12 @@ export class GameAudioClip {
4747
this.buffer.connect(this.channel.gain);
4848

4949
if (isNaN(this.pauseTime)) {
50-
this.startTime = this.clock.time;
5150
this.buffer.start(0, 0);
51+
this.startTime = this.clock.time - playOffsetFix;
5252
} else {
5353
const pausedTime = this.pauseTime - this.startTime;
54-
this.startTime = this.clock.time - pausedTime;
5554
this.buffer.start(0, pausedTime / 1000);
55+
this.startTime = this.clock.time - pausedTime;
5656
}
5757

5858
this.pauseTime = NaN;

src/audio/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,12 @@ const handleWindowLoaded = () => {
5151

5252
if (GlobalAudioCtx.state === 'running') return;
5353
window.addEventListener('pointerdown', resumeAudio);
54-
window.addEventListener('pointerover', resumeAudio);
55-
window.addEventListener('pointerleave', resumeAudio);
5654
};
5755

5856
GlobalAudioCtx.addEventListener('statechange', () => {
5957
if (GlobalAudioCtx.state !== 'running') return;
6058

6159
console.info('[Audio]', 'Resume audio success');
6260
window.removeEventListener('pointerdown', resumeAudio);
63-
window.removeEventListener('pointerover', resumeAudio);
64-
window.removeEventListener('pointerleave', resumeAudio);
6561
});
6662
window.addEventListener('load', handleWindowLoaded);

src/chart/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ export class GameChart {
120120
this.score.resize(sizer);
121121
}
122122

123-
start() {
123+
start(playOffsetFix: number = 0) {
124124
this.ticker.add(this.onTick);
125125
this.ticker.start();
126126

127127
this.game.audio.channels.effect.startTicker();
128-
this.audio.play();
128+
this.audio.play(playOffsetFix);
129129
}
130130

131131
reset() {

src/game.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export class Game {
5959
console.error('No skin loaded');
6060
return;
6161
}
62+
63+
const playOffsetFixStart = performance.now();
6264
await currentSkin.create(options.useHighQualitySkin);
6365
this.stage.set(null);
6466

@@ -73,7 +75,7 @@ export class Game {
7375

7476
this.renderer.containers.game.sortChildren();
7577
this.chart.reszie(this.renderer.size);
76-
this.chart.start();
78+
this.chart.start(playOffsetFixStart - performance.now());
7779

7880
res(this.chart);
7981
console.log(this);

0 commit comments

Comments
 (0)