Last active
May 24, 2024 16:23
-
-
Save leemartin/ba839f48a353da1c9eadfa1309b85bc5 to your computer and use it in GitHub Desktop.
Spotify Waveform Data Generation from Audio Analysis API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const data = require('./track.json') | |
let duration = data.track.duration | |
let segments = data.segments.map(segment => { | |
return { | |
start: segment.start / duration, | |
duration: segment.duration / duration, | |
loudness: 1 - (Math.min(Math.max(segment.loudness_max, -35), 0) / -35) | |
} | |
}) | |
let min = Math.min(...segments.map(s => s.loudness)) | |
let max = Math.max(...segments.map(s => s.loudness)) | |
let levels = [] | |
for (let i = 0.000; i < 1; i += 0.001) { | |
let s = segments.find(segment => { | |
return i <= segment.start + segment.duration | |
}) | |
let loudness = Math.round((s.loudness / max) * 100) / 100 | |
levels.push(loudness) | |
} | |
fs.writeFile('levels.json', JSON.stringify(levels), (err) => { | |
console.log(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing. I actually find I get more distinct waveforms using
loudness_start
instead ofloudness_max
.Waveform with
loudness_max
:Waveform with
loudness_start
:As you can see, using
loudness_start
makes it easier to visualize the highs and lows.