/*
Please feel free to use/modify this class.
If you give me credit by keeping this information or
by sending me an email before using it or by reporting bugs , i will be happy.
Email : gtiwari333@gmail.com,
Blog : http://ganeshtiwaridotcomdotnp.blogspot.com/
*/
package org.ioe.tprsa.audio;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.Line2D;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.Vector;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;
import org.ioe.tprsa.util.MessageType;
/**
* Capture/Playback sample. Record audio in different formats and then playback
* the recorded audio. The captured audio can be saved either as a WAVE, AU or
* AIFF. Or load an audio file for streaming playback.
*
* @version @(#)Manual.java 1.11 99/12/03
* @version 2.1
* @author Brian Lichtenwalter-- visualization and capture
* @modified-by Ganesh --> made a reusable class
*/
public class JSoundCapture extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
byte[] audioBytes = null;
float[] audioData = null;
final int BUFFER_SIZE = 16384;
int counter = 0;
FormatControlConf formatControls = new FormatControlConf(); // @jve:decl-index=0:
Capture capture = new Capture(); // @jve:decl-index=0:
Playback playback = new Playback(); // @jve:decl-index=0:
WaveData wd;
AudioInputStream audioInputStream; // @jve:decl-index=0:
SamplingGraph samplingGraph;
JButton playB, captB, pausB;
JButton saveB;
String errStr;
double duration, seconds;
File file; // @jve:decl-index=0:
Vector<Line2D.Double> lines = new Vector<Line2D.Double>(); // @jve:decl-index=0:
boolean isDrawingRequired;
boolean isSaveRequired;
JPanel innerPanel;
String saveFileName = null; // @jve:decl-index=0:
/**
* Instantiates a new j sound capture.
*
* @param isDrawingRequired
* the is drawing required
* @param isSaveRequired
* the is save required
*/
public JSoundCapture(boolean isDrawingRequired, boolean isSaveRequired) {
wd = new WaveData();
this.isDrawingRequired = isDrawingRequired;
this.isSaveRequired = isSaveRequired;
setLayout(new BorderLayout());
setBorder(new EmptyBorder(1, 1, 1, 1));
innerPanel = new JPanel();
innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS));
JPanel buttonsPanel = new JPanel();
buttonsPanel.setPreferredSize(new Dimension(200, 50));
buttonsPanel.setBorder(new EmptyBorder(5, 0, 1, 0));
playB = addButton("Play", buttonsPanel, false);
captB = addButton("Record", buttonsPanel, true);
pausB = addButton("Pause", buttonsPanel, false);
saveB = addButton("Save ", buttonsPanel, false);
innerPanel.add(buttonsPanel);
// samplingPanel
if (isDrawingRequired) {
JPanel samplingPanel = new JPanel(new BorderLayout());
EmptyBorder eb = new EmptyBorder(2, 2, 2, 2);
SoftBevelBorder sbb = new SoftBevelBorder(SoftBevelBorder.LOWERED);
samplingPanel.setBorder(new CompoundBorder(eb, sbb));
samplingPanel.add(samplingGraph = new SamplingGraph());
innerPanel.add(samplingPanel);
}
// whole panel
JPanel completePanel = new JPanel();
completePanel.setLayout(new BoxLayout(completePanel, BoxLayout.X_AXIS));
completePanel.add(innerPanel);
add(completePanel);
}
// public void setAutoFileSaveMode(){
//
// }
public boolean isSoundDataAvailable() {
if (audioBytes != null) return (audioBytes.length > 100);
else
return false;
}
public byte[] getAudioBytes() {
return audioBytes;
}
public String getSaveFileName() {
return saveFileName;
}
public void setSaveFileName(String saveFileName) {
this.saveFileName = saveFileName;
System.out.println("FileName Changed !!! " + saveFileName);
}
public float[] getAudioData() {
if (audioData == null) {
audioData = wd.extractFloatDataFromAudioInputStream(audioInputStream);
}
return audioData;
}
public void setAudioData(float[] audioData) {
this.audioData = audioData;
}
private JButton addButton(String name, JPanel p, boolean state) {
JButton b = new JButton(name);
b.setPreferredSize(new Dimension(85, 24));
b.addActionListener(this);
b.setEnabled(state);
b.setFocusable(false);
p.add(b);
return b;
}
/*
* (non-Javadoc)
*
* @see
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
System.out.println("actionPerformed *********");
Object obj = e.getSource();
if (isSaveRequired && obj.equals(saveB)) {
getFileNameAndSaveFile();
}
else if (obj.equals(playB)) {
if (playB.getText().startsWith("Play")) {
playCaptured();
}
else {
stopPlaying();
}
}
else if (obj.equals(captB)) {
if (captB.getText().startsWith("Record")) {
startRecord();
}
else {
stopRecording();
}
}
else if (obj.equals(pausB)) {
if (pausB.getText().startsWith("Pause")) {
pausePlaying();
}
else {
resumePlaying();
}
}
}
public void playCaptured() {
playback.start();
if (isDrawingRequired) samplingGraph.start();
captB.setEnabled(false);
pausB.setEnabled(true);
playB.setText("Stop");
}
public void stopPlaying() {
playback.stop();
if (isDrawingRequired) samplingGraph.stop();
captB.setEnabled(true);
pausB.setEnabled(false);
playB.setText("Play");
}
public void startRecord() {
file = null;
capture.start();
if (isDrawingRequired) samplingGraph.start();
playB.setEnabled(false);
pausB.setEnabled(true);
saveB.setEnabled(false);
captB.setText("Stop");
}
public void stopRecording() {
lines.removeAllElements();
capture.stop();
if (isDrawingRequired) samplingGraph.stop();
playB.setEnabled(true);
pausB.setEnabled(false);
saveB.setEnabled(true);
captB.setText("Record");
}
public void pausePlaying() {
if (capture.thread != null) {
capture.line.stop();
}
else {
if (playback.thread != null) {
playback.line.stop();
}
}
pausB.setText("Resume");
}
public void resumePlaying() {
if (capture.thread != null) {
capture.line.start();
}
else {
if (playback.thread != null) {
playback.line.start();
}
}
pausB.setText("Pause");
}
public void getFileNameAndSaveFile() {
while (saveFileName == null) {
saveFileName = JOptionPane.showInputDialog(null, "Enter WAV File Name", "Getting File Name");
}
wd.saveToFile(saveFileName, AudioFileFormat.Type.WAVE, audioInputStream);
}
// public void saveFileAutoMode() {
// wd.saveToFile(saveFileName, AudioFileFormat.Type.WAVE, audioInputStream);
// saveFileName=null;
// }
/*
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
google code mfcc java 实现 (106个子文件)
all-wcprops 945B
all-wcprops 826B
all-wcprops 806B
all-wcprops 581B
all-wcprops 560B
all-wcprops 528B
all-wcprops 291B
all-wcprops 289B
all-wcprops 270B
all-wcprops 252B
all-wcprops 240B
all-wcprops 110B
all-wcprops 101B
all-wcprops 95B
all-wcprops 91B
all-wcprops 87B
all-wcprops 87B
all-wcprops 65B
all-wcprops 62B
all-wcprops 61B
all-wcprops 61B
all-wcprops 56B
.classpath 301B
entries 1KB
entries 1KB
entries 1KB
entries 846B
entries 825B
entries 820B
entries 652B
entries 511B
entries 502B
entries 493B
entries 485B
entries 389B
entries 340B
entries 324B
entries 322B
entries 318B
entries 314B
entries 306B
entries 304B
entries 262B
entries 258B
entries 258B
JSoundCapture.java 21KB
HiddenMarkov.java 16KB
HMM_VQ_Speech_Recognition.java 15KB
Operations.java 12KB
Codebook.java 11KB
WaveData.java 6KB
ArrayWriter.java 6KB
Centroid.java 5KB
MFCC.java 5KB
FFT.java 5KB
ObjectIODataBase.java 4KB
FeatureExtract.java 4KB
Points.java 4KB
EndPointDetection.java 3KB
PreProcess.java 3KB
Delta.java 3KB
ObjectIO.java 3KB
TrainingTestingWaveFiles.java 3KB
FormatControlConf.java 2KB
FeatureVector.java 2KB
HMMModel.java 2KB
Energy.java 1KB
DCT.java 1KB
CodeBookDictionary.java 982B
ErrorManager.java 754B
DataBase.java 554B
MessageType.java 410B
Model.java 387B
org.eclipse.jdt.core.prefs 629B
.project 516B
JSoundCapture.java.svn-base 21KB
HiddenMarkov.java.svn-base 16KB
HMM_VQ_Speech_Recognition.java.svn-base 15KB
Operations.java.svn-base 12KB
Codebook.java.svn-base 11KB
WaveData.java.svn-base 6KB
ArrayWriter.java.svn-base 6KB
Centroid.java.svn-base 5KB
MFCC.java.svn-base 5KB
FFT.java.svn-base 5KB
ObjectIODataBase.java.svn-base 4KB
FeatureExtract.java.svn-base 4KB
Points.java.svn-base 4KB
EndPointDetection.java.svn-base 3KB
PreProcess.java.svn-base 3KB
Delta.java.svn-base 3KB
ObjectIO.java.svn-base 3KB
TrainingTestingWaveFiles.java.svn-base 3KB
FormatControlConf.java.svn-base 2KB
FeatureVector.java.svn-base 2KB
HMMModel.java.svn-base 2KB
Energy.java.svn-base 1KB
DCT.java.svn-base 1KB
CodeBookDictionary.java.svn-base 982B
ErrorManager.java.svn-base 754B
共 106 条
- 1
- 2
资源评论
春和景明绿满园
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功