blob: eb2ce1e31d9e8bd431abd072ff705528025d4ac0 [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;
pkasting25702cb2016-01-08 13:50:27 -080031 virtual size_t num_samples() const = 0;
aluebsb0ad43b2015-11-20 00:11:53 -080032
33 // Returns a human-readable string containing the audio format.
34 std::string FormatAsString() const;
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070035};
36
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000037// Simple C++ class for writing 16-bit PCM WAV files. All error handling is
henrikg91d6ede2015-09-17 00:24:34 -070038// by calls to RTC_CHECK(), making it unsuitable for anything but debug code.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070039class WavWriter final : public WavFile {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000040 public:
41 // Open a new WAV file for writing.
42 WavWriter(const std::string& filename, int sample_rate, int num_channels);
43
44 // Close the WAV file, after writing its header.
45 ~WavWriter();
46
47 // Write additional samples to the file. Each sample is in the range
48 // [-32768,32767], and there must be the previously specified number of
49 // interleaved channels.
50 void WriteSamples(const float* samples, size_t num_samples);
51 void WriteSamples(const int16_t* samples, size_t num_samples);
52
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070053 int sample_rate() const override { return sample_rate_; }
54 int num_channels() const override { return num_channels_; }
pkasting25702cb2016-01-08 13:50:27 -080055 size_t num_samples() const override { return num_samples_; }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000056
57 private:
58 void Close();
59 const int sample_rate_;
60 const int num_channels_;
pkasting25702cb2016-01-08 13:50:27 -080061 size_t num_samples_; // Total number of samples written to file.
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000062 FILE* file_handle_; // Output file, owned by this class
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070063
henrikg3c089d72015-09-16 05:37:44 -070064 RTC_DISALLOW_COPY_AND_ASSIGN(WavWriter);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000065};
66
67// Follows the conventions of WavWriter.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070068class WavReader final : public WavFile {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000069 public:
70 // Opens an existing WAV file for reading.
71 explicit WavReader(const std::string& filename);
72
73 // Close the WAV file.
74 ~WavReader();
75
76 // Returns the number of samples read. If this is less than requested,
77 // verifies that the end of the file was reached.
78 size_t ReadSamples(size_t num_samples, float* samples);
79 size_t ReadSamples(size_t num_samples, int16_t* samples);
80
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070081 int sample_rate() const override { return sample_rate_; }
82 int num_channels() const override { return num_channels_; }
pkasting25702cb2016-01-08 13:50:27 -080083 size_t num_samples() const override { return num_samples_; }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000084
85 private:
86 void Close();
87 int sample_rate_;
88 int num_channels_;
pkasting25702cb2016-01-08 13:50:27 -080089 size_t num_samples_; // Total number of samples in the file.
90 size_t num_samples_remaining_;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000091 FILE* file_handle_; // Input file, owned by this class.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070092
henrikg3c089d72015-09-16 05:37:44 -070093 RTC_DISALLOW_COPY_AND_ASSIGN(WavReader);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000094};
95
96} // namespace webrtc
97
98extern "C" {
99#endif // __cplusplus
100
101// C wrappers for the WavWriter class.
102typedef struct rtc_WavWriter rtc_WavWriter;
103rtc_WavWriter* rtc_WavOpen(const char* filename,
104 int sample_rate,
105 int num_channels);
106void rtc_WavClose(rtc_WavWriter* wf);
107void rtc_WavWriteSamples(rtc_WavWriter* wf,
108 const float* samples,
109 size_t num_samples);
110int rtc_WavSampleRate(const rtc_WavWriter* wf);
111int rtc_WavNumChannels(const rtc_WavWriter* wf);
pkasting25702cb2016-01-08 13:50:27 -0800112size_t rtc_WavNumSamples(const rtc_WavWriter* wf);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000113
114#ifdef __cplusplus
115} // extern "C"
116#endif
117
118#endif // WEBRTC_COMMON_AUDIO_WAV_FILE_H_