blob: 65f24537364dfaefb79b98419e578f82fd50ccd7 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000016#include <cstddef>
17#include <string>
18
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/constructor_magic.h"
Niels Möller19a1d502019-06-13 14:08:24 +020020#include "rtc_base/system/file_wrapper.h"
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070021
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;
Peter Kasting69558702016-01-12 16:26:35 -080030 virtual size_t num_channels() const = 0;
pkasting25702cb2016-01-08 13:50:27 -080031 virtual size_t num_samples() const = 0;
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070032};
33
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000034// Simple C++ class for writing 16-bit PCM WAV files. All error handling is
henrikg91d6ede2015-09-17 00:24:34 -070035// by calls to RTC_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.
Peter Kasting69558702016-01-12 16:26:35 -080039 WavWriter(const std::string& filename, int sample_rate, size_t num_channels);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000040
Artem Titove62f6002018-03-19 15:40:00 +010041 // Open a new WAV file for writing.
Niels Möller19a1d502019-06-13 14:08:24 +020042 WavWriter(FileWrapper file, int sample_rate, size_t num_channels);
Artem Titove62f6002018-03-19 15:40:00 +010043
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000044 // Close the WAV file, after writing its header.
peahd1f718b2016-02-22 02:13:28 -080045 ~WavWriter() override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000046
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
peahd1f718b2016-02-22 02:13:28 -080053 int sample_rate() const override;
54 size_t num_channels() const override;
55 size_t num_samples() const override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000056
57 private:
58 void Close();
59 const int sample_rate_;
Peter Kasting69558702016-01-12 16:26:35 -080060 const size_t num_channels_;
pkasting25702cb2016-01-08 13:50:27 -080061 size_t num_samples_; // Total number of samples written to file.
Niels Möller19a1d502019-06-13 14:08:24 +020062 FileWrapper file_; // 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
Niels Möller7ba3b812019-08-06 09:58:56 +020073 // Use an existing WAV file for reading.
74 explicit WavReader(FileWrapper file);
75
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000076 // Close the WAV file.
peahd1f718b2016-02-22 02:13:28 -080077 ~WavReader() override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000078
Artem Titov153056b2019-04-16 16:49:32 +020079 // Resets position to the beginning of the file.
80 void Reset();
81
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000082 // Returns the number of samples read. If this is less than requested,
83 // verifies that the end of the file was reached.
84 size_t ReadSamples(size_t num_samples, float* samples);
85 size_t ReadSamples(size_t num_samples, int16_t* samples);
86
peahd1f718b2016-02-22 02:13:28 -080087 int sample_rate() const override;
88 size_t num_channels() const override;
89 size_t num_samples() const override;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000090
91 private:
92 void Close();
93 int sample_rate_;
Peter Kasting69558702016-01-12 16:26:35 -080094 size_t num_channels_;
pkasting25702cb2016-01-08 13:50:27 -080095 size_t num_samples_; // Total number of samples in the file.
96 size_t num_samples_remaining_;
Niels Möller7ba3b812019-08-06 09:58:56 +020097 FileWrapper file_; // Input file, owned by this class.
98 int64_t
99 data_start_pos_; // Position in the file immediately after WAV header.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -0700100
henrikg3c089d72015-09-16 05:37:44 -0700101 RTC_DISALLOW_COPY_AND_ASSIGN(WavReader);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000102};
103
104} // namespace webrtc
105
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200106#endif // COMMON_AUDIO_WAV_FILE_H_