processing / processing-sound

Audio library for Processing built with JSyn
https://processing.org/reference/libraries/sound/
GNU Lesser General Public License v2.1
149 stars 50 forks source link

Implement pitch detector class #58

Closed Joe23232 closed 1 year ago

Joe23232 commented 4 years ago

Is it possible to get various information about an audio file such as current tempo, loudness, key/pitch etc?

I am going through the sound library and I am finding some difficulties finding how to do this.

image

For example if I want to get the loudness (I believe this is the amplitude) here is the sample code:

import processing.sound.*;
Amplitude amp;
AudioIn in;

void setup() {
  size(640, 360);
  background(255);

  // Create an Input stream which is routed into the Amplitude analyzer
  amp = new Amplitude(this);
  in = new AudioIn(this, 0);
  in.start();
  amp.input(in);
}      

void draw() {
  println(amp.analyze());
}

Where is the the sound coming in from?

kevinstadler commented 4 years ago

Currently there are only two built-in types of analysis available in the Sound library: Amplitude for 'loudness', and FFT for frequency spectrum analysis. You can derive more complex audio features like 'tempo' or 'key/pitch' from amplitude and FFT, but that's actually quite a complex task.

All analysis classes in the Sound library work in the same way: you choose which sound you want to analyse by calling:

analysisObject.input(soundObject);

So the example above analyses the amplitude of the sound coming out of an AudioIn object, i.e. the microphone or line-in. If you want to analyse the amplitude of a file that's being played back instead just use the following lines during setup():

SoundFile file = new SoundFile(this, .....);
file.play();
Amplitude amp = new Amplitude(this);
amp.input(file);

There's not a lot of user traffic over here on Github normally, if you have more questions or need coding help it's probably best to ask them over here at the Processing Discourse board: https://discourse.processing.org/c/processing/processing-libraries/19

Joe23232 commented 4 years ago

THanks for your help :)

Joe23232 commented 4 years ago

I did ask the community this but they didn't know the answer.

So in regards to tempo, how would I specifically get it if you happen to know?

kevinstadler commented 1 year ago

395a5dc added a BeatDetector class which allows you to detect beats, which is not quite the 'tempo' of a track, but as close as you might get in a non-specialized piece of audio software. The other existing Analyzers are now more well-documented and presented in the new website reference, which means the only thing missing from the picture is a built-in pitch/frequency detection class. This should be pretty straightforward to implement by simply wrapping the JSyn PitchDetector class. It might look something like this:

public class PitchDetector extends Analyzer {
  private final com.jsyn.unitgen.PitchDetector detector;
  private float minimumConfidence = 0.5;

  public PitchDetector(PApplet parent) {
    super(parent);
  }

  public PitchDetector(PApplet parent, float minimumConfidence) {
    super(parent);
    this.minimumConfidence = minimumConfidence;
  }

  // return the current frequency of the input signal if the confidence is at least as high
  // as this detector's minimumConfidence, 0 otherwise
  float analyze() {
    ...
  }

  // return the current frequency of the input signal if the confidence is at least as high
  // as the given minimumConfidence, 0 otherwise
  float analyze(float minimumConfidence) {
    ...
  }
}