Skip to content

Commit 46b251e

Browse files
Adding comments related to troubleshooting low quality output with lots of static (#325)
Co-authored-by: Laurent Ellerbach <[email protected]>
1 parent 860cffc commit 46b251e

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

samples/I2S/Output/I2sWavPlayer.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// See LICENSE file in the project root for full license information.
44
//
@@ -16,7 +16,7 @@ namespace AudioPlayer
1616
/// You have to provide pin configuration for I2S communication and a full path to the
1717
/// WAV file you want to play.
1818
/// </summary>
19-
public class I2sWavPlayer : IDisposable
19+
public class I2SWavPlayer : IDisposable
2020
{
2121
public enum Bus
2222
{
@@ -28,15 +28,15 @@ public enum Bus
2828
private readonly FileStream _stream;
2929

3030
/// <summary>
31-
/// Creating a new instance of <see cref="I2sWavPlayer" />.
31+
/// Creating a new instance of <see cref="I2SWavPlayer" />.
3232
/// </summary>
33-
/// <param name="bus">The I2S bus ID on ESP32 plattforms.</param>
33+
/// <param name="bus">The I2S bus ID on ESP32 platforms.</param>
3434
/// <param name="audioFile">Full path to WAV file.</param>
3535
/// <param name="bckPin">The Pin ID of the BCK pin. (32 for <see cref="Bus.One" />).</param>
3636
/// <param name="dataPin">The Pin ID of the Data Out pin. (33 for <see cref="Bus.One" />).</param>
3737
/// <param name="wsPin">The Pin ID of the WS pin. (25 for <see cref="Bus.One" />).</param>
3838
/// <exception cref="IOException">Throws an IOException if the WAV file provided does not have at least 44 bytes (header).</exception>
39-
public I2sWavPlayer(Bus bus, string audioFile, int bckPin = 32, int dataPin = 33, int wsPin = 25)
39+
public I2SWavPlayer(Bus bus, string audioFile, int bckPin = 32, int dataPin = 33, int wsPin = 25)
4040
{
4141
switch (bus)
4242
{
@@ -65,9 +65,10 @@ public I2sWavPlayer(Bus bus, string audioFile, int bckPin = 32, int dataPin = 33
6565

6666
var headerParser = new WavFileHeader(header);
6767

68-
_i2S = new I2sDevice(new I2sConnectionSettings(1)
68+
_i2S = new I2sDevice(new I2sConnectionSettings((int) bus)
6969
{
7070
Mode = I2sMode.Master | I2sMode.Tx,
71+
//Mode = I2sMode.Master | I2sMode.Tx | I2sMode.Pdm, // Try this if output contains lots of static and poor audio quality
7172
CommunicationFormat = I2sCommunicationFormat.I2S,
7273

7374
SampleRate = headerParser.SampleRate,
@@ -127,7 +128,7 @@ private static I2sChannelFormat ToChannelFormat(short channels)
127128
{
128129
1 => I2sChannelFormat.OnlyLeft,
129130
2 => I2sChannelFormat.RightLeft,
130-
_ => throw new ArgumentOutOfRangeException("channels", "Only supports either Mono or Stereo WAV files.")
131+
_ => throw new ArgumentOutOfRangeException(nameof(channels), "Only supports either Mono or Stereo WAV files.")
131132
};
132133
}
133134

@@ -139,9 +140,8 @@ private static I2sBitsPerSample ToBitsPerSample(short bitsPerSample)
139140
16 => I2sBitsPerSample.Bit16,
140141
24 => I2sBitsPerSample.Bit24,
141142
32 => I2sBitsPerSample.Bit32,
142-
_ => throw new ArgumentOutOfRangeException("bitsPerSample",
143-
"Only 8, 16, 24 or 32 bits per sample are supported.")
143+
_ => throw new ArgumentOutOfRangeException(nameof(bitsPerSample), "Only 8, 16, 24 or 32 bits per sample are supported.")
144144
};
145145
}
146146
}
147-
}
147+
}

samples/I2S/Output/Program.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@
3030
}
3131

3232
// SD Card:
33-
uint cs = 5;
33+
const uint cs = 5;
3434
Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI);
3535
Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK);
3636
Configuration.SetPinFunction(19, DeviceFunction.SPI1_MISO);
3737

3838
var sdCard = new SDCard(new SDCard.SDCardSpiParameters { spiBus = 1, chipSelectPin = cs });
3939
sdCard.Mount();
4040

41-
var audioFile = "D:\\Variation-CLJ013901.wav";
42-
var player = new I2sWavPlayer(I2sWavPlayer.Bus.One, audioFile);
41+
// NOTE: If the audio has low quality and lots of static you may need to update
42+
// I2sWavPlayer to add the I2sMode.Pdm flag when configuring I2sConnectionSettings
43+
44+
const string audioFile = "D:\\Variation-CLJ013901.wav";
45+
var player = new I2SWavPlayer(I2SWavPlayer.Bus.One, audioFile);
4346
player.Play();
4447
player.Dispose();
4548

0 commit comments

Comments
 (0)