| 1 | /* | 
| 2 | Copyright 2018 Google Inc. All Rights Reserved. | 
| 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 |  | 
| 17 | #ifndef RESONANCE_AUDIO_UTILS_WAV_READER_H_ | 
| 18 | #define RESONANCE_AUDIO_UTILS_WAV_READER_H_ | 
| 19 |  | 
| 20 | #include <cstdint> | 
| 21 | #include <istream> | 
| 22 |  | 
| 23 | #include "base/integral_types.h" | 
| 24 |  | 
| 25 | namespace vraudio { | 
| 26 |  | 
| 27 | // Basic RIFF WAVE decoder that supports multichannel 16-bit PCM. | 
| 28 | class WavReader { | 
| 29 |  public: | 
| 30 |   // Constructor decodes WAV header. | 
| 31 |   // | 
| 32 |   // @param binary_stream Binary input stream to read from. | 
| 33 |   explicit WavReader(std::istream* binary_stream); | 
| 34 |  | 
| 35 |   // True if WAV header was successfully parsed. | 
| 36 |   bool () const; | 
| 37 |  | 
| 38 |   // Returns the total number of samples defined in the WAV header. Note that | 
| 39 |   // the actual number of samples in the file can differ. | 
| 40 |   size_t GetNumTotalSamples() const; | 
| 41 |  | 
| 42 |   // Returns number of channels. | 
| 43 |   size_t GetNumChannels() const; | 
| 44 |  | 
| 45 |   // Returns sample rate in Hertz. | 
| 46 |   int GetSampleRateHz() const; | 
| 47 |  | 
| 48 |   // Seek to a specific frame position within the wave file. If frame_position | 
| 49 |   // is not a valid address, then the internal read position remains unchanged. | 
| 50 |   // | 
| 51 |   // @param frame_position Destination frame position for play cursor. | 
| 52 |   // @return Actual frame position of cursor after this seek operation. A | 
| 53 |   //     negative return value indicates a stream failure. | 
| 54 |   int64 SeekToFrame(const uint64 frame_position); | 
| 55 |  | 
| 56 |   // Reads samples from WAV file into target buffer. | 
| 57 |   // | 
| 58 |   // @param num_samples Number of samples to read. | 
| 59 |   // @param target_buffer Target buffer to write to. | 
| 60 |   // @return Number of decoded samples. | 
| 61 |   size_t ReadSamples(size_t num_samples, int16_t* target_buffer); | 
| 62 |  | 
| 63 |  private: | 
| 64 |   // Parses WAV header. | 
| 65 |   // | 
| 66 |   // @return True on success. | 
| 67 |   bool (); | 
| 68 |  | 
| 69 |   // Helper method to read binary data from input stream. | 
| 70 |   // | 
| 71 |   // @param size Number of bytes to read. | 
| 72 |   // @param target_ptr Target buffer to write to. | 
| 73 |   // @return Number of bytes read. | 
| 74 |   size_t ReadBinaryDataFromStream(void* target_ptr, size_t size); | 
| 75 |  | 
| 76 |   // Binary input stream. | 
| 77 |   std::istream* binary_stream_; | 
| 78 |  | 
| 79 |   // Flag indicating if the WAV header was parsed successfully. | 
| 80 |   bool init_; | 
| 81 |  | 
| 82 |   // Number of audio channels. | 
| 83 |   size_t num_channels_; | 
| 84 |  | 
| 85 |   // Sample rate in Hertz. | 
| 86 |   int sample_rate_hz_; | 
| 87 |  | 
| 88 |   // Total number of samples. | 
| 89 |   size_t num_total_samples_; | 
| 90 |  | 
| 91 |   // Number of remaining samples in WAV file. | 
| 92 |   size_t num_remaining_samples_; | 
| 93 |  | 
| 94 |   // Bytes per sample as defined in the WAV header. | 
| 95 |   size_t bytes_per_sample_; | 
| 96 |  | 
| 97 |   // Offset into data stream where PCM data begins. | 
| 98 |   uint64 pcm_offset_bytes_; | 
| 99 | }; | 
| 100 |  | 
| 101 | }  // namespace vraudio | 
| 102 |  | 
| 103 | #endif  // RESONANCE_AUDIO_UTILS_WAV_READER_H_ | 
| 104 |  |