This project allows you to generate a detailed Spectogram using Go from an audio file (compressed MP3 or simple WAV), visualize it as a color PNG, and interact with it using an HTML + Plotly heatmap.
I've call this module go-spectrogram-plotly. Hope to improve it in near future.
- 🎧 Supports
MP3andWAVaudio files - 🔊 Uses
FFT+Hannwindow for accurate frequency representation - 🎨 Generates color spectograms using a
Viridis-styleheatmap - 🖼️ Saves as static
PNGandJSONfrequency matrix - 📊 Interactive Plotly viewer with zoom and pan
- 📁 Clean folder structure and command-line usability
Goversion1.20(build and tested with1.23.4ondarwin/amd64architecture)Python3(for localHTMLserver, build and tested with3.9.21)libmpg123(for MP3 decoding, if needed)
Clone the repository or unzip the downloaded archive.
cd spectogram
go mod tidyThis will fetch necessary Go modules (especially beep, gonum).
Run the spectogram generator with:
go run cmd/main.go -in audio.mp3 -out spectogram.png -json data/spectogram.json-in– path to yourWAVorMP3file-out– name of thePNGimage to be generated-json– path where the spectogram matrix will be exported asJSON
✅ Example:
go run cmd/main.go -in examples/loop.wav -out spectogram.png -json data/spectogram.jsonThis generates:
- image
spectogram.png - output for Plotly in
data/spectogram.jsonfolder
To view your Spectogram interactively:
python3 -m http.serverhttp://localhost:8000/web/index.html
or
http://localhost:8000/web
You should now see an interactive, zoomable Plotly heatmap.
The Go script:
- Loads the audio and converts to mono (unfortunetly)
- Splits into FFT windows (1024 samples, 50% overlap)
- Applies Hann window
- Computes dB scale magnitudes
- Normalizes intensities
- Renders heatmap with axes and Viridis-style gradient
- Exports as
PNGandJSON - In this version you won't see Aphex Twin's face in "formula" track (mono analysis)
The HTML uses Plotly.js to render that JSON into an interactive spectogram.
cmd/ - main Go application
web/ - HTML viewer with Plotly
data/ - spectogram.json output goes here
README.md - this file
go.mod - Go module info
- MP3 support requires
libmpg123(Linux:sudo apt install libmpg123-dev) - You can increase resolution by changing
windowSizeandstepinmain.go - Edit the Plotly colorscale or layout in
web/index.htmlas you like - In future will add support for selecting multiple
*.jsonspectograms - Also add support for custom gradient style (with selecting pallete)
- Will implement support for stereo spectogram analysis!
MIT – feel free to use, modify, and share.