21
21
using System ;
22
22
using System . Collections . Generic ;
23
23
using System . IO ;
24
+ using System . Diagnostics ;
25
+ using System . IO ;
24
26
using System . Linq ;
25
27
using System . Runtime . InteropServices ;
26
28
using NLog ;
27
29
using PinMame ;
28
30
using UnityEngine ;
29
31
using VisualPinball . Engine . Game . Engines ;
30
32
using VisualPinball . Unity ;
33
+ using Debug = UnityEngine . Debug ;
31
34
using Logger = NLog . Logger ;
32
35
33
36
namespace VisualPinball . Engine . PinMAME
@@ -99,7 +102,12 @@ public GamelogicEngineLamp[] AvailableLamps {
99
102
private PinMameAudioInfo _audioInfo ;
100
103
private float [ ] _lastAudioFrame = { } ;
101
104
private int _lastAudioFrameOffset ;
102
- private int _maximalQueueSize = 100 ;
105
+ private const int _maximalQueueSize = 10 ;
106
+
107
+ private double _audioInputStart ;
108
+ private double _audioOutputStart ;
109
+ private int _audioNumSamplesInput ;
110
+ private int _audioNumSamplesOutput ;
103
111
104
112
private void Awake ( )
105
113
{
@@ -121,7 +129,8 @@ public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
121
129
OnLampChanged ? . Invoke ( this , new LampEventArgs ( lamp . Id , 0 ) ) ;
122
130
}
123
131
124
- _pinMame = PinMame . PinMame . Instance ( ) ;
132
+ Logger . Info ( $ "New PinMAME instance at { ( double ) AudioSettings . outputSampleRate / 1000 } kHz") ;
133
+ _pinMame = PinMame . PinMame . Instance ( AudioSettings . outputSampleRate ) ;
125
134
_pinMame . OnGameStarted += GameStarted ;
126
135
_pinMame . OnGameEnded += GameEnded ;
127
136
_pinMame . OnDisplayUpdated += DisplayUpdated ;
@@ -268,6 +277,15 @@ private int OnAudioUpdated(IntPtr framePtr, int frameSize)
268
277
return _audioInfo . SamplesPerFrame ;
269
278
}
270
279
280
+ _audioNumSamplesInput += frameSize ;
281
+ if ( _audioNumSamplesInput > 100000 ) {
282
+ var delta = AudioSettings . dspTime - _audioInputStart ;
283
+ var queueMs = System . Math . Round ( _audioQueue . Count * ( double ) _audioInfo . SamplesPerFrame / _audioInfo . SampleRate * 1000 ) ;
284
+ Debug . Log ( $ "INPUT: { System . Math . Round ( _audioNumSamplesInput / delta ) } - { _audioQueue . Count } in queue ({ queueMs } ms)") ;
285
+ _audioInputStart = AudioSettings . dspTime ;
286
+ _audioNumSamplesInput = 0 ;
287
+ }
288
+
271
289
float [ ] frame ;
272
290
if ( _audioFilterChannels == _audioInfo . Channels ) { // n channels -> n channels
273
291
frame = new float [ frameSize ] ;
@@ -311,12 +329,26 @@ private int OnAudioUpdated(IntPtr framePtr, int frameSize)
311
329
312
330
private void OnAudioFilterRead ( float [ ] data , int channels )
313
331
{
332
+ _audioNumSamplesOutput += data . Length / channels ;
333
+ if ( _audioNumSamplesOutput > 100000 ) {
334
+ var delta = AudioSettings . dspTime - _audioOutputStart ;
335
+ Debug . Log ( $ "OUTPUT: { System . Math . Round ( _audioNumSamplesOutput / delta ) } ") ;
336
+ _audioOutputStart = AudioSettings . dspTime ;
337
+ _audioNumSamplesOutput = 0 ;
338
+ }
339
+
314
340
if ( _audioFilterChannels == 0 ) {
315
341
_audioFilterChannels = channels ;
316
342
Logger . Info ( $ "Creating audio on { channels } channels.") ;
317
343
return ;
318
344
}
319
345
346
+ if ( _audioQueue . Count == 0 ) {
347
+ Logger . Error ( "Filtering audio but nothing to de-queue." ) ;
348
+ return ;
349
+ }
350
+
351
+
320
352
const int size = sizeof ( float ) ;
321
353
var dataOffset = 0 ;
322
354
var lastFrameSize = _lastAudioFrame . Length - _lastAudioFrameOffset ;
0 commit comments