blob: 1fbf9541fabcff29af06dfda72c5d439c4d82941 [file] [log] [blame]
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +00001/*
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 WEBRTC_COMMON_AUDIO_WAV_FILE_H_
12#define WEBRTC_COMMON_AUDIO_WAV_FILE_H_
13
14#ifdef __cplusplus
15
16#include <stdint.h>
17#include <cstddef>
18#include <string>
19
20namespace webrtc {
21
22// 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.
24class WavWriter {
25 public:
26 // Open a new WAV file for writing.
27 WavWriter(const std::string& filename, int sample_rate, int num_channels);
28
29 // Close the WAV file, after writing its header.
30 ~WavWriter();
31
32 // Write additional samples to the file. Each sample is in the range
33 // [-32768,32767], and there must be the previously specified number of
34 // interleaved channels.
35 void WriteSamples(const float* samples, size_t num_samples);
36 void WriteSamples(const int16_t* samples, size_t num_samples);
37
38 int sample_rate() const { return sample_rate_; }
39 int num_channels() const { return num_channels_; }
40 uint32_t num_samples() const { return num_samples_; }
41
42 private:
43 void Close();
44 const int sample_rate_;
45 const int num_channels_;
46 uint32_t num_samples_; // Total number of samples written to file.
47 FILE* file_handle_; // Output file, owned by this class
48};
49
50// Follows the conventions of WavWriter.
51class WavReader {
52 public:
53 // Opens an existing WAV file for reading.
54 explicit WavReader(const std::string& filename);
55
56 // Close the WAV file.
57 ~WavReader();
58
59 // Returns the number of samples read. If this is less than requested,
60 // verifies that the end of the file was reached.
61 size_t ReadSamples(size_t num_samples, float* samples);
62 size_t ReadSamples(size_t num_samples, int16_t* samples);
63
64 int sample_rate() const { return sample_rate_; }
65 int num_channels() const { return num_channels_; }
66 uint32_t num_samples() const { return num_samples_; }
67
68 private:
69 void Close();
70 int sample_rate_;
71 int num_channels_;
72 uint32_t num_samples_; // Total number of samples in the file.
andrew@webrtc.org048c5022014-12-16 20:17:21 +000073 uint32_t num_samples_remaining_;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000074 FILE* file_handle_; // Input file, owned by this class.
75};
76
77} // namespace webrtc
78
79extern "C" {
80#endif // __cplusplus
81
82// C wrappers for the WavWriter class.
83typedef struct rtc_WavWriter rtc_WavWriter;
84rtc_WavWriter* rtc_WavOpen(const char* filename,
85 int sample_rate,
86 int num_channels);
87void rtc_WavClose(rtc_WavWriter* wf);
88void rtc_WavWriteSamples(rtc_WavWriter* wf,
89 const float* samples,
90 size_t num_samples);
91int rtc_WavSampleRate(const rtc_WavWriter* wf);
92int rtc_WavNumChannels(const rtc_WavWriter* wf);
93uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf);
94
95#ifdef __cplusplus
96} // extern "C"
97#endif
98
99#endif // WEBRTC_COMMON_AUDIO_WAV_FILE_H_