gpt4 book ai didi

java - 复制wav文件

转载 作者:行者123 更新时间:2023-12-03 02:31:03 25 4
gpt4 key购买 nike

我已经按照here的说明复制了wav文件

尝试播放我的新文件时,它会运行,但听不到声音。有人可以帮忙吗?
我的阅读和写作课在该站点的另一篇文章中进行了解释:

这是读取文件的功能:

// read a wav file into this class
public boolean read(DataInputStream inFile) {
myData = null;
byte[] tmpInt = new byte[4];
byte[] tmpShort = new byte[2];
// byte[] buffer= new byte[offset];
try {
System.out.print(inFile.available());
} catch (IOException ex) {
Logger.getLogger(Wav.class.getName()).log(Level.SEVERE, null, ex);
}
try {
// if (offset!=0)
// inFile.read(buffer, 0,offset);

//System.out.println("Reading wav file...\n"); // for debugging only

String chunkID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

inFile.read(tmpInt); // read the ChunkSize
myChunkSize = byteArrayToIntLittle(tmpInt);

String format = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

// print what we've read so far
//System.out.println("chunkID:" + chunkID + " chunk1Size:" + myChunkSize + " format:" + format); // for debugging only

String subChunk1ID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

inFile.read(tmpInt); // read the SubChunk1Size
mySubChunk1Size = byteArrayToIntLittle(tmpInt);

inFile.read(tmpShort); // read the audio format. This should be 1 for PCM
myFormat = byteArrayToShortLittle(tmpShort);

inFile.read(tmpShort); // read the # of channels (1 or 2)
myChannels = byteArrayToShortLittle(tmpShort);

inFile.read(tmpInt); // read the samplerate
mySampleRate = byteArrayToIntLittle(tmpInt);

inFile.read(tmpInt); // read the byterate
myByteRate = byteArrayToIntLittle(tmpInt);

inFile.read(tmpShort); // read the blockalign
myBlockAlign = byteArrayToShortLittle(tmpShort);

inFile.read(tmpShort); // read the bitspersample
myBitsPerSample = byteArrayToShortLittle(tmpShort);

// print what we've read so far
System.out.println("SubChunk1ID:" + subChunk1ID + " SubChunk1Size:" + mySubChunk1Size + " AudioFormat:" + myFormat + " Channels:" + myChannels + " SampleRate:" + mySampleRate);

inFile.read(tmpShort);
extraParams= byteArrayToShortLittle(tmpShort);

// read the data chunk header - reading this IS necessary, because not all wav files will have the data chunk here - for now, we're just assuming that the data chunk is here
String dataChunkID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

inFile.read(tmpInt); // read the size of the data
myDataSize = byteArrayToIntLittle(tmpInt);
// read the data chunk
myData = new byte[(int) myDataSize];

// close the input stream
inFile.close();

} catch (Exception e) {
System.out.print(e);
return false;
}

return true;
}

然后将其另存为新的wav文件,请使用以下功能:
  public boolean save() {
try {
// Creating the file
File file = new File(myPath);
boolean isCreated = false;
try {
isCreated = file.createNewFile();
if (!isCreated) {
file.delete();
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
return false;
}

DataOutputStream outFile = new DataOutputStream(new FileOutputStream(myPath));

// write the wav file per the wav file format
outFile.writeBytes("RIFF"); // 00 - RIFF
outFile.write(intToByteArray((int) myChunkSize), 0, 4); // 04 - how big is the rest of this file?
outFile.writeBytes("WAVE"); // 08 - WAVE
outFile.writeBytes("fmt "); // 12 - fmt
outFile.write(intToByteArray((int) mySubChunk1Size), 0, 4); // 16 - size of this chunk
outFile.write(shortToByteArray((short) myFormat), 0, 2); // 20 - what is the audio format? 1 for PCM = Pulse Code Modulation
outFile.write(shortToByteArray((short) myChannels), 0, 2); // 22 - mono or stereo? 1 or 2? (or 5 or ???)
outFile.write(intToByteArray((int) mySampleRate), 0, 4); // 24 - samples per second (numbers per second)
outFile.write(intToByteArray((int) myByteRate), 0, 4); // 28 - bytes per second
outFile.write(shortToByteArray((short) myBlockAlign), 0, 2); // 32 - # of bytes in one sample, for all channels
outFile.write(shortToByteArray((short) myBitsPerSample), 0, 2); // 34 - how many bits in a sample(number)? usually 16 or 24
outFile.write(shortToByteArray((short) extraParams), 0, 2);
outFile.writeBytes("data"); // 36 - data
outFile.write(intToByteArray((int) myDataSize), 0, 4); // 40 - how big is this data chunk
outFile.write(myData); // 44 - the actual data itself - just a long string of numbers

System.out.print("size of wav "+ outFile.size()+ "\n");
outFile.close();
} catch (Exception e) {
System.out.println(e);
return false;
}

return true;
}

最佳答案

您将忽略每个read()返回的计数。不必传输多个字节。您应该使用DataInputStream.readFully()来确保所有缓冲区都已满。

但是,除非您要修改未声明的数据,否则不需要任何这些。只需使用标准的五行复制循环复制字节。

注意:您不需要所有的exist()/ isCreated()/ delete()东西。 'new FileOutputStream()'已经完成了所有这些工作。

关于java - 复制wav文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23717072/

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