convert raw pcm audio to wave audio in firemonkey android

  

How to convert raw pcm audio data in firemonkey android.

APath := TPath.GetSharedDownloadsPath + PathDelim + ‘test.raw’;

If (mRecorder.getState = TJAudioRecord.JavaClass.STATE_INITIALIZED) then
begin

mRecorder.startRecording;

memory := TMemoryStream.Create;
while isRecording do Begin
bytesRead := bytesRead + mRecorder.read(mBuffer, 0, mBuffer.Length);
memory.Write(mBuffer.Data^, mBuffer.Length); //mBuffer is TJavaArray<Byte>;
End;

memory.SaveToFile(APath);

SampleRateInHz := 16000;
mRecorder := TJAudioRecord.JavaClass.init(TJMediaRecorder_AudioSource.JavaClass.MIC,
SampleRateInHz, TJAudioFormat.JavaClass.CHANNEL_IN_MONO,
TJAudioFormat.JavaClass.ENCODING_PCM_16BIT,
bufferSize);

by using the above code i am successfull in recording raw pcm audio in android. But how to convert it to .wave file. The file is working fine in audacity but, i am unable to convert it to wave audio file.

I have tested this in java having the below function

/* Converting RAW format To WAV Format*/
private void rawToWave(final File rawFile, final File waveFile) throws IOException {

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;
try {
output = new DataOutputStream(new FileOutputStream(waveFile));
// WAVE header
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, SAMPLE_RATE); // sample rate
writeInt(output, SAMPLE_RATE * 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
// Audio data (conversion big endian -> little endian)
short[] shorts = new short[rawData.length / 2];
ByteBuffer.wrap(rawData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
ByteBuffer bytes = ByteBuffer.allocate(shorts.length * 2);
for (short s : shorts) {
bytes.putShort(s);
}
output.write(bytes.array());
} finally {
if (output != null) {
output.close();
rawFile.delete();
}
}

}

It worked nicely. But i am unable to convert this function in delphi firemonkey. Can anybody guide on this.

Comments are closed.