2010-07-15 73 views
0

鑑於這些說明問題,我非常感激,如果有人可以幫助已瞭解 寫成評論的問題。約拉動波形線(JAVA)

我的目的是建立一個java的音頻工具,其主要功能是: - 在播放音頻文件時顯示信號波形。

非常感謝你much..Ulrike

// here the list lines , does it store points (line coordinates) ??? 


    List<Byte> audioBytes; 
    List<Line2D.Double> lines; 




    public void addAudioByte(byte b) { 
       audioBytes.add(b); 
      } 




public void createWaveForm() { 

     if (audioBytes.size() == 0) { 
      return; 
     } 

     AudioFormat format = audioInputStream.getFormat(); 


     Dimension d = getSize(); 
     int w = d.width; 
     int h = d.height - 15; 



      // calculate number of frames per pixel 


int frames_per_pixel = audioBytes.size()/format.getFrameSize()/w; 
     byte my_byte = 0; 
     double y_last = 0; 
     int numChannels = format.getChannels(); 
     for (double x = 0; x < w && audioData != null; x++) { 
      // here what happens??? 
      int idx = (int) (frames_per_pixel * numChannels * x); 

      if (format.getSampleSizeInBits() == 8) { 
       my_byte = (byte) audioData[idx]; 
      } else { 
        // here what happens??? 


my_byte = (byte) (128 * audioData[idx]/32768); 
      } 
       // does this code draw the line ??? 
       // Y samples values 


double y_new = (double) (h * (128 - my_byte)/256); 
      lines.add(new Line2D.Double(x, y_last, x, y_new)); 
      y_last = y_new; 
     } 


     repaint(); 
    } 
    // does this method draw the lines??? what does it do? 
     public void paint(Graphics g) { 
      // component dimensions 
      Dimension d = getSize(); 
      g.setColor(getBackground()); 
      g.fillRect(0, 0, d.width, d.height); 

      if (audioBytes.size() == 0) { 
       return; 
      } 

      // I suppose this is where the lines are actually drawn 
     // The lines (so the waveform) are points and here they get connected based on the content 
     // of the list lines (declared above) ..is my reasoning correct?   
      g.setColor(Color.LIGHT_GRAY); 
      for (int i = 1; i < lines.size(); i++) { 
       Line2D.Double line = lines.get(i); 
       g.drawLine((int) line.x1, (int) line.y1, (int) line.x2, (int) line.y2); 
      } 
     } 
    } 

回答

0

createWaveforms()方法似乎是分析音頻文件,並創建一個名爲線(類型Line2D的)的lines名單。這些線對象每個都有一個起點和終點,用x1,y1和x2,y2表示。

paint()方法具有循環經過所有這些存儲線和繪製對應於所存儲的線段在畫布上的線。假設代碼的其餘部分起作用,那些行將看起來像 - 很可能是一個wave。

0

首先感謝你的關心,因爲我很好奇,以及對使用的號碼,如果他們應該取決於例如我目前正在記錄數據,並用速度Ø128K回放所以有改變很多多個字節超過44100

無論如何,我想我可以回答你的問題,這部分..

// here what happens??? 

    int idx = (int) (frames_per_pixel * numChannels * x); 

我相信這是確定如何音頻數據的許多幀將在每個像素代表波形並將idx設置爲跳過其間的字節的每個數據塊的第一個字節。

+0

你確定她在三年後還想知道嗎? – 2013-04-22 09:38:58