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
627 stars 41 forks source link

Running with Typescript (and Electron) #46

Open Christilut opened 3 years ago

Christilut commented 3 years ago

I'm trying to get Essentia.js working in my Electron app (with Typescipt and webpack) but I'm having trouble with it.

I also can't seem to get it working on this repl.it: https://repl.it/@christilut/essentia-typescript#index.ts

Not sure what I'm doing wrong here. It seems to complain no typings are available, but I thought they should be. My Electron app says the same but continues anyway and runs into different import errors.

What is the correct way to import Essentia.js in Typescript? Since there are multiple ways, it would help to know the right way and see what happens then.

davidhaas6 commented 2 years ago

Did you figure it out? Facing the same issue.

jmarcosfer commented 2 years ago

Hi @Christilut and @davidhaas6,

Sorry, no, we have not yet looked into using the library with Typescript or with Electron. We're currently under active development and should be working on testing soon. I'll get back to this issue as soon as I can.

Jorge

albincorreya commented 2 years ago

We don't have a default index.d.ts entry point defined for TypeScript in the repo. But the TypesScript interface is accessible using the type definition files (.d.ts) files distributed with our releases or at NPM.

Here is an example project that used essentia.js via Typescript. This file uses an old version of the lib though. But the method should be the same.

As @jmarcosfer mentioned, we don't have an example of using it specifically on an Electron app yet.

kmturley commented 8 months ago

Managed to get it working using:

npm install essentia.js fs-extra node-wav

Then:

import { readFileSync } from 'fs-extra';
import * as wav from 'node-wav';
// @ts-ignore
import { Essentia, EssentiaWASM } from 'essentia.js';

const fileBuffer = readFileSync('flute.wav');
const essentia: Essentia = new Essentia(EssentiaWASM);
const audioBuffer = wav.decode(fileBuffer);
const channelData: Float32Array = audioBuffer.channelData[0];
const inputSignalVector = essentia.arrayToVector(channelData);
const outputPyYin = essentia.PitchYinProbabilistic(inputSignalVector);
const pitches = essentia.vectorToArray(outputPyYin.pitch);
const voicedProbabilities = essentia.vectorToArray(outputPyYin.voicedProbabilities);
console.log('pitches', pitches);
console.log('voicedProbabilities', voicedProbabilities);

Which outputs:

