我正在创建一个生成信号的android应用程序并将其保存为wave文件。我到处寻找,我发现我应该像here一样“手动”“编写”文件的结构,因为Android不支持波形格式。
该文件已生成,但它不是“可播放的”,即使在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();
}
答案 0 :(得分:0)
看起来你还没有在音频数据(generatedSnd)之前写出'data'块的大小。在关闭流之前,您还需要返回并写入“RIFF”块的大小。在此示例代码中,您可以预先计算此值,而不是首先写入零。