blob: 1e32bf78557fe9a50a0e3d3e45cb56a5cd5852ef [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef COMMON_AUDIO_WAV_FILE_H_
12#define COMMON_AUDIO_WAV_FILE_H_
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000013
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000014#include <stdint.h>
15#include <cstddef>
16#include <string>
17
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/constructor_magic.h"
Artem Titove62f6002018-03-19 15:40:00 +010019#include "rtc_base/platform_file.h"
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070020
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000021namespace webrtc {
22
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070023// Interface to provide access to WAV file parameters.
24class WavFile {
25 public:
26 virtual ~WavFile() {}
27
28 virtual int sample_rate() const = 0;
Peter Kasting69558702016-01-12 16:26:35 -080029 virtual size_t num_channels() const = 0;
pkasting25702cb2016-01-08 13:50:27 -080030 virtual size_t num_samples() const = 0;
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070031};
32
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000033// Simple C++ class for writing 16-bit PCM WAV files. All error handling is
henrikg91d6ede2015-09-17 00:24:34 -070034// by calls to RTC_CHECK(), making it unsuitable for anything but debug code.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070035class WavWriter final : public WavFile {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000036 public:
37 // Open a new WAV file for writing.
Peter Kasting69558702016-01-12 16:26:35 -080038 WavWriter(const std::string& filename, int sample_rate, size_t num_channels);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000039
Artem Titove62f6002018-03-19 15:40:00 +010040 // Open a new WAV file for writing.
41 WavWriter(rtc::PlatformFile file, int sample_rate, size_t num_channels);
42
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000043 // Close the WAV file, after writing its header.
peahd1f718b2016-02-22 02:13:28 -080044 ~WavWriter() override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000045
46 // Write additional samples to the file. Each sample is in the range
47 // [-32768,32767], and there must be the previously specified number of
48 // interleaved channels.
49 void WriteSamples(const float* samples, size_t num_samples);
50 void WriteSamples(const int16_t* samples, size_t num_samples);
51
peahd1f718b2016-02-22 02:13:28 -080052 int sample_rate() const override;
53 size_t num_channels() const override;
54 size_t num_samples() const override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000055
56 private:
57 void Close();
58 const int sample_rate_;
Peter Kasting69558702016-01-12 16:26:35 -080059 const size_t num_channels_;
pkasting25702cb2016-01-08 13:50:27 -080060 size_t num_samples_; // Total number of samples written to file.
Yves Gerey665174f2018-06-19 15:03:05 +020061 FILE* file_handle_; // Output file, owned by this class
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070062
henrikg3c089d72015-09-16 05:37:44 -070063 RTC_DISALLOW_COPY_AND_ASSIGN(WavWriter);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000064};
65
66// Follows the conventions of WavWriter.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070067class WavReader final : public WavFile {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000068 public:
69 // Opens an existing WAV file for reading.
70 explicit WavReader(const std::string& filename);
71
Artem Titove62f6002018-03-19 15:40:00 +010072 // Opens an existing WAV file for reading.
73 explicit WavReader(rtc::PlatformFile file);
74
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000075 // Close the WAV file.
peahd1f718b2016-02-22 02:13:28 -080076 ~WavReader() override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000077
Artem Titov153056b2019-04-16 16:49:32 +020078 // Resets position to the beginning of the file.
79 void Reset();
80
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000081 // Returns the number of samples read. If this is less than requested,
82 // verifies that the end of the file was reached.
83 size_t ReadSamples(size_t num_samples, float* samples);
84 size_t ReadSamples(size_t num_samples, int16_t* samples);
85
peahd1f718b2016-02-22 02:13:28 -080086 int sample_rate() const override;
87 size_t num_channels() const override;
88 size_t num_samples() const override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000089
90 private:
91 void Close();
92 int sample_rate_;
Peter Kasting69558702016-01-12 16:26:35 -080093 size_t num_channels_;
pkasting25702cb2016-01-08 13:50:27 -080094 size_t num_samples_; // Total number of samples in the file.
95 size_t num_samples_remaining_;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000096 FILE* file_handle_; // Input file, owned by this class.
Artem Titov153056b2019-04-16 16:49:32 +020097 fpos_t data_start_pos_; // Position in the file immediately after WAV header.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070098
henrikg3c089d72015-09-16 05:37:44 -070099 RTC_DISALLOW_COPY_AND_ASSIGN(WavReader);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000100};
101
102} // namespace webrtc
103
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200104#endif // COMMON_AUDIO_WAV_FILE_H_