2.0.4 • Published 3 months ago

@superiortech/music-tempo v2.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Fork of Killercrush's Music-tempo wit Typescript support

Description

Typescript library for finding out tempo (BPM) of a song and beat tracking. It uses an algorithm "Beatroot" authored by Simon Dixon

Example App

Docs

Instalation

Using npm:

npm i @superiortech/music-tempo

Usage

Pass to the constructor MusicTempo the buffer that contains data in the following format: non-interleaved IEEE754 32-bit linear PCM with a nominal range between -1 and +1, that is, 32bits floating point buffer, with each samples between -1.0 and 1.0. This format is used in the AudioBuffer interface of Web Audio API. The object returned by the constructor contain properties tempo - tempo value in beats per minute and beats - array with beat times in seconds.

React.js

import { useState, useRef, useEffect } from "react";
import { extractTempo } from "music-tempo";

const context = new AudioContext({ sampleRate: 44100 });
const GetBPM = () => {
  const ref = useRef(null);
  const [result, setResult] = useState(null);

  useEffect(() => {
    const calcTempo = function (buffer) {
      let audioData = [];
      // Take the average of the two channels
      if (buffer.numberOfChannels == 2) {
        const channel1Data = buffer.getChannelData(0);
        const channel2Data = buffer.getChannelData(1);
        const length = channel1Data.length;
        for (let i = 0; i < length; i++) {
          audioData[i] = (channel1Data[i] + channel2Data[i]) / 2;
        }
      } else {
        audioData = buffer.getChannelData(0);
      }
      const musicTempo = extractTempo(audioData);

      setResult(musicTempo);
    };

    ref.current.onchange = function () {
      const files = ref.current.files;

      if (files.length == 0) return;

      const reader = new FileReader();

      reader.onload = function (fileEvent) {
        context.decodeAudioData(fileEvent.target.result, calcTempo);
      };

      reader.readAsArrayBuffer(files[0]);
    };
  }, []);

  return <input ref={ref} type="file" accept="audio/*" />;
};

Node.js

In Node.js environment can be used node-web-audio-api library

const AudioContext = require("web-audio-api").AudioContext;
const MusicTempo = require("music-tempo");
const fs = require("fs");

const calcTempo = function (buffer) {
  let audioData = [];
  // Take the average of the two channels
  if (buffer.numberOfChannels == 2) {
    const channel1Data = buffer.getChannelData(0);
    const channel2Data = buffer.getChannelData(1);
    const length = channel1Data.length;
    for (let i = 0; i < length; i++) {
      audioData[i] = (channel1Data[i] + channel2Data[i]) / 2;
    }
  } else {
    audioData = buffer.getChannelData(0);
  }
  const mt = extractTempo(audioData);

  console.log(mt.tempo);
  console.log(mt.beats);
};

const data = fs.readFileSync("songname.mp3");

const context = new AudioContext();
context.decodeAudioData(data, calcTempo);

Optional parameters

You can pass object with parameters as second argument to the constructor:

const result = extractTempo(audioData, {
  expiryTime: 30,
  maxBeatInterval: 1.5,
});

Most useful are maxBeatInterval/minBeatInterval and expiryTime. First two used for setting up maximum and minimum BPM. Default value for maxBeatInterval is 1 which means that minimum BPM is 60 (60 / 1 = 60). Default value for minBeatInterval is 0.3 which means that maximum BPM is 200 (60 / 0.3 = 200). Be careful, the more value of maximum BPM, the more probability of 2x-BPM errors (e.g. if max BPM = 210 and real tempo of a song 102 BPM, in the end you can get 204 BPM). expiryTime can be used if audio file have periods of silence or almost silence and because of that beat tracking is failing. Other parameters are listed in documentation.

Other

Dependencies

dependencies can be found on the package.json file and they can be installed using npm install or your favorite package manager.

Tests

npm test

Documentation

npm run docs

Build

npm run build

License

MIT License

