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 #ifndef _IO_WAV_WAVSTREAMREADER_H_ 17 #define _IO_WAV_WAVSTREAMREADER_H_ 18 19 #include <map> 20 21 #include "AudioEncoding.h" 22 #include "WavRIFFChunkHeader.h" 23 #include "WavFmtChunkHeader.h" 24 25 /* 26 * WAV format documentation can be found: 27 * http://soundfile.sapp.org/doc/WaveFormat/ 28 * https://web.archive.org/web/20090417165828/http://www.kk.iij4u.or.jp/~kondo/wave/mpidata.txt 29 */ 30 namespace parselib { 31 32 class InputStream; 33 34 class WavStreamReader { 35 public: 36 WavStreamReader(InputStream *stream); 37 getSampleRate()38 int getSampleRate() { return mFmtChunk->mSampleRate; } 39 getNumSampleFrames()40 int getNumSampleFrames() { 41 return mDataChunk->mChunkSize / (mFmtChunk->mSampleSize / 8) / mFmtChunk->mNumChannels; 42 } 43 getNumChannels()44 int getNumChannels() { return mFmtChunk != 0 ? mFmtChunk->mNumChannels : 0; } 45 46 int getSampleEncoding(); 47 getBitsPerSample()48 int getBitsPerSample() { return mFmtChunk->mSampleSize; } 49 50 void parse(); 51 52 // Data access 53 void positionToAudio(); 54 55 static constexpr int ERR_INVALID_FORMAT = -1; 56 static constexpr int ERR_INVALID_STATE = -2; 57 58 int getDataFloat(float *buff, int numFrames); 59 60 // int getData16(short *buff, int numFramees); 61 62 protected: 63 InputStream *mStream; 64 65 std::shared_ptr<WavRIFFChunkHeader> mWavChunk; 66 std::shared_ptr<WavFmtChunkHeader> mFmtChunk; 67 std::shared_ptr<WavChunkHeader> mDataChunk; 68 69 long mAudioDataStartPos; 70 71 std::map<RiffID, std::shared_ptr<WavChunkHeader>> mChunkMap; 72 73 private: 74 /* 75 * Individual Format Readers/Converters 76 */ 77 int getDataFloat_PCM8(float *buff, int numFrames); 78 79 int getDataFloat_PCM16(float *buff, int numFrames); 80 81 int getDataFloat_PCM24(float *buff, int numFrames); 82 83 int getDataFloat_Float32(float *buff, int numFrames); 84 int getDataFloat_PCM32(float *buff, int numFrames); 85 }; 86 87 } // namespace parselib 88 89 #endif // _IO_WAV_WAVSTREAMREADER_H_ 90