1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <android/log.h>
17
18 #include "stream/InputStream.h"
19
20 #include "WavFmtChunkHeader.h"
21
22 static const char *TAG = "WavFmtChunkHeader";
23
24 namespace parselib {
25
26 const RiffID WavFmtChunkHeader::RIFFID_FMT = makeRiffID('f', 'm', 't', ' ');
27
WavFmtChunkHeader()28 WavFmtChunkHeader::WavFmtChunkHeader() : WavChunkHeader(RIFFID_FMT) {
29 mEncodingId = ENCODING_PCM;
30 mNumChannels = 0;
31 mSampleRate = 0;
32 mAveBytesPerSecond = 0;
33 mBlockAlign = 0;
34 mSampleSize = 0;
35 mExtraBytes = 0;
36 }
37
WavFmtChunkHeader(RiffID tag)38 WavFmtChunkHeader::WavFmtChunkHeader(RiffID tag) : WavChunkHeader(tag) {
39 mEncodingId = ENCODING_PCM;
40 mNumChannels = 0;
41 mSampleRate = 0;
42 mAveBytesPerSecond = 0;
43 mBlockAlign = 0;
44 mSampleSize = 0;
45 mExtraBytes = 0;
46 }
47
normalize()48 void WavFmtChunkHeader::normalize() {
49 if (mEncodingId == ENCODING_PCM || mEncodingId == ENCODING_IEEE_FLOAT) {
50 mBlockAlign = (short) (mNumChannels * (mSampleSize / 8));
51 mAveBytesPerSecond = mSampleRate * mBlockAlign;
52 mExtraBytes = 0;
53 } else {
54 //hmmm....
55 }
56 }
57
read(InputStream * stream)58 void WavFmtChunkHeader::read(InputStream *stream) {
59 WavChunkHeader::read(stream);
60 stream->read(&mEncodingId, sizeof(mEncodingId));
61 stream->read(&mNumChannels, sizeof(mNumChannels));
62 stream->read(&mSampleRate, sizeof(mSampleRate));
63 stream->read(&mAveBytesPerSecond, sizeof(mAveBytesPerSecond));
64 stream->read(&mBlockAlign, sizeof(mBlockAlign));
65 stream->read(&mSampleSize, sizeof(mSampleSize));
66
67 if (mEncodingId != ENCODING_PCM && mEncodingId != ENCODING_IEEE_FLOAT) {
68 // only read this if NOT PCM
69 stream->read(&mExtraBytes, sizeof(mExtraBytes));
70 } else {
71 mExtraBytes = (short) (mChunkSize - 16);
72 }
73 }
74
75 } // namespace parselib
76