Skip to content

Commit bf642f4

Browse files
committed
Refactor AudioQueuePlayer to use a preallocated empty audio buffer
1 parent a1d9fbb commit bf642f4

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

src/Beep/Platforms/MacOS/AudioBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace OldBit.Beep.Platforms.MacOS;
22

3-
internal class AudioBuffer
3+
internal sealed class AudioBuffer
44
{
55
private static readonly TimeSpan AudioQueueCallbackTimeout = TimeSpan.FromSeconds(15);
66

src/Beep/Platforms/MacOS/AudioQueuePlayer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal sealed class AudioQueuePlayer : IAudioPlayer
1818
private readonly Channel<PcmDataReader> _samplesQueue;
1919
private readonly Thread _queueWorker;
2020
private readonly float[] _audioData;
21+
private readonly float[] _emptyAudioData;
2122

2223
private GCHandle _gch;
2324
private bool _isQueueRunning;
@@ -27,6 +28,7 @@ internal AudioQueuePlayer(int sampleRate, int channelCount, PlayerOptions player
2728
{
2829
_gch = GCHandle.Alloc(this);
2930
_audioData = new float[playerOptions.BufferSizeInBytes / FloatType.SizeInBytes];
31+
_emptyAudioData = new float[playerOptions.BufferSizeInBytes / FloatType.SizeInBytes];
3032

3133
_samplesQueue = Channel.CreateBounded<PcmDataReader>(new BoundedChannelOptions(playerOptions.BufferQueueSize)
3234
{
@@ -122,9 +124,7 @@ private async Task RunWorkerTask()
122124

123125
// We need to keep sending empty buffers to the audio queue to keep it running
124126
// if there is no data to play
125-
Array.Fill(_audioData, 0);
126-
127-
await Enqueue(_audioData, _audioData.Length / 4, CancellationToken.None);
127+
await Enqueue(_emptyAudioData, _emptyAudioData.Length / 4, CancellationToken.None);
128128
}
129129

130130
_queueWorkerStopped = true;

0 commit comments

Comments
 (0)