Skip to content

Commit

Permalink
Rename settings and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
voytas committed Oct 17, 2024
1 parent 6f1fabf commit 2f1537c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Beep/AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public AudioPlayer(AudioFormat audioFormat, int sampleRate = 44100, int channelC
playerOptions ??= PlayerOptions.Default;

_volumeFilter = new VolumeFilter(50);
_pcmDataReaderPool = new PcmDataReaderPool(playerOptions.MaxQueueSize, audioFormat, _volumeFilter);
_pcmDataReaderPool = new PcmDataReaderPool(playerOptions.BufferQueueSize, audioFormat, _volumeFilter);

IAudioPlayer audioPlayer;
if (OperatingSystem.IsMacOS())
Expand All @@ -48,7 +48,7 @@ public AudioPlayer(AudioFormat audioFormat, int sampleRate = 44100, int channelC
}
else
{
throw new PlatformNotSupportedException($"The current platform is not supported.");
throw new PlatformNotSupportedException($"The {Environment.OSVersion.VersionString} platform is not supported.");
}

_audioPlayer = audioPlayer;
Expand Down
6 changes: 4 additions & 2 deletions src/Beep/Beep.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
<AssemblyName>OldBit.Beep</AssemblyName>
<RootNamespace>OldBit.Beep</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>0.0.10</Version>
<Version>0.9.0</Version>
<Title>OldBit.Beep</Title>
<Authors>Wojciech Sobieszek</Authors>
<RepositoryUrl>https://github.com/oldbit-com/Beep</RepositoryUrl>
<Description>Simple cross platform library for playing audio.</Description>
<Description>Simple cross-platform library for playing PCM audio. Uses native frameworks for audio playback.</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/oldbit-com/Beep</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/oldbit-com/Beep/blob/main/LICENSE</PackageLicenseUrl>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions src/Beep/Pcm/PcmDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ internal PcmDataReader(AudioFormat audioFormat, VolumeFilter volumeFilter)
_sampleSizeInBytes = audioFormat.GetByteSize();
}

internal int ReadFrames(Span<float> destination, int frameCount)
internal int ReadSamples(Span<float> destination, int sampleCount)
{
var data = _data.Skip(_position).Take(frameCount * _sampleSizeInBytes).ToArray();
var data = _data.Skip(_position).Take(sampleCount * _sampleSizeInBytes).ToArray();
_position += data.Length;

var count = data.Length;
Expand All @@ -35,7 +35,7 @@ internal int ReadFrames(Span<float> destination, int frameCount)
return 0;
}

var offset = 0;
var actualCount = 0;

for (var i = 0; i < count; i += _sampleSizeInBytes)
{
Expand All @@ -49,10 +49,10 @@ internal int ReadFrames(Span<float> destination, int frameCount)

value = _volumeFilter.Apply(value);

destination[offset++] = value;
destination[actualCount++] = value;
}

return offset;
return actualCount;
}

