欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java顯示聲音波形圖示例

 更新時(shí)間:2014年05月01日 08:27:47   作者:  
這篇文章主要介紹了java顯示聲音波形圖示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

package _tmp;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SoundTest {

 public static class WaveformGraph extends JFrame {

  private Deque<Short> deque = new LinkedList<Short>();
  private Timer timer;
  private Image buffered;
  private Image showing;

  public WaveformGraph(int width, int height) {
   setSize(width, height);
   timer = new Timer();
   buffered = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
   timer.schedule(new TimerTask() {
    @Override public void run() {

     Graphics g = buffered.getGraphics();
     g.setColor(Color.WHITE);
     g.fillRect(0, 0, getWidth(), getHeight());
     g.setColor(Color.BLACK);

     g.translate(10, getHeight()/2);

     synchronized (deque) {
      float heightRate = 1;
      if(deque.size() > 1) {
       Iterator<Short> iter = deque.iterator();
       Short p1 = iter.next();
       Short p2 = iter.next();
       int x1 = 0, x2 = 0;
       while(iter.hasNext()) {
        g.drawLine(x1, (int)(p1*heightRate), x2, (int)(p2*heightRate));

        p1 = p2;
        p2 = iter.next();
        x1 = x2;
        x2 += 1;
       }
      }
     }
     g.dispose();

     SwingUtilities.invokeLater(new Runnable() {
      @Override public void run() {
       showing = buffered;
       repaint();
       showing = null;
      }
     });
    }
   }, 100, 100);
  }

  @Override
  public void paint(Graphics g) {
   super.paint(g);
   if(buffered!=null) {
    g.drawImage(buffered, 0, 0, null);
   }
  }

  public void put(short v) {
   synchronized (deque) {
    deque.add(v);
    if(deque.size() > 500) {
     deque.removeFirst();
    }
   }
  }

  public void clear() {
   deque.clear();
  }
 }

 public static void main(String[] args) throws Exception {
//  record();
  WaveformGraph waveformGraph = new WaveformGraph(500, 300);
  waveformGraph.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  waveformGraph.setVisible(true);

  AudioInputStream ais = AudioSystem.getAudioInputStream(new File("C:\\Documents and Settings\\wml\\My Documents\\My Music\\蘇仨 - 失眠癥.wav"));
  printFormat(ais.getFormat());
  

  SourceDataLine player = AudioSystem.getSourceDataLine(ais.getFormat());

  player.open();
  player.start();

  byte[] buf = new byte[4];
  int len;
  while((len=ais.read(buf))!=-1) {

   if(ais.getFormat().getChannels() == 2) {
    if(ais.getFormat().getSampleRate() == 16) {
     waveformGraph.put((short) ((buf[1] << 8) | buf[0]));//左聲道

//     waveformGraph.put((short) ((buf[3] << 8) | buf[2]));//右聲道
    } else {
     waveformGraph.put(buf[1]);//左聲道
     waveformGraph.put(buf[3]);//左聲道

//     waveformGraph.put(buf[2]);//右聲道
//     waveformGraph.put(buf[4]);//右聲道
    }
   } else {
    if(ais.getFormat().getSampleRate() == 16) {
     waveformGraph.put((short) ((buf[1] << 8) | buf[0]));
     waveformGraph.put((short) ((buf[3] << 8) | buf[2]));
    } else {
     waveformGraph.put(buf[1]);
     waveformGraph.put(buf[2]);
     waveformGraph.put(buf[3]);
     waveformGraph.put(buf[4]);
    }
   }

   player.write(buf, 0, len);
  }

  player.close();
  ais.close();
 }

 public static void printFormat(AudioFormat format) {
  System.out.println(format.getEncoding() + " => "
    + format.getSampleRate()+" hz, "
    + format.getSampleSizeInBits() + " bit, "
    + format.getChannels() + " channel, "
    + format.getFrameRate() + " frames/second, "
    + format.getFrameSize() + " bytes/frame");
 }

// public static void record() throws LineUnavailableException,
//   InterruptedException {
//  AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 48000F, 16, 1, 2, 48000F, false);
//  Info recordDevInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
//
//  final TargetDataLine recordLine = (TargetDataLine) AudioSystem.getLine(recordDevInfo);
//  final SourceDataLine playLine = AudioSystem.getSourceDataLine(audioFormat);
//  
//  recordLine.open(audioFormat, recordLine.getBufferSize());
//  playLine.open(audioFormat, recordLine.getBufferSize());
//  
//  Thread recorder = new Thread() {
//   public void run() {
//    recordLine.start();
//    playLine.start();
//    
//    FloatControl fc = (FloatControl) playLine.getControl(FloatControl.Type.MASTER_GAIN);
//    double value = 2;
//    float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
//    fc.setValue(dB);
//    
//    try {
//     AudioInputStream in = new AudioInputStream(recordLine);
//     byte[] buf = new byte[recordLine.getBufferSize()];
//     int len;
//     while((len=in.read(buf)) != -1) {
//      playLine.write(buf, 0, len);
//     }
//    } catch (IOException e) {
//     e.printStackTrace();
//    } finally {
//     recordLine.stop();
//     playLine.stop();
//    }
//   };
//  };
//  recorder.start();
//  recorder.join();
// }
}

