Skip to content

Commit fb1e05c

Browse files
committed
audio: Minor simplifications.
1 parent 5db3a6b commit fb1e05c

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -258,28 +258,32 @@ private int OnAudioAvailable(PinMameAudioInfo audioInfo)
258258

259259
private int OnAudioUpdated(IntPtr framePtr, int frameSize)
260260
{
261+
var frame = new float[frameSize * 2];
262+
var min = 0f;
263+
var max = 0f;
264+
261265
unsafe {
262-
var src = (short*) framePtr;
263-
var frame = new float[frameSize * 2];
264-
var min = 0f;
265-
var max = 0f;
266+
var src = (short*)framePtr;
266267
for (var i = 0; i < frameSize; i++) {
267268
frame[i * 2] = src[i] / 32768f;
268269
frame[i * 2 + 1] = frame[i * 2]; // duplicate - assuming input channels = 1, and output channels = 2.
269270
min = System.Math.Min(min, frame[i * 2]);
270271
max = System.Math.Max(max, frame[i * 2]);
271272
}
272-
lock (_audioQueue) {
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)}]");
280-
}
273+
}
274+
275+
lock (_audioQueue) {
276+
if (_audioQueue.Count >= _maximalQueueSize) {
277+
_audioQueue.Clear();
278+
Logger.Error("Clearing full audio frame queue.");
281279
}
280+
_audioQueue.Enqueue(frame);
281+
}
282+
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)}]");
282285
}
286+
283287
return _audioInfo.SamplesPerFrame;
284288
}
285289

@@ -294,9 +298,7 @@ private void OnAudioFilterRead(float[] data, int channels)
294298
_lastAudioFrameOffset = 0;
295299

296300
lock (_audioQueue) {
297-
var i = 0;
298301
while (remaining > 0 && _audioQueue.Count > 0) {
299-
i++;
300302
var frame = _audioQueue.Dequeue();
301303
if (frame.Length <= remaining) {
302304
Buffer.BlockCopy(frame, 0, data, data.Length - remaining, frame.Length);
@@ -309,10 +311,6 @@ private void OnAudioFilterRead(float[] data, int channels)
309311
remaining = 0;
310312
}
311313
}
312-
if (_audioQueue.Count >= _maximalQueueSize) {
313-
_audioQueue.Clear();
314-
Logger.Info($"Dequeued {i} audio samples, written {data.Length - remaining}, remaining {remaining}, clearing queue!");
315-
}
316314
}
317315

318316
} else {

0 commit comments

Comments
 (0)