swharden / Spectrogram

.NET library for creating spectrograms (visual representations of frequency spectrum over time)
https://nuget.org/packages/Spectrogram
MIT License
320 stars 58 forks source link

Feature Request: Image Rotation #47

Closed wiiNinja closed 2 years ago

wiiNinja commented 2 years ago

I'm writing an app that has a visual audio waterfall component. This component is almost exactly what I need, except that the display flows from right to left instead of top to bottom. I found that before I draw the image, I can rotate it 270 degrees to achieve this effect, but it's not as clean as if the component itself can be configured to do so from the start. So this is a request for that feature. If you don't have time to do it, can you suggest a way to do this using your component? Thanks for giving a really nice and useful component.

swharden commented 2 years ago

Hi @wiiNinja,

I think the best way to implement this would be to write your own image generator (copy/pasting the existing one, making a few tactical edits). After creating a SpectrogramGenerator and loading it with data, instead of calling its GetBitmap() method you would instead call GetFFTs() and pass that result into your own method almost identical to the one below, but which creates a rotated image.

https://github.com/swharden/Spectrogram/blob/577d9d2af27a7990d014b43a8e56f26dd61c0c86/src/Spectrogram/Image.cs#L13-L63

That should be enough to get you started! I'll try to implement this today and if it's as easy as I think it is I'll name it something like GetBitmapVertical and publish an updated package. I'll follow-up here to report what I find either way.

swharden commented 2 years ago

I added a rotate argument to Spectrogram.GetBitmap() in #48 and will release Spectrogram 1.6 in a few minutes

I hacked-in a checkbox to the demo app to show how it works, and you can see the spectrogram operates as expected but there is not support to draw a horizontal frequency scale. I hope you still find this useful!

https://user-images.githubusercontent.com/4165489/178158073-337056a7-b4cd-42f4-9631-c9011879789e.mp4

swharden commented 2 years ago
SpectrogramGenerator sg = new(44100, 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");
original rotated
test-image-original test-image-rotated
swharden commented 2 years ago

doh! I just realized it's upside-down 🤦

swharden commented 2 years ago
Fixed original rotated
test-image-original test-image-rotated
wiiNinja commented 2 years ago

:) I do appreciate it very much. Definitely helps. Edit: Snapshot of things working. The "Morse Keyer V4.3" in the screenshot is an external app that triggers the audio. Thanks again. screenshot 3