Skip to content

Commit 5db3a6b

Browse files
committed
audio: Log some values and clear audio frame queue if too large.
1 parent 36437e8 commit 5db3a6b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

VisualPinball.Engine.PinMAME.Unity/Runtime/PinMameGamelogicEngine.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,22 @@ private int OnAudioUpdated(IntPtr framePtr, int frameSize)
261261
unsafe {
262262
var src = (short*) framePtr;
263263
var frame = new float[frameSize * 2];
264+
var min = 0f;
265+
var max = 0f;
264266
for (var i = 0; i < frameSize; i++) {
265-
frame[i * 2] = src[i] * (float)(1.0/32768.0);
267+
frame[i * 2] = src[i] / 32768f;
266268
frame[i * 2 + 1] = frame[i * 2]; // duplicate - assuming input channels = 1, and output channels = 2.
269+
min = System.Math.Min(min, frame[i * 2]);
270+
max = System.Math.Max(max, frame[i * 2]);
267271
}
268272
lock (_audioQueue) {
269-
if (_audioQueue.Count < _maximalQueueSize) {
270-
_audioQueue.Enqueue(frame);
271-
Logger.Info($"Queueing audio sample ({frameSize}).");
272-
273-
} else {
274-
Logger.Info($"Skipping audio sample queue because it's full.");
273+
if (_audioQueue.Count >= _maximalQueueSize) {
274+
_audioQueue.Clear();
275+
Logger.Error("Clearing full audio frame queue.");
276+
}
277+
_audioQueue.Enqueue(frame);
278+
if (min < -0.1f || max > 0.1f) {
279+
Logger.Info($"Queueing audio sample ({frameSize}). [{System.Math.Round(min, 4)} {System.Math.Round(max, 4)}]");
275280
}
276281
}
277282
}
@@ -285,6 +290,8 @@ private void OnAudioFilterRead(float[] data, int channels)
285290
if (remaining >= lastFrameSize) {
286291
Buffer.BlockCopy(_lastAudioFrame, _lastAudioFrameOffset, data, 0, lastFrameSize);
287292
remaining -= lastFrameSize;
293+
_lastAudioFrame = new float[0];
294+
_lastAudioFrameOffset = 0;
288295

289296
lock (_audioQueue) {
290297
var i = 0;
@@ -302,7 +309,10 @@ private void OnAudioFilterRead(float[] data, int channels)
302309
remaining = 0;
303310
}
304311
}
305-
Logger.Info($"Dequeued {i} audio samples, written {data.Length - remaining}, remaining {remaining}.");
312+
if (_audioQueue.Count >= _maximalQueueSize) {
313+
_audioQueue.Clear();
314+
Logger.Info($"Dequeued {i} audio samples, written {data.Length - remaining}, remaining {remaining}, clearing queue!");
315+
}
306316
}
307317

308318
} else {

0 commit comments

Comments
 (0)