~ubuntu-branches/ubuntu/lucid/tuxguitar/lucid-updates

« back to all changes in this revision

Viewing changes to TuxGuitar-tuner/src/org/herac/tuxguitar/gui/tools/custom/tuner/TGTunerQueue.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2009-04-25 19:49:27 UTC
  • mfrom: (1.1.3 upstream) (2.1.7 jaunty)
  • Revision ID: james.westby@ubuntu.com-20090425194927-pblqed0zxp0pmyeq
Tags: 1.1-1
* New Upstream Release (Closes: #489859) (LP: #366476)
* Merged patch : tuxguitar_1.0.dak-1ubuntu1.patch
* debian/README.txt
  - suggests to install tuxguitar-jsa

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.tools.custom.tuner;
 
2
 
 
3
/**
 
4
 * Class that represents a frequency buffer that eliminates large frequency fluctuations.
 
5
 * 
 
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.
 
8
 * 
 
9
 * @author Nikola Kolarovic <johnny47ns@yahoo.com>
 
10
 *
 
11
 */
 
12
public class TGTunerQueue {
 
13
        
 
14
        /** size of the queue */
 
15
        int QUEUE_SIZE = 5;
 
16
        
 
17
        /** pointer to index of the newest element */
 
18
        protected int head = 0;
 
19
        
 
20
        /** the frequency queue itself */
 
21
        protected double[] queue;
 
22
        
 
23
        private double[] similars;
 
24
        
 
25
        
 
26
        // TODO: tweak the tollerances
 
27
        private final double upperTollerance = 1.05; // +5%
 
28
        private final double lowerTollerance = 0.95; // -5%
 
29
        
 
30
        
 
31
        public TGTunerQueue() {
 
32
                this.queue = new double[this.QUEUE_SIZE];
 
33
                this.similars = new double[this.QUEUE_SIZE];
 
34
                this.clear();
 
35
        }
 
36
 
 
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;
 
41
        }
 
42
        
 
43
        /** 
 
44
         * 
 
45
         * @return approximated frequency 
 
46
         */
 
47
        public double getFreqApproximation() {
 
48
                
 
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
 
51
                
 
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;
 
59
                                        
 
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) )
 
63
                                                        this.similars[i]++;
 
64
                                        
 
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) )
 
68
                                                        this.similars[i]++;
 
69
                                }
 
70
                }
 
71
                
 
72
                
 
73
                // find one with max similars
 
74
                int maxIndex = 0;
 
75
                for (int i=1; i<this.QUEUE_SIZE; i++)
 
76
                        if (this.similars[i]>=this.similars[maxIndex])
 
77
                                maxIndex=i;
 
78
                
 
79
                
 
80
                
 
81
/*              
 
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;
 
87
                
 
88
                }
 
89
                System.out.println(" =  "+queue[maxIndex]%110+" **");
 
90
*/
 
91
                
 
92
                return this.queue[maxIndex];
 
93
                //return this.queue[head];
 
94
                
 
95
        }
 
96
        
 
97
        
 
98
        /** clears the queue data */
 
99
        public void clear() {
 
100
                for (int i=0; i<this.QUEUE_SIZE; i++)
 
101
                        this.queue[i]=-1;
 
102
                this.head = 0;
 
103
        }
 
104
        
 
105
}