danoniplus 48.5.7 → 48.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/js/danoni_main.js +124 -10
  2. package/package.json +1 -1
package/js/danoni_main.js CHANGED
@@ -4,12 +4,12 @@
4
4
  *
5
5
  * Source by tickle
6
6
  * Created : 2018/10/08
7
- * Revised : 2026/08/12
7
+ * Revised : 2026/08/19
8
8
  *
9
9
  * https://github.com/cwtickle/danoniplus
10
10
  */
11
- const g_version = `Ver 48.5.7`;
12
- const g_revisedDate = `2026/08/12`;
11
+ const g_version = `Ver 48.5.8`;
12
+ const g_revisedDate = `2026/08/19`;
13
13
 
14
14
  // カスタム用バージョン (danoni_custom.js 等で指定可)
15
15
  let g_localVersion = ``;
@@ -135,6 +135,19 @@ let g_fps = 60;
135
135
  // プレイ画面再生時の内部スケジューリング用のマージン時間(100ms)
136
136
  let g_scheduleLead = 0.1;
137
137
 
138
+ // フレーム進行を音源クロック(AudioContext.currentTime)基準で補正するか
139
+ // - false の場合は従来通り performance.now() 基準で動作
140
+ let g_audioClockSync = true;
141
+
142
+ // 出力遅延(AudioContext.outputLatency)をタイミング補正に含めるか
143
+ // - true にすると出力デバイス(有線/Bluetooth等)によらず同じAdjustmentが使えるが、
144
+ // 既存のAdjustment設定値と互換性がなくなるため既定は false
145
+ let g_audioLatencyCompensation = false;
146
+
147
+ // 次フレームまでの待機時間の上限(ms)
148
+ // - 音源クロックが一時的に停止した際に待ち続けないようにするための保険
149
+ let g_maxFrameWait = 50;
150
+
138
151
  // 譜面データの&区切りを有効にするか
139
152
  let g_enableAmpersandSplit = true;
140
153
 
@@ -177,6 +190,9 @@ let g_maxScore = 1000000;
177
190
  let g_gameOverFlg = false;
178
191
  let g_finishFlg = true;
179
192
 
193
+ // 音源のAudioContext管理
194
+ let g_sharedAudioContext = null;
195
+
180
196
  /** 共通オブジェクト */
181
197
  const g_loadObj = {};
182
198
  const g_rootObj = {};
@@ -2459,10 +2475,11 @@ const drawTitleResultMotion = _displayName =>
2459
2475
  // WebAudioAPIでAudio要素風に再生するクラス
