You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This code generates the image displayed at the top of this page.
30
30
31
31
## Windows Forms
32
32
33
33
If you're using Spectrogram in a graphical application you may find it helpful to retrieve the output as a Bitmap which can be displayed on a Picturebox:
34
34
35
35
```cs
36
-
Bitmapbmp=spec.GetBitmap();
37
-
pictureBox1.Image=bmp;
36
+
pictureBox1.Image=sg.GetBitmap();
38
37
```
39
38
40
39
I find it helpful to put the Picturebox inside a Panel with auto-scroll enabled, so large spectrograms which are bigger than the size of the window can be interactively displayed.
41
40
42
41
## Real-Time Spectrogram
43
42
44
-
An example program is included in this repository which demonstrates how to use [NAudio](https://github.com/naudio/NAudio) to get samples from the sound card and display them as a spectrogram. Spectrogram was designed to be able to display spectrograms with live or growing data, so this is exceptionally easy to implement.
43
+
An example program is included in this repository which demonstrates how to use NAudio to get samples from the sound card and display them as a spectrogram. Spectrogram was designed to be able to display spectrograms with live or growing data, so this is exceptionally easy to implement.
45
44
46
-
**Run this demo: [Spectrogram.MicrophoneDemo.exe](dev/SpectrogramDemo.zip)** 👈 64-bit Windows Application
45
+
***Click-to-run demo** for 64-bit Windows: [SpectrogramDemo.exe](dev/SpectrogramDemo.zip)
47
46
48
47

49
48
50
49
To do this, keep your Spectrogram at the class level:
Whenever an audio buffer gets filled, add the data to your Spectrogram:
62
61
```cs
63
62
privatevoidGotNewBuffer(double[] audio)
64
63
{
65
-
spec.Add(audio);
64
+
sg.Add(audio);
66
65
}
67
66
```
68
67
69
68
Then set up a timer to trigger rendering:
70
69
```cs
71
70
privatevoidtimer1_Tick(objectsender, EventArgse){
72
-
Bitmapbmp=spec.GetBitmap(intensity: .4);
71
+
Bitmapbmp=sg.GetBitmap(intensity: .4);
73
72
pictureBox1.Image?.Dispose();
74
73
pictureBox1.Image=bmp;
75
74
}
@@ -82,11 +81,15 @@ Review the source code of the demo application for additional details and consid
82
81
This example demonstrates how to convert a MP3 file to a spectrogram image. A sample MP3 audio file in the [data folder](data) contains the audio track from Ken Barker's excellent piano performance of George Frideric Handel's Suite No. 5 in E major for harpsichord ([_The Harmonious Blacksmith_](https://en.wikipedia.org/wiki/The_Harmonious_Blacksmith)). This audio file is included [with permission](dev/Handel%20-%20Air%20and%20Variations.txt), and the [original video can be viewed on YouTube](https://www.youtube.com/watch?v=Mza-xqk770k).
Notice the optional conversion to Decibels while saving the image.
@@ -100,7 +103,7 @@ If you [listen to the audio track](https://www.youtube.com/watch?v=Mza-xqk770k)
100
103
The Spectrogram's `ToString()` method displays detailed information about the spectrogram:
101
104
102
105
```cs
103
-
Console.WriteLine(spec);
106
+
Console.WriteLine(sg);
104
107
```
105
108
106
109
```
@@ -114,12 +117,11 @@ Spectrogram (2993, 817)
114
117
These examples demonstrate the identical spectrogram analyzed with a variety of different colormaps. Spectrogram colormaps can be changed by calling the `SetColormap()` method:
Amplitude perception in humans, like frequency perception, is logarithmic. Therefore, Mel spectrograms typically display log-transformed spectral power and are presented using Decibel units.
140
142
141
143
```cs
142
-
// Load "I'm sorry dave, I'm afraid I can't do that" audio
*[FftSharp](https://github.com/swharden/FftSharp) - the module which actually performs the FFT and related transformations
204
204
*[MP3Sharp](https://github.com/ZaneDubya/MP3Sharp) - a library I use to read MP3 files during testing
205
205
*[FSKview](https://github.com/swharden/FSKview) - a real-time spectrogram for viewing frequency-shift-keyed (FSK) signals from audio transmitted over radio frequency.
206
-
*[NAudio](https://github.com/naudio/NAudio) - an open source .NET library which makes it easy to get samples from the microphone or sound card in real time
206
+
*[NAudio](https://github.com/naudio/NAudio) - an open source .NET library which makes it easy to get samples from the microphone or sound card in real time
207
+
208
+
## Read data from a WAV File
209
+
210
+
You should customize your file-reading method to suit your specific application. I frequently use the NAudio package to read data from WAV and MP3 files. This function reads audio data from a mono WAV file and will be used for the examples on this page.
0 commit comments