blob: 2f2e793a1e5f5c590fcb85cbb74a0d88fa307e74 [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
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070020#include "webrtc/base/constructormagic.h"
21
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000022namespace webrtc {
23
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070024// Interface to provide access to WAV file parameters.
25class 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
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000034// Simple C++ class for writing 16-bit PCM WAV files. All error handling is
35// by calls to CHECK(), making it unsuitable for anything but debug code.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070036class WavWriter final : public WavFile {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000037 public:
38 // Open a new WAV file for writing.
39 WavWriter(const std::string& filename, int sample_rate, int num_channels);
40
41 // Close the WAV file, after writing its header.
42 ~WavWriter();
43
44 // Write additional samples to the file. Each sample is in the range
45 // [-32768,32767], and there must be the previously specified number of
46 // interleaved channels.
47 void WriteSamples(const float* samples, size_t num_samples);
48 void WriteSamples(const int16_t* samples, size_t num_samples);
49
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070050 int sample_rate() const override { return sample_rate_; }
51 int num_channels() const override { return num_channels_; }
52 uint32_t num_samples() const override { return num_samples_; }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000053
54 private:
55 void Close();
56 const int sample_rate_;
57 const int num_channels_;
58 uint32_t num_samples_; // Total number of samples written to file.
59 FILE* file_handle_; // Output file, owned by this class
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070060
61 DISALLOW_COPY_AND_ASSIGN(WavWriter);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000062};
63
64// Follows the conventions of WavWriter.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070065class WavReader final : public WavFile {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000066 public:
67 // Opens an existing WAV file for reading.
68 explicit WavReader(const std::string& filename);
69
70 // Close the WAV file.
71 ~WavReader();
72
73 // Returns the number of samples read. If this is less than requested,
74 // verifies that the end of the file was reached.
75 size_t ReadSamples(size_t num_samples, float* samples);
76 size_t ReadSamples(size_t num_samples, int16_t* samples);
77
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070078 int sample_rate() const override { return sample_rate_; }
79 int num_channels() const override { return num_channels_; }
80 uint32_t num_samples() const override { return num_samples_; }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000081
82 private:
83 void Close();
84 int sample_rate_;
85 int num_channels_;
86 uint32_t num_samples_; // Total number of samples in the file.
andrew@webrtc.org048c5022014-12-16 20:17:21 +000087 uint32_t num_samples_remaining_;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000088 FILE* file_handle_; // Input file, owned by this class.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070089
90 DISALLOW_COPY_AND_ASSIGN(WavReader);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000091};
92
93} // namespace webrtc
94
95extern "C" {
96#endif // __cplusplus
97
98// C wrappers for the WavWriter class.
99typedef struct rtc_WavWriter rtc_WavWriter;
100rtc_WavWriter* rtc_WavOpen(const char* filename,
101 int sample_rate,
102 int num_channels);
103void rtc_WavClose(rtc_WavWriter* wf);
104void rtc_WavWriteSamples(rtc_WavWriter* wf,
105 const float* samples,
106 size_t num_samples);
107int rtc_WavSampleRate(const rtc_WavWriter* wf);
108int rtc_WavNumChannels(const rtc_WavWriter* wf);
109uint32_t rtc_WavNumSamples(const rtc_WavWriter* wf);
110
111#ifdef __cplusplus
112} // extern "C"
113#endif
114
115#endif // WEBRTC_COMMON_AUDIO_WAV_FILE_H_