Skip to content

Commit bf71e99

Browse files
committed
audio: Fix frame dequeuing.
1 parent 2e74dba commit bf71e99

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
using System;
2222
using System.Collections.Generic;
23+
using System.IO;
2324
using System.Linq;
2425
using System.Runtime.InteropServices;
2526
using NLog;
@@ -33,7 +34,7 @@ namespace VisualPinball.Engine.PinMAME
3334
{
3435
[Serializable]
3536
[DisallowMultipleComponent]
36-
//[RequireComponent(typeof(AudioSource))]
37+
[RequireComponent(typeof(AudioSource))]
3738
[AddComponentMenu("Visual Pinball/Game Logic Engine/PinMAME")]
3839
public class PinMameGamelogicEngine : MonoBehaviour, IGamelogicEngine
3940
{
@@ -107,6 +108,9 @@ private void Awake()
107108
private void Start()
108109
{
109110
UpdateCaches();
111+
112+
_lastAudioFrame = new float[0];
113+
_lastAudioFrameOffset = 0;
110114
}
111115

112116
public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
@@ -259,16 +263,11 @@ private int OnAudioAvailable(PinMameAudioInfo audioInfo)
259263
private int OnAudioUpdated(IntPtr framePtr, int frameSize)
260264
{
261265
var frame = new float[frameSize * 2];
262-
var min = 0f;
263-
var max = 0f;
264-
265266
unsafe {
266267
var src = (short*)framePtr;
267268
for (var i = 0; i < frameSize; i++) {
268269
frame[i * 2] = src[i] / 32768f;
269270
frame[i * 2 + 1] = frame[i * 2]; // duplicate - assuming input channels = 1, and output channels = 2.
270-
min = System.Math.Min(min, frame[i * 2]);
271-
max = System.Math.Max(max, frame[i * 2]);
272271
}
273272
}
274273

@@ -280,32 +279,35 @@ private int OnAudioUpdated(IntPtr framePtr, int frameSize)
280279
_audioQueue.Enqueue(frame);
281280
}
282281

283-
if (min < -0.1f || max > 0.1f) {
284-
Logger.Info($"Queueing audio sample ({frameSize}). [{System.Math.Round(min, 4)} {System.Math.Round(max, 4)}]");
285-
}
286-
287282
return _audioInfo.SamplesPerFrame;
288283
}
289284

290285
private void OnAudioFilterRead(float[] data, int channels)
291286
{
287+
if (channels != 2) {
288+
Logger.Error($"Got {channels} channels, expecting 2.");
289+
return;
290+
}
291+
const int size = sizeof(float);
292292
var dataOffset = 0;
293293
var lastFrameSize = _lastAudioFrame.Length - _lastAudioFrameOffset;
294294
if (data.Length >= lastFrameSize) {
295-
Buffer.BlockCopy(_lastAudioFrame, _lastAudioFrameOffset, data, dataOffset, lastFrameSize);
296-
dataOffset += lastFrameSize;
295+
if (lastFrameSize > 0) {
296+
Buffer.BlockCopy(_lastAudioFrame, _lastAudioFrameOffset * size, data, 0, lastFrameSize * size);
297+
dataOffset += lastFrameSize;
298+
}
297299
_lastAudioFrame = new float[0];
298300
_lastAudioFrameOffset = 0;
299301

300302
lock (_audioQueue) {
301303
while (dataOffset < data.Length && _audioQueue.Count > 0) {
302304
var frame = _audioQueue.Dequeue();
303305
if (frame.Length <= data.Length - dataOffset) {
304-
Buffer.BlockCopy(frame, 0, data, dataOffset, frame.Length);
306+
Buffer.BlockCopy(frame, 0, data, dataOffset * size, frame.Length * size);
305307
dataOffset += frame.Length;
306308

307309
} else {
308-
Buffer.BlockCopy(frame, 0, data, dataOffset, data.Length - dataOffset);
310+
Buffer.BlockCopy(frame, 0, data, dataOffset * size, (data.Length - dataOffset) * size);
309311
_lastAudioFrame = frame;
310312
_lastAudioFrameOffset = data.Length - dataOffset;
311313
dataOffset = data.Length;
@@ -314,7 +316,7 @@ private void OnAudioFilterRead(float[] data, int channels)
314316
}
315317

316318
} else {
317-
Buffer.BlockCopy(_lastAudioFrame, _lastAudioFrameOffset, data, 0, data.Length);
319+
Buffer.BlockCopy(_lastAudioFrame, _lastAudioFrameOffset * size, data, 0, data.Length * size);
318320
_lastAudioFrameOffset += data.Length;
319321
}
320322
}

0 commit comments

Comments
 (0)