blob: dda611be6a8f3407fa27f509b4a5985e280a46ad [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
Per Åhgren5dca3f12020-01-28 09:08:11 +010019#include "common_audio/wav_header.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
Per Åhgren5dca3f12020-01-28 09:08:11 +010024// Interface to provide access WAV file parameters.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070025class WavFile {
26 public:
Per Åhgren5dca3f12020-01-28 09:08:11 +010027 enum class SampleFormat { kInt16, kFloat };
28
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070029 virtual ~WavFile() {}
30
31 virtual int sample_rate() const = 0;
Peter Kasting69558702016-01-12 16:26:35 -080032 virtual size_t num_channels() const = 0;
pkasting25702cb2016-01-08 13:50:27 -080033 virtual size_t num_samples() const = 0;
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070034};
35
Per Åhgren5dca3f12020-01-28 09:08:11 +010036// Simple C++ class for writing 16-bit integer and 32 bit floating point PCM WAV
37// files. All error handling is by calls to RTC_CHECK(), making it unsuitable
38// 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:
Per Åhgren5dca3f12020-01-28 09:08:11 +010041 // Opens a new WAV file for writing.
42 WavWriter(const std::string& filename,
43 int sample_rate,
44 size_t num_channels,
45 SampleFormat sample_format = SampleFormat::kInt16);
46 WavWriter(FileWrapper file,
47 int sample_rate,
48 size_t num_channels,
49 SampleFormat sample_format = SampleFormat::kInt16);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000050
Per Åhgren5dca3f12020-01-28 09:08:11 +010051 // Closes the WAV file, after writing its header.
52 ~WavWriter() { Close(); }
Artem Titove62f6002018-03-19 15:40:00 +010053
Per Åhgren5dca3f12020-01-28 09:08:11 +010054 WavWriter(const WavWriter&) = delete;
55 WavWriter& operator=(const WavWriter&) = delete;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000056
57 // Write additional samples to the file. Each sample is in the range
Per Åhgren5dca3f12020-01-28 09:08:11 +010058 // [-32768.0,32767.0], and there must be the previously specified number of
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000059 // interleaved channels.
60 void WriteSamples(const float* samples, size_t num_samples);
61 void WriteSamples(const int16_t* samples, size_t num_samples);
62
Per Åhgren5dca3f12020-01-28 09:08:11 +010063 int sample_rate() const override { return sample_rate_; }
64 size_t num_channels() const override { return num_channels_; }
65 size_t num_samples() const override { return num_samples_written_; }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000066
67 private:
68 void Close();
69 const int sample_rate_;
Peter Kasting69558702016-01-12 16:26:35 -080070 const size_t num_channels_;
Per Åhgren5dca3f12020-01-28 09:08:11 +010071 size_t num_samples_written_;
72 WavFormat format_;
73 FileWrapper file_;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000074};
75
76// Follows the conventions of WavWriter.
Andrew MacDonaldbc2296d2015-08-24 17:29:26 -070077class WavReader final : public WavFile {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000078 public:
79 // Opens an existing WAV file for reading.
80 explicit WavReader(const std::string& filename);
Niels Möller7ba3b812019-08-06 09:58:56 +020081 explicit WavReader(FileWrapper file);
82
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000083 // Close the WAV file.
Per Åhgren5dca3f12020-01-28 09:08:11 +010084 ~WavReader() { Close(); }
85
86 WavReader(const WavReader&) = delete;
87 WavReader& operator=(const WavReader&) = delete;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000088
Artem Titov153056b2019-04-16 16:49:32 +020089 // Resets position to the beginning of the file.
90 void Reset();
91
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000092 // Returns the number of samples read. If this is less than requested,
93 // verifies that the end of the file was reached.
94 size_t ReadSamples(size_t num_samples, float* samples);
95 size_t ReadSamples(size_t num_samples, int16_t* samples);
96
Per Åhgren5dca3f12020-01-28 09:08:11 +010097 int sample_rate() const override { return sample_rate_; }
98 size_t num_channels() const override { return num_channels_; }
99 size_t num_samples() const override { return num_samples_in_file_; }
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000100
101 private:
102 void Close();
103 int sample_rate_;
Peter Kasting69558702016-01-12 16:26:35 -0800104 size_t num_channels_;
Per Åhgren5dca3f12020-01-28 09:08:11 +0100105 WavFormat format_;
106 size_t num_samples_in_file_;
107 size_t num_unread_samples_;
108 FileWrapper file_;
Niels Möller7ba3b812019-08-06 09:58:56 +0200109 int64_t
110 data_start_pos_; // Position in the file immediately after WAV header.
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000111};
112
113} // namespace webrtc
114
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200115#endif // COMMON_AUDIO_WAV_FILE_H_