Skip to content

SpectrogramGenerator: separate SaveImage() and GetImageBytes() #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions src/Spectrogram/SpectrogramGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,32 +262,33 @@ public SKBitmap GetBitmapMel(int melBinCount = 25, double intensity = 1, bool dB
/// <param name="intensity">Multiply the output by a fixed value to change its brightness.</param>
/// <param name="dB">If true, output will be log-transformed.</param>
/// <param name="dBScale">If dB scaling is in use, this multiplier will be applied before log transformation.</param>
/// <param name="roll">Behavior of the spectrogram when it is full of data.
/// Roll (true) adds new columns on the left overwriting the oldest ones.
/// Scroll (false) slides the whole image to the left and adds new columns to the right.</param>
/// <param name="roll">Controls overflow behavior. True wraps new data around to the start. False slides new data in.</param>
public void SaveImage(string fileName, double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false)
{
string extension = Path.GetExtension(fileName).ToLower();
byte[] bytes = GetImageBytes(extension, intensity, dB, dBScale, roll);
File.WriteAllBytes(fileName, bytes);
}

public byte[] GetImageBytes(string extension, double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false)
{
if (FFTs.Count == 0)
throw new InvalidOperationException("Spectrogram contains no data. Use Add() to add signal data.");

string extension = Path.GetExtension(fileName).ToLower();

SKEncodedImageFormat fmt;
if (extension == ".bmp")
fmt = SKEncodedImageFormat.Bmp;
else if (extension == ".png")
fmt = SKEncodedImageFormat.Png;
else if (extension == ".jpg" || extension == ".jpeg")
fmt = SKEncodedImageFormat.Jpeg;
else if (extension == ".gif")
fmt = SKEncodedImageFormat.Gif;
else
throw new ArgumentException("unknown file extension");

using var image = Image.GetBitmap(FFTs, Colormap, intensity, dB, dBScale, roll, NextColumnIndex);
using var encodedImage = image.Encode(fmt, 80);
using var fileStream = new FileStream(fileName, FileMode.Create);
encodedImage.SaveTo(fileStream);
SKEncodedImageFormat fmt = extension.ToLower() switch
{
".bmp" => SKEncodedImageFormat.Bmp,
".png" => SKEncodedImageFormat.Png,
".gif" => SKEncodedImageFormat.Gif,
".jpg" => SKEncodedImageFormat.Jpeg,
".jpeg" => SKEncodedImageFormat.Jpeg,
_ => throw new ArgumentException("unknown file extension"),
};

using SKBitmap image = Image.GetBitmap(FFTs, Colormap, intensity, dB, dBScale, roll, NextColumnIndex);
using SKData encodedImage = image.Encode(fmt, 80);
byte[] bytes = encodedImage.ToArray();
return bytes;
}

/// <summary>
Expand Down
Loading