MTG / essentia.js

JavaScript library for music/audio analysis and processing powered by Essentia WebAssembly
https://essentia.upf.edu/essentiajs
GNU Affero General Public License v3.0
645 stars 42 forks source link

Node.js example #44

Closed CharlesHolbrow closed 3 years ago

CharlesHolbrow commented 3 years ago

Any chance of adding a minimal example for using essentia.js in Node? The main README.md says this is possible, but all the examples are written for the browser.

jhartquist commented 3 years ago

+1 -- I was also looking for a way to include the TS types in a Node project.

sonoalex commented 3 years ago

Here you can find some minimal node examples: Essentia benchmarks . Hope it helps!

jakiestfu commented 1 year ago

I've been having a hard time getting this to run in a node environment.

const url = 'https://site.com/song.mp3'

const buffer = await fetch(url)
  .then((r) => r.arrayBuffer())
  .then(Buffer.from)

const results = await essentia.KeyExtractor(essentia.arrayToVector(buffer))

This yields the wrong key always. What is the recommended approach to providing the expected data to the API, @sonoalex @albincorreya ?

albincorreya commented 1 year ago

@jakiestfu You need to decode the mp3 file buffer into audio data. See some node.js examples with wav files here. You might need to use external libraries to decode mp3 file (eg: https://github.com/audiojs/audio-decode)

jakiestfu commented 1 year ago

Fantastic, @albincorreya Thank you for that reference. Does essentia only support wav containers or are other formats supported as well?

jakiestfu commented 1 year ago

@albincorreya Would you expect for this to be a correct implementation for a remote resource? I'm still getting incorrect keys and scales for MP3 audio.

import decodeAudio from 'audio-decode'
import fetch from 'node-fetch'

const main = async () => {
  const url = 'https://site.com/song.mp3'

  const buffer = await fetch(url).then((r) => r.arrayBuffer())
  const decodedAudio = await decodeAudio(buffer)

  const vector = essentia.arrayToVector(decodedAudio._data)

  const { key, scale, strength } = await essentia.KeyExtractor(vector)
}

main()
silentrob commented 1 year ago

For completion, there are also some examples for Node.js here. https://github.com/MTG/essentia.js-benchmarks/tree/master/node/benchmark

Back to the OP question. I also can't seem to get the KeyExtractor to give me the right key using a WAV or MP3 (node-lame). Based on what some online key detectors say anyway.