From 25f8205329dba6fcf075d06b6caa9d01d8e9a6fc Mon Sep 17 00:00:00 2001 From: experopero Date: Sat, 17 Dec 2022 21:31:29 +0900 Subject: [PATCH] fix wave header --- .../common/car/MicrophoneRecorder.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/car_app_library/navigation/common/src/main/java/androidx/car/app/sample/navigation/common/car/MicrophoneRecorder.java b/car_app_library/navigation/common/src/main/java/androidx/car/app/sample/navigation/common/car/MicrophoneRecorder.java index 8734b4f..1ea6861 100644 --- a/car_app_library/navigation/common/src/main/java/androidx/car/app/sample/navigation/common/car/MicrophoneRecorder.java +++ b/car_app_library/navigation/common/src/main/java/androidx/car/app/sample/navigation/common/car/MicrophoneRecorder.java @@ -202,8 +202,11 @@ private void doRecord(CarAudioRecord record) { private void addHeader(OutputStream outputStream, int totalAudioLen) throws IOException { int totalDataLen = totalAudioLen + 36; byte[] header = new byte[44]; - int dataElementSize = 8; + int channels = 1; + int dataElementSize = 16; + long blockAlign = channels * dataElementSize / 8; long longSampleRate = AUDIO_CONTENT_SAMPLING_RATE; + long byteRate = longSampleRate * blockAlign; // See http://soundfile.sapp.org/doc/WaveFormat/ header[0] = 'R'; // RIFF/WAVE header @@ -228,17 +231,17 @@ private void addHeader(OutputStream outputStream, int totalAudioLen) throws IOEx header[19] = 0; header[20] = 1; // format = 1 PCM header[21] = 0; - header[22] = 1; // Num channels (mono) + header[22] = (byte) channels; // Num channels (mono) header[23] = 0; header[24] = (byte) (longSampleRate & 0xff); // sample rate header[25] = (byte) ((longSampleRate >> 8) & 0xff); header[26] = (byte) ((longSampleRate >> 16) & 0xff); header[27] = (byte) ((longSampleRate >> 24) & 0xff); - header[28] = (byte) (longSampleRate & 0xff); // byte rate - header[29] = (byte) ((longSampleRate >> 8) & 0xff); - header[30] = (byte) ((longSampleRate >> 16) & 0xff); - header[31] = (byte) ((longSampleRate >> 24) & 0xff); - header[32] = 1; // block align + header[28] = (byte) (byteRate & 0xff); // byte rate + header[29] = (byte) ((byteRate >> 8) & 0xff); + header[30] = (byte) ((byteRate >> 16) & 0xff); + header[31] = (byte) ((byteRate >> 24) & 0xff); + header[32] = (byte) blockAlign; // block align header[33] = 0; header[34] = (byte) (dataElementSize & 0xff); // bits per sample header[35] = (byte) ((dataElementSize >> 8) & 0xff);