• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 3-Clause Clear License
5  * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6  * License was not distributed with this source code in the LICENSE file, you
7  * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
8  * Alliance for Open Media Patent License 1.0 was not distributed with this
9  * source code in the PATENTS file, you can obtain it at
10  * www.aomedia.org/license/patent.
11  */
12 
13 #ifndef CLI_WAV_READER_H_
14 #define CLI_WAV_READER_H_
15 
16 #include <cstddef>
17 #include <cstdint>
18 #include <cstdio>
19 #include <string>
20 #include <vector>
21 
22 #include "absl/status/statusor.h"
23 #include "src/dsp/read_wav_info.h"
24 
25 namespace iamf_tools {
26 
27 class WavReader {
28  public:
29   /*!\brief Factory function.
30    *
31    * \param wav_filename Filename of file to read.
32    * \param num_samples_per_frame Maximum number of samples per frame to read.
33    * \param `WavReader` on success. A specific error code if the file could not
34    *        be opened or was not detected to be a valid WAV file.
35    */
36   static absl::StatusOr<WavReader> CreateFromFile(
37       const std::string& wav_filename, size_t num_samples_per_frame);
38 
39   /*!\brief Moves the `WavReader` without closing the underlying file.*/
40   WavReader(WavReader&& original);
41 
42   /*!\brief Destructor. */
43   ~WavReader();
44 
45   /*!\brief Gets the number of channels of the reader.
46    *
47    * \return Number of channels.
48    */
num_channels()49   int num_channels() const { return info_.num_channels; }
50 
51   /*!\brief Gets the sample rate of the reader.
52    *
53    * \return Sample rate.
54    */
sample_rate_hz()55   int sample_rate_hz() const { return info_.sample_rate_hz; }
56 
57   /*!\brief Gets the bit-depth of the reader.
58    *
59    * \return Bit-depth.
60    */
bit_depth()61   int bit_depth() const { return info_.bit_depth; }
62 
63   /*!\brief Gets the number of remaining samples in the file.
64    *
65    * \return Number of samples remaining to be read.
66    */
remaining_samples()67   int remaining_samples() const { return info_.remaining_samples; }
68 
69   /*!\brief Read up to one frame worth of samples.
70    *
71    * Typically this function reads up to `(num_channels() *
72    * num_samples_per_frame_)` samples. It may read fewer samples when the end
73    * of the wav file is reached.
74    *
75    * \return Number of samples read.
76    */
77   size_t ReadFrame();
78 
79   /*!\brief Buffers stored a vector of interleaved samples.
80    *
81    * The samples are left-justified; the upper `bit_depth()` bits represent the
82    * sample, with the remaining lower bits set to 0.
83    */
84   std::vector<std::vector<int32_t>> buffers_;
85 
86   const size_t num_samples_per_frame_;
87 
88  private:
89   /*!\brief Private constructor.
90    *
91    * Used by factory function.
92    */
93   WavReader(size_t num_samples_per_frame, FILE* file, const ReadWavInfo& info);
94 
95   FILE* file_;
96   ReadWavInfo info_;
97 };
98 }  // namespace iamf_tools
99 
100 #endif  // CLI_WAV_READER_H_
101