📄 filesample.java
字号:
package abchr.audio;
import abchr.SampleHash;
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;
import java.security.NoSuchAlgorithmException;
public class FileSample extends AbstractSample {
static final long serialVersionUID = 943445419982345098L;
private static final int DATA_MAGIC = 0x64617461;
private AudioFormat format=null;
private File wave=null;
private float length;
private boolean checkHash(byte[] hash) throws IOException {
byte[] sampleHash=new byte[0];
try {
sampleHash=SampleHash.computeHash(wave,"SHA");
} catch(NoSuchAlgorithmException e) {
return false;
}
return Arrays.equals(sampleHash,hash);
}
public boolean checkHash() throws IOException {
if(!SampleHash.hashPresent(this)){return true;}
return checkHash(SampleHash.getHash(this));
}
public FileSample(File file) throws IOException,UnsupportedAudioFileException {
this.wave=file;
initialize();
}
private void initialize() throws UnsupportedAudioFileException,IOException {
AudioFileFormat fileFormat=AudioSystem.getAudioFileFormat(wave);
format=fileFormat.getFormat();
AudioInputStream stream=AudioSystem.getAudioInputStream(wave);
length=stream.getFrameLength()/format.getFrameRate();
if(length<2) {
long byteLength=0;
while(stream.available()>0) {
byteLength+=stream.skip((stream.available()/format.getFrameSize())*format.getFrameSize());
}
long frameLength=byteLength/format.getFrameSize();
length=frameLength/format.getFrameRate();
}
stream.close();
InputStream istream=new FileInputStream(wave);
DataInputStream dataStream=new DataInputStream(istream);
int k=0;
try {
int i;
for(i=0;;i++) {
k=dataStream.readInt();
if(k==DATA_MAGIC) {
dataStream.readLong();
skipBytes=(i+1)*4;
break;
}
}
} catch(EOFException e) {
throw new UnsupportedAudioFileException();
} finally {
dataStream.close();
}
}
public AudioFormat getFormat(){return format;}
int skipBytes;
public AudioInputStream getStream() {
try {
InputStream stream=new FileInputStream(wave);
stream.skip(skipBytes);
return new AudioInputStream(stream,format,(int)(length*format.getFrameRate()));
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
public File getWave(){return wave;}
public float getLength(){return length;}
public void setLength(float length){this.length=length;}
public String toString() {
return wave.getName();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -