Skip to content

Commit 058e604

Browse files
swhardenjmcmeen
andauthored
SpectrogramGenerator: separate SaveImage() and GetImageBytes() (#62)
resolves #57 Co-authored-by: John McMeen <[email protected]>
1 parent ab81058 commit 058e604

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/Spectrogram/SpectrogramGenerator.cs

+22-21
Original file line numberDiff line numberDiff line change
@@ -262,32 +262,33 @@ public SKBitmap GetBitmapMel(int melBinCount = 25, double intensity = 1, bool dB
262262
/// <param name="intensity">Multiply the output by a fixed value to change its brightness.</param>
263263
/// <param name="dB">If true, output will be log-transformed.</param>
264264
/// <param name="dBScale">If dB scaling is in use, this multiplier will be applied before log transformation.</param>
265-
/// <param name="roll">Behavior of the spectrogram when it is full of data.
266-
/// Roll (true) adds new columns on the left overwriting the oldest ones.
267-
/// Scroll (false) slides the whole image to the left and adds new columns to the right.</param>
265+
/// <param name="roll">Controls overflow behavior. True wraps new data around to the start. False slides new data in.</param>
268266
public void SaveImage(string fileName, double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false)
267+
{
268+
string extension = Path.GetExtension(fileName).ToLower();
269+
byte[] bytes = GetImageBytes(extension, intensity, dB, dBScale, roll);
270+
File.WriteAllBytes(fileName, bytes);
271+
}
272+
273+
public byte[] GetImageBytes(string extension, double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false)
269274
{
270275
if (FFTs.Count == 0)
271276
throw new InvalidOperationException("Spectrogram contains no data. Use Add() to add signal data.");
272277

273-
string extension = Path.GetExtension(fileName).ToLower();
274-
275-
SKEncodedImageFormat fmt;
276-
if (extension == ".bmp")
277-
fmt = SKEncodedImageFormat.Bmp;
278-
else if (extension == ".png")
279-
fmt = SKEncodedImageFormat.Png;
280-
else if (extension == ".jpg" || extension == ".jpeg")
281-
fmt = SKEncodedImageFormat.Jpeg;
282-
else if (extension == ".gif")
283-
fmt = SKEncodedImageFormat.Gif;
284-
else
285-
throw new ArgumentException("unknown file extension");
286-
287-
using var image = Image.GetBitmap(FFTs, Colormap, intensity, dB, dBScale, roll, NextColumnIndex);
288-
using var encodedImage = image.Encode(fmt, 80);
289-
using var fileStream = new FileStream(fileName, FileMode.Create);
290-
encodedImage.SaveTo(fileStream);
278+
SKEncodedImageFormat fmt = extension.ToLower() switch
279+
{
280+
".bmp" => SKEncodedImageFormat.Bmp,
281+
".png" => SKEncodedImageFormat.Png,
282+
".gif" => SKEncodedImageFormat.Gif,
283+
".jpg" => SKEncodedImageFormat.Jpeg,
284+
".jpeg" => SKEncodedImageFormat.Jpeg,
285+
_ => throw new ArgumentException("unknown file extension"),
286+
};
287+
288+
using SKBitmap image = Image.GetBitmap(FFTs, Colormap, intensity, dB, dBScale, roll, NextColumnIndex);
289+
using SKData encodedImage = image.Encode(fmt, 80);
290+
byte[] bytes = encodedImage.ToArray();
291+
return bytes;
291292
}
292293

293294
/// <summary>

0 commit comments

Comments
 (0)