internal IEnumerable<byte> Data
Expand Down
4 changes: 2 additions & 2 deletions src/Beep/Platforms/Linux/AlsaPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal AlsaPlayer(int sampleRate, int channelCount, PlayerOptions playerOption
_channelCount = channelCount;
_frameSize = channelCount * FloatType.SizeInBytes;

_samplesQueue = Channel.CreateBounded<PcmDataReader>(new BoundedChannelOptions(playerOptions.MaxQueueSize)
_samplesQueue = Channel.CreateBounded<PcmDataReader>(new BoundedChannelOptions(playerOptions.BufferQueueSize)
{
SingleReader = true,
SingleWriter = true,
Expand Down Expand Up @@ -149,7 +149,7 @@ private void QueueWorker()

while (_isQueueRunning)
{
var audioDataLength = samples.ReadFrames(_audioData, _audioData.Length);
var audioDataLength = samples.ReadSamples(_audioData, _audioData.Length);

if (audioDataLength == 0)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Beep/Platforms/MacOS/AudioQueuePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal AudioQueuePlayer(int sampleRate, int channelCount, PlayerOptions player
_gch = GCHandle.Alloc(this);
_audioData = new float[_playerOptions.BufferSizeInBytes / FloatType.SizeInBytes];

_samplesQueue = Channel.CreateBounded<PcmDataReader>(new BoundedChannelOptions(playerOptions.MaxQueueSize)
_samplesQueue = Channel.CreateBounded<PcmDataReader>(new BoundedChannelOptions(playerOptions.BufferQueueSize)
{
SingleReader = true,
SingleWriter = true,
Expand Down Expand Up @@ -103,7 +103,7 @@ private async void QueueWorker()
{
while (_isQueueRunning)
{
var audioDataLength = samples!.ReadFrames(_audioData, _audioData.Length);
var audioDataLength = samples!.ReadSamples(_audioData, _audioData.Length);

if (audioDataLength != 0)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Beep/Platforms/Windows/WasapiPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal CoreAudioPlayer(int sampleRate, int channelCount, PlayerOptions playerO
_frameSize = channelCount * FloatType.SizeInBytes;
_channelCount = channelCount;

_samplesQueue = Channel.CreateBounded<PcmDataReader>(new BoundedChannelOptions(playerOptions.MaxQueueSize)
_samplesQueue = Channel.CreateBounded<PcmDataReader>(new BoundedChannelOptions(playerOptions.BufferQueueSize)
{
SingleReader = true,
SingleWriter = true,
Expand Down Expand Up @@ -124,7 +124,7 @@ private void QueueWorker()
continue;
}

var audioDataLength = samples.ReadFrames(_audioData, framesAvailable * _channelCount);
var audioDataLength = samples.ReadSamples(_audioData, framesAvailable * _channelCount);
if (audioDataLength == 0)
{
break;
Expand Down
4 changes: 2 additions & 2 deletions src/Beep/PlayerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public sealed class PlayerOptions
/// <summary>
/// Gets or sets the maximum number of audio buffers in the queue. Default is 4.
/// </summary>
public int MaxQueueSize { get; set; } = 4;
public int BufferQueueSize { get; set; } = 4;

/// <summary>
/// Gets or sets the maximum number of audio buffers. Default is 4. This setting applies to macOS only.
/// Gets or sets the maximum number of audio buffers. Default is 4.
/// </summary>
/// <remarks>
/// This setting applies to AudioToolbox (macOS) only.
Expand Down
12 changes: 6 additions & 6 deletions tests/Beep.Tests/Pcm/PcmDataReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,45 @@ namespace OldBit.Beep.Tests.Pcm;
public class PcmDataReaderTests
{
[Fact]
public void Unsigned8Bit_ShouldReturnFrames()
public void Unsigned8Bit_ShouldReturnFloatSamples()
{
var reader = new PcmDataReader(AudioFormat.Unsigned8Bit, new VolumeFilter(100))
{
Data = new byte[] { 1, 50, 100, 255 }
};

var buffer = new float[10];
var count = reader.ReadFrames(buffer, buffer.Length);
var count = reader.ReadSamples(buffer, buffer.Length);

count.Should().Be(4);
buffer.Should().BeEquivalentTo([-0.9921875f, -0.609375f, -0.21875f, 0.9921875f, 0, 0, 0, 0, 0, 0]);
}

[Fact]
public void Signed16BitIntegerLittleEndian_ShouldReturnFrames()
public void Signed16BitIntegerLittleEndian_ShouldReturnFloatSamples()
{
var reader = new PcmDataReader(AudioFormat.Signed16BitIntegerLittleEndian, new VolumeFilter(100))
{
Data = new byte[] { 1, 50, 100, 255 }
};

var buffer = new float[10];
var count = reader.ReadFrames(buffer, buffer.Length);
var count = reader.ReadSamples(buffer, buffer.Length);

count.Should().Be(2);
buffer.Should().BeEquivalentTo([0.39065552f, -0.004760742f, 0, 0, 0, 0, 0, 0, 0, 0]);
}

[Fact]
public void Float32BitLittleEndian_ShouldReturnFrames()
public void Float32BitLittleEndian_ShouldReturnFloatSamples()
{
var reader = new PcmDataReader(AudioFormat.Float32BitLittleEndian, new VolumeFilter(100))
{
Data = new byte[] { 0, 0, 0, 63 }
};

var buffer = new float[10];
var count = reader.ReadFrames(buffer, buffer.Length);
var count = reader.ReadSamples(buffer, buffer.Length);

count.Should().Be(1);
buffer.Should().BeEquivalentTo([0.5f, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
Expand Down

0 comments on commit 2f1537c

Please sign in to comment.