📄 srshell.java
字号:
public SRLogic(SRshell srs)
{
this.srs=srs;
}
public void setWaveFormat(){}
//在录音前设置录音的格式
public void create()
{
//弹出音乐格式选择面板
fc.setVisible(true);
audioFormat=fc.getFormat();
//获得音乐文件信息
srs.setWavName("未命名");
srs.setWavPath("");
srs.setWavSize("");
srs.setWavSample1(audioFormat.getSampleRate()+" Hz");
srs.setWavSample2(audioFormat.getSampleSizeInBits()+" Bit");
if(audioFormat.getChannels()==1) srs.setWavTrack("单声道");
if(audioFormat.getChannels()==2) srs.setWavTrack("双声道");
srs.setWavFormat(audioFormat.getEncoding()+"");
srs.setWavDuration("");
//清空波形图
srs.createWave(null);
isPlay=false;
isRec=true;
}
//保存声音文件
public void save()
{
if (wavSave == null)
{
JOptionPane.showMessageDialog(null,"没有wav文件可以保存!","警告",JOptionPane.WARNING_MESSAGE);
return;
}
// reset to the beginnning of the captured data
try
{
wavSave.reset();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"无法重置音频流!","警告",JOptionPane.WARNING_MESSAGE);
return;
}
JFileChooser chooser = new JFileChooser(path);
chooser.setDialogTitle("保存wav文件");
//设定保存文件类型为*.wav
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("wav");
filter.setDescription("wav文件");
//装载文件类型过滤器
chooser.setFileFilter(filter);
//显示文件选取器
int result=chooser.showSaveDialog(srs);
if(result==JFileChooser.CANCEL_OPTION) return;
//获得文件保存路径及文件名
File wavFile=chooser.getSelectedFile();
if(wavFile.exists()==false)
{
wavFile=new File(wavFile+".wav");
}
//将音频流写入磁盘
try
{
if (AudioSystem.write(wavSave,AudioFileFormat.Type.WAVE,wavFile) == -1)
{
throw new IOException("Problems writing to file");
}
}
catch (Exception ex) {}
srs.setWavName(wavFile.getName());
srs.setWavPath(wavFile.getPath());
}
//载入声音文件
public void load()
{
wavStream=null;
//载入wave文件
//======读取.wav文件=======
JFileChooser chooser = new JFileChooser(path);
chooser.setDialogTitle("载入wav文件");
//设定打开文件类型为*.wav
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("wav");
filter.setDescription("wave波形文件");
chooser.setFileFilter(filter);
int result=chooser.showOpenDialog(srs);
if(result==JFileChooser.CANCEL_OPTION) return;
File wavFile=chooser.getSelectedFile();
try
{
//当绘制波形图和播放声音使用同一个AudioInputStream文件时就会出错误
//不得已,让它们两个分别读取同一个文件,即一个波形文件被读取两次
AudioInputStream ais=AudioSystem.getAudioInputStream(wavFile);
srs.createWave(ais);
ais.close();
wavStream=AudioSystem.getAudioInputStream(wavFile);
AudioFormat wavFormat=wavStream.getFormat();
//这里是有问题的,本来应该一个AudioInputStream流就可以实现全部操作
//但我用了三个,一个用来绘图,一个用来播放,一个用来保存
wavSave=wavStream;
/**
* we can't yet open the device for ALAW/ULAW playback,
* convert ALAW/ULAW to PCM
*/
if ((wavFormat.getEncoding() == AudioFormat.Encoding.ULAW) ||
(wavFormat.getEncoding() == AudioFormat.Encoding.ALAW))
{
AudioFormat tmp = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
wavFormat.getSampleRate(),
wavFormat.getSampleSizeInBits() * 2,
wavFormat.getChannels(),
wavFormat.getFrameSize() * 2,
wavFormat.getFrameRate(),
true);
wavStream = AudioSystem.getAudioInputStream(tmp, wavStream);
wavFormat = tmp;
}
DataLine.Info info = new DataLine.Info(
Clip.class,
wavStream.getFormat(),
((int) wavStream.getFrameLength() * wavFormat.getFrameSize())
);
clip = (Clip) AudioSystem.getLine(info);
clip.open(wavStream);
currentSound=clip;
clip.addLineListener(this);
//获得音乐文件信息
long milliseconds = (long)((wavStream.getFrameLength() * 1000) / wavFormat.getFrameRate());
double duration = milliseconds / 1000.0;
srs.setWavName(wavFile.getName());
srs.setWavPath(wavFile.getPath());
srs.setWavSize((int)(wavStream.getFrameLength()*wavFormat.getFrameSize()/1024)+" KB");
srs.setWavSample1(wavFormat.getSampleRate()+" Hz");
srs.setWavSample2(wavFormat.getSampleSizeInBits()+" Bit");
if(wavFormat.getChannels()==1) srs.setWavTrack("单声道");
if(wavFormat.getChannels()==2) srs.setWavTrack("双声道");
srs.setWavFormat(wavFormat.getEncoding()+"");
srs.setWavDuration(duration+ " 秒");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"读取wav文件时发生错误!","警告",JOptionPane.WARNING_MESSAGE);
}
srs.startWave();
isPlay=true;
isRec=false;
}
//分析并获得声音的频谱图
public void analyze()
{
if (wavSave == null)
{
JOptionPane.showMessageDialog(null,"请先打开一个wave文件","警告",JOptionPane.WARNING_MESSAGE);
return;
}
AnalyzeShell as=new AnalyzeShell(audioData,srs);
}
//对波形图进行缩放操作
public void zoom(String zoomType)
{
if(zoomType.equals("in"))
{
srs.zoomIn();
}
if(zoomType.equals("out"))
{
srs.zoomOut();
}
}
//播放声音的音量操作
public void setGain()
{
double value = sGain.getValue() / 50.0;
if (currentSound instanceof Clip)
{
try
{
Clip clip = (Clip) currentSound;
FloatControl gainControl =
(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float dB = (float)
(Math.log(value==0.0?0.0001:value)/Math.log(10.0)*20.0);
gainControl.setValue(dB);
} catch (Exception ex) {}
}
}
//播放声音的播放位置操作
public void setSeek()
{
if(clip==null) return;
int value = sSeek.getValue();
clip.setFramePosition((int)(clip.getFrameLength()*value/100));
}
//这个设置还没想好究竟需要设置什么
public void set(){}
//退出应用程序
public void exit()
{
System.exit(1);
}
//声音的播放
public void play()
{
if(clip==null) return;
try
{
clip.start();
}
catch(Exception e){return;}
srs.isPlay(true);
srs.isPause(false);
}
//声音的后退5秒
public void back()
{
long dec;
if(clip==null) return;
//按一次后退5秒
dec=clip.getMicrosecondPosition()-5000000;
if(dec>0) {clip.setMicrosecondPosition(dec);}
else
{
clip.setMicrosecondPosition(0);
Toolkit.getDefaultToolkit().beep();
srs.isPlay(false);
return;
}
srs.isPlay(true);
srs.isPause(false);
}
//声音的前进5秒
public void forward()
{
long inc;
if(clip==null)return;
//按一次前进5秒
inc=clip.getMicrosecondPosition()+5000000;
if(inc<clip.getMicrosecondLength()){clip.setMicrosecondPosition(inc);}
else
{
clip.setMicrosecondPosition(0);
Toolkit.getDefaultToolkit().beep();
srs.isPlay(false);
return;
}
srs.isPlay(true);
srs.isPause(false);
}
//声音的暂停
public void pause()
{
if(clip==null) return;
clip.stop();
srs.isPause(true);
}
//声音的停止
public void stop()
{
if(isPlay)
{
clip.stop();
clip.setFramePosition(0);
}
if(isRec)
{
capture.stop();
if(clip != null)
{
clip.stop();
clip.setFramePosition(0);
}
else
{
startWave();
}
}
srs.isPlay(false);
srs.isRec(false);
}
//声音的录制
public void rec()
{
if(isRec==false)
{
JOptionPane.showMessageDialog(null,"请先新建录音文件并设置录音文件格式!","警告",JOptionPane.WARNING_MESSAGE);
return;
}
capture.start();
isRec=true;
isPlay=false;
srs.isRec(true);
}
public void update(LineEvent e)
{
Clip clip=(Clip)e.getSource();
//这里有问题。
//if(!clip.isActive())// && (clip.getFramePosition()==clip.getFrameLength())) stop();
// System.out.println(clip.getFramePosition()+";"+clip.getFrameLength());
}
}
/* 下面这个设计有问题,暂时不用
private class SeekListener implements MouseListener,ChangeListener,MouseMotionListener
{
int value;
public SeekListener(){}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e)
{
System.out.println(clip.getFramePosition());
clip.stop();
clip.setFramePosition((int)(clip.getFrameLength()*value/100));
System.out.println(clip.getFramePosition());
clip.start();
}
public void mouseEntered(MouseEvent e){System.out.println("Entered");}
public void mouseExited(MouseEvent e){}
public void stateChanged(ChangeEvent e)
{
value=sSeek.getValue();
System.out.println(value);
}
}*/
/**
* Reads data from the input channel and writes to the output stream
*/
public class Capture implements Runnable
{
TargetDataLine line;
Thread thread;
public void start()
{
thread = new Thread(this);
thread.setName("Capture");
thread.start();
}
public void stop()
{
thread = null;
}
private void shutDown(String message)
{
if (thread != null)
{
thread = null;
wavMap.repaint();
}
}
public void run()
{
//duration = 0;
wavStream = null;
// define the required attributes for our line,
// and make sure a compatible line is supported.
AudioFormat format = audioFormat;
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
format);
if (!AudioSystem.isLineSupported(info))
{
JOptionPane.showMessageDialog(null,"系统不支持这种录音格式!","警告",JOptionPane.WARNING_MESSAGE);
return;
}
// get and open the target data line for capture.
try
{
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format, line.getBufferSize());
}
catch (LineUnavailableException ex)
{
System.out.println("Unable to open the line: " + ex);
return;
}
catch (SecurityException ex)
{
System.out.println(ex.toString());
//JavaSound.showInfoDialog();
return;
}
catch (Exception ex)
{
System.out.println(ex.toString());
return;
}
// play back the captured audio data
ByteArrayOutputStream out = new ByteArrayOutputStream();
int frameSizeInBytes = format.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int numBytesRead;
line.start();
while (thread != null)
{
if((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1)
{
break;
}
out.write(data, 0, numBytesRead);
}
// we reached the end of the stream. stop and close the line.
line.stop();
line.close();
line = null;
// stop and close the output stream
try
{
out.flush();
out.close();
} catch (IOException ex)
{
ex.printStackTrace();
}
// load bytes into the audio input stream for playback
byte audioBytes[] = out.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
wavStream = new AudioInputStream(bais, format, audioBytes.length / frameSizeInBytes);
AudioFormat wavFormat=wavStream.getFormat();
//这里是有问题的,本来应该只用一个音频流
wavSave=wavStream;
DataLine.Info info1 = new DataLine.Info(
Clip.class,
wavStream.getFormat(),
((int) wavStream.getFrameLength() * wavFormat.getFrameSize())
);
try
{
clip = (Clip) AudioSystem.getLine(info1);
clip.open(wavStream);
currentSound=clip;
}catch(Exception e){}
//获得音乐文件长度和时间
long milliseconds = (long)((wavStream.getFrameLength() * 1000) / format.getFrameRate());
double duration = milliseconds / 1000.0;
setWavSize((int)(wavStream.getFrameLength()*wavFormat.getFrameSize()/1024)+" KB");
setWavDuration(duration+"秒");
try
{
wavStream.reset();
}catch (Exception ex){return;}
createWave(wavStream);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -