OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_COMMON_AUDIO_WAV_FILE_H_ | 11 #ifndef WEBRTC_COMMON_AUDIO_WAV_FILE_H_ |
12 #define WEBRTC_COMMON_AUDIO_WAV_FILE_H_ | 12 #define WEBRTC_COMMON_AUDIO_WAV_FILE_H_ |
13 | 13 |
14 #ifdef __cplusplus | 14 #ifdef __cplusplus |
15 | 15 |
16 #include <stdint.h> | 16 #include <stdint.h> |
17 #include <cstddef> | 17 #include <cstddef> |
18 #include <string> | 18 #include <string> |
19 | 19 |
| 20 #include "webrtc/base/constructormagic.h" |
| 21 |
20 namespace webrtc { | 22 namespace webrtc { |
21 | 23 |
| 24 // Interface to provide access to WAV file parameters. |
| 25 class WavFile { |
| 26 public: |
| 27 virtual ~WavFile() {} |
| 28 |
| 29 virtual int sample_rate() const = 0; |
| 30 virtual int num_channels() const = 0; |
| 31 virtual uint32_t num_samples() const = 0; |
| 32 }; |
| 33 |
22 // Simple C++ class for writing 16-bit PCM WAV files. All error handling is | 34 // Simple C++ class for writing 16-bit PCM WAV files. All error handling is |
23 // by calls to CHECK(), making it unsuitable for anything but debug code. | 35 // by calls to CHECK(), making it unsuitable for anything but debug code. |
24 class WavWriter { | 36 class WavWriter final : public WavFile { |
25 public: | 37 public: |
26 // Open a new WAV file for writing. | 38 // Open a new WAV file for writing. |
27 WavWriter(const std::string& filename, int sample_rate, int num_channels); | 39 WavWriter(const std::string& filename, int sample_rate, int num_channels); |
28 | 40 |
29 // Close the WAV file, after writing its header. | 41 // Close the WAV file, after writing its header. |
30 ~WavWriter(); | 42 ~WavWriter(); |
31 | 43 |
32 // Write additional samples to the file. Each sample is in the range | 44 // Write additional samples to the file. Each sample is in the range |
33 // [-32768,32767], and there must be the previously specified number of | 45 // [-32768,32767], and there must be the previously specified number of |
34 // interleaved channels. | 46 // interleaved channels. |
35 void WriteSamples(const float* samples, size_t num_samples); | 47 void WriteSamples(const float* samples, size_t num_samples); |
36 void WriteSamples(const int16_t* samples, size_t num_samples); | 48 void WriteSamples(const int16_t* samples, size_t num_samples); |
37 | 49 |
38 int sample_rate() const { return sample_rate_; } | 50 int sample_rate() const override { return sample_rate_; } |
39 int num_channels() const { return num_channels_; } | 51 int num_channels() const override { return num_channels_; } |
40 uint32_t num_samples() const { return num_samples_; } | 52 uint32_t num_samples() const override { return num_samples_; } |
41 | 53 |
42 private: | 54 private: |
43 void Close(); | 55 void Close(); |
44 const int sample_rate_; | 56 const int sample_rate_; |
45 const int num_channels_; | 57 const int num_channels_; |
46 uint32_t num_samples_; // Total number of samples written to file. | 58 uint32_t num_samples_; // Total number of samples written to file. |
47 FILE* file_handle_; // Output file, owned by this class | 59 FILE* file_handle_; // Output file, owned by this class |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(WavWriter); |
48 }; | 62 }; |
49 | 63 |
50 // Follows the conventions of WavWriter. | 64 // Follows the conventions of WavWriter. |
51 class WavReader { | 65 class WavReader final : public WavFile { |
52 public: | 66 public: |
53 // Opens an existing WAV file for reading. | 67 // Opens an existing WAV file for reading. |
54 explicit WavReader(const std::string& filename); | 68 explicit WavReader(const std::string& filename); |
55 | 69 |
56 // Close the WAV file. | 70 // Close the WAV file. |
57 ~WavReader(); | 71 ~WavReader(); |
58 | 72 |
59 // Returns the number of samples read. If this is less than requested, | 73 // Returns the number of samples read. If this is less than requested, |
60 // verifies that the end of the file was reached. | 74 // verifies that the end of the file was reached. |
61 size_t ReadSamples(size_t num_samples, float* samples); | 75 size_t ReadSamples(size_t num_samples, float* samples); |
62 size_t ReadSamples(size_t num_samples, int16_t* samples); | 76 size_t ReadSamples(size_t num_samples, int16_t* samples); |
63 | 77 |
64 int sample_rate() const { return sample_rate_; } | 78 int sample_rate() const override { return sample_rate_; } |
65 int num_channels() const { return num_channels_; } | 79 int num_channels() const override { return num_channels_; } |
66 uint32_t num_samples() const { return num_samples_; } | 80 uint32_t num_samples() const override { return num_samples_; } |
67 | 81 |
68 private: | 82 private: |
69 void Close(); | 83 void Close(); |
70 int sample_rate_; | 84 int sample_rate_; |
71 int num_channels_; | 85 int num_channels_; |
72 uint32_t num_samples_; // Total number of samples in the file. | 86 uint32_t num_samples_; // Total number of samples in the file. |
73 uint32_t num_samples_remaining_; | 87 uint32_t num_samples_remaining_; |
74 FILE* file_handle_; // Input file, owned by this class. | 88 FILE* file_handle_; // Input file, owned by this class. |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(WavReader); |
75 }; | 91 }; |
76 | 92 |
77 } // namespace webrtc | 93 } // namespace webrtc |
78 | 94 |
79 extern "C" { | 95 extern "C" { |
80 #endif // __cplusplus | 96 #endif // __cplusplus |
81 | 97 |
82 // C wrappers for the WavWriter class. | 98 // C wrappers for the WavWriter class. |
83 typedef struct rtc_WavWriter rtc_WavWriter; | 99 typedef struct rtc_WavWriter rtc_WavWriter; |
84 rtc_WavWriter* rtc_WavOpen(const char* filename, | 100 rtc_WavWriter* rtc_WavOpen(const char* filename, |
85 int sample_rate, | 101 int sample_rate, |
86 int num_channels); | 102 int num_channels); |
87 void rtc_WavClose(rtc_WavWriter* wf); | 103 void rtc_WavClose(rtc_WavWriter* wf); |
88 void rtc_WavWriteSamples(rtc_WavWriter* wf, | 104 void rtc_WavWriteSamples(rtc_WavWriter* wf, |
89 const float* samples, | 105 const float* samples, |
90 size_t num_samples); | 106 size_t num_samples); |
91 int rtc_WavSampleRate(const rtc_WavWriter* wf); | 107 int rtc_WavSampleRate(const rtc_WavWriter* wf); |
92 int rtc_WavNumChannels(const rtc_WavWriter* wf); | 108 int rtc_WavNumChannels(const rtc_WavWriter* wf); |
93 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf); | 109 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf); |
94 | 110 |
95 #ifdef __cplusplus | 111 #ifdef __cplusplus |
96 } // extern "C" | 112 } // extern "C" |
97 #endif | 113 #endif |
98 | 114 |
99 #endif // WEBRTC_COMMON_AUDIO_WAV_FILE_H_ | 115 #endif // WEBRTC_COMMON_AUDIO_WAV_FILE_H_ |
OLD | NEW |