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 int sample_rate() const = 0; | |
28 virtual int num_channels() const = 0; | |
29 virtual uint32_t num_samples() const = 0; | |
kwiberg-webrtc
2015/08/24 12:13:48
One might argue that these should have CamelCase n
Andrew MacDonald
2015/08/24 19:50:57
A not unreasonable argument, but I would counter t
| |
30 }; | |
31 | |
22 // Simple C++ class for writing 16-bit PCM WAV files. All error handling is | 32 // 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. | 33 // by calls to CHECK(), making it unsuitable for anything but debug code. |
24 class WavWriter { | 34 class WavWriter final : public WavFile { |
25 public: | 35 public: |
26 // Open a new WAV file for writing. | 36 // Open a new WAV file for writing. |
27 WavWriter(const std::string& filename, int sample_rate, int num_channels); | 37 WavWriter(const std::string& filename, int sample_rate, int num_channels); |
28 | 38 |
29 // Close the WAV file, after writing its header. | 39 // Close the WAV file, after writing its header. |
30 ~WavWriter(); | 40 ~WavWriter(); |
31 | 41 |
32 // Write additional samples to the file. Each sample is in the range | 42 // Write additional samples to the file. Each sample is in the range |
33 // [-32768,32767], and there must be the previously specified number of | 43 // [-32768,32767], and there must be the previously specified number of |
34 // interleaved channels. | 44 // interleaved channels. |
35 void WriteSamples(const float* samples, size_t num_samples); | 45 void WriteSamples(const float* samples, size_t num_samples); |
36 void WriteSamples(const int16_t* samples, size_t num_samples); | 46 void WriteSamples(const int16_t* samples, size_t num_samples); |
37 | 47 |
38 int sample_rate() const { return sample_rate_; } | 48 int sample_rate() const override { return sample_rate_; } |
39 int num_channels() const { return num_channels_; } | 49 int num_channels() const override { return num_channels_; } |
40 uint32_t num_samples() const { return num_samples_; } | 50 uint32_t num_samples() const override { return num_samples_; } |
41 | 51 |
42 private: | 52 private: |
43 void Close(); | 53 void Close(); |
44 const int sample_rate_; | 54 const int sample_rate_; |
45 const int num_channels_; | 55 const int num_channels_; |
46 uint32_t num_samples_; // Total number of samples written to file. | 56 uint32_t num_samples_; // Total number of samples written to file. |
47 FILE* file_handle_; // Output file, owned by this class | 57 FILE* file_handle_; // Output file, owned by this class |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(WavWriter); | |
48 }; | 60 }; |
49 | 61 |
50 // Follows the conventions of WavWriter. | 62 // Follows the conventions of WavWriter. |
51 class WavReader { | 63 class WavReader final : public WavFile { |
52 public: | 64 public: |
53 // Opens an existing WAV file for reading. | 65 // Opens an existing WAV file for reading. |
54 explicit WavReader(const std::string& filename); | 66 explicit WavReader(const std::string& filename); |
55 | 67 |
56 // Close the WAV file. | 68 // Close the WAV file. |
57 ~WavReader(); | 69 ~WavReader(); |
58 | 70 |
59 // Returns the number of samples read. If this is less than requested, | 71 // Returns the number of samples read. If this is less than requested, |
60 // verifies that the end of the file was reached. | 72 // verifies that the end of the file was reached. |
61 size_t ReadSamples(size_t num_samples, float* samples); | 73 size_t ReadSamples(size_t num_samples, float* samples); |
62 size_t ReadSamples(size_t num_samples, int16_t* samples); | 74 size_t ReadSamples(size_t num_samples, int16_t* samples); |
63 | 75 |
64 int sample_rate() const { return sample_rate_; } | 76 int sample_rate() const override { return sample_rate_; } |
65 int num_channels() const { return num_channels_; } | 77 int num_channels() const override { return num_channels_; } |
66 uint32_t num_samples() const { return num_samples_; } | 78 uint32_t num_samples() const override { return num_samples_; } |
67 | 79 |
68 private: | 80 private: |
69 void Close(); | 81 void Close(); |
70 int sample_rate_; | 82 int sample_rate_; |
71 int num_channels_; | 83 int num_channels_; |
72 uint32_t num_samples_; // Total number of samples in the file. | 84 uint32_t num_samples_; // Total number of samples in the file. |
73 uint32_t num_samples_remaining_; | 85 uint32_t num_samples_remaining_; |
74 FILE* file_handle_; // Input file, owned by this class. | 86 FILE* file_handle_; // Input file, owned by this class. |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(WavReader); | |
75 }; | 89 }; |
76 | 90 |
77 } // namespace webrtc | 91 } // namespace webrtc |
78 | 92 |
79 extern "C" { | 93 extern "C" { |
80 #endif // __cplusplus | 94 #endif // __cplusplus |
81 | 95 |
82 // C wrappers for the WavWriter class. | 96 // C wrappers for the WavWriter class. |
83 typedef struct rtc_WavWriter rtc_WavWriter; | 97 typedef struct rtc_WavWriter rtc_WavWriter; |
84 rtc_WavWriter* rtc_WavOpen(const char* filename, | 98 rtc_WavWriter* rtc_WavOpen(const char* filename, |
85 int sample_rate, | 99 int sample_rate, |
86 int num_channels); | 100 int num_channels); |
87 void rtc_WavClose(rtc_WavWriter* wf); | 101 void rtc_WavClose(rtc_WavWriter* wf); |
88 void rtc_WavWriteSamples(rtc_WavWriter* wf, | 102 void rtc_WavWriteSamples(rtc_WavWriter* wf, |
89 const float* samples, | 103 const float* samples, |
90 size_t num_samples); | 104 size_t num_samples); |
91 int rtc_WavSampleRate(const rtc_WavWriter* wf); | 105 int rtc_WavSampleRate(const rtc_WavWriter* wf); |
92 int rtc_WavNumChannels(const rtc_WavWriter* wf); | 106 int rtc_WavNumChannels(const rtc_WavWriter* wf); |
93 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf); | 107 uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf); |
94 | 108 |
95 #ifdef __cplusplus | 109 #ifdef __cplusplus |
96 } // extern "C" | 110 } // extern "C" |
97 #endif | 111 #endif |
98 | 112 |
99 #endif // WEBRTC_COMMON_AUDIO_WAV_FILE_H_ | 113 #endif // WEBRTC_COMMON_AUDIO_WAV_FILE_H_ |
OLD | NEW |