Skip to content

Commit

Permalink
rm factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
albho committed Sep 11, 2024
1 parent 55bc839 commit ce98fa8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 30 deletions.
27 changes: 1 addition & 26 deletions binding/dotnet/PvSpeaker/PvSpeaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,6 @@ private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllI
[DllImport(LIBRARY, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr pv_speaker_version();

/// <summary>
/// Factory method for creating instances of PvSpeaker.
/// </summary>
/// <param name="sampleRate">
/// The sample rate of the audio to be played.
/// </param>
/// <param name="bitsPerSample">
/// The number of bits per sample of the audio to be played.
/// </param>
/// <param name="bufferSizeSecs">
/// The size in seconds of the internal buffer used to buffer PCM data
/// - i.e. internal circular buffer will be of size `sampleRate` * `bufferSizeSecs`.
/// </param>
/// <param name="deviceIndex">
/// The index of the audio device to play audio from. A value of (-1) will use the default audio device.
/// </param>
/// <returns>An instance of PvSpeaker.</returns>
public static PvSpeaker Create(int sampleRate, int bitsPerSample, int bufferSizeSecs = 20, int deviceIndex = -1)
{
return new PvSpeaker(sampleRate, bitsPerSample, bufferSizeSecs, deviceIndex);
}

/// <summary>
/// Constructor for PvSpeaker.
/// </summary>
Expand All @@ -145,7 +123,7 @@ public static PvSpeaker Create(int sampleRate, int bitsPerSample, int bufferSize
/// <param name="deviceIndex">
/// The index of the audio device to play audio from. A value of (-1) will use the default audio device.
/// </param>
private PvSpeaker(int sampleRate, int bitsPerSample, int bufferSizeSecs, int deviceIndex)
public PvSpeaker(int sampleRate, int bitsPerSample, int bufferSizeSecs = 20, int deviceIndex = -1)
{
if (sampleRate <= 0)
{
Expand Down Expand Up @@ -213,7 +191,6 @@ public void Stop()
/// <returns>Number of samples that were successfully written.</returns>
public int Write(byte[] pcm)
{
// Create a pointer to the byte array to pass to `pv_speaker_write` .
GCHandle pinnedArray = GCHandle.Alloc(pcm, GCHandleType.Pinned);
IntPtr pcmPtr = pinnedArray.AddrOfPinnedObject();

Expand All @@ -236,8 +213,6 @@ public int Write(byte[] pcm)
public int Flush(byte[] pcm = null)
{
pcm = pcm ?? Array.Empty<byte>();

// Create a pointer to the byte array to pass to `pv_speaker_flush`.
GCHandle pinnedArray = GCHandle.Alloc(pcm, GCHandleType.Pinned);
IntPtr pcmPtr = pinnedArray.AddrOfPinnedObject();

Expand Down
6 changes: 3 additions & 3 deletions binding/dotnet/PvSpeakerTest/MainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class MainTest
[TestMethod]
public void TestInit()
{
PvSpeaker speaker = PvSpeaker.Create(SAMPLE_RATE, BITS_PER_SAMPLE, BUFFER_SIZE_SECS, deviceIndex: 0);
var speaker = new PvSpeaker(SAMPLE_RATE, BITS_PER_SAMPLE, BUFFER_SIZE_SECS, deviceIndex: 0);
Assert.IsNotNull(speaker);
Assert.IsTrue(speaker.SampleRate == SAMPLE_RATE);
Assert.IsTrue(speaker.BitsPerSample == BITS_PER_SAMPLE);
Expand All @@ -40,7 +40,7 @@ public void TestInit()
[TestMethod]
public void TestStartStop()
{
using (PvSpeaker speaker = PvSpeaker.Create(SAMPLE_RATE, BITS_PER_SAMPLE, BUFFER_SIZE_SECS, deviceIndex: 0))
using (var speaker = new PvSpeaker(SAMPLE_RATE, BITS_PER_SAMPLE, BUFFER_SIZE_SECS, deviceIndex: 0))
{
Assert.IsFalse(speaker.IsStarted);
speaker.Start();
Expand Down Expand Up @@ -72,7 +72,7 @@ public void TestStartStop()
[TestMethod]
public void TestWriteFlush()
{
using (PvSpeaker speaker = PvSpeaker.Create(SAMPLE_RATE, BITS_PER_SAMPLE, BUFFER_SIZE_SECS, deviceIndex: 0))
using (var speaker = new PvSpeaker(SAMPLE_RATE, BITS_PER_SAMPLE, BUFFER_SIZE_SECS, deviceIndex: 0))
{
speaker.Start();

Expand Down
2 changes: 1 addition & 1 deletion demo/dotnet/PvSpeakerDemo/PvSpeakerDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static void Main(string[] args)
{
WavFileInfo wavInfo = GetWavFileInfo(inputWavPath);

using (PvSpeaker speaker = PvSpeaker.Create(
using (var speaker = new PvSpeaker(
sampleRate: wavInfo.SampleRate,
bitsPerSample: wavInfo.BitsPerSample,
bufferSizeSecs: bufferSizeSecs,
Expand Down

0 comments on commit ce98fa8

Please sign in to comment.