Skip to content

Commit 0fd4dce

Browse files
committed
improve empty spectrogram error message
fixes #13
1 parent 6913824 commit 0fd4dce

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Spectrogram.Tests/Mel.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,15 @@ public void Test_Mel_Graph()
8282

8383
plt1.SaveFig("mel1.png");
8484
}
85+
86+
[Test]
87+
public void Test_SaveEmpty_Throws()
88+
{
89+
(int sampleRate, double[] audio) = WavFile.ReadMono("../../../../../data/cant-do-that-44100.wav");
90+
int fftSize = 4096;
91+
var spec = new Spectrogram(sampleRate, fftSize, stepSize: 500);
92+
//spec.Add(audio);
93+
Assert.Throws<InvalidOperationException>(() => { spec.SaveImage("empty.png"); });
94+
}
8595
}
8696
}

src/Spectrogram/Spectrogram.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ public void SaveBitmap(Bitmap bmp, string fileName) { }
182182

183183
public void SaveImage(string fileName, double intensity = 1, bool dB = false, bool roll = false)
184184
{
185-
string extension = System.IO.Path.GetExtension(fileName).ToLower();
185+
if (ffts.Count == 0)
186+
throw new InvalidOperationException("Spectrogram contains no data. Use Add() to add signal data.");
187+
188+
string extension = Path.GetExtension(fileName).ToLower();
186189

187190
ImageFormat fmt;
188191
if (extension == ".bmp")

0 commit comments

Comments
 (0)