相關(guān)文章

  • Java自定義Spring配置標(biāo)簽

    Java自定義Spring配置標(biāo)簽

    這篇文章主要介紹了Java自定義Spring配置標(biāo)簽,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-08-08
  • Java使用Redis的方法實(shí)例分析

    Java使用Redis的方法實(shí)例分析

    這篇文章主要介紹了Java使用Redis的方法,接合實(shí)例形式分析了相關(guān)redis驅(qū)動(dòng)包安裝、java連接redis服務(wù)器、數(shù)據(jù)存儲(chǔ)、讀取等相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05
  • Spring中@Validated和@Valid區(qū)別淺析

    Spring中@Validated和@Valid區(qū)別淺析

    @Valid是javax.validation里的,?@Validated是@Valid?的一次封裝,是Spring提供的校驗(yàn)機(jī)制使用,下面這篇文章主要給大家介紹了關(guān)于Spring中@Validated和@Valid區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Java中同步與并發(fā)用法分析

    Java中同步與并發(fā)用法分析

    這篇文章主要介紹了Java中同步與并發(fā)用法,較為詳細(xì)的分析了java同步與并發(fā)所涉及的相關(guān)類與使用技巧,需要的朋友可以參考下
    2015-06-06
  • 使用springboot 打包插件去除jar包瘦身

    使用springboot 打包插件去除jar包瘦身

    這篇文章主要介紹了使用springboot 打包插件去除jar包瘦身的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java旋轉(zhuǎn)二維數(shù)組實(shí)例

    java旋轉(zhuǎn)二維數(shù)組實(shí)例

    這篇文章主要介紹了java旋轉(zhuǎn)二維數(shù)組,以實(shí)例形式較為詳細(xì)的講述了旋轉(zhuǎn)二維數(shù)的原理與實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-10-10
  • SpringMVC中如何獲取@PathVariable的值

    SpringMVC中如何獲取@PathVariable的值

    這篇文章主要介紹了SpringMVC中如何獲取@PathVariable的值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java線程協(xié)調(diào)運(yùn)行操作實(shí)例詳解

    Java線程協(xié)調(diào)運(yùn)行操作實(shí)例詳解

    這篇文章主要介紹了Java線程協(xié)調(diào)運(yùn)行操作,結(jié)合具體實(shí)例形式詳細(xì)分析了Java線程協(xié)調(diào)運(yùn)行原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • Java 內(nèi)省introspector相關(guān)原理代碼解析

    Java 內(nèi)省introspector相關(guān)原理代碼解析

    這篇文章主要介紹了Java 內(nèi)省introspector相關(guān)原理代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • nacos在liunx系統(tǒng)中啟動(dòng)成功瀏覽器卻訪問(wèn)不了的解決方法

    nacos在liunx系統(tǒng)中啟動(dòng)成功瀏覽器卻訪問(wèn)不了的解決方法

    在linux下搭建nacos,現(xiàn)在想要啟動(dòng),訪問(wèn)nacos頁(yè)面,訪問(wèn)不了,所以本文小編將給大家介紹nacos在liunx系統(tǒng)中啟動(dòng)成功,瀏覽器卻訪問(wèn)不了?全面的解決辦法,需要的朋友可以參考下
    2023-09-09

最新評(píng)論