我从wav文件中获取数据,并希望将字节写回wav。
我已经从wav获得了字节。这是我的代码
我知道0-43个字节是标题,44-是数据
byte[] soundBytes;
try {
InputStream inputStream = new FileInputStream(getRealPathFromURI(this,uri));
soundBytes = toByteArray(inputStream);
for (int i = 0; i != -1 ; i++) {
if(soundBytes[i]<0) {
k =soundBytes[i] + 256;
} else {k = soundBytes[i];}
System.out.println("byte " + i + ": " + k );
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
public byte[] toByteArray(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int read = 0;
byte[] buffer = new byte[1024];
while (read != -1) {
read = in.read(buffer);
if (read != -1)
out.write(buffer,0,read);
}
out.close();
return out.toByteArray();
}
答案 0 :(得分:1)
以下代码将原始pcm文件转换为原始文件。如你所说,你得到了原始字节,你要么创建一个包含这些字节的原始文件,然后将该文件发送到以下rawToWave()方法或者操作rawToWave()方法,以便将require bytes []直接转换为原始文件。
public void rawToWave(final File rawFile) throws IOException {
File waveFile = DirectoryOperations.createDirAndAudioFile("vocal.wav");// creating the empty wav file.
byte[] rawData = new byte[(int) rawFile.length()];
DataInputStream input = null;
try {
input = new DataInputStream(new FileInputStream(rawFile));
input.read(rawData);
} finally {
if (input != null) {
input.close();
}
}
DataOutputStream output = null;//following block is converting raw to wav.
try {
output = new DataOutputStream(new FileOutputStream(waveFile));
// WAVE header
// see http://ccrma.stanford.edu/courses/422/projects/WaveFormat/
writeString(output, "RIFF"); // chunk id
writeInt(output, 36 + rawData.length); // chunk size
writeString(output, "WAVE"); // format
writeString(output, "fmt "); // subchunk 1 id
writeInt(output, 16); // subchunk 1 size
writeShort(output, (short) 1); // audio format (1 = PCM)
writeShort(output, (short) 1); // number of channels
writeInt(output, RECORDER_SAMPLERATE); // sample rate
writeInt(output, RECORDER_SAMPLERATE * 2); // byte rate
writeShort(output, (short) 2); // block align
writeShort(output, (short) 16); // bits per sample
writeString(output, "data"); // subchunk 2 id
writeInt(output, rawData.length); // subchunk 2 size
output.write(fullyReadFileToBytes(rawFile));
} finally {
if (output != null) {
output.close();
}
}
}
private byte[] fullyReadFileToBytes(File f) throws IOException {
int size = (int) f.length();
byte bytes[] = new byte[size];
byte tmpBuff[] = new byte[size];
try (FileInputStream fis = new FileInputStream(f)) {
int read = fis.read(bytes, 0, size);
if (read < size) {
int remain = size - read;
while (remain > 0) {
read = fis.read(tmpBuff, 0, remain);
System.arraycopy(tmpBuff, 0, bytes, size - remain, read);
remain -= read;
}
}
}
return bytes;
}
private void writeInt(final DataOutputStream output, final int value) throws IOException {
output.write(value);
output.write(value >> 8);
output.write(value >> 16);
output.write(value >> 24);
}
private void writeShort(final DataOutputStream output, final short value) throws IOException {
output.write(value);
output.write(value >> 8);
}
private void writeString(final DataOutputStream output, final String value) throws IOException {
for (int i = 0; i < value.length(); i++) {
output.write(value.charAt(i));
}
}