1
package org.herac.tuxguitar.gui.tools.custom.tuner;
4
* Class that represents a frequency buffer that eliminates large frequency fluctuations.
6
* It is implemented as a queue list, where you put each frequency, and based on QUEUE_SIZE
7
* last frequencies it determines what current dominant frequency should be.
9
* @author Nikola Kolarovic <johnny47ns@yahoo.com>
12
public class TGTunerQueue {
14
/** size of the queue */
17
/** pointer to index of the newest element */
18
protected int head = 0;
20
/** the frequency queue itself */
21
protected double[] queue;
23
private double[] similars;
26
// TODO: tweak the tollerances
27
private final double upperTollerance = 1.05; // +5%
28
private final double lowerTollerance = 0.95; // -5%
31
public TGTunerQueue() {
32
this.queue = new double[this.QUEUE_SIZE];
33
this.similars = new double[this.QUEUE_SIZE];
37
/** add new frequency to a queue */
38
public void add(double newValue) {
39
this.head = (++this.head) % this.QUEUE_SIZE;
40
this.queue[this.head] = newValue;
45
* @return approximated frequency
47
public double getFreqApproximation() {
49
for (int i=0; i<this.QUEUE_SIZE; i++)
50
this.similars[i]=-2; // -2, because it will always find 1 exact frequency when i==j
52
for (int i=0; i<this.QUEUE_SIZE; i++) {
53
if (this.queue[i]!=-1)
54
for (int j=0; j<this.QUEUE_SIZE; j++) {
55
// exact frequency with tollerance
56
if (this.queue[i] > (this.queue[j]*this.lowerTollerance) &&
57
this.queue[i] < (this.queue[j]*this.upperTollerance) )
58
this.similars[i]=this.similars[i]+2;
60
// half frequency with tollerance
61
if (this.queue[i]/2 > (this.queue[j]*this.lowerTollerance) &&
62
this.queue[i]/2 < (this.queue[j]*this.upperTollerance) )
65
// double frequency with tollerance
66
if (this.queue[i]*2 > (this.queue[j]*this.lowerTollerance) &&
67
this.queue[i]*2 < (this.queue[j]*this.upperTollerance) )
73
// find one with max similars
75
for (int i=1; i<this.QUEUE_SIZE; i++)
76
if (this.similars[i]>=this.similars[maxIndex])
82
int test=(this.head+1)%this.QUEUE_SIZE;
83
System.out.print(Math.floor(queue[head])+ " ");
84
while (test!=this.head) {
85
System.out.print(Math.floor(queue[test])+ " ");
86
test = (++test)%this.QUEUE_SIZE;
89
System.out.println(" = "+queue[maxIndex]%110+" **");
92
return this.queue[maxIndex];
93
//return this.queue[head];
98
/** clears the queue data */
100
for (int i=0; i<this.QUEUE_SIZE; i++)