Skip to content

Commit e3b6e3f

Browse files
committed
GetBitmap: improve no-data exception message
#42
1 parent 17d3502 commit e3b6e3f

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/Spectrogram.Tests/AddTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal class AddTests
1313
public void Test_No_Data()
1414
{
1515
SpectrogramGenerator sg = new(44100, 2048, 1000);
16-
var bmp = sg.GetBitmap();
16+
Assert.Throws<InvalidOperationException>(() => sg.GetBitmap());
1717
}
1818
}
1919
}

src/Spectrogram/Image.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static Bitmap GetBitmap(
2020
int rollOffset = 0)
2121
{
2222
if (ffts.Count == 0)
23-
throw new ArgumentException("This Spectrogram contains no FFTs (likely because no signal was added)");
23+
throw new ArgumentException("Not enough data in FFTs to generate an image yet.");
2424

2525
int Width = ffts.Count;
2626
int Height = ffts[0].Length;

src/Spectrogram/SpectrogramGenerator.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,14 @@ public List<double[]> GetMelFFTs(int melBinCount)
269269
/// <param name="roll">Behavior of the spectrogram when it is full of data.
270270
/// Roll (true) adds new columns on the left overwriting the oldest ones.
271271
/// Scroll (false) slides the whole image to the left and adds new columns to the right.</param>
272-
public Bitmap GetBitmap(double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false) =>
273-
Image.GetBitmap(FFTs, Colormap, intensity, dB, dBScale, roll, NextColumnIndex);
272+
public Bitmap GetBitmap(double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false)
273+
{
274+
if (FFTs.Count == 0)
275+
throw new InvalidOperationException("Not enough data to create an image. " +
276+
$"Ensure {nameof(Width)} is >0 before calling {nameof(GetBitmap)}().");
277+
278+
return Image.GetBitmap(FFTs, Colormap, intensity, dB, dBScale, roll, NextColumnIndex);
279+
}
274280

275281
/// <summary>
276282
/// Create a Mel-scaled spectrogram.

0 commit comments

Comments
 (0)