-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathPitchDetector.java
More file actions
77 lines (66 loc) · 2.72 KB
/
PitchDetector.java
File metadata and controls
77 lines (66 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package processing.sound;
import com.jsyn.ports.UnitOutputPort;
import processing.core.PApplet;
/**
* Detects the pitch (also known as the 'fundamental frequency') of a sound
* signal. For complex signals this is not a trivial task, so the analyzer only
* returns a frequency measurement (measured in Hertz) when its measurement
* exceeds a 'confidence level' that can be specified by the user.
*
* @webref Analysis:PitchDetector
* @webBrief Detects the fundamental frequency of a sound signal
*/
public class PitchDetector extends Analyzer {
private final com.jsyn.unitgen.PitchDetector detector;
private float minimumConfidence;
/**
* @param parent typically "this"
* @param minimumConfidence the minimum confidence level required for
* frequency measurements, between <code>0.0</code> (accept all measurements,
* no matter how unreliable) to <code>1.0</code> (only accept perfect
* measurements). Defaults to 0.8.
*/
public PitchDetector(PApplet parent, float minimumConfidence) {
super(parent);
this.detector = new com.jsyn.unitgen.PitchDetector();
this.minimumConfidence = minimumConfidence;
}
public PitchDetector(PApplet parent) {
this(parent, 0.8f);
}
@Override
protected void removeInput() {
this.input = null;
}
@Override
protected void setInput(UnitOutputPort input) {
Engine.getEngine().add(this.detector);
this.detector.start();
this.detector.input.connect(input);
}
public float analyze() {
return this.analyze(this.minimumConfidence);
}
/**
* Returns an estimate of the current pitch (or 'fundamental frequency') of
* the input sound signal, in Hertz. If the confidence in the current
* measurement does not exceed the minimum confidence, this method returns
* <code>0.0</code>.
* @webref Analysis:PitchDetector
* @webBrief Detect the fundamental frequency of the input sound signal.
* @param minimumConfidence the minimum confidence level required for
* frequency measurements, between 0 (accept all measurements, no matter how
* unreliable) to 1 (only accept perfect measurements). If omitted, uses the
* confidence level specified when this PitchDetector was created.
* @param target a float array of length 2 that will be filled with the
* frequency and confidence in that frequency measurement
*/
public float analyze(float minimumConfidence) {
return (float) (this.detector.confidence.getValue() >= minimumConfidence ? this.detector.frequency.getValue() : 0.0);
}
public float analyze(float[] target) {
target[0] = (float) this.detector.frequency.getValue();
target[1] = (float) this.detector.confidence.getValue();
return (target[1] >= this.minimumConfidence ? target[0] : 0.0f);
}
}