2460
2476
  class AudioPlayer {
2461
2477
  constructor() {
2462
- this._context = new AudioContext();
2478
+ this._context = getSharedAudioContext();
2463
2479
  this._gain = this._context.createGain();
2464
2480
  this._gain.connect(this._context.destination);
2465
2481
  this._startTime = 0;
2482
+ this._scheduledTime = 0;
2466
2483
  this._fadeinPosition = 0;
2467
2484
  this._eventListeners = {};
2468
2485
  this.playbackRate = 1;
@@ -2487,17 +2504,25 @@ class AudioPlayer {
2487
2504
  * - scheduleLead は安定した再生タイミングを確保するための内部マージン
2488
2505
  */
2489
2506
  play(_adjustmentTime = 0) {
2507
+ // AudioContextの時計は1回だけ読み、以降はその値を使い回す
2508
+ // - currentTimeはレンダークォンタム単位でしか進まないため、複数回読むと
2509
+ // 予約時刻と論理開始時刻が最大1クォンタム分ずれる
2510
+ const ctxNow = this._context.currentTime;
2511
+
2490
2512
  this._source = this._context.createBufferSource();
2491
2513
  this._source.buffer = this._buffer;
2492
2514
  this._source.playbackRate.value = this.playbackRate;
2493
2515
  this._source.connect(this._gain);
2494
2516
 
2495
2517
  // 実際の予約時刻(内部スケジューリング用のマージンを含む)
2496
- const startAt = this._context.currentTime + g_scheduleLead + _adjustmentTime;
2518
+ const startAt = ctxNow + g_scheduleLead + _adjustmentTime;
2497
2519
  this._source.start(startAt, this._fadeinPosition);
2498
2520
 
2499
2521
  // ゲーム側の論理的開始時刻(g_scheduleLead を含めない)
2500
- this._startTime = this._context.currentTime + _adjustmentTime;
2522
+ this._startTime = ctxNow + _adjustmentTime;
2523
+
2524
+ // 実際に音が鳴り始めるAudioContext上の時刻(フレーム同期の基準)
2525
+ this._scheduledTime = startAt;
2501
2526
  }
2502
2527
 
2503
2528
  pause() {
@@ -2525,6 +2550,26 @@ class AudioPlayer {
2525
2550
  return this._context.currentTime - this._startTime + this._fadeinPosition;
2526
2551
  }
2527
2552
 
2553
+ /** AudioContextの現在時刻(秒) */
2554
+ get contextTime() {
2555
+ return this._context.currentTime;
2556
+ }
2557
+
2558
+ /** 実際に音が鳴り始めるAudioContext上の時刻(秒) */
2559
+ get scheduledTime() {
2560
+ return this._scheduledTime;
2561
+ }
2562
+
2563
+ /** 音が出力デバイスから実際に出るまでの遅延(秒) */
2564
+ get outputLatency() {
2565
+ return this._context.outputLatency || this._context.baseLatency || 0;
2566
+ }
2567
+
2568
+ /** AudioContextの状態(running / suspended / closed) */
2569
+ get contextState() {
2570
+ return this._context.state;
2571
+ }
2572
+
2528
2573
  set currentTime(_currentTime) {
2529
2574
  this._fadeinPosition = _currentTime;
2530
2575
  }
@@ -2589,6 +2634,47 @@ class AudioPlayer {
2589
2634
  load() { }
2590
2635
  dispatchEvent() { }
2591
2636
  }
2637
+ // グローバルで1つだけ保持(遅延生成)
2638
+ const getSharedAudioContext = () => {
2639
+ if (!g_sharedAudioContext) {
2640
+ g_sharedAudioContext = new AudioContext();
2641
+ }
2642
+ // タブのバックグラウンド化等でsuspendedになることがあるため念のためresume
2643
+ if (g_sharedAudioContext.state === `suspended`) {
2644
+ g_sharedAudioContext.resume();
2645
+ }
2646
+ return g_sharedAudioContext;
2647
+ };
2648
+ /**
2649
+ * AudioContextのウォームアップ
2650
+ * - 生成直後・resume直後のAudioContextは出力デバイスの起動待ちのため、
2651
+ * しばらく currentTime が進まない(環境により数十〜数百ms)
2652
+ * - この状態で音源をスケジュールすると、起動に要した時間がそのまま
2653
+ * 音源と譜面のずれになるため、無音を1回鳴らして時計が動き出すまで待つ
2654
+ * @returns {Promise<void>}
2655
+ */
2656
+ const warmUpAudioContext = async () => {
2657
+ const ctx = getSharedAudioContext();
2658
+ if (ctx.state !== `running`) {
2659
+ await ctx.resume().catch(() => { });
2660
+ }
2661
+ if (ctx.state !== `running`) {
2662
+ return; // ジェスチャー未取得等でresumeできない場合は何もしない
2663
+ }
2664
+
2665
+ // 無音を1サンプルだけ鳴らして出力デバイスを起動させる
2666
+ const source = ctx.createBufferSource();
2667
+ source.buffer = ctx.createBuffer(1, 1, ctx.sampleRate);
2668
+ source.connect(ctx.destination);
2669
+ source.start();
2670
+
2671
+ // currentTimeが実際に進み始めるまで待機(最大500ms)
2672
+ const baseTime = ctx.currentTime;
2673
+ const limitTime = performance.now() + 500;
2674
+ while (ctx.currentTime === baseTime && performance.now() < limitTime) {
2675
+ await new Promise(resolve => setTimeout(resolve, 10));
2676
+ }
2677
+ };
2592
2678
 
2593
2679
  /**
2594
2680
  * クリップボードコピー関数
@@ -11882,6 +11968,7 @@ const setAudio = async (_url) => {
11882
11968
  g_currentPage = `loadingIos`;
11883
11969
  lblLoading.textContent = `Click to Start!`;
11884
11970
  divRoot.appendChild(makePlayButton(evt => {
11971
+ getSharedAudioContext().resume();
11885
11972
  g_currentPage = `loading`;
11886
11973
  resetKeyControl();
11887
11974
  divRoot.removeChild(evt.target);
@@ -12100,6 +12187,9 @@ const loadingScoreInit = async () => {
12100
12187
  // ユーザカスタムイベント
12101
12188
  safeExecuteCustomHooks(`g_customJsObj.loading`, g_customJsObj.loading);
12102
12189
 
12190
+ // 初回プレイ時に出力デバイスの起動待ちでずれるのを防ぐため、
12191
+ // メイン画面へ移行する前にAudioContextを起動させておく
12192
+ await warmUpAudioContext();
12103
12193
  mainInit();
12104
12194
  };
12105
12195
 
@@ -14448,6 +14538,7 @@ const mainInit = () => {
14448
14538
  let thisTime;
14449
14539
  let buffTime;
14450
14540
  let musicStartTime;
14541
+ let musicStartCtxTime;
14451
14542
  let musicStartFlg = false;
14452
14543
 
14453
14544
  g_inputKeyBuffer = {};
@@ -15682,14 +15773,32 @@ const mainInit = () => {
15682
15773
  }
15683
15774
 
15684
15775
  // 60fpsから遅延するため、その差分を取って次回のタイミングで遅れをリカバリする
15776
+ // - WebAudioAPI使用時は音源クロック(AudioContext.currentTime)を基準とする
15777
+ // performance.now()とAudioContextの時計は独立して進むため、後者を基準にしないと
15778
+ // 再生開始時のオフセットずれやsuspend/resumeによるずれを吸収できない
15685
15779
  thisTime = performance.now();
15686
15780
  buffTime = 0;
15687
- if (g_audio instanceof AudioPlayer || currentFrame >= musicStartFrame) {
15781
+ let holdFrame = false;
15782
+
15783
+ if (g_audio instanceof AudioPlayer && g_audioClockSync && musicStartCtxTime !== undefined) {
15784
+ if (g_audio.contextState === `running`) {
15785
+ buffTime = (g_audio.contextTime - musicStartCtxTime) * 1000
15786
+ - (currentFrame - musicStartFrame) * 1000 / g_fps;
15787
+ } else {
15788
+ // AudioContext停止中は音源も止まっているため、フレーム進行も止めて復帰を待つ
15789
+ getSharedAudioContext(); // suspended時はresumeを試行
15790
+ holdFrame = true;
15791
+ }
15792
+ } else if (g_audio instanceof AudioPlayer || currentFrame >= musicStartFrame) {
15688
15793
  buffTime = (thisTime - musicStartTime - (currentFrame - musicStartFrame) * 1000 / g_fps);
15689
15794
  }
15690
- g_scoreObj.frameNum++;
15691
- g_scoreObj.baseFrame++;
15692
- g_timeoutEvtId = setTimeout(flowTimeline, 1000 / g_fps - buffTime);
15795
+
15796
+ if (!holdFrame) {
15797
+ g_scoreObj.frameNum++;
15798
+ g_scoreObj.baseFrame++;
15799
+ }
15800
+ g_timeoutEvtId = setTimeout(flowTimeline, holdFrame ? g_maxFrameWait :
15801
+ Math.min(Math.max(1000 / g_fps - buffTime, 0), g_maxFrameWait));
15693
15802
  }
15694
15803
  };
15695
15804
  safeExecuteCustomHooks(`g_skinJsObj.main`, g_skinJsObj.main);
@@ -15702,6 +15811,11 @@ const mainInit = () => {
15702
15811
  const musicStartAdjustment = (g_headerObj.blankFrame - g_stateObj.decimalAdjustment + 1) / g_fps;
15703
15812
  musicStartTime = performance.now() + (musicStartAdjustment + g_scheduleLead) * 1000;
15704
15813
  g_audio.play(musicStartAdjustment);
15814
+
15815
+ // 音源クロック基準の開始時刻(フレーム進行の基準)
15816
+ // - g_audioLatencyCompensationが有効な場合は出力遅延分だけ表示を後ろへずらす
15817
+ musicStartCtxTime = g_audio.scheduledTime
15818
+ + (g_audioLatencyCompensation ? g_audio.outputLatency : 0);
15705
15819
  }
15706
15820
 
15707
15821
  g_timeoutEvtId = setTimeout(flowTimeline, 1000 / g_fps);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "danoniplus",
3
- "version": "48.5.7",
3
+ "version": "48.5.8",
4
4
  "description": "Dancing☆Onigiri (CW Edition) - Web-based Rhythm Game",
5
5
  "main": "./js/danoni_main.js",
6
6
  "jsdelivr": "./js/danoni_main.js",