1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef COMMON_AUDIO_WAV_FILE_H_
12 #define COMMON_AUDIO_WAV_FILE_H_
13 
14 #include <stdint.h>
15 
16 #include <cstddef>
17 #include <string>
18 
19 #include "common_audio/wav_header.h"
20 #include "rtc_base/system/file_wrapper.h"
21 
22 namespace webrtc {
23 
24 // Interface to provide access WAV file parameters.
25 class WavFile {
26  public:
27   enum class SampleFormat { kInt16, kFloat };
28 
~WavFile()29   virtual ~WavFile() {}
30 
31   virtual int sample_rate() const = 0;
32   virtual size_t num_channels() const = 0;
33   virtual size_t num_samples() const = 0;
34 };
35 
36 // Simple C++ class for writing 16-bit integer and 32 bit floating point PCM WAV
37 // files. All error handling is by calls to RTC_CHECK(), making it unsuitable
38 // for anything but debug code.
39 class WavWriter final : public WavFile {
40  public:
41   // Opens a new WAV file for writing.
42   WavWriter(const std::string& filename,
43             int sample_rate,
44             size_t num_channels,
45             SampleFormat sample_format = SampleFormat::kInt16);
46   WavWriter(FileWrapper file,
47             int sample_rate,
48             size_t num_channels,
49             SampleFormat sample_format = SampleFormat::kInt16);
50 
51   // Closes the WAV file, after writing its header.
~WavWriter()52   ~WavWriter() { Close(); }
53 
54   WavWriter(const WavWriter&) = delete;
55   WavWriter& operator=(const WavWriter&) = delete;
56 
57   // Write additional samples to the file. Each sample is in the range
58   // [-32768.0,32767.0], and there must be the previously specified number of
59   // interleaved channels.
60   void WriteSamples(const float* samples, size_t num_samples);
61   void WriteSamples(const int16_t* samples, size_t num_samples);
62 
sample_rate()63   int sample_rate() const override { return sample_rate_; }
num_channels()64   size_t num_channels() const override { return num_channels_; }
num_samples()65   size_t num_samples() const override { return num_samples_written_; }
66 
67  private:
68   void Close();
69   const int sample_rate_;
70   const size_t num_channels_;
71   size_t num_samples_written_;
72   WavFormat format_;
73   FileWrapper file_;
74 };
75 
76 // Follows the conventions of WavWriter.
77 class WavReader final : public WavFile {
78  public:
79   // Opens an existing WAV file for reading.
80   explicit WavReader(const std::string& filename);
81   explicit WavReader(FileWrapper file);
82 
83   // Close the WAV file.
~WavReader()84   ~WavReader() { Close(); }
85 
86   WavReader(const WavReader&) = delete;
87   WavReader& operator=(const WavReader&) = delete;
88 
89   // Resets position to the beginning of the file.
90   void Reset();
91 
92   // Returns the number of samples read. If this is less than requested,
93   // verifies that the end of the file was reached.
94   size_t ReadSamples(size_t num_samples, float* samples);
95   size_t ReadSamples(size_t num_samples, int16_t* samples);
96 
sample_rate()97   int sample_rate() const override { return sample_rate_; }
num_channels()98   size_t num_channels() const override { return num_channels_; }
num_samples()99   size_t num_samples() const override { return num_samples_in_file_; }
100 
101  private:
102   void Close();
103   int sample_rate_;
104   size_t num_channels_;
105   WavFormat format_;
106   size_t num_samples_in_file_;
107   size_t num_unread_samples_;
108   FileWrapper file_;
109   int64_t
110       data_start_pos_;  // Position in the file immediately after WAV header.
111 };
112 
113 }  // namespace webrtc
114 
115 #endif  // COMMON_AUDIO_WAV_FILE_H_
116