ababacornacorn-globalsajvansi-escapesansi-regexansi-stylesanymatchargparseasn1assert-plusasynckitaws-sign2aws4babel-code-framebabel-generatorbabel-jestbabel-messagesbabel-plugin-istanbulbabel-plugin-jest-hoistbabel-preset-current-node-syntaxbabel-preset-jestbabel-runtimebabel-typesbalanced-matchbcrypt-pbkdfboolbasebrace-expansionbrowserslistbrowserslist-generatorbs-loggerbserbuffer-fromcallsitescaniuse-litecaselesschalkchar-regexcheerioci-infocjs-module-lexercocollect-v8-coveragecolor-convertcolor-loggercolor-namecombined-streamcompatfactoryconcat-mapcore-jscore-util-iscreate-jestcross-spawncrosspathcss-selectcss-whatcssomcssstyledashdashdebugdedentdeep-isdeepmergedelayed-streamdetect-indentdetect-newlinediff-sequencesdom-serializerdomelementtypedomhandlerdomutilsecc-jsbnelectron-to-chromiumemitteryemoji-regexentitieserror-exescaladeescape-htmlescape-string-regexpescodegenesprimaestraverseestree-walkeresutilsexecaexitexpectextendextsprintffast-deep-equalfast-json-stable-stringifyfast-levenshteinfastqfb-watchmanfind-upforever-agentform-datafs-extrafs.realpathfseventsfunction-bindgensyncget-caller-fileget-package-typeget-stdinget-streamgetpassglob-parentgraceful-fshar-schemahar-validatorhas-ansihasownhelpertypeshtml-escaperhtmlparser2http-signaturehuman-signalsice-capimport-localimurmurhashinflightinheritsinvariantis-arrayishis-core-moduleis-extglobis-finiteis-fullwidth-code-pointis-generator-fnis-streamis-typedarrayisbotisexeisstreamistanbul-lib-coverageistanbul-lib-instrumentistanbul-lib-reportistanbul-lib-source-mapsistanbul-reportsjest-changed-filesjest-circusjest-clijest-configjest-diffjest-docblockjest-eachjest-environment-nodejest-get-typejest-haste-mapjest-leak-detectorjest-matcher-utilsjest-message-utiljest-mockjest-pnp-resolverjest-regex-utiljest-resolvejest-resolve-dependenciesjest-runnerjest-runtimejest-snapshotjest-utiljest-validatejest-watcherjs-tokensjs-yamljsbnjsdomjson-parse-even-better-errorsjson-schemajson-schema-traversejson-stringify-safejsonfilejsprimklawkleurlevenlevnlines-and-columnslocate-pathlodashlodash.assigninlodash.bindlodash.defaultslodash.filterlodash.flattenlodash.foreachlodash.maplodash.memoizelodash.mergelodash.picklodash.reducelodash.rejectlodash.someloose-envifymagic-stringmake-errormakeerrormarkedmerge-streammerge2mime-dbmime-typesmimic-fnminimatchminimistmsnatural-comparenode-int64node-releasesnormalize-pathnpm-run-pathnth-checknwmatcheroauth-signobject-pathonceonetimeoptionatorp-limitp-locatep-tryparse-jsonparse5path-existspath-is-absolutepath-keypath-parseperformance-nowpicocolorspicomatchpiratespkg-dirprelude-lspretty-formatpromptspslpunycodepure-randqsqueue-microtaskrandombytesreact-isreadable-streamregenerator-runtimerepeatingrequestrequire-directoryresolveresolve-cwdresolve-fromresolve.exportsreusifyrun-parallelsafe-buffersafer-buffersaxserialize-javascriptshebang-commandshebang-regexsignal-exitsisteransismobsource-mapsprintf-jssshpkstack-utilsstring-lengthstring-widthstring_decoderstrip-ansistrip-final-newlinestrip-json-commentssupports-colorsupports-preserve-symlinks-flagsymbol-treetaffydbtersertest-excludetmplto-fast-propertiestough-cookietr46ts-clone-nodetslibtunnel-agenttweetnacltype-checktype-festua-parser-jsundici-typesupdate-browserslist-dburi-jsutil-deprecateuuidv8-to-istanbulverrorwalkerwebidl-conversionswhatwg-url-compatword-wrapwrap-ansiwrappywrite-file-atomicxml-name-validatory18nyallistyargs-parseryocto-queue
2.0.4

3 months ago

2.0.3

3 months ago