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 WEBRTC_COMMON_AUDIO_WAV_WRITER_H_
12#define WEBRTC_COMMON_AUDIO_WAV_WRITER_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 WavFile {
25 public:
26  // Open a new WAV file for writing.
27  WavFile(const std::string& filename, int sample_rate, int num_channels);
28
29  // Close the WAV file, after writing its header.
30  ~WavFile();
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}  // namespace webrtc
51
52extern "C" {
53#endif  // __cplusplus
54
55// C wrappers for the WavFile class.
56typedef struct rtc_WavFile rtc_WavFile;
57rtc_WavFile* rtc_WavOpen(const char* filename,
58                         int sample_rate,
59                         int num_channels);
60void rtc_WavClose(rtc_WavFile* wf);
61void rtc_WavWriteSamples(rtc_WavFile* wf,
62                         const float* samples,
63                         size_t num_samples);
64int rtc_WavSampleRate(const rtc_WavFile* wf);
65int rtc_WavNumChannels(const rtc_WavFile* wf);
66uint32_t rtc_WavNumSamples(const rtc_WavFile* wf);
67
68#ifdef __cplusplus
69}  // extern "C"
70#endif
71
72#endif  // WEBRTC_COMMON_AUDIO_WAV_WRITER_H_
73