PitchDetection

illustration of sound waves

Description

A pitch detection algorithm is a way of estimating the pitch or fundamental frequency of an audio signal. This method allows to use a pre-trained machine learning pitch detection model to estimate the pitch of sound file.

At present ml5.js only supports the CREPE model. This model is a direct port of github.com/marl/crepe and only works with direct input from the browser microphone.

Quickstart

  1. const audioContext = new AudioContext();
  2. // const MicStream = MicStream
  3. const pitch = ml5.pitchDetection(
  4. './model/',
  5. audioContext,
  6. MicStream,
  7. modelLoaded,
  8. );
  9. // When the model is loaded
  10. function modelLoaded() {
  11. console.log('Model Loaded!');
  12. }
  13. pitch.getPitch((err, frequency) => {
  14. console.log(frequency);
  15. });

Usage

Initialize

  1. const detector = ml5.pitchDetection(model, audioContext, stream, callback);

Parameters

  • model: REQUIRED. The path to the trained model. Only CREPE is available for now. Case insensitive.
  • audioContext: REQUIRED. The browser audioContext to use.
  • stream MediaStream: REQUIRED. The media stream to use.
  • callback: Optional. A callback to be called once the model has loaded. If no callback is provided, it will return a promise that will be resolved once the model has loaded.

Properties


.audioContext

the AudioContext instance. Contains sampleRate, currentTime, state, baseLatency.



.model

the pitch detection model.



.results

the current pitch prediction results from the classification model.



.running

a boolean value stating whether the model instance is running or not.



.stream

the MediaStream instance. Contains an id and a boolean active value.


Methods


.getPitch()

gets the pitch.

  1. detector.getPitch(?callback);

📥 Inputs

  • callback: Optional. A function to be called when the model has generated content. If no callback is provided, it will return a promise that will be resolved once the model has predicted the pitch.

📤 Outputs

  • Object: Returns the pitch from the model attempting to predict the pitch.

Examples

p5.js

p5 web editor

plain javascript

Demo

No demos yet - contribute one today!

Tutorials

No tutorials yet - contribute one today!

Acknowledgements

Contributors:

  • Hannah Davis

Credits:

  • Paper Reference | Website URL | Github Repo | Book reference | etc

Source Code

/src/PitchDetection