pitches Float32Array(613) [
  -682.4991455078125, -682.4991455078125, -682.4991455078125, -682.4991455078125,
  -682.4991455078125, -682.4991455078125, -682.4991455078125, -682.4991455078125,
  -682.4991455078125, -682.4991455078125, -682.4991455078125, -682.4991455078125,
  -682.4991455078125, -682.4991455078125,  681.5415649414062,    679.41748046875,
   693.6409301757812,  692.8771362304688,  692.0700073242188,  691.5407104492188,
   691.2211303710938,  690.7554931640625,  690.4441528320312,  690.2822875976562,
   690.1727905273438,  690.1913452148438,  690.1138305664062,  690.0697021484375,
   690.1189575195312,   690.257080078125,  690.2586059570312,       690.26953125,
   690.2762451171875,   690.264404296875,  690.2883911132812,  689.9949340820312,
     689.97998046875,  689.8556518554688,   689.650146484375,  689.4231567382812,
   689.3624267578125,  689.3429565429688,  689.5848388671875,      689.880859375,
   690.4718627929688,      691.013671875,  691.7080688476562,    691.81689453125,
   691.4556884765625,  690.5984497070312,  705.5728759765625,  703.8046264648438,
   702.6657104492188,  701.3317260742188,  700.5100708007812,  700.1961669921875,
   700.0474853515625,  700.3526611328125,    700.72216796875,  701.4735717773438,
   701.9083862304688,  702.6136474609375,  703.4396362304688,  703.8663330078125,
   704.3475952148438,  704.8639526367188,  705.2023315429688,       705.26171875,
   689.1476440429688,  690.0390014648438,  690.7661743164062,    691.58642578125,
   692.0952758789062,   691.767822265625,   691.102294921875,      689.095703125,
   703.8651123046875,  702.5081176757812,  701.4411010742188,  700.7323608398438,
   700.4495239257812,  700.8333740234375,  701.5823974609375,  701.8468017578125,
   702.9113159179688,  703.1157836914062,  703.0243530273438,  703.5252075195312,
   703.9395141601562,  704.1390380859375,  703.8682250976562,    703.29736328125,
     703.35595703125,  702.7356567382812,  702.2926635742188,  717.0540771484375,
   711.9170532226562, -723.0826416015625, -723.0826416015625, -731.4844360351562,
  ... 513 more items
]
voicedProbabilities Float32Array(613) [
  0.0071817138232290745, 0.010977386496961117, 0.006347390823066235,
   0.007238227408379316, 0.006023986265063286, 0.010828307829797268,
   0.028241265565156937,  0.05838972330093384,  0.04963346943259239,
     0.0533740371465683, 0.018215572461485863,  0.03679880499839783,
    0.05447571724653244,  0.09197627753019333,  0.09417010843753815,
     0.2271496206521988,   0.1371249556541443,   0.4167638123035431,
     0.5539039969444275,   0.5324959754943848,   0.8148526549339294,
    0.46052613854408264,  0.49049675464630127,   0.7902030944824219,
    0.46052613854408264,   0.7302618026733398,    0.940056324005127,
     0.6403498649597168,   0.7602324485778809,   0.7602324485778809,
     0.6403498649597168,    0.940056324005127,   0.7002911567687988,
    0.37061429023742676,  0.37061429023742676,   0.6403498649597168,
    0.25073182582855225,    0.940056324005127,    0.940056324005127,
     0.7302618026733398,    0.940056324005127,   0.5204673409461975,
    0.46052613854408264,  0.37061429023742676,   0.5504379868507385,
     0.8201737403869629,  0.16081997752189636,   0.8501443862915039,
      0.970026969909668,   0.7002911567687988,   0.6403498649597168,
    0.47460848093032837,  0.46052613854408264,    0.406512588262558,
     0.8869524002075195,   0.9175653457641602,   0.6326019167900085,
     0.7963614463806152,   0.8501443862915039,   0.5187482833862305,
    0.33090823888778687,   0.7739077210426331,   0.8201737403869629,
     0.7002911567687988,   0.6403498649597168,   0.6403498649597168,
     0.2207612246274948,   0.4005849063396454,   0.9100856781005859,
     0.8801150321960449,   0.7002911567687988,   0.5204673409461975,
    0.25073182582855225,  0.46052613854408264,   0.7602324485778809,
     0.6703205108642578,  0.41279804706573486,    0.940056324005127,
     0.6702680587768555,   0.9999972581863403,    0.969092071056366,
     0.9999972581863403,    0.214544877409935,    0.534544050693512,
      0.940056324005127,   0.7002911567687988,  0.46052613854408264,
     0.8201737403869629,   0.7902030944824219,    0.940056324005127,
     0.9100856781005859,   0.9100856781005859,  0.49749457836151123,
     0.2660381495952606,   0.1642051637172699,   0.2810819745063782,
    0.07983743399381638,   0.1343621015548706,  0.06890365481376648,
    0.07982081174850464,
  ... 513 more items
]
wulf11 commented 5 months ago
import EssentiaType from 'essentia.js/dist/core_api';

// use require instead of import, to use CommonJS-modules
const Essentia = require('essentia.js').Essentia;
const EssentiaWASM = require('essentia.js').EssentiaWASM;

const EssentiaInstance: EssentiaType = new Essentia(EssentiaWASM, false);

or

// @ts-ignore
import * as wasm from 'essentia.js/dist/essentia-wasm.module'
// @ts-ignore
import EssentiaJS from 'essentia.js/dist/essentia.js-core.es'
import Essentia from 'essentia.js/dist/core_api'

// export as default for entry point to node package with the js wrapper
const essentia = new EssentiaJS(wasm.EssentiaModule, false) as Essentia

but what works best for me is:

import EssentiaType from 'essentia.js/dist/core_api';
let esPkg = require('essentia.js');
const essentia: EssentiaType = new esPkg.Essentia(esPkg.EssentiaWASM);

or ES6 imports only:

import EssentiaType from 'essentia.js/dist/core_api';
import * as esPkg from 'essentia.js';
const essentiaInstance: EssentiaType = new esPkg.Essentia(esPkg.EssentiaWASM, false);