001package org.vishia.gral.test;
002
003import java.io.FileWriter;
004import java.io.IOException;
005import java.io.Writer;
006
007import org.vishia.gral.ifc.GralColor;
008import org.vishia.gral.widget.GralPlotArea;
009
010
011public class FqAnalyzer
012{
013
014  public static void main(String args[]){
015    FqAnalyzer main = new FqAnalyzer();
016    //main.genWave();
017    main.test();
018    System.out.println("success");
019  }
020  
021  
022  static float[] fOktave = {55.0f, 110.0f, 220.0f, 440.0f, 880.0f, 1760.0f, 3520.0f};
023  static float[] fTon  = new float[24]; 
024
025  static float fm = (float)(Math.pow(2.0, 1.0/24)); 
026  
027  static float tStep = 1.0f/44000.0f;
028  
029  float intg[][] = new float[fOktave.length*24][2];
030  float magn[] = new float[fOktave.length*24];
031  
032  float fI[] = new float[fOktave.length*24];
033  
034  float[] wave = new float[16384];
035  
036  
037  short[] audioBuffer = new short[92000];
038  
039  
040  char[] mc = new char[magn.length];
041
042  String title = "            |A B H C # D # E F # G # A B H c # d # e f # g # a b h c # d # e f # g # a b h c # d # e f # g # a b h c # d # e f # g # a b h c # d # e f # g # a b h c\n";
043  
044  public FqAnalyzer() {
045    float f = 1.0f;
046    for(int ton=0; ton<24; ++ton){
047      fTon[ton] = f;
048      f *= fm;
049    }  
050    for(int iOktave=0; iOktave<fOktave.length; ++iOktave){
051      int ix = iOktave * 24;
052      for(int ton=0; ton<24; ++ton){
053        float fq = fOktave[iOktave] * fTon[ton];
054        float fI1 = tStep * (2.0f * (float)Math.PI * fq); 
055        fI[ix] = fI1;
056        ix +=1;
057      } 
058      
059    }
060  }
061  
062  void genWave(){
063    float w1 = 0; float w2 = 0; float w3 = 0;
064    for(int ix = 0; ix < wave.length; ++ix){
065      w1 += Math.PI / 100.0;
066      w2 += Math.PI / 150.0f;
067      w3 += Math.PI / 5.0f;
068      if(w1 > Math.PI){ w1 -= 2*Math.PI; }
069      if(w2 > Math.PI){ w2 -= 2*Math.PI; }
070      if(w3 > Math.PI){ w3 -= 2*Math.PI; }
071      float val = (float)(3.5f * Math.sin(w1) + 2.0f * Math.sin(w2) + 3.0f * Math.sin(w3));
072      wave[ix] = val;
073    }
074  }
075
076  
077  float step(float val){
078    float kA = 0.01f; 
079    float kB = -0.01f;
080    float kM = 0.1f;
081    float dw = val;
082    for(int ix=0; ix < intg.length; ++ix) {
083      dw -= intg[ix][0];
084    }
085    for(int ix=0; ix < intg.length; ++ix) {
086      intg[ix][0] += dw * kA - intg[ix][1]*fI[ix];
087      intg[ix][1] += dw * kB + intg[ix][0]*fI[ix];
088      magn[ix] += Math.sqrt(intg[ix][0] * intg[ix][0] + intg[ix][1] * intg[ix][1]);
089      //magn[ix] += kM * (intg[ix][0] * intg[ix][0] + intg[ix][1] * intg[ix][1] - magn[ix]* magn[ix] );
090    }
091    return dw;
092  }
093  
094  
095  String wrc = " .-+x#";
096
097  //float[] wrv = {0.05f, 0.1f, 0.4f, 1.0f, 9999f};
098  float[] wrv = {25.0f, 100.0f, 400.0f, 1600.0f, 9999f};
099  
100  
101  void writedebugLine(Writer wr, int ix, float dw) {
102    //write a line for any magnitude of tones.
103    for(int im=0; im < intg.length; ++im) {
104      int iv = 0;
105      while( magn[im] >= wrv[iv]) iv+=1;
106      mc[im] = wrc.charAt(iv);
107    }
108    String dws = Float.toString(dw);
109    if(dws.length() >10){ dws = dws.substring(0, 10); }
110    String spaces = "            |";
111    String dwa = spaces.substring(dws.length());
112    if(wr !=null) { 
113      try{ 
114        if(ix % 80 == 0) {
115          wr.append(title);
116        }
117        wr.append(dws).append(dwa).append(new String(mc)).append('\n');
118      } catch(IOException exc){} 
119    }
120    
121  }
122  
123  
124  void test(){
125    
126    //WaveFileUtils.fillBuffer(audioBuffer, "t:\\tmp\\--\\piano_g3.wav");
127    graphicInit();
128    float dw;
129    Writer wr = null;
130    try{ wr = new FileWriter("t:/tmp/FqAnalyter.txt");}
131    catch(IOException exc){System.err.println(exc.getMessage()); }
132    for(int ix = 0; ix < audioBuffer.length; ++ix){
133      float val = audioBuffer[ix] * 0.0001f;
134      dw = step(val);
135      writedebugLine(wr, ix, dw); 
136      plotStep(ix);
137    }
138    try{ wr.close(); } catch(IOException exc){}
139  }
140  
141  GralPlotArea plot;
142  GralPlotArea.UserUnits scaling;
143  GralColor color = org.vishia.gral.ifc.GralColor.getColor("red");
144  
145  void graphicInit() {
146    GralPlotWindow wind = org.vishia.gral.test.GralPlotWindow.create("Test CurveInterpolation");
147    plot = wind.canvas();
148    scaling = plot.userUnitsPerGrid(0, 0, 1.0f, 1.0f);
149  //JZcmd   plot.drawLine(color, scaling, result, 1);
150  //JZcmd   
151  }
152  
153  
154  void plotStep(int ix){
155    if(ix % 100 == 0){
156      float[][] points = new float[2][2];
157      points[0][0] = ix/100 -1;
158      points[0][1] = ix/100;
159      points[1][0] = ix/100;
160      points[1][1] = ix/100;
161      
162      plot.drawLine(color, scaling, points, 1);  
163      
164    }
165    
166  
167  }
168  
169  
170  
171  
172}