gpt4 book ai didi

android 我怎样才能将字节写入wav文件

转载 作者:行者123 更新时间:2023-11-30 00:35:03 29 4
gpt4 key购买 nike

我从一个 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();
}

最佳答案

以下代码将原始 pcm 文件转换为原始文件。正如你所说,你得到了原始字节,你可以按照你得到的这些字节创建一个原始文件并将该文件发送到以下 rawToWave() 方法或操作 rawToWave() 方法来直接将字节 [] 转换为原始文件。

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));
}
}

关于android 我怎样才能将字节写入wav文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43569304/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com