Skip to content

Commit

Permalink
GetBitmap: add rotate argument
Browse files Browse the repository at this point in the history
resolves #47
  • Loading branch information
swharden committed Jul 10, 2022
1 parent 1880e71 commit bc4ad21
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 48 deletions.
21 changes: 21 additions & 0 deletions src/Spectrogram.Tests/ImageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using NUnit.Framework;

namespace Spectrogram.Tests;

internal class ImageTests
{
[Test]
public void Test_Image_Rotations()
{
string filePath = $"../../../../../data/cant-do-that-44100.wav";
(double[] audio, int sampleRate) = AudioFile.ReadWAV(filePath);
SpectrogramGenerator sg = new(sampleRate, 4096, 500, maxFreq: 3000);
sg.Add(audio);

System.Drawing.Bitmap bmp1 = sg.GetBitmap(rotate: false);
bmp1.Save("test-image-original.png");

System.Drawing.Bitmap bmp2 = sg.GetBitmap(rotate: true);
bmp2.Save("test-image-rotated.png");
}
}
59 changes: 13 additions & 46 deletions src/Spectrogram/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,22 @@ namespace Spectrogram
{
public static class Image
{
public static Bitmap GetBitmap(
List<double[]> ffts,
Colormap cmap,
double intensity = 1,
bool dB = false,
double dBScale = 1,
bool roll = false,
int rollOffset = 0)
public static Bitmap GetBitmap(List<double[]> ffts, Colormap cmap, double intensity = 1,
bool dB = false, double dBScale = 1, bool roll = false, int rollOffset = 0, bool rotate = false)
{
if (ffts.Count == 0)
throw new ArgumentException("Not enough data in FFTs to generate an image yet.");

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

Bitmap bmp = new Bitmap(Width, Height, PixelFormat.Format8bppIndexed);
cmap.Apply(bmp);

var lockRect = new Rectangle(0, 0, Width, Height);
BitmapData bitmapData = bmp.LockBits(lockRect, ImageLockMode.ReadOnly, bmp.PixelFormat);
int stride = bitmapData.Stride;

byte[] bytes = new byte[bitmapData.Stride * bmp.Height];
Parallel.For(0, Width, col =>
ImageMaker maker = new()
{
int sourceCol = col;
if (roll)
{
sourceCol += Width - rollOffset % Width;
if (sourceCol >= Width)
sourceCol -= Width;
}
for (int row = 0; row < Height; row++)
{
double value = ffts[sourceCol][row];
if (dB)
value = 20 * Math.Log10(value * dBScale + 1);
value *= intensity;
value = Math.Min(value, 255);
int bytePosition = (Height - 1 - row) * stride + col;
bytes[bytePosition] = (byte)value;
}
});

Marshal.Copy(bytes, 0, bitmapData.Scan0, bytes.Length);
bmp.UnlockBits(bitmapData);

return bmp;
Colormap = cmap,
Intensity = intensity,
IsDecibel = dB,
DecibelScaleFactor = dBScale,
IsRoll = roll,
RollOffset = rollOffset,
IsRotated = rotate,
};

return maker.GetBitmap(ffts);
}
}
}
101 changes: 101 additions & 0 deletions src/Spectrogram/ImageMaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Spectrogram
{
/// <summary>
/// This class converts a collection of FFTs to a colormapped spectrogram image
/// </summary>
public class ImageMaker
{
/// <summary>
/// Colormap used to translate intensity to pixel color
/// </summary>
public Colormap Colormap;

/// <summary>
/// Intensity is multiplied by this number before converting it to the pixel color according to the colormap
/// </summary>
public double Intensity = 1;

/// <summary>
/// If True, intensity will be log-scaled to represent Decibels
/// </summary>
public bool IsDecibel = false;

/// <summary>
/// If <see cref="IsDecibel"/> is enabled, intensity will be scaled by this value prior to log transformation
/// </summary>
public double DecibelScaleFactor = 1;

/// <summary>
/// If False, the spectrogram will proceed in time from left to right across the whole image.
/// If True, the image will be broken and the newest FFTs will appear on the left and oldest on the right.
/// </summary>
public bool IsRoll = false;

/// <summary>
/// If <see cref="IsRoll"/> is enabled, this value indicates the pixel position of the break point.
/// </summary>
public int RollOffset = 0;

/// <summary>
/// If True, the spectrogram will flow top-down (oldest to newest) rather than left-right.
/// </summary>
public bool IsRotated = false;

public ImageMaker()
{

}

public Bitmap GetBitmap(List<double[]> ffts)
{
if (ffts.Count == 0)
throw new ArgumentException("Not enough data in FFTs to generate an image yet.");

int Width = IsRotated ? ffts[0].Length : ffts.Count;
int Height = IsRotated ? ffts.Count : ffts[0].Length;

Bitmap bmp = new(Width, Height, PixelFormat.Format8bppIndexed);
Colormap.Apply(bmp);

Rectangle lockRect = new(0, 0, Width, Height);
BitmapData bitmapData = bmp.LockBits(lockRect, ImageLockMode.ReadOnly, bmp.PixelFormat);
int stride = bitmapData.Stride;

byte[] bytes = new byte[bitmapData.Stride * bmp.Height];
Parallel.For(0, Width, col =>
{
int sourceCol = col;
if (IsRoll)
{
sourceCol += Width - RollOffset % Width;
if (sourceCol >= Width)
sourceCol -= Width;
}
for (int row = 0; row < Height; row++)
{
double value = IsRotated ? ffts[row][sourceCol] : ffts[sourceCol][row];
if (IsDecibel)
value = 20 * Math.Log10(value * DecibelScaleFactor + 1);
value *= Intensity;
value = Math.Min(value, 255);
int bytePosition = (Height - 1 - row) * stride + col;
bytes[bytePosition] = (byte)value;
}
});

Marshal.Copy(bytes, 0, bitmapData.Scan0, bytes.Length);
bmp.UnlockBits(bitmapData);

return bmp;
}
}
}
1 change: 1 addition & 0 deletions src/Spectrogram/Spectrogram.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions src/Spectrogram/SpectrogramGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,16 @@ public List<double[]> GetMelFFTs(int melBinCount)
/// <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.
/// <param name="rotate">If True, the image will be rotated so time flows from top to bottom (rather than left to right).
/// 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>
public Bitmap GetBitmap(double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false)
public Bitmap GetBitmap(double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false, bool rotate = false)
{
if (FFTs.Count == 0)
throw new InvalidOperationException("Not enough data to create an image. " +
$"Ensure {nameof(Width)} is >0 before calling {nameof(GetBitmap)}().");

return Image.GetBitmap(FFTs, Colormap, intensity, dB, dBScale, roll, NextColumnIndex);
return Image.GetBitmap(FFTs, Colormap, intensity, dB, dBScale, roll, NextColumnIndex, rotate);
}

/// <summary>
Expand Down

0 comments on commit bc4ad21

Please sign in to comment.