Skip to content

Commit

Permalink
Move GetByteSize to extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
voytas committed May 2, 2024
1 parent b5eab8e commit 330d949
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
18 changes: 18 additions & 0 deletions src/Beeper/Extensions/AudioFormatExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace OldBit.Beeper.Extensions;

public static class AudioFormatExtensions
{
/// <summary>
/// Gets the byte size of a sample for the specified audio format.
/// </summary>
/// <param name="format">The audio format.</param>
/// <returns>The byte size of a sample for the specified audio format.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when an unsupported audio format is provided.</exception>
public static int GetByteSize(this AudioFormat format) => format switch
{
AudioFormat.Unsigned8Bit => 1,
AudioFormat.Signed16BitIntegerLittleEndian => 2,
AudioFormat.Float32BitLittleEndian => 4,
_ => throw new ArgumentOutOfRangeException(nameof(format))
};
}
14 changes: 0 additions & 14 deletions src/Beeper/Helpers/AudioFormatHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ namespace OldBit.Beeper.Helpers;

public static class AudioFormatHelper
{
/// <summary>
/// Gets the byte size of a sample for the specified audio format.
/// </summary>
/// <param name="format">The audio format.</param>
/// <returns>The byte size of a sample for the specified audio format.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when an unsupported audio format is provided.</exception>
public static int GetByteSize(AudioFormat format) => format switch
{
AudioFormat.Unsigned8Bit => 1,
AudioFormat.Signed16BitIntegerLittleEndian => 2,
AudioFormat.Float32BitLittleEndian => 4,
_ => throw new ArgumentOutOfRangeException(nameof(format))
};

/// <summary>
/// The size of a float in bytes.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Beeper/IO/PcmDataReader.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using OldBit.Beeper.Extensions;
using OldBit.Beeper.Helpers;

namespace OldBit.Beeper.IO;
Expand All @@ -19,7 +20,7 @@ internal PcmDataReader(Stream input, AudioFormat format)

internal int Read(Span<float> buffer)
{
var byteSize = AudioFormatHelper.GetByteSize(_format);
var byteSize = _format.GetByteSize();
var byteBuffer = new byte[buffer.Length * byteSize];
var bytesRead = _stream.Read(byteBuffer, 0, buffer.Length * byteSize);

Expand Down
5 changes: 3 additions & 2 deletions src/Demo/Generator/SinWaveGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OldBit.Beeper;
using OldBit.Beeper.Extensions;
using OldBit.Beeper.Helpers;

namespace Demo.Generator;
Expand All @@ -17,7 +18,7 @@ public IEnumerable<byte> Generate(float frequency, TimeSpan duration)
{
var sampleLength = sampleRate / frequency;

var byteSize = AudioFormatHelper.GetByteSize(format) * channelCount;
var byteSize = format.GetByteSize() * channelCount;
var bufferSize = CalculateBufferSize(duration);

for (var i = 0; i < bufferSize / byteSize; i++)
Expand Down Expand Up @@ -73,7 +74,7 @@ public IEnumerable<byte> Generate(float frequency, TimeSpan duration)
/// <returns>The calculated buffer size.</returns>
private int CalculateBufferSize(TimeSpan duration)
{
var size = AudioFormatHelper.GetByteSize(format) * channelCount * sampleRate * duration.TotalSeconds;
var size = format.GetByteSize() * channelCount * sampleRate * duration.TotalSeconds;

return (int)Math.Ceiling(size / 4) * 4;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Beeper.UnitTests/AudioFormatHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using OldBit.Beeper.Extensions;
using OldBit.Beeper.Helpers;

namespace OldBit.Beeper.UnitTests;
Expand All @@ -10,7 +11,7 @@ public class AudioFormatHelperTests
[InlineData(AudioFormat.Float32BitLittleEndian, 4)]
public void GetByteSize_ShouldReturnCorrectNumberOfBytes(AudioFormat audioFormat, int expectedBytes)
{
var numberOfBytes = AudioFormatHelper.GetByteSize(audioFormat);
var numberOfBytes = audioFormat.GetByteSize();

Assert.Equal(expectedBytes, numberOfBytes);
}
Expand Down

0 comments on commit 330d949

Please sign in to comment.