gpt4 book ai didi

audio - WAVE文件格式

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

我正在制作一个可生成信号并将其另存为wave文件的android应用程序。我到处看,发现我应该像here一样“手动”“编写”文件的结构,因为Android不支持wave格式。

该文件已生成,但不是“可播放的”,即使在Matlab上我也收到此消息:Incorrect chunk size information in WAV file.
这是代码,如果结构有问题,请告诉我。

public class MainActivity extends Activity {  

int duration=1;
int sampleRate=48000;
int numSample=duration*sampleRate;
double sample[]=new double[numSample];
double freq1=23000;
double freq2=24000;
byte[] generatedSnd= new byte[2*numSample];
Handler handler=new Handler();
long mySubChunk1Size = 16;
short myBitsPerSample= 16;
int myFormat = 1;
int myChannels = 1;
long myByteRate = sampleRate * myChannels * myBitsPerSample/8;
int myBlockAlign = (int) (myChannels * myBitsPerSample/8);
long myChunk2Size = generatedSnd.length* myChannels * myBitsPerSample/8;
long myChunkSize = 36 + myChunk2Size;


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread thread=new Thread(new Runnable(){
public void run(){
try {
genTone();
} catch (IOException e) {
// TODO Auto-generated catch block

e.printStackTrace();
}
handler.post(new Runnable(){
public void run(){
playSound();
}
});
}
});
thread.start();

}

protected void onResume()
{
super.onResume();

}
void genTone() throws IOException{


double instfreq=0, numerator;

for (int i=0;i<numSample; i++ )
{
numerator=(double)(i)/(double)numSample;
instfreq =freq1+(numerator*(freq2-freq1));
if ((i % 1000) == 0) {
Log.e("Current Freq:", String.format("Freq is: %f at loop %d of %d", instfreq, i, numSample));
}
sample[i]=Math.sin(2*Math.PI*i/(sampleRate/instfreq));

}
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767)); // max positive sample for signed 16 bit integers is 32767
// in 16 bit wave PCM, first byte is the low order byte (pcm: pulse control modulation)
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);


}
DataOutputStream dd=new DataOutputStream( new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tessst.wav" ));
dd.writeBytes("RIFF");
dd.writeInt(0); // Final file size not known yet, write 0
dd.writeBytes("WAVE");
dd.writeBytes("fmt ");
dd.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
dd.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
dd.writeShort(Short.reverseBytes( (short) myChannels));// Number of channels, 1 for mono, 2 for stereo
dd.writeInt(Integer.reverseBytes(sampleRate)); // Sample rate
dd.writeInt(Integer.reverseBytes( (sampleRate*myBitsPerSample*myChannels/8))); // Byte rate, SampleRate*NumberOfChannels*BitsPerSample/8
dd.writeShort(Short.reverseBytes((short) (myChannels*myBitsPerSample/8))); // Block align, NumberOfChannels*BitsPerSample/8
dd.writeShort(Short.reverseBytes( myBitsPerSample)); // Bits per sample
dd.writeBytes("data");
dd.write(generatedSnd,0,numSample);
dd.close();


}

最佳答案

看起来您还没有在音频数据(generatedSnd)之前写入“数据”块的大小。您还需要返回并在关闭流之前写入“RIFF”块的大小。在此示例代码中,您可以预先计算出该数值,而不必首先写入零。

关于audio - WAVE文件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23388285/

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