summaryrefslogtreecommitdiff
path: root/common_audio/wav_writer.h
blob: 09667279c573554c8b8000ebdcc3d2e77ace30dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_COMMON_AUDIO_WAV_WRITER_H_
#define WEBRTC_COMMON_AUDIO_WAV_WRITER_H_

#ifdef __cplusplus

#include <stdint.h>
#include <cstddef>
#include <string>

namespace webrtc {

// Simple C++ class for writing 16-bit PCM WAV files. All error handling is
// by calls to CHECK(), making it unsuitable for anything but debug code.
class WavFile {
 public:
  // Open a new WAV file for writing.
  WavFile(const std::string& filename, int sample_rate, int num_channels);

  // Close the WAV file, after writing its header.
  ~WavFile();

  // Write additional samples to the file. Each sample is in the range
  // [-32768,32767], and there must be the previously specified number of
  // interleaved channels.
  void WriteSamples(const float* samples, size_t num_samples);
  void WriteSamples(const int16_t* samples, size_t num_samples);

  int sample_rate() const { return sample_rate_; }
  int num_channels() const { return num_channels_; }
  uint32_t num_samples() const { return num_samples_; }

 private:
  void Close();
  const int sample_rate_;
  const int num_channels_;
  uint32_t num_samples_;  // total number of samples written to file
  FILE* file_handle_;  // output file, owned by this class
};

}  // namespace webrtc

extern "C" {
#endif  // __cplusplus

// C wrappers for the WavFile class.
typedef struct rtc_WavFile rtc_WavFile;
rtc_WavFile* rtc_WavOpen(const char* filename,
                         int sample_rate,
                         int num_channels);
void rtc_WavClose(rtc_WavFile* wf);
void rtc_WavWriteSamples(rtc_WavFile* wf,
                         const float* samples,
                         size_t num_samples);
int rtc_WavSampleRate(const rtc_WavFile* wf);
int rtc_WavNumChannels(const rtc_WavFile* wf);
uint32_t rtc_WavNumSamples(const rtc_WavFile* wf);

#ifdef __cplusplus
}  // extern "C"
#endif

#endif  // WEBRTC_COMMON_AUDIO_WAV_WRITER_H_