Skip to content

Instantly share code, notes, and snippets.

@leemartin
Last active May 24, 2024 16:23
Show Gist options
  • Save leemartin/ba839f48a353da1c9eadfa1309b85bc5 to your computer and use it in GitHub Desktop.
Save leemartin/ba839f48a353da1c9eadfa1309b85bc5 to your computer and use it in GitHub Desktop.
Spotify Waveform Data Generation from Audio Analysis API
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)
})
@jasongrishkoff
Copy link

Thanks for sharing. I actually find I get more distinct waveforms using loudness_start instead of loudness_max.

Waveform with loudness_max:
Screen Shot 2022-12-13 at 8 51 48 AM

Waveform with loudness_start:
Screen Shot 2022-12-13 at 8 51 10 AM

As you can see, using loudness_start makes it easier to visualize the highs and lows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment